#!/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] # install.sh --check --hostname [--profile full|core] # install.sh --local [--access-key-file ] [--keep-access-key-file] [--no-sample-data] # # --local (ALLOT-1781) installs for one person in a virtual machine on their own computer, as the Mac # app does: no hostname, no certificate and no network check, the light profile, and each tool on # 127.0.0.1 in one block of ports the VM forwards (the shell at http://127.0.0.1:47300). An # installed server keeps its mode: --local is refused on an upgrade, and on a server. A local install # needs no access key (ALLOT-1985): it asks for none and goes on without one, unless one is given or kept. # # Before it asks for the access key, and before any image is pulled, a fresh install runs the # installer's checks (ALLOT-1480, `moorhold check` from the verified bundle): the system, memory for # the profile, disk and unallocated LVM space, sleep on a desktop install, ports 80 and 443, DNS, # NAT loopback and carrier-grade NAT. A blocking finding stops it there. --check runs the same checks # and stops: it installs no Docker, writes nothing outside its temporary directory and asks for no # key (when curl, minisign or xz is missing it says so and stops with exit 3, because it installs # nothing and won't run what it can't verify). On a terminal, a fresh install asks for the profile and whether # to load the sample when the flags don't say (the sample's answer is No unless you type y). # # 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 offers the sample, "Moorhold sample: AI Release Radar", and does not load it unless you # say so: --sample-data, or y at the prompt on a terminal (ALLOT-1501, then ALLOT-1880: with no flag and no # terminal it is left out). --no-sample-data says no without asking. Installing again keeps what the server # has. An admin can add it from the setup page, and `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.11.4" 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 ' # Local mode's own line (ALLOT-1781), beside USAGE, which stays as #219 approved it. LOCAL_USAGE=' or: install.sh --local [--access-key-file ] [--keep-access-key-file] [--no-sample-data] (one person, on this machine only)' USAGE='usage: install.sh --hostname [--profile full|core] [--access-key-file ] [--acme production|staging] [--keep-access-key-file] [--no-sample-data] [--change-hostname] [--check]' stage() { printf '== %s\n' "$*"; } # A terminal on stdin, where the installer may ask. Tests only: MOORHOLD_INSTALL_TERMINAL=1. on_terminal() { [ -t 0 ] || [ -n "${MOORHOLD_INSTALL_TERMINAL:-}" ]; } # Whether $1 matches the pattern $2, byte by byte, whatever locale the installer was started in. Under a UTF-8 locale # glibc orders letters by collation, so a range such as [a-z] takes "á" and [0-9] takes an Arabic-Indic digit: a value # that only looks like a name or a sha256 would pass (review of 16 Nov 2026). The C locale lasts for this check alone. matches_ascii() ( LC_ALL=C && [[ "$1" =~ $2 ]] ) # ── 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='' CHECK='' CHANGE_HOSTNAME='' LOCAL='' # The Moorhold app's own update route (ALLOT-2230): not in the usage text, because a person never gives these. MAC_APP='' LOCK_SHA='' LOCK_SHA_SET='' MAC_ACCOUNT='' MAC_ACCOUNT_SET='' 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 ;; --check) CHECK=1; shift ;; --change-hostname) CHANGE_HOSTNAME=1; shift ;; --local) LOCAL=1; shift ;; --mac-app) MAC_APP=1; shift ;; --lock-sha256) LOCK_SHA=${2:-}; LOCK_SHA_SET=1; shift 2 || die "$USAGE" 2 ;; --mac-account) MAC_ACCOUNT=${2:-}; MAC_ACCOUNT_SET=1; shift 2 || die "$USAGE" 2 ;; -h | --help) printf '%s\n%s\n' "$USAGE" "$LOCAL_USAGE"; exit 0 ;; *) die "$USAGE" 2 ;; esac done # Local mode (ALLOT-1781): its own messages; every server message above and below stays as it was. if [ -n "$LOCAL" ]; then [ -z "$HOSTNAME_ARG" ] || die 'local mode has no hostname: Moorhold is reached on this machine only, at 127.0.0.1. Leave out --hostname, or leave out --local to install a server' 2 [ -z "$ACME_SET$CHANGE_HOSTNAME" ] || die 'local mode has no certificate and no hostname: leave out --acme and --change-hostname' 2 [ -z "$CHECK" ] || die "local mode has no --check: it runs on this machine only, so there's nothing to reach from outside" 2 [ -z "$PROFILE_SET" ] || [ "$PROFILE" = light ] || die "local mode installs the light profile, not $PROFILE: leave out --profile, or give --profile light" 2 PROFILE=light PROFILE_SET=1 elif [ "$PROFILE" = light ]; then die 'the light profile is for local mode, on your own computer: give --local, or choose full or core for a server' 2 fi # The flags that ask an installed server for a change, as given on the command line (ALLOT-1692). ASKED_CHANGE="$ACME_SET$SAMPLE$CHANGE_HOSTNAME$PROFILE_SET" # A server that runs another release already is upgraded, not installed afresh (ALLOT-1513). UPGRADE_FROM='' RUNNING='' 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 INSTALLED='' [ ! -f "$P/opt/moorhold/stack/stack.yaml" ] || INSTALLED=1 # A local install's Stack has spec.local (ALLOT-1781), as moorhold install --local writes it. STACK_LOCAL='' [ -z "$INSTALLED" ] || ! grep -q '^ local: {}' "$P/opt/moorhold/stack/stack.yaml" || STACK_LOCAL=1 # The update button in the Moorhold app on a Mac (ALLOT-2230; ALLOT-2188, signed by Adam on 15 Nov 2026). The app # starts the installer it carries, with the sha256 of the release lock it carries, to upgrade the local install in its # private space. The new release's own CLI then does the upgrade, and the app's button stands for the approval. So it is # only for a local install that runs an earlier release, and it takes no other flag. Nothing is asked on this route. if [ -n "$MAC_APP$LOCK_SHA_SET$MAC_ACCOUNT_SET" ]; then # The whole value is matched, not one line of it: a sha256 with anything after it is not a sha256. { [ -n "$MAC_APP" ] && matches_ascii "$LOCK_SHA" '^[0-9a-f]{64}$'; } || die '--mac-app and --lock-sha256 are given together, by the Moorhold app' 2 # --mac-account : the Mac account that pressed the button, which the app gives so that the records can name it # (ALLOT-2188: who approved it). A short name and nothing else: letters, digits, ".", "_" and "-", 64 at most. A # value that isn't one stops the run here, before anything is fetched, locked or written. It is never cleaned up # into a name, and it goes no further. The CLI checks it again (MAC_ACCOUNT in apps/cli/src/upgrade.ts; a test # holds the words together). The letters and digits are ASCII's, in any locale (matches_ascii). if [ -n "$MAC_ACCOUNT_SET" ] && ! matches_ascii "$MAC_ACCOUNT" '^[A-Za-z0-9_][A-Za-z0-9._-]{0,63}$'; then die '--mac-account must be a Mac account'"'"'s short name: letters, digits, ".", "_" and "-", 64 characters at most' 2 fi [ -z "$HOSTNAME_ARG$KEY_FILE$KEEP$CHECK$LOCAL$ASKED_CHANGE" ] || die '--mac-app takes --lock-sha256 and nothing else: the Moorhold app starts it' 2 [ -n "$INSTALLED" ] || die '--mac-app updates the Moorhold that the Moorhold app set up on this Mac, and none is set up here. Open Moorhold and let it finish setting up.' 2 [ -n "$STACK_LOCAL" ] || die "--mac-app is the Moorhold app's own update route, for Moorhold on a Mac, and this is a server. Update it with sudo moorhold upgrade , or from the Updates page in the portal." 2 if [ -z "$UPGRADE_FROM" ]; then if [ -z "$RUNNING" ] || [ -e "$P/opt/moorhold/.install-unfinished" ]; then die "Moorhold's setup on this Mac didn't finish, so there's nothing to update yet. Open Moorhold and let it finish setting up." 2 fi say "Moorhold $MOORHOLD_VERSION is running here already, so there's nothing to update." exit 0 fi # One update run at a time, whoever started it (ALLOT-1515): the lock `moorhold upgrade --approved` and # `sudo moorhold upgrade` hold. Those routes take it in the installed CLI, before this script is run. This route # has no CLI above it, so the script takes the lock itself, here, before it fetches or writes anything: the release # key and the release's folder are written further down, and an update run started from the Updates page may be # running from that very folder (review of 15 Nov 2026). A run by hand stops first if moorhold-update.service is # going, as the CLI's does (holdUpdateRunLock in apps/cli/src/approved-updates.ts). The lock is on descriptor 9 # until this script ends, and the new release's CLI is handed that descriptor (--update-lock-fd), so it takes the # lock on the same open file and never opens a second one. Without flock it stops: it fails closed. The words are # the CLI's own (APP_UPDATE_BUSY and APP_UPDATE_NO_LOCK in apps/cli/src/upgrade.ts; a test holds them together). UPDATE_BUSY="Another update of Moorhold is running on this Mac, so this one didn't start. Wait for it to finish, then look at the Updates page in the launchpad. Nothing has changed." UPDATE_NO_LOCK="Moorhold couldn't check whether another update is running (flock isn't there), so this one didn't start. Nothing has changed." if command -v systemctl >/dev/null 2>&1; then case "$(systemctl show -p ActiveState --value moorhold-update.service 2>/dev/null || true)" in activating | active | reloading | deactivating) die "$UPDATE_BUSY" 3 ;; esac fi command -v flock >/dev/null 2>&1 || die "$UPDATE_NO_LOCK" 3 mkdir -p -m 0755 "$P/run/moorhold" 2>/dev/null || die "$UPDATE_NO_LOCK" 3 exec 9>>"$P/run/moorhold/update.lock" || die "$UPDATE_NO_LOCK" 3 flock -n 9 || die "$UPDATE_BUSY" 3 fi # A server Moorhold is installed on keeps its profile unless --profile says otherwise (ALLOT-1685): # moorhold.service starts it with that profile. Unread, it is asked for, or full, as on a fresh server. UNIT="$P/etc/systemd/system/moorhold.service" if [ -n "$INSTALLED" ] && [ -z "$PROFILE_SET" ] && [ -f "$UNIT" ]; then kept=$(awk '/^ExecStart/ { for (i = 1; i < NF; i++) if ($i == "--profile") print $(i + 1) }' "$UNIT") # A local install's unit says --profile light, which isn't matched: PROFILE then stays "full" and unset. Nothing # reads it on the routes a local install can take from here. Run again with --local, the profile is set to light # above. On the Moorhold app's route the upgrade passes no --profile, so the staged CLI keeps the installed one, and # the memory floors below are for a first install only. A `light` case here would be refused by the check on the # profile below, which allows light with --local alone. case "$kept" in full | core) PROFILE=$kept PROFILE_SET=1 ;; esac fi if [ -n "$CHECK" ]; then # The check reads an installed Stack's hostname itself. [ -n "$HOSTNAME_ARG$INSTALLED" ] || die "$USAGE" 2 elif [ -n "$UPGRADE_FROM" ]; then # An upgrade keeps the installed Stack's mode (ALLOT-1781). [ -z "$LOCAL" ] || die "Moorhold $UPGRADE_FROM is installed here, so the installer upgrades it and keeps its mode: leave out --local" 2 [ -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 # A local install (ALLOT-1781) run again without --local: it keeps its mode, and never needs a # hostname. On this same release there's nothing to update. if [ -n "$STACK_LOCAL" ] && [ -z "$HOSTNAME_ARG$LOCAL" ]; then [ -z "$ASKED_CHANGE" ] || die "Moorhold here is a local install, for this machine only, and the flags you gave change a server. To change a local install, run the installer with --local." 2 if [ -n "$RUNNING" ] && [ "$RUNNING" = "$MOORHOLD_VERSION" ] && [ ! -e "$P/opt/moorhold/.install-unfinished" ]; then say "Moorhold $MOORHOLD_VERSION is already installed here, for this machine only, so there's nothing to update. To repair it, run the installer with --local." exit 0 fi die "Moorhold here is a local install, for this machine only: run the installer with --local." 2 fi # Run again without --hostname on a server already on this release, as the notes' update step # does on an updated server: there's nothing to update, so it says so and exits 0 (ALLOT-1692). # An install that didn't finish, or a flag that asks for a change, needs --hostname: exit 2. if [ -z "$HOSTNAME_ARG$LOCAL" ] && [ -n "$RUNNING" ] && [ "$RUNNING" = "$MOORHOLD_VERSION" ]; then [ ! -e "$P/opt/moorhold/.install-unfinished" ] || die "the last install of Moorhold $MOORHOLD_VERSION here didn't finish. Run the installer again with --hostname to finish it." 2 [ -z "$ASKED_CHANGE" ] || die "Moorhold $MOORHOLD_VERSION is already installed here, so there's nothing to update. To change its settings with --profile, --acme, --sample-data, --no-sample-data or --change-hostname, run the installer with --hostname as well." 2 say "Moorhold $MOORHOLD_VERSION is already installed here, so there's nothing to update. To repair or move it, run the installer with --hostname ." exit 0 fi [ -n "$HOSTNAME_ARG$LOCAL" ] || 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 | core) ;; light) [ -n "$LOCAL" ] || die 'the profile must be full or core' 2 ;; *) 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' # On a terminal, a fresh install asks what the flags don't say (task 6.1). An upgrade keeps the # server's profile and sample; --check uses the profile given (full by default) and asks nothing. if [ -z "$CHECK$UPGRADE_FROM" ] && on_terminal; then if [ -z "$PROFILE_SET" ]; then while :; do printf 'Core needs an 8 GB machine, full needs a 12 GB machine. Which do you want? [core/full] ' >&2 IFS= read -r answer || die 'the profile must be full or core' 2 case "$answer" in full | core) PROFILE=$answer PROFILE_SET=1; break ;; esac done fi if [ -z "$SAMPLE$INSTALLED" ]; then printf 'Load the sample data, Moorhold sample: AI Release Radar? You can remove it later. [y/N] ' >&2 IFS= read -r answer || answer='' case "$answer" in y | Y | yes | Yes | YES) SAMPLE=on ;; *) SAMPLE=off ;; esac fi fi # The floors are measured, not summed (ADR-0045, ALLOT-2016): the smallest memory each profile passed the floor test in, # with the sample loaded and a dashboard open, set just under the server size it allows (a 12 GB server reports 11.7 GiB). case "$PROFILE" in full) NEED_MIB=11264 NEED_SAY='11 GiB' CHOOSE='choose a 12 GB server, or --profile core on 8 GB' ;; core) NEED_MIB=5632 NEED_SAY='5.5 GiB' CHOOSE='choose an 8 GB server' ;; light) NEED_MIB=3584 NEED_SAY='3.5 GiB' CHOOSE='choose a 4 GB server' ;; esac # ── 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 # aarch64 is for local mode (the Mac app's VM). An ARM server takes a release only when the release # carries a signed arm64 drill record that passes (ALLOT-1868, checked after the lock, below): # ARM64_PASSES_NEEDED releases in a row drilled on a real arm64 server. Until then ARM servers are # refused as in ALLOT-1867. MOORHOLD_ARM64_DRILL=1 is for the drill's own throwaway server only # (scripts/release/arm64-drill.mjs): it installs without the record and marks the server as a drill # server, which Moorhold doesn't support. Local mode still needs the lock's aarch64 line (below). ARM64_PASSES_NEEDED=2 ARM_SERVER='' ARM_DRILL='' if [ "$ARCH" = aarch64 ] && [ -z "$LOCAL$STACK_LOCAL" ]; then if [ "${MOORHOLD_ARM64_DRILL:-}" = 1 ]; then ARM_DRILL=1 say 'MOORHOLD_ARM64_DRILL=1: installing on an ARM server for the arm64 drill, which Moorhold does not support' else ARM_SERVER=1 fi fi ARM_REFUSAL='this server has an ARM (aarch64) processor: Moorhold servers need x86_64. ARM is for local mode on a Mac (--local)' 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 (the installer accepts Ubuntu 24.04, Ubuntu 26.04 or Debian 12)" ;; esac MEM_KB=$(awk '/^MemTotal:/ { print $2 }' "$P/proc/meminfo") # The need is the measured floor above (NEED_MIB). A server being # upgraded runs its profile already, which the upgrade keeps. # --check reports the memory with the other checks instead. if [ -n "$LOCAL" ] && [ -z "$UPGRADE_FROM" ] && [ "$MEM_KB" -lt $((NEED_MIB * 1024)) ]; then die "this machine has $((MEM_KB / 1024 / 1024)) GiB of memory; local mode's light profile needs $NEED_SAY (give the VM 4 GiB)" fi if [ -z "$UPGRADE_FROM$CHECK" ] && [ "$MEM_KB" -lt $((NEED_MIB * 1024)) ]; then die "this server has $((MEM_KB / 1024 / 1024)) GiB of memory; the $PROFILE profile needs $NEED_SAY ($CHOOSE)" fi WORK="$P/run/moorhold-install" mkdir -p "$WORK" chmod 0700 "$WORK" DL="$WORK/download" rm -rf "$DL" mkdir -m 0700 "$DL" PROMPTED='' CHECK_COPY='' cleanup() { rm -rf "$DL" if [ -n "$PROMPTED" ]; then rm -f "$PROMPTED"; fi if [ -n "$CHECK_COPY" ]; then rm -rf "$CHECK_COPY"; fi } trap cleanup EXIT KEPT="$P/etc/moorhold/secrets/registry-access-key" # Where the key comes from is settled here, before any download; the prompt itself waits until # the checks have passed (ALLOT-1480). ASK_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 "$CHECK" ]; then : # The check asks for no key and reads none. elif { [ -n "$LOCAL" ] || [ -n "$STACK_LOCAL" ]; } && [ ! -f "$KEPT" ] && { [ -z "$KEY_FILE" ] || { [ ! -e "$KEY_FILE" ] && [ ! -L "$KEY_FILE" ]; }; }; then # A local (Mac) install needs no access key (ALLOT-1985): with none given or kept it asks for none and passes none, and # the CLI pulls without a login. That includes running this installer again, without --local, on an installed local # stack (an upgrade). A key that is given or kept is used as ever. if [ -n "$KEY_FILE" ]; then say "no access key is needed: a local install pulls without one (the key file $KEY_FILE isn't there, so it isn't used)" else say 'no access key is needed: a local install pulls without one' fi KEY_FILE='' elif [ -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 on_terminal || 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_KEY=1 fi elif [ -z "$KEY_FILE" ] && [ -f "$KEPT" ]; then say 'using the access key this server kept' elif [ -z "$KEY_FILE" ]; then on_terminal || 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_KEY=1 fi if [ -z "$CHECK" ] && [ -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 # The packages each tool comes in, from the system's own archive. missing_packages() { for tool in "$@"; do case "$tool" in docker) command -v docker >/dev/null 2>&1 || printf ' docker.io' ;; compose) docker compose version >/dev/null 2>&1 || printf ' docker-compose-v2' ;; xz) command -v xz >/dev/null 2>&1 || printf ' xz-utils' ;; curl) command -v curl >/dev/null 2>&1 || printf ' curl ca-certificates' ;; *) command -v "$tool" >/dev/null 2>&1 || printf ' %s' "$tool" ;; esac done } APT_UPDATED='' apt_install() { export DEBIAN_FRONTEND=noninteractive # 9>&-: the Moorhold app's route holds the update run lock on descriptor 9, and nothing a package starts may keep it. [ -n "$APT_UPDATED" ] || apt-get update -q 9>&- APT_UPDATED=1 # shellcheck disable=SC2086 # one word per package apt-get install -y -q --no-install-recommends $1 9>&- } # ── 3. The signed release lock ──────────────────────────────────────────────────────────────── stage 'Checking the signed release' # What downloading and checking the signature needs; Docker waits until the checks have passed. # --check installs nothing, so it says what's missing and stops (exit 3). missing=$(missing_packages minisign curl xz) if [ -n "$missing" ] && [ -n "$CHECK" ]; then die "the check needs${missing} to download and verify the release, and this server doesn't have it. Install it with: sudo apt-get install${missing}, then run the check again" 3 elif [ -n "$missing" ]; then say "installing${missing} from the system's own archive, to check the signature of what it downloads" apt_install "$missing" fi 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 Moorhold app's update route (ALLOT-2230): the signed lock must also be the one the app carries, byte for byte, # before anything is unpacked or written. Exit 3: refused, with nothing changed. if [ -n "$MAC_APP" ]; then printf '%s %s\n' "$LOCK_SHA" "$DL/release.lock" | sha256sum -c --quiet - >/dev/null 2>&1 || die "the release lock downloaded for Moorhold $MOORHOLD_VERSION isn't the one this copy of the Moorhold app carries, so nothing was installed. Download Moorhold again from moorhold.dev, then update. Nothing has changed." 3 fi # An ARM server (ALLOT-1868): the signed arm64 drill record for this version and this lock, with this # version and enough releases in a row passed. Anything less is the ALLOT-1867 refusal, before # anything else is written. The rules match apps/cli/src/arm64-drill-evidence.ts. if [ -n "$ARM_SERVER" ]; then arm_refuse() { say "no ARM server support for $MOORHOLD_VERSION: $1" die "$ARM_REFUSAL" } fetch "$BASE_URL/$MOORHOLD_VERSION/arm64-drill" "$DL/arm64-drill" 2>/dev/null || arm_refuse 'it has no arm64 drill record' fetch "$BASE_URL/$MOORHOLD_VERSION/arm64-drill.minisig" "$DL/arm64-drill.minisig" 2>/dev/null || arm_refuse 'its arm64 drill record is not signed' minisign -V -q -P "$RELEASE_PUBKEY" -m "$DL/arm64-drill" -x "$DL/arm64-drill.minisig" >/dev/null 2>&1 || arm_refuse "its arm64 drill record's signature does not verify" awk -v v="$MOORHOLD_VERSION" -v n="$ARM64_PASSES_NEEDED" ' function sha(x) { return length(x) == 64 && x !~ /[^0-9a-f]/ } NR == 1 && $0 != "moorhold-arm64-drill 1" { bad = 1 } $1 == "version" { nv++; if (NF != 2 || $2 != v) bad = 1 } $1 == "lock" { nl++; if (NF != 2 || !sha($2)) bad = 1 } $1 == "pass" { if (NF != 3 || $2 !~ /^[0-9][0-9][0-9][0-9][.][0-9][0-9]?[.][0-9]+$/ || !sha($3)) bad = 1 else seen[$2] = 1 } END { for (k in seen) c++; exit (bad || nv != 1 || nl != 1 || !(v in seen) || c < n) } ' "$DL/arm64-drill" || arm_refuse "its arm64 drill record doesn't show $ARM64_PASSES_NEEDED releases in a row passed, this one included" printf '%s %s\n' "$(awk '$1 == "lock" { print $2 }' "$DL/arm64-drill")" "$DL/release.lock" | sha256sum -c --quiet - >/dev/null 2>&1 || arm_refuse 'its arm64 drill record is for another copy of the release lock' fi # 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). --check doesn't. if [ -z "$CHECK" ]; then 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" # The arm64 drill's throwaway server says so, and `moorhold upgrade` reads it (ALLOT-1868). [ -z "$ARM_DRILL" ] || printf 'arm64 drill server (ALLOT-1868): not supported\n' >"$P/etc/moorhold/arm64-drill-server" fi 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 # ── 4a. --check: the checks from the verified bundle, in the temporary directory, then stop ──── # Not under /run: Ubuntu mounts it noexec, so Node can't run from there (ALLOT-1684). Hardened # servers often make /tmp and /var/tmp noexec too, so the copy goes in /var/lib, and is removed on exit. if [ -n "$CHECK" ]; then C="$P/var/lib/moorhold-install-check" rm -rf "$C" mkdir -p "$P/var/lib" mkdir -m 0700 "$C" CHECK_COPY="$C" mkdir -p "$C/node" "$C/app" tar --no-same-owner -xJf "$DL/node.tar.xz" -C "$C/node" --strip-components=1 tar --no-same-owner -xzf "$DL/moorhold.tar.gz" -C "$C/app" set -- check --profile "$PROFILE" --home "$P/opt/moorhold" [ -z "$HOSTNAME_ARG" ] || set -- "$@" --hostname "$HOSTNAME_ARG" cd "$C/app" status=0 "$C/node/bin/node" apps/cli/src/main.ts "$@" || status=$? cd / exit "$status" fi 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" # ── 4b. The checks, before the key prompt and before anything is pulled (ALLOT-1480) ────────── # A fresh install only: an installed server runs Moorhold already (its front holds the ports), and an # upgrade has its own checks. A blocking finding stops the install here. PREFLIGHT='' # Local mode has no hostname to check, and nothing to reach from outside (ALLOT-1781). if [ -z "$UPGRADE_FROM$INSTALLED$LOCAL" ]; then (cd "$REL/app" && "$REL/node/bin/node" apps/cli/src/main.ts check --before-key-prompt \ --hostname "$HOSTNAME_ARG" --profile "$PROFILE" --home "$P/opt/moorhold") || exit 1 PREFLIGHT=1 fi [ -z "$ASK_KEY" ] || ask_for_key stage 'Installing Docker and its tools' say "installing what is missing from the system's own archive" missing=$(missing_packages docker compose openssl) [ -z "$missing" ] || apt_install "$missing" if command -v systemctl >/dev/null 2>&1; then systemctl enable --now docker >/dev/null; fi # ── 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 # The Moorhold app's update button (ALLOT-2230): the CLI takes it as the approval on a local install, checks the # unpacked lock against the app's again, and never asks. It is handed the descriptor this script holds the update # run lock on. [ -z "$MAC_APP" ] || set -- "$@" --mac-app --lock-sha256 "$LOCK_SHA" --update-lock-fd 9 [ -z "$MAC_ACCOUNT_SET" ] || set -- "$@" --mac-account "$MAC_ACCOUNT" 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" # Without root, the release's Node can't be reached (the umask above leaves /opt/moorhold 0700): it # says to use sudo rather than leave the shell's "Permission denied" (ALLOT-1615). The same text # is WRAPPER in apps/cli/src/upgrade.ts, which an upgrade writes. cat >"$P/usr/local/bin/moorhold.new" <<'WRAPPER' #!/bin/sh # Written by the Moorhold installer: the current release's CLI. node=/opt/moorhold/current/node/bin/node if [ ! -x "$node" ] && [ "$(id -u)" -ne 0 ]; then printf 'Run it with sudo: sudo moorhold %s\n' "${*:-}" >&2 exit 1 fi exec "$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)" if [ -n "$LOCAL" ]; then set -- install --lock "$REL/release.lock" --local --profile "$PROFILE" else set -- install --lock "$REL/release.lock" --hostname "$HOSTNAME_ARG" --profile "$PROFILE" fi # No key file: `moorhold install` uses the key this server kept (ALLOT-1502). [ -z "$KEY_FILE" ] || set -- "$@" --access-key-file "$KEY_FILE" [ -n "$LOCAL" ] || set -- "$@" --acme "$ACME" set -- "$@" \ --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" # The network check ran with the others, before the key prompt. [ -z "$PREFLIGHT" ] || set -- "$@" --no-network-check [ -z "$KEEP" ] || set -- "$@" --keep-access-key-file # A new hostname for an installed Moorhold, agreed to without a terminal (ALLOT-1480). [ -z "$CHANGE_HOSTNAME" ] || set -- "$@" --change-hostname # A key the prompt wrote, rather than a file the operator gave. [ -z "$PROMPTED" ] || set -- "$@" --access-key-prompted # The sample: off for a fresh install unless a flag or the prompt says, kept as it is on a reinstall (ALLOT-1501, ALLOT-1880). 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" # ── 7. The ending: where the setup code is, and where to use it (task 6.3) ───────────────────── printf 'To read your setup code: sudo cat /etc/moorhold/secrets/MOORHOLD_SETUP_CODE\n' if [ -n "$LOCAL" ]; then printf 'Open http://127.0.0.1:47300/setup\n' else printf 'Open https://%s/setup\n' "$HOSTNAME_ARG" fi