#!/usr/bin/env bash # InsightGrid one-step install or upgrade for a client server: # # curl -fsSL https://raw.githubusercontent.com/PathwaysInternational-ke/insightgrid-install/main/get.sh | bash # # It checks the server, installs Docker if missing, signs in to Pathways' private registry with # the access token you were given, downloads the release bundle, analyses the server, asks the # site questions once, and installs. Running it again upgrades to the latest release. # Environment: IG_VERSION (default stable), IG_REGISTRY_USER, IG_TOKEN, IG_HOME, IG_RELEASES. set -Eeuo pipefail REGISTRY=ghcr.io BUNDLE_IMAGE=${IG_BUNDLE_IMAGE:-$REGISTRY/pathwaysinternational-ke/insightgrid-client-release} TAG=${IG_VERSION:-stable} RELEASES=${IG_RELEASES:-/opt/insightgrid-releases} GET_URL=${IG_GET_URL:-https://raw.githubusercontent.com/PathwaysInternational-ke/insightgrid-install/main/get.sh} export IG_HOME=${IG_HOME:-/opt/insightgrid} bold=$'\033[1m' red=$'\033[1;31m' orange=$'\033[1;38;5;208m' green=$'\033[1;32m' off=$'\033[0m' [ -t 1 ] || bold="" red="" orange="" green="" off="" say() { printf '%s==>%s %s\n' "$bold" "$off" "$*"; } warn() { printf '%s==> %s%s\n' "$orange" "$*" "$off"; } die() { printf '%sError:%s %s\n' "$red" "$off" "$*" >&2; exit 1; } # stdin is this script (curl | bash), so questions read the terminal. ask() { # asks again after a declined Ctrl+C local reply rc ASK_PROMPT="$2" while :; do read -r ${3:+-s} -p "$2" reply /dev/null || true die "Stopped by request. Run the same command again to continue." } trap on_interrupt INT # run_bg LOG CMD...: runs CMD in its own session with output to LOG, so a declined Ctrl+C cannot # interrupt it (Ctrl+C then "y" stops it). With a real LOG, its last line is shown as progress. run_bg() { local log=$1 rc=0; shift setsid "$@" "$log" 2>&1 & WORKER=$! while kill -0 "$WORKER" 2>/dev/null; do [ "$log" = /dev/null ] || [ ! -t 1 ] || printf '\r\033[K %s' "$(tail -n1 "$log" 2>/dev/null | cut -c1-90 || true)" sleep 1 || true done [ "$log" = /dev/null ] || [ ! -t 1 ] || printf '\r\033[K' wait "$WORKER" || rc=$?; WORKER="" return "$rc" } # A token handed over by the non-root run arrives in a private file, never on a command line. if [ -n "${IG_TOKEN_FILE:-}" ]; then IG_TOKEN=$(cat "$IG_TOKEN_FILE"); rm -f "$IG_TOKEN_FILE"; export IG_TOKEN; fi [ "$(uname -s)" = Linux ] || die "InsightGrid installs on Linux servers." case "$(uname -m)" in x86_64|amd64) ;; *) die "InsightGrid images are built for x86_64; this server is $(uname -m)." ;; esac os_id=$(. /etc/os-release 2>/dev/null && echo "${ID:-}"); os_name=$(. /etc/os-release 2>/dev/null && echo "${PRETTY_NAME:-unknown}") case "$os_id" in ubuntu|debian) ;; *) warn "Untested system ($os_name); Ubuntu or Debian is recommended." ;; esac [ -r /dev/tty ] || [ -n "${IG_TOKEN:-}" ] || die "Run this from an interactive terminal, or set IG_REGISTRY_USER and IG_TOKEN." ALLOW_ROOT=() if [ "$(id -u)" -ne 0 ]; then # Without root: rerun this script under sudo with the keyboard as input, so prompts and Ctrl+C # behave normally (piping into sudo leaves sudo unable to pass Ctrl+C on). # Without root, continue only with Docker access and full access to the releases and site folders # (a root-owned site folder would hide an unfinished upgrade's markers). if ! { docker info >/dev/null 2>&1 && mkdir -p "$RELEASES" 2>/dev/null && [ -w "$RELEASES" ] \ && { { [ -d "$IG_HOME" ] && [ -r "$IG_HOME" ] && [ -w "$IG_HOME" ] && [ -x "$IG_HOME" ]; } \ || { [ ! -e "$IG_HOME" ] && [ -w "$(dirname "$IG_HOME")" ] && [ -x "$(dirname "$IG_HOME")" ]; }; }; }; then [ -z "${IG_REEXEC:-}" ] || die "sudo did not give administrator rights; ask your server administrator." self="${BASH_SOURCE[0]:-}" if [ ! -f "$self" ]; then self=$(mktemp /tmp/insightgrid-get.XXXXXX); run_bg /dev/null curl -fsSL "$GET_URL" -o "$self" || die "Cannot download $GET_URL" fi say "Administrator rights are needed; sudo may ask for your password." mapfile -t pass < <(env | grep -E '^IG_[A-Z_]+=' | grep -v '^IG_TOKEN=' || true); pass+=(IG_REEXEC=1) if [ -n "${IG_TOKEN:-}" ]; then tokfile=$(mktemp /tmp/insightgrid-token.XXXXXX); printf '%s' "$IG_TOKEN" >"$tokfile"; pass+=(IG_TOKEN_FILE="$tokfile") fi exec sudo env "${pass[@]}" bash "$self" /dev/null; then say "Installing Docker Engine from download.docker.com (a few minutes)" dscript=$(mktemp /tmp/insightgrid-docker.XXXXXX); dlog=$(mktemp /tmp/insightgrid-docker-install.XXXXXX) run_bg /dev/null curl -fsSL https://get.docker.com -o "$dscript" || die "Cannot reach download.docker.com. Allow outbound HTTPS, or ask Pathways for the offline bundle." run_bg "$dlog" sh "$dscript" || die "Docker installation failed; see $dlog" rm -f "$dscript" command -v systemctl >/dev/null && systemctl enable --now docker >/dev/null 2>&1 || true fi docker compose version >/dev/null 2>&1 || die "Docker is installed but the Compose plugin is missing: install docker-compose-plugin." # Same fingerprint as install.sh's bundle_hash. # Fails on a symlink (its target could change without changing the hash) or an unreadable file. bundle_hash() { local links; links=$(find "$1" -type l -print -quit) && [ -z "$links" ] || return 1 (set -o pipefail; cd "$1" && find . -type f ! -name .bundle-digest -print0 | LC_ALL=C sort -z | xargs -0 sha256sum | sha256sum | cut -c1-64) } # An upgrade that stopped halfway is finished with the release it started, not a newer latest. resume="" if [ -f "$IG_HOME/.upgrade-target" ]; then read -r thash resume <"$IG_HOME/.upgrade-target" || true [ -x "$resume/install.sh" ] || die "An upgrade is unfinished but its release folder $resume is missing. Ask Pathways before continuing." { h=$(bundle_hash "$resume") && [ "$h" = "$thash" ]; } || die "The release folder $resume changed after its upgrade began. Ask Pathways before continuing." say "Resuming the unfinished upgrade with $(basename "$resume")" elif [ -f "$IG_HOME/.upgrade-backup" ]; then die "An upgrade started by an earlier installer is unfinished. Finish it by running ./install.sh in the release folder you used." fi if [ -z "$resume" ] && [ "${IG_NO_PULL:-0}" != 1 ]; then if ! run_bg /dev/null docker pull -q "$BUNDLE_IMAGE:$TAG"; then say "Sign in to Pathways' image registry (read-only access you were given)" user=${IG_REGISTRY_USER:-}; token=${IG_TOKEN:-} [ -n "$user" ] || ask user " Registry username: " [ -n "$token" ] || ask token " Access token (hidden): " secret printf '%s' "$token" | docker login "$REGISTRY" -u "$user" --password-stdin >/dev/null 2>&1 \ || die "The registry refused that username and token. Check them with Pathways." unset token say "Downloading release bundle ($TAG)" run_bg /dev/null docker pull -q "$BUNDLE_IMAGE:$TAG" || die "Cannot download $BUNDLE_IMAGE:$TAG. Is this token allowed to read it?" fi fi # Unpack next to the releases, then rename into place: an interrupted copy never leaves a half release. if [ -n "$resume" ]; then dest=$resume; release=$(basename "$resume") else # Named before it exists, so cleanup on exit can always find the temporary container. cname="insightgrid-bundle-$$-$RANDOM"; stage=$(mktemp -d "$RELEASES/.incoming.XXXXXX") trap 'rm -rf "$stage"; docker rm -f "$cname" >/dev/null 2>&1 || true' EXIT # Resolved once: the bundle unpacked is the one recorded in .bundle-digest, even if the tag moves meanwhile. # A loaded image (IG_NO_PULL) may carry no registry digest; it is then opened by tag and not recorded. ref=$(docker image inspect -f '{{range .RepoDigests}}{{println .}}{{end}}' "$BUNDLE_IMAGE:$TAG" 2>/dev/null | grep -m1 "^$BUNDLE_IMAGE@sha256:") || ref="" [ -n "$ref" ] || [ "${IG_NO_PULL:-0}" = 1 ] || die "Cannot resolve the downloaded bundle's digest." run_bg /dev/null docker create --name "$cname" "${ref:-$BUNDLE_IMAGE:$TAG}" /none || die "Cannot open the release bundle." run_bg /dev/null docker cp "$cname:/bundle/." "$stage/" || die "Cannot unpack the release bundle." docker rm "$cname" >/dev/null 2>&1 || true release=$(grep -m1 '^IG_VERSION=' "$stage/release.env" | cut -d= -f2-) [[ "$release" =~ ^[A-Za-z0-9._-]+$ ]] && [ -x "$stage/install.sh" ] || die "The downloaded bundle is incomplete." # An older installer would not refuse a downgrade itself, so an undated release never replaces a dated one. if grep -q '^IG_RELEASED_AT=.' "$IG_HOME/installed-release.env" 2>/dev/null && ! grep -q '^IG_RELEASED_AT=.' "$stage/release.env"; then die "Release $release carries no release date but this site's installed release does; it may be older. Ask Pathways for a current release." fi dest="$RELEASES/$release" if [ -d "$dest" ]; then modes() { (set -o pipefail; cd "$1" && find . -mindepth 1 ! -name .bundle-digest -printf '%m %p\n' | LC_ALL=C sort); } { diff -rq -x .bundle-digest "$stage" "$dest" >/dev/null 2>&1 && m1=$(modes "$stage") && m2=$(modes "$dest") && [ "$m1" = "$m2" ]; } \ || die "$dest exists with different contents; move it aside and rerun." else chmod 755 "$stage"; mv "$stage" "$dest" fi if [ -n "$ref" ]; then echo "$ref" >"$dest/.bundle-digest"; else rm -f "$dest/.bundle-digest"; fi fi say "Release $release in $dest" cd "$dest" say "Analysing this server" trap ':' INT # the installer asks about Ctrl+C itself rc=0; ./install.sh check || rc=$? [ "$rc" != 130 ] || exit 130 [ "$rc" = 0 ] || die "Fix the items marked ✗ above, then run the same command again." trap on_interrupt INT say "Installing InsightGrid $release" trap ':' INT # the installer asks about Ctrl+C itself rc=0; ./install.sh "${ALLOW_ROOT[@]}" || rc=$? trap on_interrupt INT [ "$rc" = 0 ] || exit "$rc" if grep -q '^IG_TLS=internal' "$IG_HOME/site.env" 2>/dev/null; then warn "Private site: install $IG_HOME/extra-ca.crt as a trusted root certificate on each computer that opens InsightGrid." fi