#!/usr/bin/env bash # The Moorhold installer (SHIP-79). Run as root on a fresh Ubuntu 24.04 or 26.04, or Debian 12, server, by # hand or from cloud-init (installer/cloud-init/). It: # 1. refuses an access key on the command line or in the environment (ADR-0025: a file or a prompt); # 2. installs what is missing from the distribution's own signed archive: Docker Engine and # Compose, minisign, curl, openssl, xz; # 3. downloads the release lock and checks its minisign signature against the public key below; # 4. downloads Node and the Moorhold bundle and checks each against the sha256 the lock pins; # 5. hands over to `moorhold install`, which generates every secret, pulls every image by digest # with the access key (sent on stdin), starts and applies the stack, and prints its URL and the # one-time setup code for /setup (on a terminal; from cloud-init, only where root can read it). # # install.sh --hostname [--profile full|core] [--access-key-file ] # [--acme production|staging] [--keep-access-key-file] [--no-sample-data] # # On a server that already runs another release of Moorhold, the installer upgrades it (ALLOT-1513): # --hostname may be left out (the installed Stack has it), the release is unpacked beside the running # one without switching /opt/moorhold/current, and the new release's `moorhold upgrade --staged` # does the rest, switching only once the new release is up. Every run keeps this installer's release # key in /etc/moorhold/release.pub, which `sudo moorhold upgrade ` checks later releases with. # # A fresh install loads the sample, "Moorhold sample: AI Release Radar" (ALLOT-1501); --no-sample-data # leaves it out. Installing again keeps what the server has. `moorhold sample-data add|remove` changes # it later. # # The access key comes from --access-key-file (a file of mode 0600, which is deleted once the images # are pulled, unless --keep-access-key-file) or, in a terminal, from a prompt that does not echo. # Once the registry accepts it, the server keeps its own copy, root only, in # /etc/moorhold/secrets/registry-access-key (ALLOT-1502). Running the installer again, with no key # file or with one an earlier run deleted, uses that copy. `moorhold access-key set|forget` change it. # Nothing it prints holds a secret. docs/decisions/0027-signed-installer.md has the design. set -euo pipefail umask 077 # Stamped by the release (scripts/release/release-files.mjs stamp); a development copy refuses to run. MOORHOLD_VERSION="2026.10.7" RELEASE_PUBKEY="RWScfyH28xJtf1DCwDTYUUS3wlSzmrKr2426mvg+EfqIV+mqziD5q5Si" BASE_URL="https://get.moorhold.dev" # Tests only (scripts/test/installer.test.mjs): a sandbox directory every absolute path is under. P=${MOORHOLD_INSTALL_PREFIX:-} say() { printf 'moorhold-install: %s\n' "$*"; } die() { printf 'moorhold-install: %s\n' "$1" >&2 exit "${2:-1}" } KEY_ON_ARGV='refusing an access key on the command line, where other users and logs can read it; put it in a file of mode 0600 and give --access-key-file ' USAGE='usage: install.sh --hostname [--profile full|core] [--access-key-file ] [--acme production|staging] [--keep-access-key-file] [--no-sample-data]' # ── 1. Inputs ───────────────────────────────────────────────────────────────────────────────── for a in "$@"; do case "$a" in --access-key | --access-key=*) die "$KEY_ON_ARGV" 2 ;; esac if printf '%s' "$a" | grep -Eq 'mh_[0-9a-z]{8,32}_[A-Za-z0-9_-]{20,}'; then die "$KEY_ON_ARGV" 2; fi done if [ -n "${MOORHOLD_ACCESS_KEY:-}" ]; then unset MOORHOLD_ACCESS_KEY die 'refusing MOORHOLD_ACCESS_KEY from the environment, which child processes and /proc can read; put the key in a file of mode 0600 and give --access-key-file ' 2 fi HOSTNAME_ARG='' PROFILE=full PROFILE_SET='' KEY_FILE='' ACME=production ACME_SET='' KEEP='' SAMPLE='' while [ $# -gt 0 ]; do case "$1" in --hostname) HOSTNAME_ARG=${2:-}; shift 2 || die "$USAGE" 2 ;; --profile) PROFILE=${2:-}; PROFILE_SET=1; shift 2 || die "$USAGE" 2 ;; --access-key-file) KEY_FILE=${2:-}; shift 2 || die "$USAGE" 2 ;; --acme) ACME=${2:-}; ACME_SET=1; shift 2 || die "$USAGE" 2 ;; --keep-access-key-file) KEEP=1; shift ;; --sample-data) [ "$SAMPLE" != off ] || die "$USAGE" 2; SAMPLE=on; shift ;; --no-sample-data) [ "$SAMPLE" != on ] || die "$USAGE" 2; SAMPLE=off; shift ;; -h | --help) printf '%s\n' "$USAGE"; exit 0 ;; *) die "$USAGE" 2 ;; esac done # A server that runs another release already is upgraded, not installed afresh (ALLOT-1513). UPGRADE_FROM='' if [ -f "$P/opt/moorhold/stack/stack.yaml" ] && [ -L "$P/opt/moorhold/current" ]; then RUNNING=$(readlink "$P/opt/moorhold/current") RUNNING=${RUNNING%/} RUNNING=${RUNNING##*/} [ "$RUNNING" = "$MOORHOLD_VERSION" ] || UPGRADE_FROM=$RUNNING fi if [ -n "$UPGRADE_FROM" ]; then [ -z "$ACME_SET$SAMPLE" ] || die "this server runs Moorhold $UPGRADE_FROM, so the installer upgrades it and keeps its settings: leave out --acme, --sample-data and --no-sample-data (moorhold sample-data add|remove changes the sample)" 2 else [ -n "$HOSTNAME_ARG" ] || die "$USAGE" 2 fi [ -z "$HOSTNAME_ARG" ] || printf '%s' "$HOSTNAME_ARG" | grep -Eq '^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$' || die 'the hostname must be lowercase letters, digits, hyphens and dots, e.g. data.yourorg.example' 2 case "$PROFILE" in full) NEED_GIB=24 ;; core) NEED_GIB=12 ;; *) die 'the profile must be full or core' 2 ;; esac case "$ACME" in production | staging) ;; *) die 'acme must be production or staging' 2 ;; esac case "$MOORHOLD_VERSION$RELEASE_PUBKEY" in *__MOORHOLD_*) die 'this is a development copy of the installer, with no release version or signing key; download a released installer from https://moorhold.dev' ;; esac printf '%s' "$RELEASE_PUBKEY" | grep -Eq '^RW[A-Za-z0-9+/]{54}$' || die 'the installer'"'"'s release key is malformed' # ── 2. The server ───────────────────────────────────────────────────────────────────────────── [ "$(id -u)" = 0 ] || die 'run the installer as root (sudo)' case "$(uname -m)" in x86_64) ARCH=x86_64 ;; aarch64 | arm64) ARCH=aarch64 ;; *) die "unsupported processor: $(uname -m) (x86_64 and aarch64 are supported)" ;; esac OS_ID=$(. "$P/etc/os-release" && printf '%s %s' "$ID" "$VERSION_ID") case "$OS_ID" in # Every package below (docker.io, docker-compose-v2, minisign, curl, openssl, xz-utils) is in each # system's own archive: Ubuntu 26.04 (resolute) ships docker.io 29.1, Compose 2.40 and minisign 0.12. 'ubuntu 24.04' | 'ubuntu 26.04' | 'debian 12') ;; *) die "unsupported system: $OS_ID (Ubuntu 24.04, Ubuntu 26.04 and Debian 12 are supported)" ;; esac MEM_KB=$(awk '/^MemTotal:/ { print $2 }' "$P/proc/meminfo") # A 32 GB server reports about 31 GiB; the need is the components' limits plus room. A server being # upgraded runs its profile already, which the upgrade keeps. if [ -z "$UPGRADE_FROM" ] && [ "$MEM_KB" -lt $((NEED_GIB * 1024 * 1024)) ]; then die "this server has $((MEM_KB / 1024 / 1024)) GiB of memory; the $PROFILE profile needs $NEED_GIB GiB (choose a 32 GB server, or --profile core on 16 GB)" fi WORK="$P/run/moorhold-install" mkdir -p "$WORK" chmod 0700 "$WORK" DL="$WORK/download" rm -rf "$DL" mkdir -m 0700 "$DL" PROMPTED='' cleanup() { rm -rf "$DL" if [ -n "$PROMPTED" ]; then rm -f "$PROMPTED"; fi } trap cleanup EXIT KEPT="$P/etc/moorhold/secrets/registry-access-key" ask_for_key() { PROMPTED="$WORK/access-key" printf 'Moorhold access key (from account.moorhold.dev; not shown): ' >&2 IFS= read -rs key printf '\n' >&2 printf '%s\n' "$key" >"$PROMPTED" unset key KEY_FILE=$PROMPTED } if [ -n "$KEY_FILE" ] && [ ! -e "$KEY_FILE" ] && [ ! -L "$KEY_FILE" ]; then # An earlier run deleted the file it was given. With a kept key, `moorhold install` says so and # uses it; otherwise a terminal is asked for the key, as a first install is. if [ ! -f "$KEPT" ]; then [ -t 0 ] || die "the access key file $KEY_FILE does not exist; save your key to it again, or run the installer in a terminal to be asked for it" 2 say "the access key file $KEY_FILE isn't there any more" ask_for_key fi elif [ -z "$KEY_FILE" ] && [ -f "$KEPT" ]; then say 'using the access key this server kept' elif [ -z "$KEY_FILE" ]; then [ -t 0 ] || die 'no access key: give --access-key-file (a file of mode 0600), or run the installer in a terminal to be asked for it' 2 ask_for_key fi if [ -n "$KEY_FILE" ] && { [ -e "$KEY_FILE" ] || [ -L "$KEY_FILE" ]; }; then [ ! -L "$KEY_FILE" ] || die "the access key file $KEY_FILE is a symbolic link; give the file itself" 2 [ -f "$KEY_FILE" ] || die "the access key file $KEY_FILE is not a regular file" 2 fi say "installing what is missing from the system's own archive" missing='' command -v docker >/dev/null 2>&1 || missing="$missing docker.io" if ! docker compose version >/dev/null 2>&1; then missing="$missing docker-compose-v2"; fi for tool in minisign curl openssl xz; do command -v "$tool" >/dev/null 2>&1 || case "$tool" in xz) missing="$missing xz-utils" ;; curl) missing="$missing curl ca-certificates" ;; *) missing="$missing $tool" ;; esac done if [ -n "$missing" ]; then export DEBIAN_FRONTEND=noninteractive apt-get update -q # shellcheck disable=SC2086 # one word per package apt-get install -y -q --no-install-recommends $missing fi if command -v systemctl >/dev/null 2>&1; then systemctl enable --now docker >/dev/null; fi # ── 3. The signed release lock ──────────────────────────────────────────────────────────────── fetch() { curl -fsSL --proto '=https' --tlsv1.2 --retry 3 -o "$2" "$1"; } say "checking the signature of Moorhold $MOORHOLD_VERSION" fetch "$BASE_URL/$MOORHOLD_VERSION/release.lock" "$DL/release.lock" fetch "$BASE_URL/$MOORHOLD_VERSION/release.lock.minisig" "$DL/release.lock.minisig" minisign -V -q -P "$RELEASE_PUBKEY" -m "$DL/release.lock" -x "$DL/release.lock.minisig" >/dev/null 2>&1 || die "the release lock's signature does not verify: stop, do not install, and tell hello@moorhold.dev" [ "$(head -n 1 "$DL/release.lock")" = 'moorhold-release 1' ] || die 'the release lock is not one this installer reads' [ "$(awk '$1 == "version" { print $2 }' "$DL/release.lock")" = "$MOORHOLD_VERSION" ] || die "the release lock is not for $MOORHOLD_VERSION" # The key `sudo moorhold upgrade` checks later releases against (ALLOT-1513): public, root's, 0644, # as a minisign public key file (`minisign -V -p`, ALLOT-1444's checks read it so). mkdir -p "$P/etc/moorhold" printf 'untrusted comment: Moorhold release key, written by the installer for %s\n%s\n' \ "$MOORHOLD_VERSION" "$RELEASE_PUBKEY" >"$P/etc/moorhold/release.pub.new" chmod 0644 "$P/etc/moorhold/release.pub.new" mv "$P/etc/moorhold/release.pub.new" "$P/etc/moorhold/release.pub" NODE_URL=$(awk -v a="$ARCH" '$1 == "node" && $2 == a { print $3 }' "$DL/release.lock") NODE_SHA=$(awk -v a="$ARCH" '$1 == "node" && $2 == a { print $4 }' "$DL/release.lock") BUNDLE_URL=$(awk '$1 == "bundle" { print $2 }' "$DL/release.lock") BUNDLE_SHA=$(awk '$1 == "bundle" { print $3 }' "$DL/release.lock") [ -n "$NODE_URL" ] && [ -n "$BUNDLE_URL" ] || die "the release lock has no Node or bundle for $ARCH" # ── 4. Node and the bundle, pinned by sha256 ────────────────────────────────────────────────── pinned() { fetch "$1" "$DL/$3" printf '%s %s\n' "$2" "$DL/$3" | sha256sum -c --quiet - >/dev/null 2>&1 || die "$3 does not match the sha256 the signed lock pins: stop, do not install, and tell hello@moorhold.dev" } say 'downloading Node and Moorhold, checked against the signed lock' pinned "$NODE_URL" "$NODE_SHA" node.tar.xz pinned "$BUNDLE_URL" "$BUNDLE_SHA" moorhold.tar.gz REL="$P/opt/moorhold/releases/$MOORHOLD_VERSION" rm -rf "$REL.new" mkdir -p "$REL.new/node" "$REL.new/app" # --no-same-owner: the files belong to root (the installer), never to whichever uid built the archive, # so no later local account can rewrite code root runs. tar --no-same-owner -xJf "$DL/node.tar.xz" -C "$REL.new/node" --strip-components=1 tar --no-same-owner -xzf "$DL/moorhold.tar.gz" -C "$REL.new/app" cp "$DL/release.lock" "$DL/release.lock.minisig" "$REL.new/" # Readable by every uid, writable by root only: the installer's umask (077) would otherwise leave # the code unreadable to the non-root containers that mount it (the sample supplier is uid 1000). chmod -R u+rwX,go+rX,go-w "$REL.new" chmod 0755 "$REL.new" rm -rf "$REL" mv "$REL.new" "$REL" # ── 5a. An upgrade: the new release's CLI takes over, and switches `current` once it is up ───── if [ -n "$UPGRADE_FROM" ]; then say "upgrading Moorhold $UPGRADE_FROM to $MOORHOLD_VERSION" set -- upgrade --staged "$P/opt/moorhold/releases/$MOORHOLD_VERSION" \ --home "$P/opt/moorhold" --secrets-dir "$P/etc/moorhold/secrets" --runtime-dir "$P/run/moorhold" [ -z "$HOSTNAME_ARG" ] || set -- "$@" --hostname "$HOSTNAME_ARG" [ -z "$PROFILE_SET" ] || set -- "$@" --profile "$PROFILE" # No key file: the upgrade uses the key this server kept (ALLOT-1502). [ -z "$KEY_FILE" ] || set -- "$@" --access-key-file "$KEY_FILE" [ -z "$KEEP" ] || set -- "$@" --keep-access-key-file cd "$REL/app" umask 022 "$REL/node/bin/node" apps/cli/src/main.ts "$@" exit $? fi ln -sfn "$REL" "$P/opt/moorhold/current" # `moorhold` on the PATH, for what an operator runs later (`moorhold relay ...`, ADR-0032; # `moorhold sample-data ...`): the current release's CLI with the release's own Node. mkdir -p "$P/usr/local/bin" cat >"$P/usr/local/bin/moorhold.new" <<'WRAPPER' #!/bin/sh # Written by the Moorhold installer: the current release's CLI. exec /opt/moorhold/current/node/bin/node /opt/moorhold/current/app/apps/cli/src/main.ts "$@" WRAPPER chmod 0755 "$P/usr/local/bin/moorhold.new" mv "$P/usr/local/bin/moorhold.new" "$P/usr/local/bin/moorhold" # ── 5. The install itself ───────────────────────────────────────────────────────────────────── say "starting moorhold install (profile $PROFILE)" set -- install --lock "$REL/release.lock" --hostname "$HOSTNAME_ARG" --profile "$PROFILE" # No key file: `moorhold install` uses the key this server kept (ALLOT-1502). [ -z "$KEY_FILE" ] || set -- "$@" --access-key-file "$KEY_FILE" set -- "$@" --acme "$ACME" \ --home "$P/opt/moorhold" --secrets-dir "$P/etc/moorhold/secrets" --runtime-dir "$P/run/moorhold" # The memory read above: the Stack records DataHub's and OpenSearch's limits sized for it, once # (ALLOT-1494); installing again keeps what the Stack has. set -- "$@" --host-memory-kib "$MEM_KB" [ -z "$KEEP" ] || set -- "$@" --keep-access-key-file # A key the prompt wrote, rather than a file the operator gave. [ -z "$PROMPTED" ] || set -- "$@" --access-key-prompted # The sample: on for a fresh install, kept as it is on a reinstall, unless a flag says (ALLOT-1501). case "$SAMPLE" in on) set -- "$@" --sample-data ;; off) set -- "$@" --no-sample-data ;; esac cd "$REL/app" # `moorhold install` sets the mode of every secret it writes (0600 files, 0700 directories) itself; # what else it writes (the Stack, the project, the code) is read by non-root containers. umask 022 "$REL/node/bin/node" apps/cli/src/main.ts "$@" # ── 6. Start at boot (ALLOT-1556, ALLOT-1621) ───────────────────────────────────────────────── # The stack's boot secrets live on a tmpfs (/run/moorhold), so after a reboot nothing runs, the # backup schedule included, until `moorhold start` writes them again. moorhold.service runs it at # boot. The CLI writes and enables it (apps/cli/src/boot-unit.ts), as every upgrade does, so the # unit's text has one source. "$REL/node/bin/node" apps/cli/src/main.ts start-at-boot --profile "$PROFILE" --systemd-dir "$P/etc/systemd/system"