| 1 | #!/usr/bin/env bash |
| 2 | # Thin v1 production upgrade for equipo: from the staged version production |
| 3 | # serves to the next staged version, on the same database, runners first |
| 4 | # (the staging release order, scripts/staging-thin.py release). |
| 5 | # |
| 6 | # scripts/upgrade-thin.sh <version> # print the plan only (default) |
| 7 | # scripts/upgrade-thin.sh <version> --execute # run it, step by step |
| 8 | # scripts/upgrade-thin.sh rollback [--execute] # restore the recorded prior state |
| 9 | # |
| 10 | # <version> is a `make release` version staging passed on exactly these |
| 11 | # bytes (`make staged-check`), read from $EQUIPO_RELEASE_REPO/releases |
| 12 | # (default: this checkout). It changes production. An agent may run |
| 13 | # --execute when the version's staging run is green with no waiver |
| 14 | # (Paul, 2026-11-20); a waived version needs Paul's approval of the waiver, |
| 15 | # then an agent runs it too (Paul, 2026-11-22). Each step asks first and stops on the first failure. The run |
| 16 | # is resumable: steps done are recorded in $BACKUPS/upgrade.state and |
| 17 | # skipped. When the next upgrade starts, a finished or rolled-back one's |
| 18 | # state is kept as upgrade-<version>-<stamp>.state. |
| 19 | # |
| 20 | # Upgrade: |
| 21 | # 1 preflight staged-check, artifacts, CARLOS sign-in; the cutover (or the |
| 22 | # last upgrade) finished and production serves its version, |
| 23 | # which <version> descends from; the runner unit and roots are |
| 24 | # production's, not staging's; the replica reads clean (PRAGMA |
| 25 | # integrity_check) and nothing is in flight; records the prior |
| 26 | # state (served version, runner service.env and unit) |
| 27 | # 2 backup carlos db pull, integrity-checked, counted, hashed, read-only. |
| 28 | # The upgrade changes no configuration: the same database. |
| 29 | # 3 runner the staged runner, tool proxy, gitleaks (the preview |
| 30 | # scanner) and page renderer (install-renderer) beside the |
| 31 | # current one, checked by verify-installed, which renders a |
| 32 | # fixture page; service.env and unit written from the |
| 33 | # preflight's record (scripts/runner-config.py); nothing in |
| 34 | # flight on a reading just before the restart (a short window, |
| 35 | # not a drain: Equipo has no maintenance mode); the restart |
| 36 | # keeps the runner's state and enrolment |
| 37 | # 4 instance the staged instance with --swap hold, until /api/version |
| 38 | # reports it. Its migrations run when it starts. |
| 39 | # 5 verify the version; in this boot and unit invocation, the staged |
| 40 | # runner's process welcomed by the instance and not dropped |
| 41 | # since, on two readings |
| 42 | # |
| 43 | # Rollback restores the previous runner (service.env and unit) and promotes |
| 44 | # the previously served version. It does not roll back the database: once |
| 45 | # step 4 has started, the new version's migrations may have run, and the |
| 46 | # backup from step 2 is the only copy from before them. |
| 47 | set -euo pipefail |
| 48 | |
| 49 | APP=equipo |
| 50 | ACCOUNT=oficina |
| 51 | CONSOLE=https://console.carloku.com |
| 52 | HOST=equipo.bds.oncarlos.com |
| 53 | ORIGIN=https://$HOST |
| 54 | PUBLIC_ORIGIN=${EQUIPO_PUBLIC_ORIGIN:-https://equipo.oficina.works} |
| 55 | UNIT=equipo-runner.service |
| 56 | RUNNER_ROOT=$HOME/.local/share/equipo-runner |
| 57 | STAGING_ROOT=$HOME/.local/share/equipo-runner-staging |
| 58 | BACKUPS=$HOME/.local/share/equipo-cutover |
| 59 | CUTOVER_STATE=$BACKUPS/cutover.state |
| 60 | STATE=$BACKUPS/upgrade.state |
| 61 | REPO=$(cd "$(dirname "$0")/.." && pwd) |
| 62 | RELEASE_REPO=${EQUIPO_RELEASE_REPO:-$REPO} |
| 63 | RELEASES=$RELEASE_REPO/releases |
| 64 | PROC=${UPGRADE_PROC:-/proc} |
| 65 | carlos_app=(--app "$APP" --account "$ACCOUNT" --console "$CONSOLE") |
| 66 | # Open tasks holding the work of an Engineering step that ran out of time |
| 67 | # (done, step_timeout; protocol 32): a release before it takes that work |
| 68 | # for finished, so a rollback refuses while any is open. |
| 69 | UNFINISHED="SELECT count(DISTINCT k.id) FROM chat_tasks k JOIN chat_steps s ON s.task_id=k.id WHERE s.specialist='engineering' AND s.state='done' AND s.reason='step_timeout' AND k.stage NOT IN ('done','cancelled')" |
| 70 | UNFINISHED_REFUSAL="open tasks hold unfinished work a release before protocol 32 takes for finished: finish or cancel them, or set UPGRADE_ALLOW_UNFINISHED=1 to roll back anyway" |
| 71 | IN_FLIGHT="SELECT (SELECT count(*) FROM chat_steps WHERE state IN ('queued','running'))||'/'||(SELECT count(*) FROM chat_steps WHERE specialist='merge' AND state IN ('queued','running'))||'/'||(SELECT count(*) FROM chat_turns WHERE dispatched=1 AND state NOT IN ('replied','failed','cancelled'))" |
| 72 | |
| 73 | mode=upgrade |
| 74 | version=${1:-} |
| 75 | [[ $version == rollback ]] && mode=rollback |
| 76 | execute=0 |
| 77 | [[ ${2:-} == --execute ]] && execute=1 |
| 78 | if [[ $mode == upgrade && ! $version =~ ^[0-9a-f]{7,12}$ ]]; then |
| 79 | sed -n '2,44p' "$0" |
| 80 | exit 2 |
| 81 | fi |
| 82 | |
| 83 | die() { echo "refused: $*" >&2; exit 1; } |
| 84 | # A refusal read from the records stops --execute; the plan notes it and goes on. |
| 85 | refuse() { |
| 86 | if ((execute)); then die "$*"; fi |
| 87 | echo " ! would refuse: $*" |
| 88 | } |
| 89 | step() { |
| 90 | printf '\n== %s\n' "$1" |
| 91 | if ((execute)) && [[ ${UPGRADE_YES:-} != 1 ]]; then |
| 92 | read -r -p "Run this step? Type yes: " answer |
| 93 | [[ $answer == yes ]] || { echo "stopped before: $1"; exit 1; } |
| 94 | fi |
| 95 | } |
| 96 | run() { |
| 97 | printf ' $ %s\n' "$*" |
| 98 | if ((execute)); then "$@"; fi |
| 99 | } |
| 100 | # Recorded facts and finished steps, one KEY=value per line. |
| 101 | get_in() { [[ -f $1 ]] && sed -n "s/^$2=//p" "$1" | tail -1 || true; } |
| 102 | get() { get_in "$STATE" "$1"; } |
| 103 | put() { |
| 104 | ((execute)) || return 0 |
| 105 | printf '%s=%s\n' "$1" "$2" >>"$STATE" || die "could not record $1=$2 in $STATE" |
| 106 | } |
| 107 | # A finished or rolled-back upgrade's record is history: its steps are not this run's. |
| 108 | history=0 |
| 109 | done_step() { ((!history)) && [[ $(get "done_$1") == 1 ]]; } |
| 110 | finish() { put "done_$1" 1; } |
| 111 | file_value() { sed -n "s/^$1=\"\{0,1\}\([^\"]*\)\"\{0,1\}$/\1/p" "$2"; } |
| 112 | overlaps() { case "$1/" in "$2"/*) return 0 ;; esac; case "$2/" in "$1"/*) return 0 ;; esac; return 1; } |
| 113 | wait_serving() { run bash -c "for i in \$(seq 60); do [ \"\$(curl -fsS $ORIGIN/api/version)\" = $1 ] && exit 0; sleep 2; done; exit 1"; } |
| 114 | # Steps queued or running, merges among them, and dispatched turns not yet |
| 115 | # ended: a runner restart fails a running step or turn, and a merge needs |
| 116 | # the runner it started with. One reading of the replica: a short window, |
| 117 | # not a drain. |
| 118 | idle() { |
| 119 | local out |
| 120 | out=$(carlos db query "${carlos_app[@]}" --sql "$IN_FLIGHT" | tail -1) || die "could not read the work in flight. $REPLICA" |
| 121 | [[ $out =~ ^[0-9]+/[0-9]+/[0-9]+$ ]] || die "could not read the work in flight. $REPLICA" |
| 122 | [[ $out == 0/0/0 ]] || die "in flight (steps/merges/turns): $out; run again when nothing runs" |
| 123 | } |
| 124 | # No open task holds unfinished work (UNFINISHED), on one reading of the |
| 125 | # replica, unless the operator said to go on. |
| 126 | unfinished_none() { |
| 127 | local n |
| 128 | [[ ${UPGRADE_ALLOW_UNFINISHED:-} == 1 ]] && return 0 |
| 129 | n=$(carlos db query "${carlos_app[@]}" --sql "$UNFINISHED" | tail -1) || die "could not read the open tasks. $REPLICA" |
| 130 | [[ $n =~ ^[0-9]+$ ]] || die "could not read the open tasks. $REPLICA" |
| 131 | echo " open tasks with unfinished work: $n" |
| 132 | [[ $n == 0 ]] |
| 133 | } |
| 134 | # The runner scans every preview with the gitleaks beside its own |
| 135 | # executable, held to the hash linked into the runner; without it every |
| 136 | # preview fails (preview_scanner). $1 is a runner release directory: its |
| 137 | # gitleaks runs, and its sha256 is the one linked into its equipo-runner. |
| 138 | scanner_linked() { |
| 139 | local sum |
| 140 | [[ -f $1/gitleaks && ! -L $1/gitleaks && -x $1/gitleaks ]] || return 1 |
| 141 | sum=$(sha256sum "$1/gitleaks" | cut -d ' ' -f 1) || return 1 |
| 142 | grep -qaF "$sum" "$1/equipo-runner" |
| 143 | } |
| 144 | scanner() { |
| 145 | echo " $1/gitleaks: executable, and its sha256 is linked into $1/equipo-runner" |
| 146 | if ((execute)) && ! scanner_linked "$1"; then |
| 147 | die "$1/gitleaks is missing, cannot run, or is not the scanner equipo-runner was built with" |
| 148 | fi |
| 149 | } |
| 150 | # The runner renders pages with the renderer tree beside its own |
| 151 | # executable, held to the pin linked into the runner (the sha256 of the |
| 152 | # tree's MANIFEST). $1 is a runner release directory: the MANIFEST of its |
| 153 | # renderer.tar.gz has the sha256 linked into its equipo-runner. |
| 154 | renderer_linked() { |
| 155 | local sum |
| 156 | [[ -f $1/renderer.tar.gz && ! -L $1/renderer.tar.gz ]] || return 1 |
| 157 | sum=$(tar -xzOf "$1/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || return 1 |
| 158 | grep -qaF "$sum" "$1/equipo-runner" |
| 159 | } |
| 160 | renderer() { |
| 161 | echo " $1/renderer.tar.gz: its MANIFEST's sha256 is linked into $1/equipo-runner" |
| 162 | if ((execute)) && ! renderer_linked "$1"; then |
| 163 | die "$1/renderer.tar.gz is missing, or is not the renderer equipo-runner was built with" |
| 164 | fi |
| 165 | } |
| 166 | # The unit's main process runs the binary with sha256 $1. |
| 167 | runs() { |
| 168 | local pid |
| 169 | pid=$(systemctl --user show -p MainPID --value "$UNIT") |
| 170 | [[ $pid =~ ^[1-9][0-9]*$ && $(sha256sum "$PROC/$pid/exe" 2>/dev/null | cut -d ' ' -f 1) == "$1" ]] |
| 171 | } |
| 172 | # Connected now: in this boot and the unit's current invocation, the |
| 173 | # process running the binary with sha256 $1 logged the instance's welcome, |
| 174 | # and no stream drop after it, on two readings in a row of the same |
| 175 | # invocation and process. $2 names that binary. |
| 176 | connected() { |
| 177 | local seen= inv pid last |
| 178 | for _ in $(seq "${UPGRADE_TRIES:-30}"); do |
| 179 | inv=$(systemctl --user show -p InvocationID --value "$UNIT") |
| 180 | pid=$(systemctl --user show -p MainPID --value "$UNIT") |
| 181 | last= |
| 182 | if [[ $inv =~ ^[0-9a-f]{32}$ && $pid =~ ^[1-9][0-9]*$ ]]; then |
| 183 | [[ $(sha256sum "$PROC/$pid/exe" | cut -d ' ' -f 1) == "$1" ]] || die "the running runner is not $2" |
| 184 | last=$(journalctl --user -b _SYSTEMD_INVOCATION_ID="$inv" _PID="$pid" -o cat --no-pager | grep -E 'msg=welcome|msg="runner stream' | tail -1 || true) |
| 185 | fi |
| 186 | if [[ $last == *msg=welcome* ]]; then |
| 187 | [[ $seen == "$inv $pid" ]] && return 0 |
| 188 | seen="$inv $pid" |
| 189 | else |
| 190 | seen= |
| 191 | fi |
| 192 | sleep "${UPGRADE_WAIT:-5}" |
| 193 | done |
| 194 | return 1 |
| 195 | } |
| 196 | # The in-flight check and the backup both read the platform's replica of |
| 197 | # the database (carlos db restores a copy); an unreadable one stops here. |
| 198 | REPLICA="Check the platform's replica with: carlos db query ${carlos_app[*]} --sql 'PRAGMA integrity_check' (it must print ok)" |
| 199 | replica() { |
| 200 | local said |
| 201 | said=$(carlos db query "${carlos_app[@]}" --sql 'PRAGMA integrity_check' 2>/dev/null | tail -1) || said= |
| 202 | [[ $said == ok ]] || die "the platform's replica of $APP's database cannot be read, and the in-flight check and the backup both need it. $REPLICA" |
| 203 | } |
| 204 | database_note() { |
| 205 | echo " The database is not rolled back. If $(get version) started, its migrations may have run," |
| 206 | echo " and $(get prev_version) now runs on the migrated database. The copy from before the upgrade:" |
| 207 | echo " $(get backup) (sha256 $(get backup_sha256))" |
| 208 | echo " No carlos verb writes a database back to an instance (carlos db only reads a copy)." |
| 209 | echo " To restore it, a platform operator replaces the database of $HOST (the file the activator" |
| 210 | echo " passes as --db) with that file, with the instance stopped. Check it first:" |
| 211 | echo " python3 -c \"import sqlite3; print(sqlite3.connect('file:$(get backup)?mode=ro', uri=True).execute('pragma integrity_check').fetchone()[0])\"" |
| 212 | } |
| 213 | |
| 214 | if [[ $mode == rollback ]]; then |
| 215 | [[ -f $STATE ]] || die "no upgrade was recorded ($STATE)" |
| 216 | [[ $(get rolled_back) != 1 ]] || die "already rolled back" |
| 217 | snap=$BACKUPS/upgrade-$(get stamp) |
| 218 | [[ -n $(get stamp) && -f $snap/service.env && -f $snap/equipo-runner.service ]] || die "the prior state was not recorded" |
| 219 | prev_exec=$(sed -n 's/^ExecStart=\(.*\) run$/\1/p' "$snap/equipo-runner.service") |
| 220 | [[ -f $prev_exec ]] || die "the previous runner $prev_exec is missing" |
| 221 | prev_sha=$(sha256sum "$prev_exec" | cut -d ' ' -f 1) |
| 222 | step "rollback 0 no open task holds unfinished work" |
| 223 | echo " open tasks whose Engineering ran out of time and kept its work: 0 (a release before" |
| 224 | echo " protocol 32 takes that work for finished), read now and again on two readings once" |
| 225 | echo " the runner is stopped (only it reports steps); finish or cancel them first, or set" |
| 226 | echo " UPGRADE_ALLOW_UNFINISHED=1 to roll back anyway" |
| 227 | if ((execute)); then unfinished_none || die "$UNFINISHED_REFUSAL"; fi |
| 228 | step "rollback 1 stop the runner" |
| 229 | # A retry after the previous runner started again does not stop it a second time. |
| 230 | if ((execute)) && runs "$prev_sha"; then echo " $UNIT already runs $prev_exec"; else |
| 231 | run systemctl --user stop "$UNIT" |
| 232 | # Nothing reports a step now: two readings, a wait apart, so the |
| 233 | # replica has caught up with the last report the runner sent. |
| 234 | if ((execute)); then |
| 235 | for reading in 1 2; do |
| 236 | ((reading == 1)) || sleep "${UPGRADE_WAIT:-5}" |
| 237 | unfinished_none || { run systemctl --user start "$UNIT"; die "$UNFINISHED_REFUSAL (the runner was started again; nothing else changed)"; } |
| 238 | done |
| 239 | fi |
| 240 | fi |
| 241 | step "rollback 2 the previous runner's service.env and unit" |
| 242 | run cp -p "$snap/service.env" "$RUNNER_ROOT/service.env" |
| 243 | run cp -p "$snap/equipo-runner.service" "$RUNNER_ROOT/equipo-runner.service" |
| 244 | run systemctl --user daemon-reload |
| 245 | step "rollback 3 the previous instance version" |
| 246 | run carlos promote "${carlos_app[@]}" "$(get prev_version)" edge |
| 247 | wait_serving "$(get prev_version)" |
| 248 | step "rollback 4 start the previous runner, and see it connected" |
| 249 | run systemctl --user start "$UNIT" |
| 250 | echo " $UNIT runs $prev_exec, welcomed by the instance in this invocation, on two readings" |
| 251 | if ((execute)); then |
| 252 | connected "$prev_sha" "the previous runner $prev_exec" || die "the previous runner is not connected; nothing recorded as rolled back" |
| 253 | fi |
| 254 | put rolled_back 1 |
| 255 | if ((!execute)) || [[ $(get instance_started) == 1 ]]; then database_note; fi |
| 256 | ((execute)) || echo $'\nPlan only. Run it with --execute.' |
| 257 | exit 0 |
| 258 | fi |
| 259 | |
| 260 | instance=$RELEASES/equipo-linux-arm64-$version |
| 261 | runner_release=$RELEASES/equipo-runner-linux-amd64-$version |
| 262 | staged=$RELEASES/staged/$version.json |
| 263 | |
| 264 | # The version production should serve: an unfinished upgrade to this |
| 265 | # version resumes from its record; otherwise the last upgrade (finished, or |
| 266 | # rolled back to its previous version) names it, or else the cutover. |
| 267 | if [[ -f $STATE && $(get rolled_back) == 1 ]]; then |
| 268 | base=$(get prev_version) history=1 |
| 269 | elif [[ -f $STATE ]] && done_step verify; then |
| 270 | base=$(get version) history=1 |
| 271 | elif [[ -f $STATE ]]; then |
| 272 | [[ $(get version) == "$version" ]] || die "an upgrade to $(get version) is unfinished; finish it or roll it back" |
| 273 | base=$(get prev_version) |
| 274 | else |
| 275 | [[ -f $CUTOVER_STATE ]] || refuse "no cutover was recorded ($CUTOVER_STATE)" |
| 276 | [[ $(get_in "$CUTOVER_STATE" done_verify) == 1 && $(get_in "$CUTOVER_STATE" rolled_back) != 1 ]] || |
| 277 | refuse "the cutover has not finished ($CUTOVER_STATE has no done_verify)" |
| 278 | base=$(get_in "$CUTOVER_STATE" version) |
| 279 | fi |
| 280 | [[ $base != "$version" ]] || die "production is already on $version" |
| 281 | echo "Upgrade production from ${base:--unknown-} to $version." |
| 282 | |
| 283 | step "1 preflight" |
| 284 | run make -C "$RELEASE_REPO" staged-check RELEASE_VERSION="$version" |
| 285 | run test -f "$instance" |
| 286 | run test -x "$runner_release/equipo-runner" |
| 287 | run test -x "$runner_release/equipo-tool-proxy" |
| 288 | scanner "$runner_release" |
| 289 | renderer "$runner_release" |
| 290 | run test -f "$RUNNER_ROOT/equipo-runner.service" |
| 291 | run test -f "$RUNNER_ROOT/service.env" |
| 292 | run carlos apps list --account "$ACCOUNT" --console "$CONSOLE" |
| 293 | echo " production serves ${base:--unknown-} ($ORIGIN/api/version), and $version descends from it (git merge-base)" |
| 294 | echo " $UNIT is $RUNNER_ROOT/equipo-runner.service, enrolled, talking to $PUBLIC_ORIGIN, roots not shared with $STAGING_ROOT" |
| 295 | echo " the replica reads clean (the in-flight check and the backup need it):" |
| 296 | printf ' $ carlos db query %s --sql "PRAGMA integrity_check" # must be ok\n' "${carlos_app[*]}" |
| 297 | echo " nothing in flight (steps/merges/turns):" |
| 298 | printf ' $ carlos db query %s --sql "%s" # must be 0/0/0\n' "${carlos_app[*]}" "$IN_FLIGHT" |
| 299 | if ((execute)) && ! done_step preflight; then |
| 300 | served=$(curl -fsS "$ORIGIN/api/version") || die "could not read the served version" |
| 301 | [[ $served == "$base" ]] || die "production serves $served, but the records say $base" |
| 302 | git -C "$RELEASE_REPO" merge-base --is-ancestor "$base" "$version" || die "$version does not descend from $base" |
| 303 | unit_file=$(systemctl --user show -p FragmentPath --value "$UNIT") || die "could not read $UNIT" |
| 304 | [[ $(readlink -f "$unit_file") == "$RUNNER_ROOT/equipo-runner.service" ]] || die "$UNIT is not the production runner unit" |
| 305 | grep -qx "EnvironmentFile=$RUNNER_ROOT/service.env" "$RUNNER_ROOT/equipo-runner.service" || die "$UNIT does not read $RUNNER_ROOT/service.env" |
| 306 | [[ $(file_value EQUIPO_RUNNER_STATE_DIR "$RUNNER_ROOT/service.env") == "$RUNNER_ROOT/state" ]] || die "the runner state is not $RUNNER_ROOT/state" |
| 307 | [[ -f $RUNNER_ROOT/state/credentials.json ]] || die "the runner is not enrolled" |
| 308 | [[ $(file_value EQUIPO_ORIGIN "$RUNNER_ROOT/service.env") == "$PUBLIC_ORIGIN" ]] || die "the runner does not talk to $PUBLIC_ORIGIN: finish the cutover first" |
| 309 | if [[ -f $STAGING_ROOT/service.env ]]; then |
| 310 | for key in EQUIPO_RUNNER_STATE_DIR EQUIPO_WORKSPACE_ROOT EQUIPO_SCRATCH_ROOT; do |
| 311 | mine=$(file_value "$key" "$RUNNER_ROOT/service.env") |
| 312 | theirs=$(file_value "$key" "$STAGING_ROOT/service.env") |
| 313 | if [[ -n $mine && -n $theirs ]] && overlaps "$mine" "$theirs"; then |
| 314 | die "$key is shared with the staging runner" |
| 315 | fi |
| 316 | done |
| 317 | fi |
| 318 | replica |
| 319 | idle |
| 320 | install -d -m 0700 "$BACKUPS" |
| 321 | if ((history)); then mv "$STATE" "$BACKUPS/upgrade-$(get version)-$(get stamp).state"; history=0; fi |
| 322 | stamp=$(date -u +%Y%m%dT%H%M%SZ) |
| 323 | install -d -m 0700 "$BACKUPS/upgrade-$stamp" |
| 324 | cp -p "$RUNNER_ROOT/service.env" "$RUNNER_ROOT/equipo-runner.service" "$BACKUPS/upgrade-$stamp/" |
| 325 | put version "$version" |
| 326 | put stamp "$stamp" |
| 327 | put prev_version "$served" |
| 328 | finish preflight |
| 329 | fi |
| 330 | stamp=PLAN |
| 331 | ((history)) || stamp=$(get stamp) |
| 332 | stamp=${stamp:-PLAN} |
| 333 | |
| 334 | step "2 backup: the current database (the upgrade keeps it: no configuration changes)" |
| 335 | if ! done_step backup; then |
| 336 | if ((execute)); then replica; fi |
| 337 | backup=$BACKUPS/equipo-upgrade-$stamp.db |
| 338 | run carlos db pull "${carlos_app[@]}" --out "$backup" |
| 339 | run python3 -c "import sqlite3,sys; c=sqlite3.connect('file:$backup?mode=ro', uri=True); r=c.execute('pragma integrity_check').fetchone()[0]; print('integrity', r); [print(t, c.execute('select count(*) from '+t).fetchone()[0]) for t in ('teams','chat_tasks','chat_steps','chat_turns','runners')]; sys.exit(r!='ok')" |
| 340 | run sha256sum "$backup" |
| 341 | run chmod 0400 "$backup" |
| 342 | if ((execute)); then |
| 343 | put backup "$backup" |
| 344 | put backup_sha256 "$(sha256sum "$backup" | cut -d ' ' -f 1)" |
| 345 | fi |
| 346 | finish backup |
| 347 | fi |
| 348 | |
| 349 | step "3 runner: the staged runner beside the current one, then the unit switches" |
| 350 | new=$RUNNER_ROOT/releases/$version |
| 351 | config=(--exec "$new/equipo-runner" "EQUIPO_TOOL_PROXY_PATH=$new/equipo-tool-proxy") |
| 352 | if ! done_step runner; then |
| 353 | run install -d -m 0700 "$new" |
| 354 | run install -m 0700 "$runner_release/equipo-runner" "$runner_release/equipo-tool-proxy" "$runner_release/gitleaks" "$new/" |
| 355 | # The page renderer beside it, unpacked by the staged runner itself and |
| 356 | # held to the pin linked into it; verify-installed then renders a page. |
| 357 | run "$new/equipo-runner" install-renderer "$runner_release/renderer.tar.gz" |
| 358 | # The staged runner holds every program its configuration names, its |
| 359 | # own tool proxy, the gitleaks and the renderer beside it, to its |
| 360 | # release's digests before anything switches. |
| 361 | run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; EQUIPO_TOOL_PROXY_PATH='$new/equipo-tool-proxy' '$new/equipo-runner' verify-installed" |
| 362 | # Written from the preflight's record, never the live files, so a retry |
| 363 | # gives the same files; each key, ExecStart and RestartSec exactly once. |
| 364 | run python3 "$REPO/scripts/runner-config.py" render "$BACKUPS/upgrade-$stamp" "$RUNNER_ROOT" "${config[@]}" |
| 365 | run systemctl --user daemon-reload |
| 366 | echo " the loaded unit starts $new/equipo-runner (systemctl show -p ExecStart)" |
| 367 | if ((execute)); then |
| 368 | [[ $(systemctl --user show -p ExecStart --value "$UNIT") == *"path=$new/equipo-runner ;"* ]] || die "the loaded $UNIT does not start $new/equipo-runner" |
| 369 | fi |
| 370 | finish runner |
| 371 | fi |
| 372 | want=$( [[ -f $staged ]] && python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["sha256"]["runner"])' "$staged" || echo "the runner hash in $staged") |
| 373 | echo " nothing in flight, read again right before the restart. This is a short window, not a drain:" |
| 374 | echo " work dispatched after the reading, or not yet in the replica, can still be cut by the restart." |
| 375 | if ! done_step restart; then |
| 376 | # A retry after the restart happened finds the staged runner running and |
| 377 | # does not restart it again. |
| 378 | if ((execute)) && runs "$want"; then echo " $UNIT already runs the staged runner"; else |
| 379 | if ((execute)); then idle; fi |
| 380 | run systemctl --user restart "$UNIT" |
| 381 | fi |
| 382 | finish restart |
| 383 | fi |
| 384 | |
| 385 | step "4 instance: the staged instance with --swap hold; its migrations run on start" |
| 386 | echo " From here a rollback restores the previous version but not the database: that needs the step 2 backup." |
| 387 | if ! done_step instance; then |
| 388 | put instance_started 1 |
| 389 | run carlos deploy "${carlos_app[@]}" --channel edge --target linux-arm64 --swap hold --version "$version" --timeout 5m --no-prompt "$instance" |
| 390 | wait_serving "$version" |
| 391 | finish instance |
| 392 | fi |
| 393 | |
| 394 | step "5 verify" |
| 395 | run curl -fsS "$ORIGIN/api/version" |
| 396 | echo " sha256 of $PROC/<MainPID of $UNIT>/exe must be $want" |
| 397 | echo " $ journalctl --user -b _SYSTEMD_INVOCATION_ID=<InvocationID> _PID=<MainPID> -o cat --no-pager" |
| 398 | echo " the last stream line is msg=welcome (no later \"runner stream\" drop), on two readings of the same process" |
| 399 | if ((execute)) && ! done_step verify; then |
| 400 | [[ $(curl -fsS "$ORIGIN/api/version") == "$version" ]] || die "production does not report $version" |
| 401 | connected "$want" "the staged $version runner" || die "the runner is not connected to the instance now; to undo: scripts/upgrade-thin.sh rollback --execute" |
| 402 | finish verify |
| 403 | fi |
| 404 | echo " Then, as the owner: sign in at $PUBLIC_ORIGIN/conversation, check the offline line is" |
| 405 | echo " gone, say hello and see the reply stream." |
| 406 | echo " To undo: scripts/upgrade-thin.sh rollback --execute" |
| 407 | ((execute)) || echo $'\nPlan only. Run it with --execute.' |
| 408 | |