[linux] Installer (one copied line) and the container test of the Linux target with real systemd
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
//! TEST ONLY: drives the Linux backend with real systemd for tests/linux (the container test).
|
||||||
|
//! It skips the signature check on purpose, so it is an example, never part of a release.
|
||||||
|
//!
|
||||||
|
//! deploy <root> <app> <version> <unit> <health-url|-> <archive.tar.gz>
|
||||||
|
|
||||||
|
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use legatus_linux::{HttpHealth, Layout, LinuxBackend, Outcome, Plan, Systemctl};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||||
|
let [root, app, version, unit, health, archive] = a.as_slice() else {
|
||||||
|
eprintln!("usage: deploy <root> <app> <version> <unit> <health-url|-> <archive.tar.gz>");
|
||||||
|
std::process::exit(2);
|
||||||
|
};
|
||||||
|
let bytes = std::fs::read(archive).expect("the archive");
|
||||||
|
let sha = legatus_protocol::sha256_hex(&bytes);
|
||||||
|
let backend = LinuxBackend::new(Layout::new(root), Systemctl, HttpHealth);
|
||||||
|
let plan = Plan {
|
||||||
|
app,
|
||||||
|
version,
|
||||||
|
unit,
|
||||||
|
health_url: (health != "-").then_some(health.as_str()),
|
||||||
|
health_timeout: Duration::from_secs(15),
|
||||||
|
keep: 3,
|
||||||
|
};
|
||||||
|
match backend.deploy(&plan, &bytes[..], &sha) {
|
||||||
|
Ok(Outcome::Succeeded { previous }) => {
|
||||||
|
println!(
|
||||||
|
"succeeded previous={}",
|
||||||
|
previous.unwrap_or_else(|| "-".into())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Ok(Outcome::RolledBack {
|
||||||
|
to,
|
||||||
|
healthy,
|
||||||
|
reason,
|
||||||
|
}) => {
|
||||||
|
println!("rolled_back to={to} healthy={healthy} reason={reason}")
|
||||||
|
}
|
||||||
|
Ok(Outcome::Failed { reason }) => println!("failed reason={reason}"),
|
||||||
|
Err(e) => println!("error {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
# Legatus installer, served by the Fabrica server at /legatus/install.sh:
|
||||||
|
# curl -fsSL https://<fabrica>/legatus/install.sh | sudo sh -s -- lgt1-<code>
|
||||||
|
# Downloads the Legatus binary for this machine from the same server, checks its SHA-256 against
|
||||||
|
# the SHA256SUMS beside it (and the minisign signature when minisign is installed), installs it,
|
||||||
|
# enrolls with the one-time code, and starts legatus.service.
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
CODE=${1:-}
|
||||||
|
BASE=${LEGATUS_BASE:-} # Fabrica sets this in the script it serves, e.g. https://fabrica.example
|
||||||
|
KEY=${LEGATUS_RELEASE_KEY:-} # and the minisign key Legatus releases are signed with
|
||||||
|
|
||||||
|
die() { echo "legatus install: $*" >&2; exit 1; }
|
||||||
|
[ -n "$CODE" ] || die "usage: install.sh lgt1-<code>"
|
||||||
|
case "$CODE" in lgt1-*) ;; *) die "that is not an enrollment code" ;; esac
|
||||||
|
[ -n "$BASE" ] || die "this script must be served by Fabrica (LEGATUS_BASE is not set)"
|
||||||
|
[ "$(id -u)" = 0 ] || die "run it with sudo"
|
||||||
|
command -v systemctl >/dev/null || die "systemd is needed"
|
||||||
|
command -v sha256sum >/dev/null || die "sha256sum is needed"
|
||||||
|
|
||||||
|
case "$(uname -m)" in
|
||||||
|
x86_64|amd64) ARCH=x86_64 ;;
|
||||||
|
aarch64|arm64) ARCH=aarch64 ;;
|
||||||
|
*) die "this CPU ($(uname -m)) is not supported yet" ;;
|
||||||
|
esac
|
||||||
|
FILE="legatus-linux-$ARCH"
|
||||||
|
|
||||||
|
TMP=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$TMP"' EXIT
|
||||||
|
curl -fsSL --proto '=https' --tlsv1.2 -o "$TMP/$FILE" "$BASE/legatus/download/$FILE"
|
||||||
|
curl -fsSL --proto '=https' --tlsv1.2 -o "$TMP/SHA256SUMS" "$BASE/legatus/download/SHA256SUMS"
|
||||||
|
if [ -n "$KEY" ] && command -v minisign >/dev/null; then
|
||||||
|
curl -fsSL --proto '=https' --tlsv1.2 -o "$TMP/SHA256SUMS.minisig" "$BASE/legatus/download/SHA256SUMS.minisig"
|
||||||
|
minisign -Vm "$TMP/SHA256SUMS" -P "$KEY" >/dev/null || die "SHA256SUMS is not signed by the Legatus release key"
|
||||||
|
fi
|
||||||
|
WANT=$(awk -v f="$FILE" '$2 == f || $2 == "*"f { print $1 }' "$TMP/SHA256SUMS")
|
||||||
|
[ -n "$WANT" ] || die "SHA256SUMS does not list $FILE"
|
||||||
|
GOT=$(sha256sum "$TMP/$FILE" | cut -d' ' -f1)
|
||||||
|
[ "$WANT" = "$GOT" ] || die "the download does not match SHA256SUMS"
|
||||||
|
|
||||||
|
install -m 755 "$TMP/$FILE" /usr/local/bin/legatus
|
||||||
|
install -d -m 700 /var/lib/legatus
|
||||||
|
/usr/local/bin/legatus enroll "$CODE"
|
||||||
|
|
||||||
|
cat > /etc/systemd/system/legatus.service <<'EOF'
|
||||||
|
[Unit]
|
||||||
|
Description=Legatus, the Fabrica Domi agent
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/local/bin/legatus run
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5
|
||||||
|
StateDirectory=legatus
|
||||||
|
StateDirectoryMode=0700
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now legatus.service
|
||||||
|
echo "Legatus is running. It shows up in Fabrica under Targets."
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# The Linux target's integration test: Debian with real systemd as PID 1.
|
||||||
|
FROM docker.io/library/debian:trixie-slim
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends systemd systemd-sysv python3 curl ca-certificates \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& systemctl mask systemd-logind.service getty.target console-getty.service
|
||||||
|
STOPSIGNAL SIGRTMIN+3
|
||||||
|
CMD ["/sbin/init"]
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
# The Linux target against real systemd, in a container (Docker or Podman).
|
||||||
|
# tests/linux/run.sh # from the repo root
|
||||||
|
# Builds the test-only `deploy` example in a Rust container, starts Debian with systemd as PID 1,
|
||||||
|
# and runs scenario.sh in it. On Mark's PC the container engine keeps its data on D:.
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
ENGINE=${ENGINE:-$(command -v podman || command -v docker || true)}
|
||||||
|
[ -n "$ENGINE" ] || { echo "run.sh: needs podman or docker" >&2; exit 2; }
|
||||||
|
REPO=$(cd "$(dirname "$0")/../.." && pwd)
|
||||||
|
NAME=legatus-linux-it
|
||||||
|
RUST_IMAGE=${RUST_IMAGE:-docker.io/library/rust:1.89-slim-trixie}
|
||||||
|
|
||||||
|
cleanup() { "$ENGINE" rm -f "$NAME" >/dev/null 2>&1 || true; }
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
echo "== building the test driver"
|
||||||
|
"$ENGINE" volume create legatus-it-cargo >/dev/null
|
||||||
|
"$ENGINE" run --rm -v "$REPO:/src:ro" -v legatus-it-cargo:/cargo -e CARGO_HOME=/cargo/home \
|
||||||
|
-e CARGO_TARGET_DIR=/cargo/target -w /src "$RUST_IMAGE" \
|
||||||
|
sh -c 'cargo build --locked --example deploy -p legatus-linux && cp /cargo/target/debug/examples/deploy /cargo/legatus-deploy-test'
|
||||||
|
|
||||||
|
echo "== starting systemd"
|
||||||
|
"$ENGINE" build -q -t legatus-systemd "$REPO/tests/linux" >/dev/null
|
||||||
|
cleanup
|
||||||
|
"$ENGINE" run -d --name "$NAME" --privileged --cgroupns=host \
|
||||||
|
--tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:rw \
|
||||||
|
-v legatus-it-cargo:/cargo:ro legatus-systemd >/dev/null
|
||||||
|
"$ENGINE" exec "$NAME" install -m 755 /cargo/legatus-deploy-test /usr/local/bin/legatus-deploy-test
|
||||||
|
"$ENGINE" cp "$REPO/tests/linux/scenario.sh" "$NAME:/scenario.sh"
|
||||||
|
|
||||||
|
echo "== scenario"
|
||||||
|
"$ENGINE" exec "$NAME" sh /scenario.sh
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
# Runs inside the systemd container: four releases of a small web app through the Linux target.
|
||||||
|
# 1.0.0 healthy, 2.0.0 healthy, 3.0.0 crashes at start, 4.0.0 runs but its health URL fails.
|
||||||
|
# Expected: 1 and 2 succeed; 3 and 4 are rolled back to 2.0.0, which keeps running.
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
ROOT=/var/lib/legatus/apps
|
||||||
|
DEPLOY=/usr/local/bin/legatus-deploy-test
|
||||||
|
HEALTH=http://127.0.0.1:8080/health
|
||||||
|
fail() { echo "FAIL: $*"; exit 1; }
|
||||||
|
|
||||||
|
# Wait for systemd to be up.
|
||||||
|
for _ in $(seq 1 60); do
|
||||||
|
s=$(systemctl is-system-running 2>/dev/null || true)
|
||||||
|
[ "$s" = running ] || [ "$s" = degraded ] && break
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
cat > /etc/systemd/system/demo.service <<'EOF'
|
||||||
|
[Unit]
|
||||||
|
Description=Legatus demo app
|
||||||
|
[Service]
|
||||||
|
ExecStart=/var/lib/legatus/apps/demo/current/run.sh
|
||||||
|
Restart=no
|
||||||
|
EOF
|
||||||
|
systemctl daemon-reload
|
||||||
|
|
||||||
|
work=$(mktemp -d)
|
||||||
|
release() { # version kind
|
||||||
|
d="$work/$1"; mkdir -p "$d/www"
|
||||||
|
case "$2" in
|
||||||
|
good) echo "ok $1" > "$d/www/health"
|
||||||
|
printf '#!/bin/sh\nexec python3 -m http.server 8080 --bind 127.0.0.1 --directory "$(dirname "$(readlink -f "$0")")/www"\n' > "$d/run.sh" ;;
|
||||||
|
crash) printf '#!/bin/sh\necho "cannot start" >&2\nexit 1\n' > "$d/run.sh" ;;
|
||||||
|
unhealthy) printf '#!/bin/sh\nexec python3 -m http.server 8080 --bind 127.0.0.1 --directory "$(dirname "$(readlink -f "$0")")/www"\n' > "$d/run.sh" ;;
|
||||||
|
esac
|
||||||
|
chmod 755 "$d/run.sh"
|
||||||
|
tar -C "$d" -czf "$work/$1.tar.gz" .
|
||||||
|
}
|
||||||
|
release 1.0.0 good; release 2.0.0 good; release 3.0.0 crash; release 4.0.0 unhealthy
|
||||||
|
|
||||||
|
deploy() { "$DEPLOY" "$ROOT" demo "$1" demo.service "$HEALTH" "$work/$1.tar.gz"; }
|
||||||
|
current() { readlink "$ROOT/demo/current"; }
|
||||||
|
|
||||||
|
out=$(deploy 1.0.0); echo "1.0.0: $out"
|
||||||
|
case "$out" in "succeeded previous=-") ;; *) fail "1.0.0: $out";; esac
|
||||||
|
[ "$(current)" = releases/1.0.0 ] || fail "current is $(current)"
|
||||||
|
[ "$(curl -fsS $HEALTH)" = "ok 1.0.0" ] || fail "1.0.0 does not answer"
|
||||||
|
|
||||||
|
out=$(deploy 2.0.0); echo "2.0.0: $out"
|
||||||
|
case "$out" in "succeeded previous=1.0.0") ;; *) fail "2.0.0: $out";; esac
|
||||||
|
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer"
|
||||||
|
|
||||||
|
out=$(deploy 3.0.0); echo "3.0.0: $out"
|
||||||
|
case "$out" in "rolled_back to=2.0.0 healthy=true"*) ;; *) fail "3.0.0: $out";; esac
|
||||||
|
[ "$(current)" = releases/2.0.0 ] || fail "after 3.0.0 current is $(current)"
|
||||||
|
systemctl is-active --quiet demo.service || fail "demo is not active after the rollback"
|
||||||
|
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer after the rollback"
|
||||||
|
|
||||||
|
out=$(deploy 4.0.0); echo "4.0.0: $out"
|
||||||
|
case "$out" in "rolled_back to=2.0.0 healthy=true"*) ;; *) fail "4.0.0: $out";; esac
|
||||||
|
[ "$(curl -fsS $HEALTH)" = "ok 2.0.0" ] || fail "2.0.0 does not answer after the second rollback"
|
||||||
|
|
||||||
|
echo "PASS: atomic switch, systemd restart, health check and auto rollback with real systemd"
|
||||||
Reference in New Issue
Block a user