#!/bin/sh # Rouh on a Linux server, in one command (task 176, steps 176-11 and 176-25): # # curl -fsSL https://rouh.land/install.sh | sh # # What it does, in order, and it can be run again at any time: # 1. checks this server and says what it found, in plain words: Linux and which, the processor, # cores, memory, disk, Python, whether port 8420 is free, and whether it reaches rouh.land, # GitHub's container registry and GitHub; # 2. says what it will install and asks once (--yes, for a script, does not ask); # 3. installs what is missing: curl, git, openssl, and Docker with Compose (Docker's own # get.docker.com), with sudo, which may ask for your password; and Python 3.12 for Rouh's # updater, a checked copy of its own, when this server's is older; # 4. trusts nothing it has not checked, and checks before Docker or anything big is installed: # rouh.land's release and its newest tested code are each signed with Rouh's release key, # which is written below, and the updater (rouh-core) is checked out at exactly the signed # commit, never at whatever main is; # 5. hands over to the `rouh` command it installs (/usr/local/bin/rouh): the settings, Bring back # your Rouh when a kept one is here, the nightly update (between 03:00 and 05:00, after a # backup), the first start, and then `rouh setup`, the first run in this terminal. # # Nothing of Rouh's listens beyond this machine: its page is on 127.0.0.1 only, and setup happens # here, in the terminal, so nobody else can claim a fresh server. # # Options, after sh -s --: curl -fsSL https://rouh.land/install.sh | sh -s -- --yes --no-setup # --yes do not ask before installing (for a script or cloud-init) # --no-setup start Rouh and stop there; run rouh setup later, in a terminal # Settings, all optional, given before sh: curl -fsSL https://rouh.land/install.sh | ROUH_DIR=/srv/rouh sh # ROUH_DIR the settings and releases folder (~/rouh) # ROUH_CORE_DIR the updater's checkout (~/rouh-core) # ROUH_LAND https://rouh.land set -eu ROUH_LAND="${ROUH_LAND:-https://rouh.land}" ROUH_DIR="${ROUH_DIR:-$HOME/rouh}" ROUH_CORE_DIR="${ROUH_CORE_DIR:-$HOME/rouh-core}" ROUH_CORE_REPO="${ROUH_CORE_REPO:-https://github.com/rouh-land/rouh-core.git}" #: Rouh's release key: the public half of the Ed25519 key every release and tested commit is signed #: with (rouh-core deploy/container/mac_release.py PUBLIC_KEYS, rouh.land's MAC_STACK_KEYS and Rouh #: for Mac's StackRelease.trustedKeys hold the same). KEY_ID="rouh-mac-stack-1" KEY_RAW="XyU09FKab+ZrKEvIfHSaYvnLF3F8FSOnIdX8frWCjiA=" #: Python 3.12 for the updater when this server's is older (Debian 12 has 3.11): a standalone build, #: pinned by its SHA-256 (github.com/astral-sh/python-build-standalone). PY_RELEASE="20261110" PY_VERSION="3.12.16" PY_SHA_x86_64="6a9e62dad033790aef722dbd370e0ce76ce17b73627ba3ae679f537049350b39" PY_SHA_aarch64="b6a41defec4975d94b4ff1950236e7383e5c02ba40ea48c9ec33c66087a35aab" PORT=8420 PROJECT=rouh YES=0 NO_SETUP=0 for a in "$@"; do case "$a" in --yes|-y) YES=1 ;; --no-setup) NO_SETUP=1 ;; --help|-h) sed -n '2,32p' "$0" 2>/dev/null || true; exit 0 ;; *) printf 'Unknown option %s. The options are --yes and --no-setup.\n' "$a" >&2; exit 2 ;; esac done if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then B=$(printf '\033[1m'); R=$(printf '\033[0m'); G=$(printf '\033[32m'); Y=$(printf '\033[33m'); D=$(printf '\033[2m') else B=""; R=""; G=""; Y=""; D="" fi say() { printf '%s\n' "$*"; } good() { printf ' %s✓%s %s\n' "$G" "$R" "$*"; } note() { printf ' %s!%s %s\n' "$Y" "$R" "$*"; } step() { printf '\n%s%s%s\n' "$B" "$*" "$R"; } stop() { printf '\n%sRouh is not installed:%s %s\n' "$B" "$R" "$*" >&2; exit 1; } has_tty() { [ -r /dev/tty ] && [ -w /dev/tty ] && ( : /dev/null; } # A command line as one string for sg docker -c, each word in single quotes. quoted() { out="" for a in "$@"; do out="$out '$(printf '%s' "$a" | sed "s/'/'\\\\''/g")'" done printf '%s' "$out" } # Runs a command that talks to Docker: as it is, or, right after this script put the user in the # docker group, through sg so that no sign-out is needed. dock() { if [ "$DOCKER_HOW" = sg ]; then sg docker -c "$(quoted "$@")"; else "$@"; fi } python_ok() { # python_ok BINARY MINOR: that Python is 3.MINOR or newer "$1" -c "import sys; sys.exit(0 if sys.version_info >= (3, $2) else 1)" 2>/dev/null } reaches() { # reaches HOST: an HTTPS answer from it within 10 seconds curl -sS -o /dev/null --max-time 10 "https://$1/" 2>/dev/null } # --- 1. what this server is ------------------------------------------------------------------------ check_server() { step "Checking this server" [ "$(uname -s)" = Linux ] || stop "this is for Linux. On a Mac, download Rouh for Mac from $ROUH_LAND/start/?for=mac" case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; aarch64|arm64) ARCH=aarch64 ;; *) stop "Rouh needs a 64-bit Intel, AMD or ARM processor, and this one is $(uname -m)." ;; esac if [ "$(id -u)" = 0 ]; then case "$HOME" in /root|/) stop "Rouh runs as a normal user, not as root. Make one and run this again as that user: adduser rouh && usermod -aG sudo rouh && su - rouh" ;; esac SUDO="" elif command -v sudo >/dev/null 2>&1; then SUDO="sudo" else stop "installing needs sudo, which this machine does not have. Install sudo, or ask whoever runs this server." fi OS="Linux" OS_OK=maybe if [ -r /etc/os-release ]; then # shellcheck disable=SC1091 OS=$(. /etc/os-release; printf '%s' "${PRETTY_NAME:-$NAME}") id=$(. /etc/os-release; printf '%s' "${ID:-}"); ver=$(. /etc/os-release; printf '%s' "${VERSION_ID:-}") case "$id:$ver" in ubuntu:22.04|ubuntu:24.04|ubuntu:2[4-9].*|debian:12|debian:13|debian:1[4-9]) OS_OK=yes ;; esac fi [ "$OS_OK" = yes ] && good "$OS, $(uname -m)" || note "$OS, $(uname -m): Rouh is tested on Ubuntu 22.04 and 24.04 and Debian 12 and 13; this may work" cores=$(nproc 2>/dev/null || echo 1) [ "$cores" -ge 2 ] || stop "Rouh needs at least 2 processor cores, 4 to run well, and this server has $cores." [ "$cores" -ge 4 ] && good "$cores processor cores" || note "$cores processor cores: Rouh runs, and 4 are recommended" mem_kb=$(awk '/^MemTotal:/ {print $2}' /proc/meminfo 2>/dev/null || echo 0) mem_gb=$(( (mem_kb + 524288) / 1048576 )) [ "$mem_gb" -ge 4 ] || stop "Rouh needs at least 4 GB of memory, 8 GB to run well, and this server has ${mem_gb} GB." [ "$mem_gb" -ge 8 ] && good "${mem_gb} GB of memory" || note "${mem_gb} GB of memory: Rouh runs, and 8 GB are recommended" mkdir -p "$ROUH_DIR" free_gb=$(df -Pk "$ROUH_DIR" | awk 'NR==2 {print int($4/1048576)}') [ "$free_gb" -ge 15 ] || stop "Rouh needs at least 15 GB of free disk, 64 GB to run well, and there is ${free_gb} GB." [ "$free_gb" -ge 64 ] && good "${free_gb} GB of free disk" || note "${free_gb} GB of free disk: Rouh runs, and 64 GB are recommended" NEED_PY=no if command -v python3 >/dev/null 2>&1 && python_ok python3 12; then PY=python3 good "$(python3 -V 2>&1) for Rouh's updater" else NEED_PY=yes note "$(python3 -V 2>/dev/null || echo 'No Python 3'): Rouh's updater needs 3.12, so it gets a checked copy of its own" fi if [ -f "$ROUH_DIR/releases/current.json" ]; then good "Rouh is here already: this updates it and sets up what is missing" HERE_ALREADY=yes else HERE_ALREADY=no if command -v ss >/dev/null 2>&1 && [ -n "$(ss -Hltn "sport = :$PORT" 2>/dev/null)" ]; then stop "something on this server already listens on port $PORT, which Rouh's page uses. Stop it, then run this again." fi good "Port $PORT is free (Rouh's page, on this machine only)" fi for h in rouh.land ghcr.io github.com; do reaches "$h" || stop "this server does not reach $h. Check its internet connection and DNS, then run this again." done good "Reaches rouh.land, ghcr.io and github.com" } # --- 2. what it will do, said before anything changes ------------------------------------------------ summary() { step "What happens now" missing="" for t in curl git openssl; do command -v "$t" >/dev/null 2>&1 || missing="$missing $t"; done command -v ss >/dev/null 2>&1 || missing="$missing iproute2" [ -n "$missing" ] && say " · Install:$missing (with sudo)" command -v docker >/dev/null 2>&1 || say " · Install Docker, from Docker's own packages (get.docker.com), with sudo" [ "$NEED_PY" = yes ] && say " · Get Python $PY_VERSION for Rouh's updater, into ~/.local/share/rouh/python" say " · Get Rouh's updater (rouh-core) into $ROUH_CORE_DIR, at the commit rouh.land signed" say " · Install the rouh command, and turn on the nightly update (03:00 to 05:00, after a backup)" if [ "$HERE_ALREADY" = yes ]; then say " · Update Rouh to the newest signed release, if there is a newer one" else say " · Download and start Rouh: about 2 GB, about 5 minutes" fi [ "$NO_SETUP" = 1 ] || say " · Then set your Rouh up, here in this terminal: rouh setup" say " Nothing of Rouh's listens beyond this machine. Uninstall any time: rouh uninstall" if [ "$YES" = 1 ]; then return 0 fi if ! has_tty; then say " ${D}No terminal to ask in, so this goes on. Give --yes to say so on purpose.${R}" return 0 fi printf '\nGo on? [Y/n] ' >/dev/tty read -r answer /dev/null 2>&1; then $SUDO env DEBIAN_FRONTEND=noninteractive apt-get update -qq $SUDO env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null elif command -v dnf >/dev/null 2>&1; then $SUDO dnf install -y -q "$@" elif command -v yum >/dev/null 2>&1; then $SUDO yum install -y -q "$@" elif command -v zypper >/dev/null 2>&1; then $SUDO zypper --non-interactive install "$@" elif command -v pacman >/dev/null 2>&1; then $SUDO pacman -Sy --noconfirm "$@" else stop "install $* yourself (this machine has no apt, dnf, yum, zypper or pacman), then run this again." fi } get_basics() { step "Installing what is missing" missing="" for t in curl git openssl python3; do command -v "$t" >/dev/null 2>&1 || missing="$missing $t"; done command -v ss >/dev/null 2>&1 || missing="$missing iproute2" if [ -n "$missing" ]; then # shellcheck disable=SC2086 pkg_install $missing ca-certificates good "Installed:$missing" fi openssl version | grep -q '^OpenSSL 3' || stop "Rouh needs OpenSSL 3, to check that releases are signed by Rouh's key, and this is $(openssl version)." } get_python_and_docker() { if [ "$NEED_PY" = yes ]; then dest="$HOME/.local/share/rouh/python" if ! { [ -x "$dest/bin/python3" ] && python_ok "$dest/bin/python3" 12; }; then eval "sum=\$PY_SHA_$ARCH" file="cpython-$PY_VERSION+$PY_RELEASE-$ARCH-unknown-linux-gnu-install_only_stripped.tar.gz" tmp=$(mktemp -d) curl -fsSL -o "$tmp/py.tgz" "https://github.com/astral-sh/python-build-standalone/releases/download/$PY_RELEASE/$(printf '%s' "$file" | sed 's/+/%2B/')" \ || stop "Python $PY_VERSION could not be downloaded." printf '%s %s\n' "$sum" "$tmp/py.tgz" | sha256sum -c - >/dev/null 2>&1 || { rm -rf "$tmp"; stop "the Python download did not match its SHA-256, so it was not used."; } rm -rf "$dest"; mkdir -p "$dest" tar -C "$dest" --strip-components=1 -xzf "$tmp/py.tgz" rm -rf "$tmp" fi PY="$dest/bin/python3" good "Python $("$PY" -c 'import platform; print(platform.python_version())') for Rouh's updater, checked" fi get_docker } docker_works() { docker info >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; } docker_works_sg() { sg docker -c 'docker info >/dev/null 2>&1 && docker compose version >/dev/null 2>&1'; } get_docker() { if ! command -v docker >/dev/null 2>&1; then say " Installing Docker (a few minutes)…" curl -fsSL https://get.docker.com -o "$ROUH_DIR/.get-docker.sh" $SUDO sh "$ROUH_DIR/.get-docker.sh" >"$ROUH_DIR/.get-docker.log" 2>&1 \ || { tail -n 15 "$ROUH_DIR/.get-docker.log" >&2; stop "Docker did not install; its last lines are above."; } rm -f "$ROUH_DIR/.get-docker.sh" fi if ! ${SUDO:+$SUDO} docker compose version >/dev/null 2>&1; then pkg_install docker-compose-plugin 2>/dev/null || pkg_install docker-compose-v2 2>/dev/null \ || stop "Docker is here without Compose. Install Docker's Compose plugin (docs.docker.com/compose/install/linux), then run this again." fi if command -v systemctl >/dev/null 2>&1; then $SUDO systemctl enable --now docker >/dev/null 2>&1 || true fi if docker_works; then DOCKER_HOW=direct elif [ -n "$SUDO" ]; then if ! getent group docker | cut -d: -f4 | tr ',' '\n' | grep -qx "$(id -un)"; then $SUDO usermod -aG docker "$(id -un)" fi docker_works_sg && DOCKER_HOW=sg || stop "Docker does not answer. Check it with: sudo systemctl status docker" else stop "Docker does not answer. Check it with: systemctl status docker" fi good "$(dock docker --version | cut -d, -f1), Compose $(dock docker compose version --short 2>/dev/null)" } # --- 4. trust nothing unchecked ---------------------------------------------------------------------- # check_signed FILE KIND: FILE is a signed envelope (or rouh.land's newest.json holding one as # "record") whose signature is good with Rouh's release key and whose payload is of KIND; prints # the payload's commit (the release's code commit for a release). check_signed() { python3 - "$1" "$2" "$KEY_ID" "$KEY_RAW" <<'PY' import base64, json, subprocess, sys, tempfile, os path, kind, key_id, raw = sys.argv[1:5] try: doc = json.load(open(path)) env = doc.get("record", doc) if env.get("format") != 1 or env.get("key") != key_id: raise ValueError("it is not signed with Rouh's release key") payload, sig = env["payload"], base64.b64decode(env["signature"]) der = bytes.fromhex("302a300506032b6570032100") + base64.b64decode(raw) pem = b"-----BEGIN PUBLIC KEY-----\n" + base64.b64encode(der) + b"\n-----END PUBLIC KEY-----\n" with tempfile.TemporaryDirectory() as t: for name, data in (("k", pem), ("m", payload.encode()), ("s", sig)): with open(os.path.join(t, name), "wb") as f: f.write(data) ok = subprocess.run(["openssl", "pkeyutl", "-verify", "-pubin", "-inkey", os.path.join(t, "k"), "-rawin", "-in", os.path.join(t, "m"), "-sigfile", os.path.join(t, "s")], capture_output=True).returncode == 0 if not ok: raise ValueError("its signature does not match Rouh's release key") p = json.loads(payload) if p.get("kind") != kind: raise ValueError(f"it is a {p.get('kind')!r}, not a {kind!r}") commit = p.get("commit") if kind == "rouh-code-pass" else (p.get("code") or {}).get("commit", "") print(commit or "") except Exception as e: # noqa: BLE001 - every way it can fail is the same answer: not trusted print(f"NOT TRUSTED: {e}", file=sys.stderr) sys.exit(1) PY } check_trust() { step "Checking what rouh.land hands out, before anything is installed" work=$(mktemp -d) curl -fsS --max-time 30 -o "$work/release.json" "$ROUH_LAND/release/stable.json" || stop "rouh.land did not hand out its release. Run this again in a minute." check_signed "$work/release.json" rouh-release >/dev/null 2>"$work/why" \ || stop "the release rouh.land handed out did not check out ($(sed 's/^NOT TRUSTED: //' "$work/why")), so nothing of it was used." good "rouh.land's release: signed with Rouh's key" curl -fsS --max-time 30 -o "$work/code.json" "$ROUH_LAND/mac/code/newest.json" || stop "rouh.land did not say which code is the newest tested. Run this again in a minute." COMMIT=$(check_signed "$work/code.json" rouh-code-pass 2>"$work/why") \ || stop "rouh.land's newest tested code did not check out ($(sed 's/^NOT TRUSTED: //' "$work/why")), so nothing of it was used." printf '%s' "$COMMIT" | grep -Eq '^[0-9a-f]{40}$' || stop "rouh.land's newest tested code names no commit." good "rouh.land's newest tested code: signed with Rouh's key ($(printf '%s' "$COMMIT" | cut -c1-10))" rm -rf "$work" } get_updater() { step "Getting Rouh's updater" if [ ! -d "$ROUH_CORE_DIR/.git" ]; then [ ! -e "$ROUH_CORE_DIR" ] || stop "$ROUH_CORE_DIR is there and is not Rouh's updater. Move it away, or give another folder: ROUH_CORE_DIR=... sh" git init -q "$ROUH_CORE_DIR" git -C "$ROUH_CORE_DIR" remote add origin "$ROUH_CORE_REPO" fi git -C "$ROUH_CORE_DIR" fetch -q --depth 1 origin "$COMMIT" || stop "GitHub did not hand out the signed commit $COMMIT." git -C "$ROUH_CORE_DIR" checkout -q --force "$COMMIT" [ "$(git -C "$ROUH_CORE_DIR" rev-parse HEAD)" = "$COMMIT" ] || stop "the updater is not at the signed commit." [ -f "$ROUH_CORE_DIR/deploy/linux/rouh.py" ] || stop "this rouh-core has no rouh command yet (deploy/linux/rouh.py)." good "Rouh's updater at the signed, tested commit $(printf '%s' "$COMMIT" | cut -c1-10)" } # --- 5. over to the rouh command ---------------------------------------------------------------------- hand_over() { set -- install [ "$YES" = 1 ] && set -- "$@" --yes [ "$NO_SETUP" = 1 ] && set -- "$@" --no-setup export ROUH_DIR ROUH_LAND if [ "$DOCKER_HOW" = sg ]; then export ROUH_VIA_SG=1 fi if has_tty; then dock "$PY" "$ROUH_CORE_DIR/deploy/linux/rouh.py" "$@"