oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#!/bin/sh
2# The runner bootstrap. Nothing runs until the whole script has arrived:
3# the functions are only defined until main, on the last line.
4
5# own_home is this user's home from the user directory, never an
6# inherited HOME (su keeps the caller's).
7own_home() {
8user=$(id -un)
9h=
10if command -v dscl >/dev/null 2>&1; then
11 h=$(dscl . -read "/Users/$user" NFSHomeDirectory 2>/dev/null | sed -n 's/^NFSHomeDirectory: //p')
12elif command -v getent >/dev/null 2>&1; then
13 h=$(getent passwd "$user" | cut -d: -f6)
14fi
15[ -n "$h" ] && [ -d "$h" ] || return 1
16printf '%s\n' "$h"
17}
18
19# make_scratch makes one private scratch directory without needing
20# TMPDIR: TMPDIR when it is a directory this user may write; the
21# platform's own temporary directory ($1, a Mac's per-user one); on a Mac
22# mktemp's default (not on Linux, where /tmp may be a shared RAM disk);
23# then a private folder in this user's own cache.
24make_scratch() {
25for root in "${TMPDIR:-}" "$1"; do
26 if [ -n "$root" ] && [ -d "$root" ] && [ -w "$root" ]; then
27 mktemp -d "$root/equipo-bootstrap-XXXXXXXX" && return 0
28 fi
29done
30if [ "$2" = darwin ]; then
31 mktemp -d 2>/dev/null && return 0
32fi
33home=$(own_home) || return 1
34mkdir -p "$home/.cache/equipo-bootstrap" && chmod 700 "$home/.cache/equipo-bootstrap" && mktemp -d "$home/.cache/equipo-bootstrap/run-XXXXXXXX"
35}
36
37linux() {
38scratch=$(make_scratch "" linux)
39trap 'rm -rf "$scratch"' EXIT
40trap 'exit 1' HUP INT TERM
41curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/install-manifest.json" -o "$scratch/manifest.json"
42# Python parses data, not shell source. TLS authenticates the configured origin.
43digest=$(python3 - "$scratch/manifest.json" "$version" <<'PY'
44import json, re, sys
45m = json.load(open(sys.argv[1]))
46r = m['release']
47assert m['version'] == sys.argv[2] and m['target'] == 'linux/amd64'
48# One build: the runner and tool proxy digests the release recorded.
49t = m['targets']['linux/amd64']
50assert r['schema'] == 3 and r['runner_target'] == 'linux/amd64'
51assert re.fullmatch('[0-9a-f]{64}', m['runner_sha256']) and t['runner_sha256'] == m['runner_sha256']
52assert re.fullmatch('[0-9a-f]{64}', t['tool_proxy_sha256'])
53assert [b['sha256'] for b in r['binaries'] if b['name'] == 'equipo-tool-proxy'] == [t['tool_proxy_sha256']]
54print(m['runner_sha256'])
55PY
56)
57curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/equipo-runner" -o "$scratch/equipo-runner"
58printf '%s %s\n' "$digest" "$scratch/equipo-runner" | sha256sum --check --status
59chmod 700 "$scratch/equipo-runner"
60"$scratch/equipo-runner" verify-install-manifest "$scratch/manifest.json" >/dev/null
61# Prompts read the terminal because the bootstrap script may arrive on stdin.
62"$scratch/equipo-runner" install --release "$version" --origin "$origin" --instance "$instance" </dev/tty
63}
64
65# A Mac worker (Apple silicon), installed as the user running this: the
66# runner itself refuses root, administrators and users without equipo
67# in their name. plutil reads the manifest (JSON), so no Python is needed.
68darwin() {
69[ "$(id -u)" != 0 ] || exit 1
70# Under su, HOME and TMPDIR may be the caller's: use this user's own.
71HOME=$(own_home) || { echo "Equipo could not find this Mac user's home folder." >&2; exit 1; }
72export HOME
73scratch=$(make_scratch "$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null || true)" darwin)
74trap 'rm -rf "$scratch"' EXIT
75trap 'exit 1' HUP INT TERM
76curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/install-manifest.json" -o "$scratch/manifest.json"
77[ "$(plutil -extract version raw -o - "$scratch/manifest.json")" = "$version" ] || exit 1
78digest=$(plutil -extract 'targets.darwin/arm64.runner_sha256' raw -o - "$scratch/manifest.json")
79case "$digest" in *[!0-9a-f]*|'') exit 1 ;; esac
80[ "${#digest}" -eq 64 ] || exit 1
81curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/darwin-arm64/equipo-runner" -o "$scratch/equipo-runner"
82printf '%s %s\n' "$digest" "$scratch/equipo-runner" | shasum -a 256 --check --status
83chmod 700 "$scratch/equipo-runner"
84"$scratch/equipo-runner" install --release "$version" --origin "$origin" --instance "$instance"
85}
86
87main() {
88set -eu
89umask 077
90# su may leave a narrow PATH: the system's tools are always reachable.
91PATH="${PATH:-}${PATH:+:}/usr/bin:/bin:/usr/sbin:/sbin"
92export PATH
93case "$(uname -s)/$(uname -m)" in
94Linux/x86_64) platform=linux ;;
95Darwin/arm64) platform=darwin ;;
96*) exit 1 ;;
97esac
98origin=$1
99version=$2
100instance=$3
101case "$origin" in https://*) ;; *) exit 1 ;; esac
102case "$version" in ''|*[!a-zA-Z0-9._-]*|latest|dev) exit 1 ;; esac
103"$platform"
104}
105main "$@"
106