| 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). |
| 7 | own_home() { |
| 8 | user=$(id -un) |
| 9 | h= |
| 10 | if command -v dscl >/dev/null 2>&1; then |
| 11 | h=$(dscl . -read "/Users/$user" NFSHomeDirectory 2>/dev/null | sed -n 's/^NFSHomeDirectory: //p') |
| 12 | elif command -v getent >/dev/null 2>&1; then |
| 13 | h=$(getent passwd "$user" | cut -d: -f6) |
| 14 | fi |
| 15 | [ -n "$h" ] && [ -d "$h" ] || return 1 |
| 16 | printf '%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. |
| 24 | make_scratch() { |
| 25 | for 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 |
| 29 | done |
| 30 | if [ "$2" = darwin ]; then |
| 31 | mktemp -d 2>/dev/null && return 0 |
| 32 | fi |
| 33 | home=$(own_home) || return 1 |
| 34 | mkdir -p "$home/.cache/equipo-bootstrap" && chmod 700 "$home/.cache/equipo-bootstrap" && mktemp -d "$home/.cache/equipo-bootstrap/run-XXXXXXXX" |
| 35 | } |
| 36 | |
| 37 | linux() { |
| 38 | scratch=$(make_scratch "" linux) |
| 39 | trap 'rm -rf "$scratch"' EXIT |
| 40 | trap 'exit 1' HUP INT TERM |
| 41 | curl --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. |
| 43 | digest=$(python3 - "$scratch/manifest.json" "$version" <<'PY' |
| 44 | import json, re, sys |
| 45 | m = json.load(open(sys.argv[1])) |
| 46 | r = m['release'] |
| 47 | assert m['version'] == sys.argv[2] and m['target'] == 'linux/amd64' |
| 48 | # One build: the runner and tool proxy digests the release recorded. |
| 49 | t = m['targets']['linux/amd64'] |
| 50 | assert r['schema'] == 3 and r['runner_target'] == 'linux/amd64' |
| 51 | assert re.fullmatch('[0-9a-f]{64}', m['runner_sha256']) and t['runner_sha256'] == m['runner_sha256'] |
| 52 | assert re.fullmatch('[0-9a-f]{64}', t['tool_proxy_sha256']) |
| 53 | assert [b['sha256'] for b in r['binaries'] if b['name'] == 'equipo-tool-proxy'] == [t['tool_proxy_sha256']] |
| 54 | print(m['runner_sha256']) |
| 55 | PY |
| 56 | ) |
| 57 | curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/equipo-runner" -o "$scratch/equipo-runner" |
| 58 | printf '%s %s\n' "$digest" "$scratch/equipo-runner" | sha256sum --check --status |
| 59 | chmod 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. |
| 68 | darwin() { |
| 69 | [ "$(id -u)" != 0 ] || exit 1 |
| 70 | # Under su, HOME and TMPDIR may be the caller's: use this user's own. |
| 71 | HOME=$(own_home) || { echo "Equipo could not find this Mac user's home folder." >&2; exit 1; } |
| 72 | export HOME |
| 73 | scratch=$(make_scratch "$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null || true)" darwin) |
| 74 | trap 'rm -rf "$scratch"' EXIT |
| 75 | trap 'exit 1' HUP INT TERM |
| 76 | curl --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 |
| 78 | digest=$(plutil -extract 'targets.darwin/arm64.runner_sha256' raw -o - "$scratch/manifest.json") |
| 79 | case "$digest" in *[!0-9a-f]*|'') exit 1 ;; esac |
| 80 | [ "${#digest}" -eq 64 ] || exit 1 |
| 81 | curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/darwin-arm64/equipo-runner" -o "$scratch/equipo-runner" |
| 82 | printf '%s %s\n' "$digest" "$scratch/equipo-runner" | shasum -a 256 --check --status |
| 83 | chmod 700 "$scratch/equipo-runner" |
| 84 | "$scratch/equipo-runner" install --release "$version" --origin "$origin" --instance "$instance" |
| 85 | } |
| 86 | |
| 87 | main() { |
| 88 | set -eu |
| 89 | umask 077 |
| 90 | # su may leave a narrow PATH: the system's tools are always reachable. |
| 91 | PATH="${PATH:-}${PATH:+:}/usr/bin:/bin:/usr/sbin:/sbin" |
| 92 | export PATH |
| 93 | case "$(uname -s)/$(uname -m)" in |
| 94 | Linux/x86_64) platform=linux ;; |
| 95 | Darwin/arm64) platform=darwin ;; |
| 96 | *) exit 1 ;; |
| 97 | esac |
| 98 | origin=$1 |
| 99 | version=$2 |
| 100 | instance=$3 |
| 101 | case "$origin" in https://*) ;; *) exit 1 ;; esac |
| 102 | case "$version" in ''|*[!a-zA-Z0-9._-]*|latest|dev) exit 1 ;; esac |
| 103 | "$platform" |
| 104 | } |
| 105 | main "$@" |
| 106 | |