#!/bin/sh # SPDX-License-Identifier: CPAL-1.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"