#!/bin/sh # Superatom one-line installer: curl -fsSL https://install.superatom.ai | sh # # New install: asks for an install token (super-admin console → project → "Generate # install token"), prepares the install folder, and only then redeems the token, downloads # the code and runs ./setup.sh --docker with the returned config pre-filled. # Re-run: pick an install to update it (the project's API key authenticates the download) # or to resume one whose setup didn't finish (no new token needed). # No terminal (cloud-init, CI, an agent): curl … | SA_INSTALL_TOKEN= [SA_INSTALL_DIR=] sh # # POSIX sh on purpose (piped into dash on Ubuntu). Server responses are KEY=value # lines, so nothing beyond curl + tar is needed on a fresh VM. # # Overrides (testing): SA_INSTALL_URL (default https://install.superatom.ai), # SA_INSTALL_ROOT (default /opt/superatom) set -eu BASE_URL="${SA_INSTALL_URL:-https://dev.install.superatom.ai}" INSTALL_ROOT="${SA_INSTALL_ROOT:-/opt/superatom}" REGISTRY="${HOME}/.superatom-installs" # install folders outside INSTALL_ROOT, one per line MARKER=".superatom-install" # per install: project id, installed commit MANIFEST=".superatom-files" # per install: files that came from the tarball PREFILL=".install.env" # redeemed config; setup.sh deletes it once backend/.env is written if [ -t 1 ]; then RED=$(printf '\033[0;31m'); GREEN=$(printf '\033[0;32m'); YELLOW=$(printf '\033[1;33m') CYAN=$(printf '\033[0;36m'); BLUE=$(printf '\033[0;34m'); NC=$(printf '\033[0m') else RED=""; GREEN=""; YELLOW=""; CYAN=""; BLUE=""; NC="" fi step() { printf '%s➤ %s%s\n' "$CYAN" "$1" "$NC"; } ok() { printf '%s✓ %s%s\n' "$GREEN" "$1" "$NC"; } warn() { printf '%s⚠ %s%s\n' "$YELLOW" "$1" "$NC"; } die() { printf '%s✗ %s%s\n' "$RED" "$1" "$NC" >&2; exit 1; } TMP_DIR="" TTY="" # set by check_system when a terminal can be opened USERNS_SYSCTL=/proc/sys/kernel/apparmor_restrict_unprivileged_userns # return 0: as an EXIT trap its status replaces the script's, and the updater # exits 0 on every "nothing to do" tick. cleanup() { [ -n "$TMP_DIR" ] && rm -rf "$TMP_DIR"; return 0; } trap cleanup EXIT INT TERM # True when a terminal can actually be opened; `[ -r /dev/tty ]` passes without one (ssh, cloud-init). has_tty() { (: < /dev/tty) 2> /dev/null } # Reads a line from the terminal (stdin is this script when piped into sh). # Without a terminal every question takes its default (REPLY=""). ask() { if [ -z "$TTY" ]; then printf '%s(default)\n' "$1"; REPLY=""; return 0; fi printf '%s%s%s' "$CYAN" "$1" "$NC" > /dev/tty IFS= read -r REPLY < /dev/tty || REPLY="" } ask_secret() { printf '%s%s%s' "$CYAN" "$1" "$NC" > /dev/tty stty -echo < /dev/tty 2>/dev/null || true IFS= read -r REPLY < /dev/tty || REPLY="" stty echo < /dev/tty 2>/dev/null || true printf '\n' > /dev/tty } check_system() { case "$(uname -s)" in Linux) ;; Darwin) warn "macOS: needs Docker Desktop running. Linux is the supported target for clients." ;; MINGW*|MSYS*|CYGWIN*) die "Windows shells are not supported. Use WSL2 (Ubuntu) on Windows Server 2022+, or a Linux VM." ;; *) die "Unsupported OS: $(uname -s)" ;; esac for cmd in curl tar bash; do command -v "$cmd" > /dev/null 2>&1 || die "'$cmd' is required. Install it and re-run." done # A root-run installer leaves the stack root-owned, and the user's next docker/update fails. if [ "$(id -u)" = "0" ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then die "Don't run the installer with sudo. Run it as $SUDO_USER; it asks for sudo itself where needed." fi if has_tty; then TTY=1 elif [ -z "${SA_INSTALL_TOKEN:-}" ] && [ -z "${SA_INSTALL_DIR:-}" ]; then die "No terminal. Run this from an interactive shell, or pass the token: SA_INSTALL_TOKEN= [SA_INSTALL_DIR=] sh (resume an install: SA_INSTALL_DIR= sh)" fi } # The coding agent's sandbox needs unprivileged user namespaces; Ubuntu 24.04+ restricts them by default. # setup.sh loads an AppArmor profile for it, so stop only where that cannot work: before the token is # redeemed, so fixing the host costs no new token. check_userns() { [ "$(cat "$USERNS_SYSCTL" 2> /dev/null || true)" = "1" ] || return 0 command -v apparmor_parser > /dev/null 2>&1 && return 0 printf '%s✗ This host blocks the coding agent'"'"'s sandbox (kernel.apparmor_restrict_unprivileged_userns = 1)%s\n' "$RED" "$NC" >&2 printf ' and has no apparmor_parser to allow it for the sandbox alone. Install AppArmor (sudo apt-get install apparmor),\n' >&2 printf ' or allow user namespaces host-wide, then run the installer again; the token is still unused:\n' >&2 printf ' sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0\n' >&2 printf ' echo kernel.apparmor_restrict_unprivileged_userns=0 | sudo tee /etc/sysctl.d/99-superatom-userns.conf\n' >&2 exit 1 } # Refuses root working on a folder another user owns (the leftover files break that user's next run). # $1 = folder refuse_foreign_owner() { [ "$(id -u)" = "0" ] && [ -e "$1" ] || return 0 owner=$(stat -c '%U' "$1" 2> /dev/null || ls -ld "$1" | awk '{print $3}') [ "$owner" = "root" ] && return 0 die "$1 belongs to $owner; run this as $owner, not root. (Already mixed up? sudo chown -R $owner:$owner $1)" } # POSTs a JSON body; server replies with KEY=value lines (or an error message). # Usage: api_post [bearer] api_post() { if [ -n "${4:-}" ]; then status=$(curl -sS -o "$3" -w '%{http_code}' -X POST -H 'Content-Type: application/json' \ -H "Authorization: Bearer $4" --data "$2" "$BASE_URL$1") || die "Cannot reach $BASE_URL" else status=$(curl -sS -o "$3" -w '%{http_code}' -X POST -H 'Content-Type: application/json' \ --data "$2" "$BASE_URL$1") || die "Cannot reach $BASE_URL" fi if [ "$status" != "200" ]; then die "Server refused ($status): $(head -c 300 "$3" | tr -d '\r' | head -n 1)" fi } # Loads allowlisted KEY=value lines from a server response into R_ variables. parse_response() { while IFS= read -r line || [ -n "$line" ]; do case "$line" in ''|'#'*) continue ;; *=*) ;; *) continue ;; esac key=${line%%=*} val=${line#*=} case "$key" in INSTALL_NAME|CODE_URL|CODE_COMMIT|SUPERATOM_API_KEY|SUPERATOM_PROJECT_ID|\ SA_API_URL|SA_ORG_ID|SA_ORG_SLUG|SA_WEBSOCKET_URL|OPENROUTER_API_KEY|SA_INTERNAL_SERVICE_TOKEN|RESEND_API_KEY|\ PLATFORM_UI_URL|USER_UI_URL|\ RELEASE_VERSION|IMAGE|IMAGE_DIGEST|REGISTRY_HOST|BUNDLE_URL|BUNDLE_SHA256|ENGINE_VERSION|COMPOSE_VERSION) ;; *) continue ;; esac # Values land in env files and shell variables: refuse anything that could be interpreted. case "$val" in *[\"\'\`\$\\\ ]*) die "Unexpected characters in server value for $key" ;; esac eval "R_$key=\$val" done < "$1" } valid_name() { case "$1" in ''|-*|*[!a-z0-9_-]*) return 1 ;; esac } value_in() { grep -E "^$2=" "$1" 2>/dev/null | tail -n 1 | cut -d '=' -f 2- || true } # Creates an empty, writable install folder (with sudo if needed, then owned by the user). # Runs BEFORE the token is redeemed, so a bad folder never costs the one-time token. prepare_dir() { dir=$1 if [ -e "$dir" ] && [ ! -d "$dir" ]; then die "$dir exists and is not a folder."; fi if [ -d "$dir" ] && [ -n "$(ls -A "$dir" 2>/dev/null)" ]; then die "$dir already exists and is not empty. Choose another folder." fi if mkdir -p "$dir" 2>/dev/null && [ -w "$dir" ]; then return 0; fi # Under the default root, own the root itself so later installs don't need sudo. case "$dir" in "$INSTALL_ROOT"/*) owned=$INSTALL_ROOT ;; *) owned=$dir ;; esac command -v sudo > /dev/null 2>&1 || die "Cannot create $dir (no permission, and sudo is not available)." ask "Creating $owned needs sudo. Continue? [Y/n]: " case "$REPLY" in [Nn]*) die "Choose a folder you can write to (e.g. \$HOME/)." ;; esac sudo mkdir -p "$owned" && sudo chown "$(id -u):$(id -g)" "$owned" || die "Could not create $owned with sudo." mkdir -p "$dir" 2>/dev/null && [ -w "$dir" ] || die "$dir is still not writable." } remember_install() { case "$1" in "$INSTALL_ROOT"/*) return 0 ;; esac # found by scanning the root anyway touch "$REGISTRY" 2>/dev/null || return 0 grep -qxF "$1" "$REGISTRY" 2>/dev/null || printf '%s\n' "$1" >> "$REGISTRY" } # The source tarball, for a project that has no release yet. Kept so existing clients # keep updating on the build path until a release is published for each of them. # $1 = url, $2 = commit (for the message). download_code() { step "Downloading code (commit ${2:-unknown})..." rm -rf "$TMP_DIR/code" && mkdir -p "$TMP_DIR/code" curl -fsSL -o "$TMP_DIR/code.tar.gz" "$1" || die "Code download failed. Re-run the installer to retry." tar -xzf "$TMP_DIR/code.tar.gz" -C "$TMP_DIR/code" --strip-components=1 || die "The code archive is corrupt." [ -f "$TMP_DIR/code/setup.sh" ] && [ -f "$TMP_DIR/code/docker-compose.yml" ] \ || die "The downloaded code has no Docker setup (setup.sh / docker-compose.yml)." (cd "$TMP_DIR/code" && find . -type f | sed 's|^\./||' | LC_ALL=C sort) > "$TMP_DIR/files.new" } # Takes the release bundle when the project has one, else the source tarball. # $1 = dir (for the current version, only used in messages). fetch_code() { if [ -n "$R_IMAGE" ] && [ -n "$R_BUNDLE_URL" ]; then download_bundle "$R_BUNDLE_URL" "$R_BUNDLE_SHA256" "$R_RELEASE_VERSION" else download_code "$R_CODE_URL" "$R_CODE_COMMIT" fi } # Downloads the release's deploy bundle into $TMP_DIR/code: the source tree the image # was built from, its compose file and setup.sh, and RELEASE.md. Pinned to the image. # $1 = url, $2 = expected sha256, $3 = version (for the message). download_bundle() { step "Downloading the deploy bundle (release ${3:-unknown})..." rm -rf "$TMP_DIR/code" && mkdir -p "$TMP_DIR/code" curl -fsSL -o "$TMP_DIR/bundle.tar.gz" "$1" \ || die "Bundle download failed. Re-run the installer to retry (no new token needed)." if [ -n "$2" ]; then got=$(sha256sum "$TMP_DIR/bundle.tar.gz" | cut -d ' ' -f1) [ "$got" = "$2" ] || die "Bundle checksum mismatch (published $2, downloaded $got)." fi tar -xzf "$TMP_DIR/bundle.tar.gz" -C "$TMP_DIR/code" || die "The bundle is corrupt. Re-run to retry." [ -f "$TMP_DIR/code/setup.sh" ] && [ -f "$TMP_DIR/code/docker-compose.yml" ] \ || die "The bundle has no setup.sh / docker-compose.yml." (cd "$TMP_DIR/code" && find . -type f | sed 's|^\./||' | LC_ALL=C sort) > "$TMP_DIR/files.new" } # Copies the downloaded code into $1; removes files a previous version had but this one dropped. # Config (.env files) and Docker volumes are never in the archive, so they are untouched. place_code() { if [ -f "$1/$MANIFEST" ]; then LC_ALL=C sort "$1/$MANIFEST" > "$TMP_DIR/files.old" LC_ALL=C comm -23 "$TMP_DIR/files.old" "$TMP_DIR/files.new" | while IFS= read -r gone; do case "$gone" in ''|/*|*..*) continue ;; esac rm -f "$1/$gone" done fi cp -a "$TMP_DIR/code/." "$1/" cp "$TMP_DIR/files.new" "$1/$MANIFEST" } # The marker also tells setup.sh's summary where updates come from and the client UI links; # the links are only known at redeem, so later rewrites (updates) keep the recorded ones. write_marker() { platform=${R_PLATFORM_UI_URL:-$(value_in "$1/$MARKER" PLATFORM_UI_URL)} user_ui=${R_USER_UI_URL:-$(value_in "$1/$MARKER" USER_UI_URL)} install_name=${R_INSTALL_NAME:-$(value_in "$1/$MARKER" INSTALL_NAME)} { printf 'PROJECT_ID=%s\n' "$2" printf 'CODE_COMMIT=%s\n' "$3" printf 'INSTALL_URL=%s\n' "$BASE_URL" [ -n "$install_name" ] && printf 'INSTALL_NAME=%s\n' "$install_name" [ -n "${R_RELEASE_VERSION:-}" ] && printf 'RELEASE_VERSION=%s\n' "$R_RELEASE_VERSION" [ -n "${R_IMAGE:-}" ] && printf 'IMAGE=%s\n' "$R_IMAGE" [ -n "${R_IMAGE_DIGEST:-}" ] && printf 'IMAGE_DIGEST=%s\n' "$R_IMAGE_DIGEST" [ -n "${R_REGISTRY_HOST:-}" ] && printf 'REGISTRY_HOST=%s\n' "$R_REGISTRY_HOST" [ -n "${R_ENGINE_VERSION:-}" ] && printf 'ENGINE_VERSION=%s\n' "$R_ENGINE_VERSION" [ -n "${R_COMPOSE_VERSION:-}" ] && printf 'COMPOSE_VERSION=%s\n' "$R_COMPOSE_VERSION" [ -n "$platform" ] && printf 'PLATFORM_UI_URL=%s\n' "$platform" [ -n "$user_ui" ] && printf 'USER_UI_URL=%s\n' "$user_ui" printf 'UPDATED_AT=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" } > "$1/$MARKER.tmp" mv "$1/$MARKER.tmp" "$1/$MARKER" } run_setup() { cd "$1" step "Running ./setup.sh --docker ${SETUP_ARGS:+$SETUP_ARGS }in $1" echo "" # No terminal (SA_INSTALL_TOKEN): setup.sh takes every default and never reads input. if [ -z "$TTY" ]; then if [ -f "$PREFILL" ]; then SA_UNATTENDED=1 exec bash ./setup.sh --docker ${SETUP_ARGS:-} --config "$PREFILL" < /dev/null fi SA_UNATTENDED=1 exec bash ./setup.sh --docker ${SETUP_ARGS:-} < /dev/null fi if [ -f "$PREFILL" ]; then exec bash ./setup.sh --docker ${SETUP_ARGS:-} --config "$PREFILL" < /dev/tty fi exec bash ./setup.sh --docker ${SETUP_ARGS:-} < /dev/tty } # The release a project should run, authenticated by its API key. # $1 = api key, $2 = project id, $3 = install name (optional), $4 = running version (optional) fetch_update() { case "$2" in ''|*[!A-Za-z0-9-]*) die "Unexpected project id: $2" ;; esac body="{\"projectId\":\"$2\"" [ -n "${3:-}" ] && body="$body,\"installName\":\"$3\"" [ -n "${4:-}" ] && body="$body,\"currentVersion\":\"$4\"" api_post "/api/update" "$body}" "$TMP_DIR/update.txt" "$1" R_CODE_URL=""; R_CODE_COMMIT=""; R_IMAGE=""; R_BUNDLE_URL=""; R_BUNDLE_SHA256=""; R_RELEASE_VERSION="" parse_response "$TMP_DIR/update.txt" # No release is normal while a client is still on the build path: the server # answers with the source contract instead, and fetch_code follows it. if [ -z "$R_IMAGE" ] || [ -z "$R_BUNDLE_URL" ]; then [ -n "$R_CODE_URL" ] || die "Incomplete response from the server." fi } new_install() { if [ -n "${SA_INSTALL_TOKEN:-}" ]; then REPLY=$SA_INSTALL_TOKEN; else ask_secret "Install token: "; fi token=$(printf '%s' "$REPLY" | tr -d '[:space:]') case "$token" in ''|*[!A-Za-z0-9_-]*) die "That doesn't look like an install token." ;; esac # 1. Check the token without using it. step "Checking install token..." api_post "/api/token-info" "{\"token\":\"$token\"}" "$TMP_DIR/info.txt" R_INSTALL_NAME="" parse_response "$TMP_DIR/info.txt" valid_name "$R_INSTALL_NAME" || die "Server returned an invalid install name." ok "Token is valid for $R_INSTALL_NAME" check_userns # 2. Prepare the folder while the token is still unused. if [ -n "${SA_INSTALL_DIR:-}" ]; then REPLY=$SA_INSTALL_DIR; else ask "Install folder [$INSTALL_ROOT/$R_INSTALL_NAME]: "; fi dir=${REPLY:-$INSTALL_ROOT/$R_INSTALL_NAME} case "$dir" in "~"/*) dir="$HOME/${dir#\~/}" ;; /*) ;; *) dir="$(pwd)/$dir" ;; esac prepare_dir "$dir" ok "Folder ready: $dir" # 3. Redeem, and save the config immediately so nothing after this can lose it. step "Redeeming install token..." api_post "/api/redeem" "{\"token\":\"$token\"}" "$TMP_DIR/redeem.txt" R_CODE_URL=""; R_CODE_COMMIT=""; R_SUPERATOM_PROJECT_ID=""; R_PLATFORM_UI_URL=""; R_USER_UI_URL="" R_IMAGE=""; R_BUNDLE_URL=""; R_BUNDLE_SHA256=""; R_RELEASE_VERSION="" parse_response "$TMP_DIR/redeem.txt" valid_name "$R_INSTALL_NAME" || die "Server returned an invalid install name." [ -n "$R_SUPERATOM_PROJECT_ID" ] || die "Incomplete response from the server." umask 077 { printf '# From install token redemption; deleted by setup.sh once backend/.env is written.\n' printf 'PROJECT_NAME=%s\n' "$R_INSTALL_NAME" for key in SUPERATOM_API_KEY SUPERATOM_PROJECT_ID SA_API_URL SA_ORG_ID SA_ORG_SLUG SA_WEBSOCKET_URL \ OPENROUTER_API_KEY SA_INTERNAL_SERVICE_TOKEN RESEND_API_KEY; do eval "val=\${R_$key:-}" [ -n "$val" ] && printf '%s=%s\n' "$key" "$val" done } > "$dir/$PREFILL" umask 022 write_marker "$dir" "$R_SUPERATOM_PROJECT_ID" "" remember_install "$dir" # Only now: the token is spent, so the key must be saved before anything can fail. if [ -z "$R_IMAGE" ] || [ -z "$R_BUNDLE_URL" ]; then [ -n "$R_CODE_URL" ] \ || die "This project has no published release and no source to fall back to. Publish a release, then re-run the installer to resume." fi ok "Project $R_INSTALL_NAME · ${R_RELEASE_VERSION:-commit ${R_CODE_COMMIT:-?}} (config saved; re-run resumes if anything below fails)" # 4. The release bundle (or source), then setup, which pulls or builds to match. fetch_code "$dir" place_code "$dir" write_marker "$dir" "$R_SUPERATOM_PROJECT_ID" "$R_CODE_COMMIT" ok "Deploy bundle installed in $dir" run_setup "$dir" } # Redeemed earlier but setup never finished: fetch the code if it's missing, then run setup. resume_install() { dir=$1 refuse_foreign_owner "$dir" if [ ! -f "$dir/setup.sh" ]; then api_key=$(value_in "$dir/$PREFILL" SUPERATOM_API_KEY) project_id=$(value_in "$dir/$PREFILL" SUPERATOM_PROJECT_ID) [ -n "$api_key" ] && [ -n "$project_id" ] || die "No saved config in $dir/$PREFILL; cannot resume." step "Fetching the code for the unfinished install..." fetch_update "$api_key" "$project_id" fetch_code "$dir" place_code "$dir" write_marker "$dir" "$project_id" "$R_CODE_COMMIT" fi run_setup "$dir" } update_install() { dir=$1 refuse_foreign_owner "$dir" env_file="$dir/backend/.env" api_key=$(value_in "$env_file" SUPERATOM_API_KEY) project_id=$(value_in "$env_file" SUPERATOM_PROJECT_ID) [ -n "$api_key" ] && [ -n "$project_id" ] || die "No API key / project id in $env_file; cannot update." current=$(value_in "$dir/$MARKER" RELEASE_VERSION) step "Checking for updates..." fetch_update "$api_key" "$project_id" "$(value_in "$dir/$MARKER" INSTALL_NAME)" "$current" # A build-path install has no release version; it tracks the commit instead. if [ -n "$R_IMAGE" ]; then have="$current"; want="$R_RELEASE_VERSION" else have=$(value_in "$dir/$MARKER" CODE_COMMIT); want="$R_CODE_COMMIT" fi if [ -n "$want" ] && [ "$want" = "$have" ]; then ok "Already on the latest ($have)." ask "Restart anyway? [y/N]: " case "$REPLY" in [Yy]*) run_setup "$dir" ;; *) exit 0 ;; esac fi fetch_code "$dir" place_code "$dir" write_marker "$dir" "$project_id" "$R_CODE_COMMIT" ok "Updating ${have:-?} → ${want:-?}" run_setup "$dir" } # Installs under the root plus remembered folders elsewhere; one path per line. list_installs() { { if [ -d "$INSTALL_ROOT" ]; then for d in "$INSTALL_ROOT"/*/; do [ -f "$d$MARKER" ] && printf '%s\n' "${d%/}"; done fi if [ -f "$REGISTRY" ]; then while IFS= read -r d; do [ -f "$d/$MARKER" ] && printf '%s\n' "$d"; done < "$REGISTRY" fi } | LC_ALL=C sort -u } unfinished() { [ -f "$1/$PREFILL" ] && [ ! -f "$1/backend/.env" ] } # JSON-safe and bounded: these strings end up in a request body. esc() { printf '%s' "$1" | tr -d '"\\' | tr '\n\r\t' ' ' | cut -c1-300 } # Tells the control plane what this install now runs, or why it could not update. # Best-effort: a reporting failure must never fail an install. # $1 = dir, $2 = release version, $3 = error (optional) report_install() { rep_key=$(value_in "$1/backend/.env" SUPERATOM_API_KEY) rep_pid=$(value_in "$1/backend/.env" SUPERATOM_PROJECT_ID) rep_name=$(value_in "$1/$MARKER" INSTALL_NAME) [ -n "$rep_key" ] && [ -n "$rep_pid" ] && [ -n "$rep_name" ] || return 0 body="{\"projectId\":\"$rep_pid\",\"installName\":\"$(esc "$rep_name")\",\"releaseVersion\":\"$(esc "${2:-}")\"" [ -n "${3:-}" ] && body="$body,\"error\":\"$(esc "$3")\"" curl -fsS -o /dev/null -m 20 -X POST -H 'Content-Type: application/json' \ -H "Authorization: Bearer $rep_key" --data "$body}" "$BASE_URL/api/installed" > /dev/null 2>&1 || true } # The update the systemd agent runs: no prompts, no terminal, and it exits 0 when # the approved version is already running (the poll above recorded the heartbeat). unattended_update() { dir="$1" [ -n "$dir" ] || die "--unattended needs --dir " refuse_foreign_owner "$dir" [ -f "$dir/$MARKER" ] && [ -f "$dir/backend/.env" ] || die "No finished install in $dir" # Every die() from here on reports the failure, so the UI stops saying "updating". die() { printf '%s✗ %s%s\n' "$RED" "$1" "$NC" >&2 report_install "$dir" "$(value_in "$dir/$MARKER" RELEASE_VERSION)" "$1" exit 1 } api_key=$(value_in "$dir/backend/.env" SUPERATOM_API_KEY) project_id=$(value_in "$dir/backend/.env" SUPERATOM_PROJECT_ID) [ -n "$api_key" ] && [ -n "$project_id" ] || die "No API key / project id in $dir/backend/.env" TMP_DIR=$(mktemp -d) current=$(value_in "$dir/$MARKER" RELEASE_VERSION) fetch_update "$api_key" "$project_id" "$(value_in "$dir/$MARKER" INSTALL_NAME)" "$current" # Unattended runs only ever move between releases. A project still on the build # path has no approved target, so there is nothing to apply without a person. [ -n "$R_IMAGE" ] || exit 0 [ -n "$R_RELEASE_VERSION" ] && [ "$R_RELEASE_VERSION" != "$current" ] || exit 0 step "Updating ${current:-?} → $R_RELEASE_VERSION" download_bundle "$R_BUNDLE_URL" "$R_BUNDLE_SHA256" "$R_RELEASE_VERSION" place_code "$dir" # setup.sh reads the target from the marker, so it is written first; a failed apply puts the old # one back, else the agent sees target == current (no retry) and the VM claims the new version. cp "$dir/$MARKER" "$TMP_DIR/marker.prev" write_marker "$dir" "$project_id" "$R_CODE_COMMIT" restore_marker() { cp "$TMP_DIR/marker.prev" "$dir/$MARKER"; } # setup.sh reports the outcome once the stack is actually up. cd "$dir" || { restore_marker; die "Cannot enter $dir"; } SA_UNATTENDED=1 bash ./setup.sh --docker || { restore_marker; die "setup.sh failed while applying $R_RELEASE_VERSION"; } } # --enable-updates: the update agent for an install made without it; one command per VM. enable_updates() { dir="$1" if [ -z "$dir" ]; then found=$(list_installs) case "$(printf '%s\n' "$found" | grep -c .)" in 0) die "No Superatom install found under $INSTALL_ROOT. Pass --dir ." ;; 1) dir="$found" ;; *) die "More than one install here; pick one with --dir: $(printf '%s ' $found)" ;; esac fi [ -f "$dir/$MARKER" ] && [ -f "$dir/backend/.env" ] || die "No finished install in $dir" grep -q -- '--updater-only' "$dir/setup.sh" 2>/dev/null \ || die "$dir runs a release older than the update agent. Update it once by hand (re-run this installer without flags), then run --enable-updates again." step "Adding the update agent to $dir" (cd "$dir" && bash ./setup.sh --updater-only) || die "The update agent could not be installed in $dir" ok "Update agent installed: $dir now applies the version picked on the Updates page" } main() { printf '\n%sSuperatom installer%s\n\n' "$BLUE" "$NC" check_system TMP_DIR=$(mktemp -d) list_installs > "$TMP_DIR/installs.txt" # No terminal and no token: resume (or restart) the install SA_INSTALL_DIR names. if [ -z "$TTY" ] && [ -z "${SA_INSTALL_TOKEN:-}" ]; then [ -f "$SA_INSTALL_DIR/$MARKER" ] || die "No Superatom install in $SA_INSTALL_DIR." if unfinished "$SA_INSTALL_DIR"; then resume_install "$SA_INSTALL_DIR"; fi refuse_foreign_owner "$SA_INSTALL_DIR" run_setup "$SA_INSTALL_DIR" fi # A token means a new install (cloud-init, CI, an agent), unless it names an unfinished one to resume. if [ -n "${SA_INSTALL_TOKEN:-}" ]; then if [ -n "${SA_INSTALL_DIR:-}" ] && unfinished "$SA_INSTALL_DIR"; then resume_install "$SA_INSTALL_DIR"; fi new_install return fi if [ ! -s "$TMP_DIR/installs.txt" ]; then new_install return fi echo "Existing installs:" i=0 while IFS= read -r d; do i=$((i + 1)) if unfinished "$d"; then state="setup not finished — pick to resume"; else state="commit $(value_in "$d/$MARKER" CODE_COMMIT)"; fi printf ' %s) %s (%s)\n' "$i" "$d" "$state" done < "$TMP_DIR/installs.txt" echo " n) New install" ask "Choose [n]: " choice=${REPLY:-n} case "$choice" in n|N) new_install ;; *[!0-9]*) die "Invalid choice." ;; *) d=$(sed -n "${choice}p" "$TMP_DIR/installs.txt") [ -n "$d" ] || die "Invalid choice." if unfinished "$d"; then resume_install "$d"; fi update_install "$d" ;; esac } UNATTENDED_DIR="" ENABLE_UPDATES="" # Passed to setup.sh: the timer that applies the release an admin picks on the Updates page. # On by default: an install without it only updates when someone logs in to the VM. SETUP_ARGS="--with-updater" while [ $# -gt 0 ]; do case "$1" in --unattended) UNATTENDED="1" ;; --with-updater) SETUP_ARGS="--with-updater" ;; --no-updates) SETUP_ARGS="" ;; --enable-updates) ENABLE_UPDATES="1" ;; --dir) shift; UNATTENDED_DIR="${1:-}" ;; --help|-h) printf 'Usage: install.sh interactive install or update (with the update agent)\n' printf ' install.sh --no-updates ... without the update agent\n' printf ' install.sh --enable-updates [--dir D] add the update agent to an existing install\n' printf ' install.sh --unattended --dir D apply the approved release in D\n' printf '\nNo terminal (cloud-init, CI): SA_INSTALL_TOKEN= [SA_INSTALL_DIR=] sh install.sh\n' printf ' resume or restart an install: SA_INSTALL_DIR= sh install.sh\n' printf 'Environment: SA_INSTALL_TOKEN, SA_INSTALL_DIR, SA_INSTALL_URL, SA_INSTALL_ROOT\n' exit 0 ;; *) die "Unknown option: $1" ;; esac shift done if [ -n "${UNATTENDED:-}" ]; then unattended_update "$UNATTENDED_DIR" elif [ -n "$ENABLE_UPDATES" ]; then enable_updates "$UNATTENDED_DIR" else main fi