#!/usr/bin/env bash # The Moorhold installer (SHIP-79). Run as root on a fresh Ubuntu 24.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] [--sample-data] # # 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. # 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.0" 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] [--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 KEY_FILE='' ACME=production KEEP='' SAMPLE='' while [ $# -gt 0 ]; do case "$1" in --hostname) HOSTNAME_ARG=${2:-}; shift 2 || die "$USAGE" 2 ;; --profile) PROFILE=${2:-}; shift 2 || die "$USAGE" 2 ;; --access-key-file) KEY_FILE=${2:-}; shift 2 || die "$USAGE" 2 ;; --acme) ACME=${2:-}; shift 2 || die "$USAGE" 2 ;; --keep-access-key-file) KEEP=1; shift ;; --sample-data) SAMPLE=1; shift ;; -h | --help) printf '%s\n' "$USAGE"; exit 0 ;; *) die "$USAGE" 2 ;; esac done [ -n "$HOSTNAME_ARG" ] || die "$USAGE" 2 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 'ubuntu 24.04' | 'debian 12') ;; *) die "unsupported system: $OS_ID (Ubuntu 24.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. if [ "$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 if [ -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 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 fi [ ! -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 does not exist" 2 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" 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/" chmod -R go-w "$REL.new" chmod 0755 "$REL.new" rm -rf "$REL" mv "$REL.new" "$REL" ln -sfn "$REL" "$P/opt/moorhold/current" # ── 5. The install itself ───────────────────────────────────────────────────────────────────── say "starting moorhold install (profile $PROFILE)" set -- install --lock "$REL/release.lock" --hostname "$HOSTNAME_ARG" --profile "$PROFILE" \ --access-key-file "$KEY_FILE" --acme "$ACME" \ --home "$P/opt/moorhold" --secrets-dir "$P/etc/moorhold/secrets" --runtime-dir "$P/run/moorhold" [ -z "$KEEP" ] || set -- "$@" --keep-access-key-file [ -z "$SAMPLE" ] || set -- "$@" --sample-data cd "$REL/app" "$REL/node/bin/node" apps/cli/src/main.ts "$@"