#!/bin/sh # Install prebuilt `senate` and `senated` binaries. # # Usage: # curl -fsSL https://senate.elata.ai/install.sh | sh # curl -fsSL https://senate.elata.ai/install.sh | sh -s -- --version v0.15.0 # # By default everything (the latest tag, the archive and SHA256SUMS.txt) is # fetched from the Senate website, which proxies GitHub Releases through its # own edge, so no direct GitHub access is needed. # # Environment variables: # SENATE_VERSION Release tag to install (default: latest release) # SENATE_INSTALL_DIR Destination directory (default: $HOME/.local/bin) # SENATE_BASE_URL Website origin (default: https://senate.elata.ai) # SENATE_DOWNLOAD_BASE_URL Directory holding the release's archives and # SHA256SUMS.txt (default: # $SENATE_BASE_URL/releases/download/) # SENATE_GITHUB_FALLBACK 1 (default): retry from GitHub Releases if the # website is unreachable; 0: never contact GitHub # SENATE_REPO GitHub repository for the fallback # (default: oops-rs/senate) set -eu REPO="${SENATE_REPO:-oops-rs/senate}" VERSION="${SENATE_VERSION:-}" INSTALL_DIR="${SENATE_INSTALL_DIR:-$HOME/.local/bin}" BASE_URL="${SENATE_BASE_URL:-https://senate.elata.ai}" BASE_URL="${BASE_URL%/}" DOWNLOAD_BASE_URL="${SENATE_DOWNLOAD_BASE_URL:-}" GITHUB_FALLBACK="${SENATE_GITHUB_FALLBACK:-1}" DRY_RUN=0 say() { printf 'senate-install: %s\n' "$*" } warn() { printf 'senate-install: warning: %s\n' "$*" >&2 } die() { printf 'senate-install: error: %s\n' "$*" >&2 exit 1 } usage() { cat <<'EOF' Install prebuilt `senate` and `senated` binaries. Usage: install.sh [options] Options: -v, --version Release tag or version to install (e.g. v0.15.0 or 0.15.0) -d, --dir Install directory (default: ~/.local/bin) --github Download straight from GitHub Releases --no-fallback Never contact GitHub, even if the website is unreachable --dry-run Print resolved target, version, URLs, and exit -h, --help Show this help message Environment: SENATE_VERSION Same as --version SENATE_INSTALL_DIR Same as --dir SENATE_BASE_URL Website origin (default: https://senate.elata.ai) SENATE_DOWNLOAD_BASE_URL Directory with the release archives + SHA256SUMS.txt SENATE_GITHUB_FALLBACK 1 (default) retry from GitHub on failure, 0 never SENATE_REPO GitHub repository (default: oops-rs/senate) EOF } USE_GITHUB=0 while [ $# -gt 0 ]; do case "$1" in -v|--version) [ $# -ge 2 ] || die "$1 requires an argument" VERSION="$2" shift 2 ;; -d|--dir) [ $# -ge 2 ] || die "$1 requires an argument" INSTALL_DIR="$2" shift 2 ;; --github) USE_GITHUB=1 shift ;; --no-fallback) GITHUB_FALLBACK=0 shift ;; --dry-run) DRY_RUN=1 shift ;; -h|--help) usage exit 0 ;; *) die "unknown argument: $1 (run with --help for usage)" ;; esac done detect_target() { os="$(uname -s 2>/dev/null || echo unknown)" arch="$(uname -m 2>/dev/null || echo unknown)" case "$os" in Darwin) # Under Rosetta, uname -m reports x86_64 on Apple silicon. if [ "$arch" = "x86_64" ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = "1" ]; then arch="arm64" fi case "$arch" in arm64|aarch64) echo "aarch64-apple-darwin" ;; x86_64|amd64) echo "x86_64-apple-darwin" ;; *) die "unsupported macOS architecture: $arch" ;; esac ;; Linux) case "$arch" in x86_64|amd64) echo "x86_64-unknown-linux-gnu" ;; aarch64|arm64) echo "aarch64-unknown-linux-gnu" ;; *) die "unsupported Linux architecture: $arch" ;; esac ;; MINGW*|MSYS*|CYGWIN*) die "detected Windows shell ($os). Run PowerShell instead: irm ${BASE_URL}/install.ps1 | iex" ;; *) die "unsupported operating system: $os" ;; esac } have_cmd() { command -v "$1" >/dev/null 2>&1 } # HTTPS URLs may only redirect to HTTPS. Plain http:// is used only when an # override explicitly asks for it (e.g. an internal mirror or local testing). download() { url="$1" dest="$2" case "$url" in https://*) curl_tls="--proto =https --tlsv1.2"; wget_tls="--https-only" ;; *) curl_tls=""; wget_tls="" ;; esac if have_cmd curl; then # shellcheck disable=SC2086 curl -fsSL $curl_tls --retry 3 --retry-delay 1 "$url" -o "$dest" elif have_cmd wget; then # shellcheck disable=SC2086 wget -q $wget_tls -O "$dest" "$url" else die "curl or wget is required to download release archives" fi } fetch_text() { case "$1" in https://*) curl_tls="--proto =https --tlsv1.2"; wget_tls="--https-only" ;; *) curl_tls=""; wget_tls="" ;; esac if have_cmd curl; then # shellcheck disable=SC2086 curl -fsSL $curl_tls --retry 2 --retry-delay 1 "$1" 2>/dev/null elif have_cmd wget; then # shellcheck disable=SC2086 wget -q $wget_tls -O- "$1" 2>/dev/null else die "curl or wget is required" fi } is_valid_tag() { printf '%s\n' "$1" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z][0-9A-Za-z.-]*)?$' } resolve_latest_tag_website() { fetch_text "${BASE_URL}/releases/latest" | tr -d '\r' | head -n 1 || true } resolve_latest_tag_github() { tag="" if have_cmd curl; then effective="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/${REPO}/releases/latest" 2>/dev/null || true)" case "$effective" in */releases/tag/*) tag="${effective##*/releases/tag/}" ;; esac fi if [ -z "$tag" ]; then tag="$(fetch_text "https://api.github.com/repos/${REPO}/releases/latest" \ | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' \ | head -n 1 || true)" fi printf '%s\n' "$tag" } resolve_latest_tag() { tag="" if [ "$USE_GITHUB" -eq 0 ]; then tag="$(resolve_latest_tag_website)" if ! is_valid_tag "$tag"; then tag="" if [ "$GITHUB_FALLBACK" = "1" ]; then warn "could not resolve the latest release from ${BASE_URL}; trying GitHub" fi fi fi if [ -z "$tag" ] && { [ "$USE_GITHUB" -eq 1 ] || [ "$GITHUB_FALLBACK" = "1" ]; }; then tag="$(resolve_latest_tag_github)" fi is_valid_tag "$tag" || die "could not resolve the latest release tag (pass --version vX.Y.Z explicitly)" printf '%s\n' "$tag" } sha256_of() { if have_cmd sha256sum; then sha256sum "$1" | awk '{ print $1 }' elif have_cmd shasum; then shasum -a 256 "$1" | awk '{ print $1 }' elif have_cmd openssl; then openssl dgst -sha256 "$1" | awk '{ print $NF }' else die "sha256sum, shasum, or openssl is required to verify download integrity" fi } verify_sha256() { archive_file="$1" sums_file="$2" archive_name="$(basename "$archive_file")" expected="$(awk -v name="$archive_name" '$2 == name || $2 == "*" name { print tolower($1); exit }' "$sums_file")" printf '%s\n' "$expected" | grep -Eq '^[0-9a-f]{64}$' \ || die "checksum entry for ${archive_name} not found in SHA256SUMS.txt" actual="$(sha256_of "$archive_file" | tr 'A-F' 'a-f')" if [ "$actual" != "$expected" ]; then die "SHA-256 mismatch for ${archive_name}: expected ${expected}, got ${actual} (refusing to install)" fi say "verified SHA-256 (${actual})" } TARGET="$(detect_target)" if [ -z "$VERSION" ]; then VERSION="$(resolve_latest_tag)" else case "$VERSION" in v*) ;; *) VERSION="v${VERSION}" ;; esac is_valid_tag "$VERSION" || die "invalid version: ${VERSION} (expected vX.Y.Z)" fi ARCHIVE_NAME="senate-${VERSION}-${TARGET}.tar.gz" GITHUB_BASE_URL="https://github.com/${REPO}/releases/download/${VERSION}" if [ -n "$DOWNLOAD_BASE_URL" ]; then PRIMARY_BASE_URL="${DOWNLOAD_BASE_URL%/}" elif [ "$USE_GITHUB" -eq 1 ]; then PRIMARY_BASE_URL="$GITHUB_BASE_URL" else PRIMARY_BASE_URL="${BASE_URL}/releases/download/${VERSION}" fi if [ "$DRY_RUN" -eq 1 ]; then say "dry run:" printf ' version: %s\n' "$VERSION" printf ' target: %s\n' "$TARGET" printf ' archive: %s\n' "${PRIMARY_BASE_URL}/${ARCHIVE_NAME}" printf ' checksums: %s\n' "${PRIMARY_BASE_URL}/SHA256SUMS.txt" if [ "$GITHUB_FALLBACK" = "1" ] && [ "$PRIMARY_BASE_URL" != "$GITHUB_BASE_URL" ]; then printf ' fallback: %s\n' "${GITHUB_BASE_URL}/${ARCHIVE_NAME}" else printf ' fallback: (none)\n' fi printf ' install dir: %s\n' "$INSTALL_DIR" exit 0 fi have_cmd tar || die "tar is required to unpack ${ARCHIVE_NAME}" TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/senate-install.XXXXXX")" trap 'rm -rf "$TMP_DIR"' EXIT INT TERM # Downloads the archive and SHA256SUMS.txt from one base URL. Both files come # from the same place so a partial fallback can never mix sources. fetch_release() { base="$1" rm -f "${TMP_DIR}/${ARCHIVE_NAME}" "${TMP_DIR}/SHA256SUMS.txt" download "${base}/${ARCHIVE_NAME}" "${TMP_DIR}/${ARCHIVE_NAME}" \ && download "${base}/SHA256SUMS.txt" "${TMP_DIR}/SHA256SUMS.txt" } say "downloading ${ARCHIVE_NAME} (${VERSION}) from ${PRIMARY_BASE_URL%%/releases/*}..." if ! fetch_release "$PRIMARY_BASE_URL"; then if [ "$GITHUB_FALLBACK" = "1" ] && [ "$PRIMARY_BASE_URL" != "$GITHUB_BASE_URL" ]; then warn "download from ${PRIMARY_BASE_URL} failed; retrying from GitHub Releases" fetch_release "$GITHUB_BASE_URL" \ || die "failed to download ${ARCHIVE_NAME} (check that release ${VERSION} has prebuilt binaries for ${TARGET})" else die "failed to download ${PRIMARY_BASE_URL}/${ARCHIVE_NAME} (check that release ${VERSION} has prebuilt binaries for ${TARGET})" fi fi # Fail closed: a missing entry or a mismatch aborts before anything is installed. verify_sha256 "${TMP_DIR}/${ARCHIVE_NAME}" "${TMP_DIR}/SHA256SUMS.txt" tar -xzf "${TMP_DIR}/${ARCHIVE_NAME}" -C "$TMP_DIR" UNPACKED="${TMP_DIR}/senate-${VERSION}-${TARGET}" [ -f "${UNPACKED}/senate" ] && [ -f "${UNPACKED}/senated" ] \ || die "archive ${ARCHIVE_NAME} did not contain expected senate and senated binaries" mkdir -p "$INSTALL_DIR" # Install atomically via sibling temp files so running binaries (especially on # macOS) are never overwritten in place. for bin in senate senated; do tmp_bin="${INSTALL_DIR}/.${bin}.new.$$" cp "${UNPACKED}/${bin}" "$tmp_bin" chmod 755 "$tmp_bin" mv -f "$tmp_bin" "${INSTALL_DIR}/${bin}" done say "installed into ${INSTALL_DIR}:" printf ' %s\n' "$("${INSTALL_DIR}/senate" --version)" printf ' %s\n' "$("${INSTALL_DIR}/senated" --version)" case ":${PATH:-}:" in *":${INSTALL_DIR}:"*) existing="$(command -v senate 2>/dev/null || true)" if [ -n "$existing" ] && [ "$existing" != "${INSTALL_DIR}/senate" ]; then warn "another 'senate' appears earlier on PATH: ${existing}" warn "put ${INSTALL_DIR} ahead of $(dirname "$existing") on PATH or remove the older copy" fi ;; *) warn "${INSTALL_DIR} is not on your PATH." printf ' Add it to your shell profile:\n' printf ' export PATH="%s:$PATH"\n' "$INSTALL_DIR" ;; esac printf '\nNext steps:\n' printf ' First time: senate setup init --local --person && senate setup install --yes && senate setup service install --yes\n' printf ' Upgrading: senate setup service install --yes && senate setup doctor\n'