diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c95fad28..273eb73d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -242,6 +242,10 @@ jobs: id: assert_isolation if: ${{ !cancelled() }} run: bash tests/e2e/linux.sh isolation + - name: "assert: deb install pass" + id: assert_deb_install + if: ${{ !cancelled() }} + run: bash tests/test-deb-install.sh - name: "setup: Build the API allowlist kernel test image" id: setup_allowlist_image if: ${{ !cancelled() }} @@ -262,13 +266,15 @@ jobs: APPLIANCE: ${{ steps.assert_appliance.outcome }} ISOLATION: ${{ steps.assert_isolation.outcome }} ALLOWLIST: ${{ steps.assert_allowlist.outcome }} - run: bash scripts/require-success.sh "deploy=$DEPLOY" "appliance=$APPLIANCE" "isolation=$ISOLATION" "allowlist=$ALLOWLIST" + DEB_INSTALL: ${{ steps.assert_deb_install.outcome }} + run: bash scripts/require-success.sh "deploy=$DEPLOY" "appliance=$APPLIANCE" "isolation=$ISOLATION" "allowlist=$ALLOWLIST" "deb-install=$DEB_INSTALL" - name: "assert: categorize outcome" id: assert_categorize if: always() env: CAT_ASSERT_ALLOWLIST: ${{ steps.assert_allowlist.outcome }} CAT_ASSERT_APPLIANCE: ${{ steps.assert_appliance.outcome }} + CAT_ASSERT_DEB_INSTALL: ${{ steps.assert_deb_install.outcome }} CAT_ASSERT_DEPLOY: ${{ steps.assert_deploy.outcome }} CAT_ASSERT_DOCKER_MODES: ${{ steps.assert_docker_modes.outcome }} CAT_ASSERT_ISOLATION: ${{ steps.assert_isolation.outcome }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 62f2d020..54f1735a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -127,6 +127,13 @@ jobs: bash scripts/build-release-bundle.sh "$TAG" relbin/rigforge-"$TAG"-*-unknown-linux-musl bash scripts/build-homebrew-formula.sh "$TAG" SHA256SUMS rigforge.rb "rigforge-$TAG.tar.gz" + - name: Build the .deb package + run: | + set -euo pipefail + deb=$(SOURCE_DATE_EPOCH=$(git log -1 --format=%ct) bash scripts/build-deb.sh "rigforge-$TAG.tar.gz" "$TAG") + sha256sum "$deb" >> SHA256SUMS + echo "--- SHA256SUMS (with .deb) ---"; cat SHA256SUMS + - name: Extract release notes from CHANGELOG.md run: | set -euo pipefail @@ -151,12 +158,7 @@ jobs: # Mark 0.x tags as pre-releases. pre="" case "${TAG#v}" in 0.*) pre="--prerelease" ;; esac - # Create the release as a DRAFT so a human reviews the generated notes + bundles and clicks - # "Publish" — a deliberate gate for a tool that installs a root miner. Drop --draft here to - # go back to publishing automatically on tag push. - # Checksums only, by decision (2026-07-11): release signing was removed — see - # SECURITY.md > Release integrity. SHA256SUMS proves the bundle matches what CI built. - assets=("rigforge-$TAG.zip" "rigforge-$TAG.tar.gz" SHA256SUMS rigforge.rb) + assets=("rigforge-$TAG.zip" "rigforge-$TAG.tar.gz" "rigforge_${TAG#v}_all.deb" SHA256SUMS rigforge.rb) for binary in relbin/rigforge-"$TAG"-*-unknown-linux-musl; do [ -e "$binary" ] && assets+=("$binary") done diff --git a/Makefile b/Makefile index 59561c5d..36955bd2 100644 --- a/Makefile +++ b/Makefile @@ -48,9 +48,11 @@ test-suite: ## rigforge test suite: unit + black-box, every CPU/OS profile simul bash tests/test-release-bundle.sh bash tests/test-env-override-gate.sh python3 tests/test-server-deadline.py + bash tests/test-build-deb.sh test-e2e: ## Full end-to-end run in disposable Linux containers (needs Docker) bash tests/e2e/linux.sh + bash tests/test-deb-install.sh e2e-docs-replay: ## Replay documented setup and two reboots in an explicitly supplied disposable KVM guest python3 tests/docs_replay.py diff --git a/docs/adr/0003-package-ownership-upgrade-authority-and-key-custody.md b/docs/adr/0003-package-ownership-upgrade-authority-and-key-custody.md index 24c4a61e..eed534c3 100644 --- a/docs/adr/0003-package-ownership-upgrade-authority-and-key-custody.md +++ b/docs/adr/0003-package-ownership-upgrade-authority-and-key-custody.md @@ -29,7 +29,7 @@ Git-clone and appliance installs keep today's behaviour unchanged, with no marke - An apt upgrade of an already-configured host queues the real upgrade **outside dpkg**: postinst writes an upgrade-pending marker under `/var/lib/rigforge` and starts the one-shot `rigforge upgrade` unit via `systemctl start --no-block` (#785). A first install (no config) is inert. - The miner **restarts automatically, but only after a successful upgrade** (owner, 2026-11-20). On failure the previous build keeps running, the marker and failure stay visible in `status`/`doctor`, postinst still exits 0 so dpkg never half-configures the package, and retry is one documented `rigforge upgrade` command. `apt-mark hold` defers. - `brew upgrade` never restarts a running miner — it prints a hint; the auto-restart ruling applies to apt only (#787). -- **Purge** removes `/etc/rigforge` and `/var/lib/rigforge` but **never reverts system tuning** — not GRUB, hugepages, MSR or sysctl. Only `rigforge uninstall` undoes tuning (#784). Remove keeps config and state. +- **Purge** deletes `/var/lib/rigforge` (`config.json` included; since #780 it is state that `setup` creates, never a shipped conffile) and any `/etc/rigforge` directory the package owns, and **never reverts system tuning** — not GRUB, hugepages, MSR or sysctl. Only `rigforge uninstall` undoes tuning (#784). Remove keeps config and state. ### D3. Signing-key custody (owner, 2026-11-20 — supersedes pithead #3459) diff --git a/docs/getting-started.md b/docs/getting-started.md index cb73eaff..47491fcf 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -51,6 +51,24 @@ so run `rigforge backup` before and `rigforge restore` after. Brew cannot do the HugePages, MSR): on Linux, that is `rigforge setup`'s job, and Linuxbrew's user-owned prefix is not a safe place for code that root-run units execute, so prefer the git route below on a Linux rig. +### Install with apt (Debian / Ubuntu on Linux) + +The apt steps (signing key, source entry, `apt install rigforge`) are in the +[README](../README.md), under **Prefer `apt`?**. Installing changes nothing on the host: the +package puts the release bundle (without the developer notes under `docs/dev`) in the root-owned +`/usr/lib/rigforge`, adds the one-shot upgrade unit, and installs a `/usr/bin/rigforge` that runs +it with `RIGFORGE_LIB=/usr/lib/rigforge` and `RIGFORGE_STATE=/var/lib/rigforge` +([where files live](operations.md#where-files-live)). Provision with `sudo rigforge setup`, which +creates `config.json`, the XMRig build and every other piece of state under `/var/lib/rigforge`; +that directory is root's, so run `backup` and `restore` with `sudo` too. + +`apt upgrade rigforge` on a configured host queues the +[package upgrade](operations.md#upgrading-through-apt). `apt remove` stops and disables the units `setup` rendered for the package (never those of a git install) +and keeps `/var/lib/rigforge`. `apt purge` also deletes it, the control state under +`/var/lib/rigforge-control*`, every unit, timer and path `setup` rendered, the API firewall rules +file and the logrotate rule. Neither reverts system tuning (GRUB, HugePages, MSR, fstab, limits); +that is `rigforge uninstall`, run before `apt purge`. + --- ## 2. Get the code diff --git a/docs/operations.md b/docs/operations.md index 9a7bca3f..0df7c004 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -643,6 +643,8 @@ RigForge reads code from one directory and writes state to another. Both default | `RIGFORGE_LIB` | the directory of `rigforge.sh` | `rigforge.sh`, `util/` scripts, `systemd/` unit templates, `VERSION`, `RIGFORGE_REF`, the `.rigforge-package` marker. Never written by a worker command, so it can be root-owned and read-only. | | `RIGFORGE_STATE` | the same directory (`RIGFORGE_HOME` when set) | `config.json`, the `.rigforge-*` markers and sidecars (appliance marker, config provenance, revision key), `config-backups/`, `backups/`, support tarballs and the default worker root `data/worker`. | +The apt package runs `rigforge.sh` with `RIGFORGE_LIB=/usr/lib/rigforge` and `RIGFORGE_STATE=/var/lib/rigforge`. A tree whose `.rigforge-package` marker reads `apt` uses those values whenever `RIGFORGE_STATE` is unset, so running `/usr/lib/rigforge/rigforge.sh` directly behaves like `rigforge`. There is no `/etc/rigforge`: `config.json` is state that `setup` creates, not a dpkg conffile, so the first-run wizard still runs on a fresh install and upgrades never prompt about it. + `RIGFORGE_HOME` is the older sandbox override: it sets the state directory and the template, `VERSION` and `proposed-grub.sh` lookups, while the `util/` scripts still come from the directory of `rigforge.sh`. Set `RIGFORGE_LIB` and `RIGFORGE_STATE` instead; each wins over it. Rendered units take `ExecStart` and the server script from the lib directory and the `config.json` credential from the state directory. When either variable is set, the miner unit and the scheduled verb units (autotune, watchdog, api-refresh, control-apply, control-upgrade) and the shared-host runner carry it in their environment. Both values must be clean absolute paths (letters, digits, `.`, `_`, `-`, `/`), and as root `RIGFORGE_LIB` must be owned by root and not group- or world-writable; otherwise `rigforge.sh` refuses to start. The `upgrade` verb updates a git checkout in place, so it still needs a writable checkout. ## Backup & restore diff --git a/packaging/debian/control b/packaging/debian/control new file mode 100644 index 00000000..0eb82848 --- /dev/null +++ b/packaging/debian/control @@ -0,0 +1,13 @@ +Package: rigforge +Version: @VERSION@ +Section: net +Priority: optional +Architecture: all +Recommends: linux-tools-generic | linux-cpupower +Depends: bash, jq, curl, nftables, git, build-essential, cmake, libuv1-dev, libssl-dev, libhwloc-dev, gettext-base, python3, msr-tools, linux-tools-common | linux-cpupower +Maintainer: RigForge maintainers +Homepage: https://github.com/p2pool-starter-stack/rigforge +Description: Provision and run a tuned XMRig worker for RandomX mining + RigForge provisions and runs an XMRig worker for a Pithead coordinator, + any RandomX Stratum pool, or a solo daemon pool. Installing changes + nothing on the host; run "sudo rigforge setup" to provision. diff --git a/packaging/debian/postinst b/packaging/debian/postinst index 9d909b3b..22f1b1d6 100755 --- a/packaging/debian/postinst +++ b/packaging/debian/postinst @@ -3,16 +3,21 @@ set -e ROOT=${RIGFORGE_PKG_ROOT:-} STATE=$ROOT/var/lib/rigforge -CONFIG=$ROOT/etc/rigforge/config.json +CONFIG=$STATE/config.json RETRY='sudo rigforge upgrade' case "$1" in configure) old=$2 + mkdir -p "$STATE" || true + if [ -d "$ROOT/run/systemd/system" ]; then systemctl daemon-reload || true; fi + if [ -z "$old" ]; then + echo "rigforge: installed; nothing on this host changed. Provision with: sudo rigforge setup" + fi new=$(dpkg-query -W -f '${Version}' rigforge 2>/dev/null || true) if [ -n "$old" ] && [ -n "$new" ] && [ -f "$CONFIG" ] && dpkg --compare-versions "$new" gt "$old"; then - if install -d -m 0755 "$STATE" && tmp=$(mktemp "$STATE/.upgrade-pending.XXXXXX") && chmod 0644 "$tmp" && printf '%s\n' "$new" >"$tmp" && mv -f "$tmp" "$STATE/upgrade-pending"; then - if [ -d "$ROOT/run/systemd/system" ] && systemctl daemon-reload && systemctl start --no-block rigforge-upgrade.service; then + if tmp=$(mktemp "$STATE/.upgrade-pending.XXXXXX") && chmod 0644 "$tmp" && printf '%s\n' "$new" >"$tmp" && mv -f "$tmp" "$STATE/upgrade-pending"; then + if [ -d "$ROOT/run/systemd/system" ] && systemctl start --no-block rigforge-upgrade.service; then echo "rigforge: upgrade to $new queued outside dpkg; follow it with: journalctl -u rigforge-upgrade" else echo "rigforge: could not start the upgrade unit; run: $RETRY" >&2 diff --git a/packaging/debian/postrm b/packaging/debian/postrm new file mode 100755 index 00000000..a4b93fe0 --- /dev/null +++ b/packaging/debian/postrm @@ -0,0 +1,29 @@ +#!/bin/sh +set -e + +ROOT=${RIGFORGE_PKG_ROOT:-} +UNITS="xmrig.service rigforge-upgrade.service rigforge-autotune.timer rigforge-autotune.service rigforge-watchdog.timer rigforge-watchdog.service rigforge-api.service rigforge-api-refresh.timer rigforge-api-refresh.service rigforge-control.service rigforge-control-apply.path rigforge-control-apply.service rigforge-control-upgrade.path rigforge-control-upgrade.service" + +case "$1" in +purge) + U=$ROOT/etc/systemd/system + ours() { grep -Eqs '/usr/lib/rigforge/|RIGFORGE_STATE=/var/lib/rigforge$' "$U/$1"; } + drop="" + for unit in $UNITS; do + case "$unit" in *.timer | *.path) svc=${unit%.*}.service ;; *) svc=$unit ;; esac + if ours "$svc"; then drop="$drop $unit"; fi + done + if ours xmrig.service || ours rigforge-api.service || ours rigforge-control.service; then drop="$drop rigforge-api-firewall.nft"; fi + if ours rigforge-control.service; then rm -rf "$ROOT/var/lib/rigforge-control" "$ROOT/var/lib/rigforge-control-upgrade"; fi + for unit in $drop; do rm -f "$U/$unit"; done + if grep -qs /var/lib/rigforge/ "$ROOT/etc/logrotate.d/xmrig"; then rm -f "$ROOT/etc/logrotate.d/xmrig"; fi + if [ "$(cat "$ROOT/etc/rigforge-root-guard" 2>/dev/null)" = "allow /usr/lib/rigforge" ]; then rm -f "$ROOT/etc/rigforge-root-guard"; fi + rm -rf "$ROOT/var/lib/rigforge" + if [ -d "$ROOT/run/systemd/system" ]; then systemctl daemon-reload || true; fi + ;; +remove) + if [ -d "$ROOT/run/systemd/system" ]; then systemctl daemon-reload || true; fi + ;; +esac + +exit 0 diff --git a/packaging/debian/prerm b/packaging/debian/prerm new file mode 100755 index 00000000..72e73acc --- /dev/null +++ b/packaging/debian/prerm @@ -0,0 +1,21 @@ +#!/bin/sh +set -e + +ROOT=${RIGFORGE_PKG_ROOT:-} +UNITS="xmrig.service rigforge-upgrade.service rigforge-autotune.timer rigforge-autotune.service rigforge-watchdog.timer rigforge-watchdog.service rigforge-api.service rigforge-api-refresh.timer rigforge-api-refresh.service rigforge-control.service rigforge-control-apply.path rigforge-control-apply.service rigforge-control-upgrade.path rigforge-control-upgrade.service" + +case "$1" in +remove) + U=$ROOT/etc/systemd/system + if [ -d "$ROOT/run/systemd/system" ]; then + systemctl stop rigforge-upgrade.service + for unit in $UNITS; do + case "$unit" in *.timer | *.path) svc=${unit%.*}.service ;; *) svc=$unit ;; esac + grep -Eqs '/usr/lib/rigforge/|RIGFORGE_STATE=/var/lib/rigforge$' "$U/$svc" || continue + if grep -qs '^\[Install\]' "$U/$unit"; then systemctl disable --now "$unit"; else systemctl stop "$unit"; fi + done + fi + ;; +esac + +exit 0 diff --git a/packaging/debian/rigforge b/packaging/debian/rigforge new file mode 100755 index 00000000..758e437c --- /dev/null +++ b/packaging/debian/rigforge @@ -0,0 +1,3 @@ +#!/bin/sh +export RIGFORGE_LIB=/usr/lib/rigforge RIGFORGE_STATE=/var/lib/rigforge +exec /usr/lib/rigforge/rigforge.sh "$@" diff --git a/rigforge.sh b/rigforge.sh index 7c537830..b28e14f1 100755 --- a/rigforge.sh +++ b/rigforge.sh @@ -60,6 +60,11 @@ _script_dir() { case "$src" in */*) d="${src%/*}" ;; *) d=. ;; esac cd -P "${d:-/}" &>/dev/null && pwd } +_pkg_marker="${RIGFORGE_PACKAGE_MARKER:-$(_script_dir)/.rigforge-package}" +[ -n "${RIGFORGE_STATE:-}" ] || [ ! -f "$_pkg_marker" ] || { + read -r _pkg_marker <"$_pkg_marker" || : + [ "$_pkg_marker" != apt ] || export RIGFORGE_LIB="${RIGFORGE_LIB:-$(_script_dir)}" RIGFORGE_STATE=/var/lib/rigforge +} for _v in RIGFORGE_LIB RIGFORGE_STATE; do [ -z "${!_v:-}" ] || [[ ${!_v} =~ ^/[A-Za-z0-9._/-]+$ ]] || exit "$(echo "rigforge: $_v must be a clean absolute path" >&2 && echo 1)"; done [ -z "${RIGFORGE_LIB:-}" ] || [ "$(id -u)" != 0 ] || [ -z "$(find -H "$RIGFORGE_LIB" -maxdepth 0 \( ! -user root -o -perm -020 -o -perm -002 \) 2>/dev/null)" ] || exit "$(echo "rigforge: RIGFORGE_LIB must be root-owned and not group/world-writable" >&2 && echo 1)" [ "${RIGFORGE_TEST_OVERRIDES:-0}" = 1 ] || unset XMRIG_VERSION XMRIG_COMMIT API_CMD DMIDECODE RDMSR_BIN SERVICE_NAME LOGROTATE_DIR GRUB_DEFAULT FSTAB LIMITS_CONF MODULES_LOAD_DIR MODULES_FILE SYSTEMD_DIR HUGEPAGES_1G_DIR BIN_DIR MEMINFO CPUINFO MSR_MODULE_DIR GOVERNOR_FILE HUGEPAGES_1G_NR NR_HUGEPAGES_FILE CPUFREQ_MAX CPU_SYSFS DMI_DIR SMT_CONTROL LOCKDOWN_FILE RIGFORGE_APPLIANCE_MARKER RIGFORGE_LEGACY_APPLIANCE_UNIT @@ -4078,16 +4083,7 @@ for path in sys.argv[1:]: PY } -_config_replace() { - local tmp="$CONFIG_JSON.new.$$" - (umask 077 && cp "$1" "$tmp") && chmod 600 "$tmp" && mv -f "$tmp" "$CONFIG_JSON" && return 0 - rm -f "$tmp" - return 1 -} -# Merge an allowlisted, parse-valid staged change into config.json after snapshotting the old one. -# rc 0 = committed, 1 = invalid/rejected, 2 = install failed with old config restored, and 3 = -# storage failed after rename and recovery is uncertain. The caller records the matching terminal. -_control_commit() { # +_control_commit() { local staged="$1" backups="$2" local CONTROL_WRITABLE_KEYS="pools DONATION autotune watchdog watchdog_interval_min max_temp_c" local change allow badkeys stamp backup cand msg diff --git a/scripts/build-deb.sh b/scripts/build-deb.sh new file mode 100755 index 00000000..fd9cf71a --- /dev/null +++ b/scripts/build-deb.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROG=${0##*/} +PKG_SRC="$(cd "$(dirname "${BASH_SOURCE[0]}")/../packaging/debian" && pwd)" + +die() { + printf '%s: %s\n' "$PROG" "$1" >&2 + exit 1 +} + +[[ $# -eq 2 ]] || { + printf 'usage: %s \n' "$PROG" >&2 + exit 2 +} + +ARCHIVE=$1 +VERSION=${2#v} +[[ -f $ARCHIVE ]] || die "release archive not found: $ARCHIVE" +[[ $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "not a release version: $2" + +umask 022 +STAGING=$(mktemp -d) +trap 'rm -rf -- "$STAGING"' EXIT +tar -xzf "$ARCHIVE" -C "$STAGING" + +SRC="" +for d in "$STAGING"/*/; do + [[ -f $d/rigforge.sh ]] && SRC=${d%/} && break +done +[[ -n $SRC ]] || die "no top-level directory with rigforge.sh in the archive" +[[ "$(tr -d '[:space:]' <"$SRC/VERSION")" == "$VERSION" ]] || die "VERSION in archive ($(tr -d '[:space:]' <"$SRC/VERSION")) disagrees with $VERSION" + +export SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(stat -c %Y "$SRC/VERSION")} + +ROOT="$STAGING/root" +mkdir -p "$ROOT/usr/lib" "$ROOT/usr/bin" "$ROOT/usr/lib/systemd/system" "$ROOT/var/lib/rigforge" "$ROOT/DEBIAN" +cp -a "$SRC" "$ROOT/usr/lib/rigforge" +rm -rf "$ROOT/usr/lib/rigforge/docs/dev" +chmod -R go-w,ug-s "$ROOT/usr/lib/rigforge" +printf 'apt\n' >"$ROOT/usr/lib/rigforge/.rigforge-package" +install -m 0755 "$PKG_SRC/rigforge" "$ROOT/usr/bin/rigforge" +install -m 0644 "$PKG_SRC/rigforge-upgrade.service" "$ROOT/usr/lib/systemd/system/" +install -m 0755 "$PKG_SRC/postinst" "$PKG_SRC/prerm" "$PKG_SRC/postrm" "$ROOT/DEBIAN/" +sed "s/@VERSION@/$VERSION/" "$PKG_SRC/control" >"$ROOT/DEBIAN/control" + +find "$ROOT" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} + +dpkg-deb --build --root-owner-group -Zxz "$ROOT" "rigforge_${VERSION}_all.deb" >/dev/null +printf '%s\n' "rigforge_${VERSION}_all.deb" diff --git a/tests/README.md b/tests/README.md index 7ca61a37..521f15b8 100644 --- a/tests/README.md +++ b/tests/README.md @@ -52,7 +52,9 @@ CI median/p90, failures and occupied hours; missing evidence leaves acceptance o | **Linux container e2e** | [`e2e/linux.sh`](e2e/linux.sh) → [`e2e/in-container.sh`](e2e/in-container.sh) | Disposable Ubuntu container, **needs Docker**. **In CI.** | The genuine Linux deploy path against a real (throwaway) `/etc` with real GNU tools (`sed -i`, `tee`, `envsubst`) + idempotency on re-run. Only the heavy/privileged bits (compile, package install, `systemctl`/`mount`) are stubbed. A second pass re-runs the container with `RIGFORGE_APPLIANCE=1` (#348) and proves the appliance contracts on that real `/etc`: units land in `/run/systemd/system`, no package installs, no fstab/limits.conf/logrotate writes, every enable carries `--runtime`. A third script, [`e2e/isolation.sh`](e2e/isolation.sh) (#668), puts the rig on a Docker `--internal` network whose only reachable peer is a SOCKS5 proxy fronting a stand-in GitHub (a TLS git smart-HTTP server plus the releases API). With `tor_socks5` set it runs `upgrade --check`, `setup`, a local `upgrade` with an XMRig pin change and the `control-upgrade` oneshot in its own empty environment, and proves from the proxy's log that every connection named a hostname. It also proves the block is real (a direct connection and a keyless `upgrade --check` fail), that an unreachable proxy fails closed without reaching the upstream, and that the live build survives the failed upgrade. | `make test-e2e` | | **Native macOS e2e** | [`e2e/macos.sh`](e2e/macos.sh) | A real Mac, **manual, local only. Not in CI** (macOS is best effort). | The macOS deploy path with genuine BSD tools the Linux CI can only stub: BSD `sed`, the macOS config profile, `mac_*` process control (real `nohup` + PID file), the launchd login agent, `backup`/`restore`. | `make test-e2e-macos` | | **Coverage gate** | [`coverage.sh`](coverage.sh) | kcov in **Docker**. **In CI.** | Line coverage of `rigforge.sh` and its shell helpers by running `run.sh` under kcov; enforces the committed floor ([`coverage-floor.txt`](coverage-floor.txt)) plus a patch-coverage gate (diff-cover) on changed lines. | `make coverage` | -| **Package upgrade** | [`test-package-upgrade.sh`](test-package-upgrade.sh) | Any host, no Docker. **In CI** (`make test-suite`). | [`../packaging/debian/postinst`](../packaging/debian/postinst) queues only on a greater version with a config, never fails dpkg; `package-upgrade` re-renders units and clears the marker on success, and on failure keeps it, records it for status/doctor and does not restart. | `bash tests/test-package-upgrade.sh` | +| **Package upgrade** | [`test-package-upgrade.sh`](test-package-upgrade.sh) | Any host, no Docker. **In CI** (`make test-suite`). | [`../packaging/debian/postinst`](../packaging/debian/postinst) creates the state dir, reloads systemd and prints the first-run step on a first install, and queues only on a greater version with a config, never failing dpkg; `prerm` stops and disables only the units the package rendered, on remove only and `postrm` accepts every dpkg action and on purge deletes config, state, control state, every rendered unit, timer and path, the firewall rules and logrotate rule, and keeps an `xmrig.service` the package did not render; `package-upgrade` re-renders units and clears the marker on success, and on failure keeps it, records it for status/doctor and does not restart. | `bash tests/test-package-upgrade.sh` | +| **Debian package build** | [`test-build-deb.sh`](test-build-deb.sh) | Linux with `dpkg-deb`, no Docker. **In CI** (`make test-suite`). | [`../scripts/build-deb.sh`](../scripts/build-deb.sh) builds a byte-reproducible `.deb` from the release bundle with the marker where `util/package-mode.sh` reads it, the `packaging/debian` maintainer scripts and upgrade unit, the full `Depends`, and no apt keyring. | `bash tests/test-build-deb.sh` | +| **Debian package install** | [`test-deb-install.sh`](test-deb-install.sh) | Clean `ubuntu:24.04` container, **needs Docker**. **In CI** (`make test-e2e`). | `apt install` of the built `.deb`, `rigforge --version`, `doctor`, an upgrade to the next version, with config in `/var/lib/rigforge` and nothing written under `/usr/lib/rigforge`, a direct `rigforge.sh` run using the package layout, an upgrade that queues the package upgrade, then remove keeps config, state and units and purge removes them. | `bash tests/test-deb-install.sh` | | **APT feed** | [`test-apt-feed.sh`](test-apt-feed.sh) | Ubuntu 24.04, **root**, no Docker (needs `apt-utils`). **In CI.** (`ci.yml` job) | [`../scripts/build-apt.sh`](../scripts/build-apt.sh) end to end with throwaway keys: refusal of missing/bad fingerprints and keyrings without the secret key; a signed flat feed that `apt-get update` and `download` accept from `file://`; refusal of tampered `Release`/`InRelease`/`.deb` and of a feed signed by another key. | CI only (`sudo bash tests/test-apt-feed.sh`) | | **Release smoke (quick)** | [`smoke.sh`](smoke.sh) | Real Linux rig, **manual**. Not in CI. | The compiled binary actually starts and hashes (`xmrig --bench`, fully offline). Fast pre-tag confidence that the worker we ship runs. | `make smoke` | | **Release e2e (full)** | [`e2e-real.sh`](e2e-real.sh) | Real Linux rig, **manual, root**. Not in CI. | The real thing end to end: build + tune + kernel tuning + service + a real hash, the writable control path (#236) against real systemd (`control` phase), the remote-upgrade chain with real git as the root oneshot (`upgrade` phase, #322), then a clean uninstall. **The release gate.** | `make e2e-real` (see [`RELEASING.md`](../RELEASING.md)) | diff --git a/tests/manifest.json b/tests/manifest.json index 87acd8e3..85a6ac89 100644 --- a/tests/manifest.json +++ b/tests/manifest.json @@ -241,6 +241,30 @@ }, "needs": [] }, + { + "name": "build-deb", + "file": "tests/test-build-deb.sh", + "phases": [], + "timeout_seconds": 300, + "inputs": [], + "exit_codes": { + "0": "pass", + "1": "fail" + }, + "needs": [] + }, + { + "name": "deb-install", + "file": "tests/test-deb-install.sh", + "phases": [], + "timeout_seconds": 900, + "inputs": [], + "exit_codes": { + "0": "pass", + "1": "fail" + }, + "needs": [] + }, { "name": "e2e-gate", "file": "tests/test-e2e-gate.sh", diff --git a/tests/test-build-deb.sh b/tests/test-build-deb.sh new file mode 100644 index 00000000..ad81ab61 --- /dev/null +++ b/tests/test-build-deb.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +set -euo pipefail +ROOT=$(cd "$(dirname "$0")/.." && pwd) + +if [[ $(uname -s) != Linux ]] || ! command -v dpkg-deb >/dev/null 2>&1; then + printf 'SKIP: build-deb needs Linux with dpkg-deb\n' + exit 0 +fi + +fail=0 +check() { + if [ "$2" -eq 0 ]; then printf 'ok: %s\n' "$1"; else + printf 'FAIL: %s\n' "$1" >&2 + fail=1 + fi +} + +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT +VER=$(tr -d '[:space:]' <"$ROOT/VERSION") + +bundle() { + local out=$1 ver=$2 stage="$WORK/rigforge-v$2" + rm -rf "$stage" + mkdir -p "$stage" + (cd "$ROOT" && cp -a rigforge.sh util systemd config.minimal.json config.reference.json README.md docs images LICENSE VERSION "$stage/") + printf '%s\n' "$ver" >"$stage/VERSION" + tar -czf "$out" -C "$WORK" "rigforge-v$ver" +} + +bundle "$WORK/bundle.tar.gz" "$VER" +B="$ROOT/scripts/build-deb.sh" +check "two arguments are required" "$(bash "$B" "$WORK/bundle.tar.gz" >/dev/null 2>&1 && echo 1 || echo 0)" +check "missing archive fails" "$(bash "$B" "$WORK/nope.tar.gz" "$VER" >/dev/null 2>&1 && echo 1 || echo 0)" +check "a non-release version fails" "$(bash "$B" "$WORK/bundle.tar.gz" "v$VER-rc1" >/dev/null 2>&1 && echo 1 || echo 0)" +check "VERSION disagreeing with the tag fails" "$(cd "$WORK" && bash "$B" "$WORK/bundle.tar.gz" 0.0.1 >/dev/null 2>&1 && echo 1 || echo 0)" + +mkdir -p "$WORK/a" "$WORK/b" +(cd "$WORK/a" && bash "$B" "$WORK/bundle.tar.gz" "v$VER" >/dev/null) +sleep 1 +(cd "$WORK/b" && bash "$B" "$WORK/bundle.tar.gz" "v$VER" >/dev/null) +DEB="$WORK/a/rigforge_${VER}_all.deb" +check "builds rigforge__all.deb" "$([ -f "$DEB" ] && echo 0 || echo 1)" +check "two builds of one bundle are byte-identical" "$(cmp -s "$DEB" "$WORK/b/rigforge_${VER}_all.deb" && echo 0 || echo 1)" + +check "control Package is rigforge" "$([ "$(dpkg-deb -f "$DEB" Package)" = rigforge ] && echo 0 || echo 1)" +check "control Version is $VER" "$([ "$(dpkg-deb -f "$DEB" Version)" = "$VER" ] && echo 0 || echo 1)" +check "control Architecture is all" "$([ "$(dpkg-deb -f "$DEB" Architecture)" = all ] && echo 0 || echo 1)" +DEPENDS=$(dpkg-deb -f "$DEB" Depends) +for p in bash jq curl nftables git build-essential cmake libuv1-dev libssl-dev libhwloc-dev gettext-base python3 msr-tools; do + check "Depends lists $p" "$(sed 's/[,|]/\n/g; s/ //g' <<<"$DEPENDS" | grep -qx "$p" && echo 0 || echo 1)" +done +check "Maintainer has the Name form" "$(dpkg-deb -f "$DEB" Maintainer | grep -qE '^[^<]+ <[^>]+@[^>]+>$' && echo 0 || echo 1)" + +X="$WORK/x" +dpkg-deb --raw-extract "$DEB" "$X" +L="$X/usr/lib/rigforge" +check "code tree under /usr/lib/rigforge" "$([ -f "$L/rigforge.sh" ] && [ -d "$L/util" ] && [ -d "$L/systemd" ] && echo 0 || echo 1)" +check "/usr/bin/rigforge is the packaging/debian wrapper" "$(cmp -s "$X/usr/bin/rigforge" "$ROOT/packaging/debian/rigforge" && [ -x "$X/usr/bin/rigforge" ] && echo 0 || echo 1)" +mkdir -p "$WORK/probe" +printf '#!/bin/sh\necho "$RIGFORGE_LIB $RIGFORGE_STATE $*"\n' >"$WORK/probe/rigforge.sh" +chmod +x "$WORK/probe/rigforge.sh" +sed "s|/usr/lib/rigforge/rigforge.sh|$WORK/probe/rigforge.sh|" "$X/usr/bin/rigforge" >"$WORK/probe/wrapper" +check "wrapper runs rigforge.sh with RIGFORGE_LIB and RIGFORGE_STATE set" "$([ "$(env -u RIGFORGE_LIB -u RIGFORGE_STATE sh "$WORK/probe/wrapper" doctor --json)" = "/usr/lib/rigforge /var/lib/rigforge doctor --json" ] && echo 0 || echo 1)" +check "state dir /var/lib/rigforge" "$([ -d "$X/var/lib/rigforge" ] && echo 0 || echo 1)" +check "package marker is where util/package-mode.sh reads it" "$(RIGFORGE_LIB="$L" bash -c 'source "$RIGFORGE_LIB/util/config-apply-state.sh"; source "$RIGFORGE_LIB/util/package-mode.sh"; [ "$(_package_channel)" = apt ]' && echo 0 || echo 1)" +check "ships the upgrade unit" "$(cmp -s "$X/usr/lib/systemd/system/rigforge-upgrade.service" "$ROOT/packaging/debian/rigforge-upgrade.service" && echo 0 || echo 1)" +for s in postinst prerm postrm; do + check "DEBIAN/$s is packaging/debian/$s" "$(cmp -s "$X/DEBIAN/$s" "$ROOT/packaging/debian/$s" && [ -x "$X/DEBIAN/$s" ] && echo 0 || echo 1)" +done +check "no group- or world-writable or setuid file in the package" "$([ -z "$(find "$X/usr" \( -perm -0020 -o -perm -0002 -o -perm -4000 -o -perm -2000 \) ! -type l)" ] && echo 0 || echo 1)" +check "ships no apt keyring" "$([ ! -e "$X/etc/apt" ] && echo 0 || echo 1)" +check "ships no developer notes" "$([ ! -e "$L/docs/dev" ] && echo 0 || echo 1)" +check "prerm and postrm act on the same unit list" "$([ "$(grep '^UNITS=' "$X/DEBIAN/prerm")" = "$(grep '^UNITS=' "$X/DEBIAN/postrm")" ] && echo 0 || echo 1)" + +[ "$fail" -eq 0 ] && printf 'build-deb: all checks passed\n' +exit "$fail" diff --git a/tests/test-deb-install.sh b/tests/test-deb-install.sh new file mode 100755 index 00000000..d11846ad --- /dev/null +++ b/tests/test-deb-install.sh @@ -0,0 +1,135 @@ +#!/usr/bin/env bash +set -euo pipefail +ROOT=$(cd "$(dirname "$0")/.." && pwd) +IMAGE=ubuntu:24.04 + +doctor_runs() { + local rc + set +e + out=$("${2:-rigforge}" doctor 2>&1) + rc=$? + set -e + printf '%s\n' "$out" | sed 's/^/ doctor| /' + case "$rc" in + 0 | 1) ;; + *) + echo "FAIL: doctor exited $rc ($1)" + return 1 + ;; + esac + grep -q "Checking the worker" <<<"$out" || { + echo "FAIL: doctor did not run its checks ($1)" + return 1 + } + echo "ok: doctor runs its checks from the packaged tree ($1, exit $rc)" +} + +guest() { + local a=$1 b=$2 + export DEBIAN_FRONTEND=noninteractive + apt-get update -qq + apt-get install -y -qq "/debs/rigforge_${a}_all.deb" >/dev/null + [ "$(rigforge --version)" = "RigForge $a" ] || { + echo "FAIL: rigforge --version after install" + return 1 + } + echo "ok: apt install ./rigforge_${a}_all.deb, rigforge --version is $a" + [ "$(cat /usr/lib/rigforge/.rigforge-package)" = apt ] || { + echo "FAIL: package marker" + return 1 + } + echo "ok: package marker reads apt" + lib_before=$(find /usr/lib/rigforge -printf '%p %s %T@\n' | sort | sha256sum) + doctor_runs "no config yet" + grep -q "no config.json" <<<"$out" || { + echo "FAIL: doctor found a config before setup" + return 1 + } + cp /usr/lib/rigforge/config.minimal.json /var/lib/rigforge/config.json + doctor_runs "config in /var/lib/rigforge" + if grep -q "no config.json" <<<"$out"; then + echo "FAIL: doctor did not read /var/lib/rigforge/config.json" + return 1 + fi + echo "ok: rigforge reads its config from RIGFORGE_STATE" + doctor_runs "direct /usr/lib/rigforge/rigforge.sh" /usr/lib/rigforge/rigforge.sh + if grep -q "no config.json" <<<"$out"; then + echo "FAIL: running /usr/lib/rigforge/rigforge.sh directly did not use /var/lib/rigforge" + return 1 + fi + echo "ok: running /usr/lib/rigforge/rigforge.sh directly uses the package layout too" + [ "$(find /usr/lib/rigforge -printf '%p %s %T@\n' | sort | sha256sum)" = "$lib_before" ] || { + echo "FAIL: running rigforge wrote under /usr/lib/rigforge" + return 1 + } + echo "ok: nothing was written under /usr/lib/rigforge" + echo kept >/var/lib/rigforge/state-probe + mkdir -p /etc/systemd/system + printf '[Service]\nEnvironment=RIGFORGE_STATE=/var/lib/rigforge\nExecStart=/var/lib/rigforge/data/worker/xmrig/build/xmrig\n' >/etc/systemd/system/xmrig.service + printf '[Timer]\nOnCalendar=monthly\n' >/etc/systemd/system/rigforge-autotune.timer + printf '[Service]\nExecStart=/usr/lib/rigforge/rigforge.sh autotune\n' >/etc/systemd/system/rigforge-autotune.service + apt-get install -y -qq "/debs/rigforge_${b}_all.deb" >/dev/null + [ "$(rigforge --version)" = "RigForge $b" ] || { + echo "FAIL: rigforge --version after upgrade" + return 1 + } + [ -f /var/lib/rigforge/config.json ] && [ -f /var/lib/rigforge/state-probe ] || { + echo "FAIL: upgrade lost config or state" + return 1 + } + [ "$(cat /var/lib/rigforge/upgrade-pending 2>/dev/null)" = "$b" ] || { + echo "FAIL: upgrade of a configured host did not queue the package upgrade" + return 1 + } + echo "ok: upgrade to $b keeps config and state and queues the package upgrade" + apt-get remove -y -qq rigforge >/dev/null + [ ! -e /usr/bin/rigforge ] && [ ! -e /usr/lib/rigforge ] || { + echo "FAIL: remove left the code" + return 1 + } + [ -f /var/lib/rigforge/config.json ] && [ -f /var/lib/rigforge/state-probe ] && [ -f /etc/systemd/system/xmrig.service ] || { + echo "FAIL: remove dropped config, state or the rendered unit" + return 1 + } + echo "ok: remove deletes the code and keeps config and state" + apt-get purge -y -qq rigforge >/dev/null + [ ! -e /var/lib/rigforge ] && [ ! -e /etc/systemd/system/xmrig.service ] && [ ! -e /etc/systemd/system/rigforge-autotune.timer ] && [ ! -e /etc/systemd/system/rigforge-autotune.service ] || { + echo "FAIL: purge left residue" + return 1 + } + if dpkg -s rigforge >/dev/null 2>&1; then + echo "FAIL: dpkg still knows rigforge after purge" + return 1 + fi + echo "ok: purge leaves no rigforge residue" +} + +if [ "${1:-}" = --guest ]; then + guest "$2" "$3" + exit +fi + +if ! command -v docker >/dev/null 2>&1 || ! docker info >/dev/null 2>&1 || ! command -v dpkg-deb >/dev/null 2>&1; then + if [ -n "${CI:-}" ]; then + echo "FAIL: CI needs docker and dpkg-deb for the package install test" + exit 1 + fi + echo "SKIP: package install test needs docker and dpkg-deb" + exit 0 +fi + +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT +A=$(tr -d '[:space:]' <"$ROOT/VERSION") +B="${A%.*}.$((${A##*.} + 1))" +mkdir -p "$WORK/debs" +for v in "$A" "$B"; do + stage="$WORK/rigforge-v$v" + mkdir -p "$stage" + (cd "$ROOT" && cp -a rigforge.sh util systemd config.minimal.json config.reference.json README.md docs images LICENSE VERSION "$stage/") + printf '%s\n' "$v" >"$stage/VERSION" + tar -czf "$WORK/rigforge-v$v.tar.gz" -C "$WORK" "rigforge-v$v" + (cd "$WORK/debs" && bash "$ROOT/scripts/build-deb.sh" "$WORK/rigforge-v$v.tar.gz" "v$v" >/dev/null) +done +docker run --rm -v "$WORK/debs:/debs:ro" -v "$ROOT/tests/test-deb-install.sh:/t.sh:ro" "$IMAGE" bash /t.sh --guest "$A" "$B" +echo "deb install: all checks passed" diff --git a/tests/test-package-mode.sh b/tests/test-package-mode.sh index ed2a841b..f1422385 100644 --- a/tests/test-package-mode.sh +++ b/tests/test-package-mode.sh @@ -111,3 +111,37 @@ for ch in "" apt; do esac done rm -f "$DR/.rigforge-package" +PL="$(mktemp -d)" +printf 'apt\n' >"$PL/apt" +printf 'brew\n' >"$PL/brew" +layout() { + ( + unset RIGFORGE_STATE RIGFORGE_LIB RIGFORGE_HOME + id() { echo 1000; } + RIGFORGE_PACKAGE_MARKER="$PL/$1" + [ -z "${2:-}" ] || RIGFORGE_STATE="$2" + source "$SCRIPT" >/dev/null 2>&1 + printf '%s %s' "$CONFIG_JSON" "${RIGFORGE_LIB:-}" + ) +} +TREE="$(cd -P "$(dirname "$SCRIPT")" && pwd)" +assert_eq "an apt-marked tree defaults to the package layout" "$(layout apt)" "/var/lib/rigforge/config.json $TREE" +assert_eq "an explicit RIGFORGE_STATE wins over the apt layout" "$(layout apt "$PL")" "$PL/config.json " +assert_eq "a brew-marked tree keeps state beside the script" "$(layout brew)" "$TREE/config.json " +mkdir -p "$PL/tree" +cp -R "$TREE/rigforge.sh" "$TREE/VERSION" "$TREE/util" "$TREE/systemd" "$PL/tree/" +printf 'apt\n' >"$PL/tree/.rigforge-package" +ln -s "$PL/tree/rigforge.sh" "$PL/link.sh" +PLT="$(cd -P "$PL/tree" && pwd)" +for form in bare symlink; do + assert_eq "an apt-marked tree run by $form name uses the package layout" "$( + cd "$PL/tree" || exit + unset RIGFORGE_STATE RIGFORGE_LIB RIGFORGE_HOME RIGFORGE_PACKAGE_MARKER + id() { echo 1000; } + entry=rigforge.sh + [ "$form" = bare ] || entry="$PL/link.sh" + source "$entry" >/dev/null 2>&1 + printf '%s %s' "$CONFIG_JSON" "${RIGFORGE_LIB:-}" + )" "/var/lib/rigforge/config.json $PLT" +done +rm -rf "$PL" diff --git a/tests/test-package-upgrade.sh b/tests/test-package-upgrade.sh index 02bd929d..9fe56012 100644 --- a/tests/test-package-upgrade.sh +++ b/tests/test-package-upgrade.sh @@ -22,7 +22,7 @@ STUB cat >"$STUBS/systemctl" <<'STUB' #!/usr/bin/env bash echo "systemctl $*" >>"$CALLS" -[ -z "${SYSTEMCTL_FAIL:-}" ] +[ -z "${SYSTEMCTL_FAIL:-}" ] || [ "$1" = cat ] STUB chmod +x "$STUBS"/* export CALLS @@ -37,24 +37,27 @@ check() { postinst() { local r="$D/$1" - mkdir -p "$r/etc/rigforge" - [ "$4" != y ] || echo '{}' >"$r/etc/rigforge/config.json" + mkdir -p "$r/var/lib/rigforge" + [ "$4" != y ] || echo '{}' >"$r/var/lib/rigforge/config.json" [ "${5:-y}" != y ] || mkdir -p "$r/run/systemd/system" : >"$CALLS" NEW_VERSION="$3" RIGFORGE_PKG_ROOT="$r" PATH="$STUBS:$PATH" sh "$POSTINST" configure "$2" >"$r/out" 2>&1 } postinst first "" 2.0.0 y -check "first install is inert" "$([ ! -e "$D/first/var/lib/rigforge/upgrade-pending" ] && [ ! -s "$CALLS" ] && echo 0 || echo 1)" +check "first install queues nothing" "$([ ! -e "$D/first/var/lib/rigforge/upgrade-pending" ] && echo 0 || echo 1)" +check "first install creates the state dir" "$([ -d "$D/first/var/lib/rigforge" ] && echo 0 || echo 1)" +check "first install reloads systemd only" "$([ "$(cat "$CALLS")" = 'systemctl daemon-reload' ] && echo 0 || echo 1)" +check "first install prints the first-run step" "$(grep -q 'sudo rigforge setup' "$D/first/out" && echo 0 || echo 1)" postinst noconf 1.0.0 2.0.0 n -check "upgrade without a configured miner is inert" "$([ ! -e "$D/noconf/var/lib/rigforge/upgrade-pending" ] && [ ! -s "$CALLS" ] && echo 0 || echo 1)" +check "upgrade without a configured miner is inert" "$([ ! -e "$D/noconf/var/lib/rigforge/upgrade-pending" ] && [ "$(cat "$CALLS")" = 'systemctl daemon-reload' ] && ! grep -q 'sudo rigforge setup' "$D/noconf/out" && echo 0 || echo 1)" postinst same 2.0.0 2.0.0 y -check "same version is inert" "$([ ! -e "$D/same/var/lib/rigforge/upgrade-pending" ] && [ ! -s "$CALLS" ] && echo 0 || echo 1)" +check "same version is inert" "$([ ! -e "$D/same/var/lib/rigforge/upgrade-pending" ] && [ "$(cat "$CALLS")" = 'systemctl daemon-reload' ] && ! grep -q 'sudo rigforge setup' "$D/same/out" && echo 0 || echo 1)" postinst older 2.0.0 1.9.0 y -check "downgrade is inert" "$([ ! -e "$D/older/var/lib/rigforge/upgrade-pending" ] && [ ! -s "$CALLS" ] && echo 0 || echo 1)" +check "downgrade is inert" "$([ ! -e "$D/older/var/lib/rigforge/upgrade-pending" ] && [ "$(cat "$CALLS")" = 'systemctl daemon-reload' ] && ! grep -q 'sudo rigforge setup' "$D/older/out" && echo 0 || echo 1)" postinst queue 1.9.0 2.0.0 y check "upgrade writes the marker" "$([ "$(cat "$D/queue/var/lib/rigforge/upgrade-pending")" = 2.0.0 ] && echo 0 || echo 1)" @@ -71,6 +74,73 @@ rc=$? set -e check "systemctl failure never fails dpkg" "$([ "$rc" -eq 0 ] && grep -q 'sudo rigforge upgrade' "$D/sysfail/out" && echo 0 || echo 1)" +render() { + local r=$1 lib=$2 state=$3 t + mkdir -p "$r/etc/systemd/system" "$r/etc/logrotate.d" "$r/var/lib/rigforge-control" + for t in "$ROOT"/systemd/*.template; do + sed "s|\$SCRIPT_DIR|$lib|g; s|\$RIGFORGE_STATE|$state|g" "$t" | + RIGFORGE_LIB=$lib RIGFORGE_STATE=$state bash -c 'source "$1/util/config-apply-state.sh"; _unit_split_env' _ "$ROOT" >"$r/etc/systemd/system/$(basename "$t" .template)" + done + : >"$r/etc/systemd/system/rigforge-api-firewall.nft" + printf '%s/data/worker/xmrig.log {\n}\n' "$state" >"$r/etc/logrotate.d/xmrig" +} + +maint() { + local r="$D/$1" script=$2 arg=$3 + mkdir -p "$r/run/systemd/system" "$r/var/lib/rigforge" + echo '{}' >"$r/var/lib/rigforge/config.json" + render "$r" "${LIB:-/usr/lib/rigforge}" "${STATE:-/var/lib/rigforge}" + printf '[Service]\nExecStart=/usr/lib/rigforge/rigforge.sh other\n' >"$r/etc/systemd/system/rigforge-other.service" + : >"$CALLS" + RIGFORGE_PKG_ROOT="$r" PATH="$STUBS:$PATH" sh "$ROOT/packaging/debian/$script" "$arg" >"$r/out" 2>&1 +} +for arg in upgrade failed-upgrade abort-install abort-upgrade disappear remove purge deconfigure; do + for script in prerm postrm; do + set +e + maint "m-$script-$arg" "$script" "$arg" + rc=$? + set -e + check "$script $arg exits 0" "$rc" + done +done +maint prerm-remove prerm remove +for u in xmrig.service rigforge-watchdog.timer rigforge-autotune.timer rigforge-api-refresh.timer rigforge-control.service rigforge-control-apply.path rigforge-control-upgrade.path rigforge-api.service; do + check "prerm remove stops and disables $u" "$(grep -qx "systemctl disable --now $u" "$CALLS" && echo 0 || echo 1)" +done +for u in rigforge-upgrade.service rigforge-watchdog.service rigforge-autotune.service rigforge-api-refresh.service rigforge-control-apply.service rigforge-control-upgrade.service; do + check "prerm remove stops the static unit $u without disabling it" "$(grep -qx "systemctl stop $u" "$CALLS" && ! grep -q "disable --now $u" "$CALLS" && echo 0 || echo 1)" +done +LIB=/opt/rigforge STATE=/opt/rigforge maint prerm-foreign prerm remove +check "prerm remove leaves a non-package install's units alone" "$([ "$(cat "$CALLS")" = 'systemctl stop rigforge-upgrade.service' ] && echo 0 || echo 1)" +maint prerm-deconfigure prerm deconfigure +check "prerm deconfigure leaves the miner running" "$([ ! -s "$CALLS" ] && echo 0 || echo 1)" +maint prerm-upgrade prerm upgrade +check "prerm upgrade leaves the miner running" "$([ ! -s "$CALLS" ] && echo 0 || echo 1)" +maint postrm-remove postrm remove +check "remove keeps config and state" "$([ -f "$D/postrm-remove/var/lib/rigforge/config.json" ] && echo 0 || echo 1)" +maint postrm-purge postrm purge +check "purge removes config and state" "$([ ! -e "$D/postrm-purge/var/lib/rigforge" ] && echo 0 || echo 1)" +check "fixture miner unit carries the package state dir" "$(grep -qx 'Environment=RIGFORGE_STATE=/var/lib/rigforge' "$D/postrm-remove/etc/systemd/system/xmrig.service" && echo 0 || echo 1)" +left=$(cd "$D/postrm-purge/etc/systemd/system" && ls) +check "purge removes every rendered unit, timer and path and the firewall rules (left: $left)" "$([ "$left" = rigforge-other.service ] && echo 0 || echo 1)" +check "purge removes the logrotate rule and the control state" "$([ ! -e "$D/postrm-purge/etc/logrotate.d/xmrig" ] && [ ! -e "$D/postrm-purge/var/lib/rigforge-control" ] && echo 0 || echo 1)" +LIB=/opt/rigforge STATE=/opt/rigforge maint postrm-foreign postrm purge +r="$D/postrm-minerfw" +maint postrm-minerfw postrm remove +rm -f "$r/etc/systemd/system/rigforge-api.service" "$r/etc/systemd/system/rigforge-control.service" +RIGFORGE_PKG_ROOT="$r" PATH="$STUBS:$PATH" sh "$ROOT/packaging/debian/postrm" purge >/dev/null 2>&1 +check "purge removes the firewall rules the package miner unit loads" "$([ ! -e "$r/etc/systemd/system/rigforge-api-firewall.nft" ] && echo 0 || echo 1)" +foreign=$(cd "$D/postrm-foreign/etc/systemd/system" && ls | wc -l | tr -d ' ') +check "purge keeps every unit and the control state of a non-package install" "$([ "$foreign" -eq "$(($(ls "$ROOT"/systemd/*.template | wc -l) + 2))" ] && [ -d "$D/postrm-foreign/var/lib/rigforge-control" ] && [ -f "$D/postrm-foreign/etc/logrotate.d/xmrig" ] && echo 0 || echo 1)" +check "purge keeps a unit it did not render" "$([ -f "$D/postrm-purge/etc/systemd/system/rigforge-other.service" ] && echo 0 || echo 1)" +check "remove keeps the rendered units" "$([ -f "$D/postrm-remove/etc/systemd/system/xmrig.service" ] && echo 0 || echo 1)" +set +e +SYSTEMCTL_FAIL=1 maint prerm-fail prerm remove +rc=$? +set -e +check "prerm remove fails when a unit cannot be stopped" "$([ "$rc" -ne 0 ] && echo 0 || echo 1)" +check "purge never touches tuning" "$(! grep -rqE 'grub|sysctl|hugepages|wrmsr|modprobe' "$ROOT/packaging/debian/postrm" "$ROOT/packaging/debian/prerm" && echo 0 || echo 1)" + STATE="$D/state" export RIGFORGE_PKG_STATE="$STATE" pkg_run() { diff --git a/tests/test-workflow-contract.sh b/tests/test-workflow-contract.sh index b41f78dc..c7e1e6e7 100755 --- a/tests/test-workflow-contract.sh +++ b/tests/test-workflow-contract.sh @@ -133,7 +133,7 @@ for wf in sys.argv[1:]: allowed_ifs = ("runner.os == 'Linux'",) elif sidv == "assert_categorize": allowed_ifs = ("always()",) - elif job == "e2e" and sidv in ("assert_deploy", "assert_appliance", "assert_isolation", "setup_allowlist_image"): + elif job == "e2e" and sidv in ("assert_deploy", "assert_appliance", "assert_isolation", "assert_deb_install", "setup_allowlist_image"): allowed_ifs = ("${{ !cancelled() }}",) elif job == "e2e" and sidv == "assert_allowlist": allowed_ifs = ("${{ !cancelled() && steps.setup_allowlist_image.outcome == 'success' }}",) @@ -236,7 +236,8 @@ for want in \ 'run: bash tests/e2e/linux.sh deploy' \ 'run: bash tests/e2e/linux.sh appliance' \ 'run: bash tests/e2e/linux.sh isolation' \ - 'run: bash scripts/require-success.sh "deploy=$DEPLOY" "appliance=$APPLIANCE" "isolation=$ISOLATION" "allowlist=$ALLOWLIST"' \ + 'run: bash tests/test-deb-install.sh' \ + 'run: bash scripts/require-success.sh "deploy=$DEPLOY" "appliance=$APPLIANCE" "isolation=$ISOLATION" "allowlist=$ALLOWLIST" "deb-install=$DEB_INSTALL"' \ 'run: make coverage' \ 'run: sudo bash tests/test-apt-feed.sh' \ 'run: timeout 1800 bash tests/test-stock-home.sh "$TEST_REVISION" "$TEST_EXPECTATION"' \ diff --git a/util/config-apply-state.sh b/util/config-apply-state.sh index e4f76b21..a7e6b0df 100644 --- a/util/config-apply-state.sh +++ b/util/config-apply-state.sh @@ -73,3 +73,10 @@ _unit_split_env() { [ -z "${RIGFORGE_LIB:-}" ] || e="${e:+$e$'\n'}Environment=RIGFORGE_LIB=$RIGFORGE_LIB" UNIT_ENV="$e" awk '{ print } /^\[Service\]$/ && ENVIRON["UNIT_ENV"] != "" { print ENVIRON["UNIT_ENV"] }' } + +_config_replace() { + local tmp="$CONFIG_JSON.new.$$" + (umask 077 && cp "$1" "$tmp") && chmod 600 "$tmp" && mv -f "$tmp" "$CONFIG_JSON" && return 0 + rm -f "$tmp" + return 1 +}