|
7 | 7 | # - Optional disk cache dir /var/lib/gapura/cache |
8 | 8 | # - Writes sshd config fragment /etc/ssh/sshd_config.d/99-gapura.conf |
9 | 9 | # |
10 | | -# Requires: sudo/root, OpenSSH, and a built binary at: |
11 | | -# sentinel/target/release/gapura-sentinel |
| 10 | +# Default behavior: download `gapura-sentinel` from GitHub Releases and install it. |
| 11 | +# Fallback: set `SENTINEL_BIN=/path/to/gapura-sentinel` to install a local binary. |
12 | 12 | set -euo pipefail |
13 | 13 |
|
14 | 14 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
15 | 15 |
|
16 | 16 | SENTINEL_BIN="${SENTINEL_BIN:-$ROOT/sentinel/target/release/gapura-sentinel}" |
| 17 | +REPO="${REPO:-sangkan-dev/gapura}" |
| 18 | +TMP_DIR="${TMP_DIR:-/tmp/gapura-install.$$}" |
17 | 19 | INSTALL_BIN_DIR="${INSTALL_BIN_DIR:-/usr/local/bin}" |
18 | 20 | CONFIG_DIR="${CONFIG_DIR:-/etc/gapura}" |
19 | 21 | CACHE_DIR="${CACHE_DIR:-/var/lib/gapura/cache}" |
20 | 22 | SSHD_FRAGMENT="${SSHD_FRAGMENT:-/etc/ssh/sshd_config.d/99-gapura.conf}" |
21 | 23 | SENTINEL_USER="${SENTINEL_USER:-gapura-sentinel}" |
22 | 24 |
|
23 | | -if [[ ! -x "$SENTINEL_BIN" ]]; then |
24 | | - echo "missing executable: $SENTINEL_BIN" >&2 |
25 | | - echo "build it with: (cd sentinel && cargo build --release)" >&2 |
26 | | - exit 1 |
| 25 | +need() { |
| 26 | + command -v "$1" >/dev/null 2>&1 || { echo "missing dependency: $1" >&2; exit 1; } |
| 27 | +} |
| 28 | + |
| 29 | +download_latest_release() { |
| 30 | + need curl |
| 31 | + need jq |
| 32 | + need sha256sum |
| 33 | + |
| 34 | + mkdir -p "$TMP_DIR" |
| 35 | + local api="https://api.github.com/repos/$REPO/releases/latest" |
| 36 | + echo "==> fetching latest release: $REPO" |
| 37 | + local json |
| 38 | + json="$(curl -fsSL "$api")" |
| 39 | + |
| 40 | + local url_bin url_sums |
| 41 | + url_bin="$(echo "$json" | jq -r '.assets[] | select(.name=="gapura-sentinel-linux-x86_64") | .browser_download_url' | head -n1)" |
| 42 | + url_sums="$(echo "$json" | jq -r '.assets[] | select(.name=="SHA256SUMS") | .browser_download_url' | head -n1)" |
| 43 | + |
| 44 | + if [[ -z "$url_bin" || "$url_bin" == "null" ]]; then |
| 45 | + echo "release asset not found: gapura-sentinel-linux-x86_64" >&2 |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + if [[ -z "$url_sums" || "$url_sums" == "null" ]]; then |
| 49 | + echo "release asset not found: SHA256SUMS" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + |
| 53 | + echo "==> downloading artifacts" |
| 54 | + curl -fL "$url_bin" -o "$TMP_DIR/gapura-sentinel-linux-x86_64" |
| 55 | + curl -fL "$url_sums" -o "$TMP_DIR/SHA256SUMS" |
| 56 | + |
| 57 | + echo "==> verifying checksum" |
| 58 | + (cd "$TMP_DIR" && sha256sum -c SHA256SUMS --ignore-missing) |
| 59 | + |
| 60 | + chmod +x "$TMP_DIR/gapura-sentinel-linux-x86_64" |
| 61 | + SENTINEL_BIN="$TMP_DIR/gapura-sentinel-linux-x86_64" |
| 62 | +} |
| 63 | + |
| 64 | +cleanup() { |
| 65 | + if [[ "${TMP_DIR:-}" == /tmp/gapura-install.* && -d "${TMP_DIR:-}" ]]; then |
| 66 | + rm -rf "$TMP_DIR" || true |
| 67 | + fi |
| 68 | +} |
| 69 | +trap cleanup EXIT |
| 70 | + |
| 71 | +if [[ -x "$SENTINEL_BIN" ]]; then |
| 72 | + echo "==> installing from local binary: $SENTINEL_BIN" |
| 73 | +else |
| 74 | + download_latest_release |
27 | 75 | fi |
28 | 76 |
|
29 | 77 | sudo mkdir -p "$INSTALL_BIN_DIR" |
|
0 commit comments