|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +root="${CATALOG:?CATALOG must be set}" |
| 5 | +net="$root/net" |
| 6 | +export PTY_ROOT="$net/pty" |
| 7 | +export XDG_STATE_HOME="$root/state" |
| 8 | + |
| 9 | +live_id="migrate.live.agent" |
| 10 | +absent_id="migrate.absent.agent" |
| 11 | +control_id="migrate.control.agent" |
| 12 | +live_spec="$net/agents/migrate/live/agent.kdl" |
| 13 | + |
| 14 | +pty_at() { |
| 15 | + env -u PTY_SESSION PTY_ROOT="$PTY_ROOT" pty "$@" |
| 16 | +} |
| 17 | + |
| 18 | +session() { |
| 19 | + pty_at list --json | jq -c --arg id "$1" '.[] | select(.name == $id)' |
| 20 | +} |
| 21 | + |
| 22 | +status_of() { |
| 23 | + session "$1" | jq -r '.status // empty' |
| 24 | +} |
| 25 | + |
| 26 | +pid_of() { |
| 27 | + session "$1" | jq -r '.pid // empty' |
| 28 | +} |
| 29 | + |
| 30 | +run_count() { |
| 31 | + file="$net/$1-count" |
| 32 | + test -f "$file" || { |
| 33 | + printf '0\n' |
| 34 | + return |
| 35 | + } |
| 36 | + cat "$file" |
| 37 | +} |
| 38 | + |
| 39 | +wait_status() { |
| 40 | + id="$1" |
| 41 | + expected="$2" |
| 42 | + for _ in $(seq 1 100); do |
| 43 | + status="$(status_of "$id")" |
| 44 | + test "$status" = "$expected" && return 0 |
| 45 | + sleep 0.05 |
| 46 | + done |
| 47 | + printf '%s did not reach status %s (last status: %s)\n' \ |
| 48 | + "$id" "$expected" "${status:-missing}" >&2 |
| 49 | + return 1 |
| 50 | +} |
| 51 | + |
| 52 | +wait_count() { |
| 53 | + label="$1" |
| 54 | + expected="$2" |
| 55 | + for _ in $(seq 1 100); do |
| 56 | + test "$(run_count "$label")" -eq "$expected" && return 0 |
| 57 | + sleep 0.05 |
| 58 | + done |
| 59 | + printf '%s did not reach generation %s\n' "$label" "$expected" >&2 |
| 60 | + return 1 |
| 61 | +} |
| 62 | + |
| 63 | +retire() { |
| 64 | + spec="$1" |
| 65 | + grep -Fq 'retired #true' "$spec" || |
| 66 | + sed -i '/role "worker"/a\\ retired #true' "$spec" |
| 67 | +} |
| 68 | + |
| 69 | +cleanup() { |
| 70 | + for identity in live absent control; do |
| 71 | + retire "$net/agents/migrate/$identity/agent.kdl" 2>/dev/null || true |
| 72 | + done |
| 73 | + st2 up --once --catalog "$net" --host migrate >/dev/null 2>&1 || true |
| 74 | + sleep 0.1 |
| 75 | + st2 up --once --catalog "$net" --host migrate >/dev/null 2>&1 || true |
| 76 | + for id in "$live_id" "$absent_id" "$control_id"; do |
| 77 | + pty_at kill "$id" >/dev/null 2>&1 || true |
| 78 | + pty_at rm "$id" >/dev/null 2>&1 || true |
| 79 | + done |
| 80 | +} |
| 81 | +trap cleanup EXIT |
| 82 | + |
| 83 | +# Seed exactly one pre-existing process generation before st2 sees the |
| 84 | +# declaration. The declared launch command is therefore observable but must not |
| 85 | +# execute during adoption. |
| 86 | +mkdir -p "$PTY_ROOT" |
| 87 | +CATALOG="$net" pty_at run -d --id "$live_id" --tag keep=true \ |
| 88 | + --cwd "$net/workspace" -- bash "$net/task.sh" live >/dev/null |
| 89 | +wait_status "$live_id" running |
| 90 | +wait_count live 1 |
| 91 | +live_pid_before="$(pid_of "$live_id")" |
| 92 | +test -n "$live_pid_before" |
| 93 | + |
| 94 | +st2 up --once --catalog "$net" --host migrate >"$root/first-pass.out" |
| 95 | + |
| 96 | +if |
| 97 | + test "$(status_of "$live_id")" = running && |
| 98 | + test "$(pid_of "$live_id")" = "$live_pid_before" && |
| 99 | + test "$(run_count live)" -eq 1 && |
| 100 | + grep -Fq 'adopted (1): live' "$root/first-pass.out" |
| 101 | +then |
| 102 | + echo "LIVE-ADOPTED-UNCHANGED-GREEN-a091" |
| 103 | +fi |
| 104 | + |
| 105 | +if |
| 106 | + test "$(run_count absent)" -eq 0 && |
| 107 | + test -z "$(session "$absent_id")" && |
| 108 | + grep -Fq 'held (1): migrate.absent.agent' "$root/first-pass.out" |
| 109 | +then |
| 110 | + echo "ABSENT-ADOPT-ONLY-HELD-GREEN-a091" |
| 111 | +fi |
| 112 | + |
| 113 | +if |
| 114 | + wait_count control 1 && |
| 115 | + test "$(status_of "$control_id")" = running && |
| 116 | + grep -Fq "$control_id" "$root/first-pass.out" |
| 117 | +then |
| 118 | + echo "ORDINARY-MISSING-LAUNCH-CONTROL-GREEN-a091" |
| 119 | +fi |
| 120 | + |
| 121 | +# The adopted process exits. Adopt-only must retain the dead record and refuse |
| 122 | +# both collection and replacement. |
| 123 | +pty_at kill "$live_id" >/dev/null |
| 124 | +wait_status "$live_id" exited |
| 125 | +live_record_before="$(session "$live_id")" |
| 126 | +test "$(run_count live)" -eq 1 |
| 127 | + |
| 128 | +st2 up --once --catalog "$net" --host migrate >"$root/held-pass.out" |
| 129 | + |
| 130 | +if |
| 131 | + test "$(run_count live)" -eq 1 && |
| 132 | + test "$(status_of "$live_id")" = exited && |
| 133 | + test "$(session "$live_id")" = "$live_record_before" && |
| 134 | + grep -Fq 'held (2): migrate.absent.agent, migrate.live.agent' "$root/held-pass.out" |
| 135 | +then |
| 136 | + echo "EXITED-ADOPTED-GENERATION-HELD-GREEN-a091" |
| 137 | +fi |
| 138 | + |
| 139 | +# This is the explicit authorization edge. The declaration becomes an ordinary |
| 140 | +# service task; only now may st2 reap the exited record and cold-launch. |
| 141 | +sed -i 's/lifecycle "adopt-only"/lifecycle "service"/' "$live_spec" |
| 142 | +st2 up --once --catalog "$net" --host migrate >"$root/replacement-pass.out" |
| 143 | + |
| 144 | +if |
| 145 | + wait_count live 2 && |
| 146 | + test "$(status_of "$live_id")" = running && |
| 147 | + test "$(pid_of "$live_id")" != "$live_pid_before" && |
| 148 | + grep -Fq "$live_id" "$root/replacement-pass.out" && |
| 149 | + test "$(run_count absent)" -eq 0 |
| 150 | +then |
| 151 | + echo "EXPLICIT-ORDINARY-REPLACEMENT-GREEN-a091" |
| 152 | +fi |
| 153 | + |
| 154 | +cleanup |
| 155 | +trap - EXIT |
| 156 | +test "$(pty_at list --json | jq 'length')" -eq 0 |
| 157 | +echo "SYNTHETIC-MIGRATION-ROOT-CLEAN-GREEN-a091" |
0 commit comments