oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#!/usr/bin/env bash
2# Thin v1 production cutover for equipo (spec section 8): a drained move to
3# the thin instance and runner, on a fresh database, with a verified export
4# of the old one kept, and a complete rollback.
5#
6# scripts/cutover-thin.sh <version> # print the plan only (default)
7# scripts/cutover-thin.sh <version> --execute # a person runs it, step by step
8# scripts/cutover-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`). Agents must not run --execute: it changes
12# production. Each step asks first and stops on the first failure. The run
13# is resumable: steps done are recorded in $BACKUPS/cutover.state and skipped.
14#
15# Cutover:
16# 1 preflight staged-check, artifacts, CARLOS sign-in, runner unit, roots
17# not shared with the staging runner; records the prior
18# configuration (served version, EQUIPO_ALWAYS_ON and
19# EQUIPO_DATABASE, runner service.env and unit, instance id)
20# 2 drain stop the old runner
21# 3 export carlos db pull, integrity-checked, counted, hashed, read-only
22# 4 config EQUIPO_DATABASE=equipo-v1.db (fresh; equipo.db untouched),
23# EQUIPO_ALWAYS_ON removed
24# 5 deploy the staged instance, --swap hold, until /api/version reports it
25# 6 identity read the new instance id from the new database through the
26# platform replica; it must exist and differ from the old one.
27# Nothing on the runner has changed yet, so a failure here
28# still rolls back cleanly.
29# 7 runner install the staged runner, tool proxy, gitleaks (the
30# preview scanner) and page renderer beside the old one, new service.env
31# (tool proxy path, new instance id) and unit
32# (RestartPreventExitStatus=3), old state moved aside
33# 8 enrol the runner enrols with the new instance, the owner approves
34# 9 verify runner active, version, replica reads the new tables
35#
36# Rollback restores the recorded prior configuration exactly: stops the
37# runner, moves the new state aside under a timestamped name, restores the
38# old state, service.env and unit, restores EQUIPO_ALWAYS_ON and
39# EQUIPO_DATABASE, promotes the previously served version, starts the runner.
40set -euo pipefail
41
42APP=equipo
43ACCOUNT=oficina
44CONSOLE=https://console.carloku.com
45HOST=equipo.bds.oncarlos.com
46# ORIGIN is the platform route, where the served version is read. PUBLIC_ORIGIN
47# is the address people use (EQUIPO_ORIGIN); the cutover moves both the
48# instance and the runner to it.
49ORIGIN=https://$HOST
50PUBLIC_ORIGIN=${EQUIPO_PUBLIC_ORIGIN:-https://equipo.oficina.works}
51NEW_DB=equipo-v1.db
52UNIT=equipo-runner.service
53RUNNER_ROOT=$HOME/.local/share/equipo-runner
54STAGING_ROOT=$HOME/.local/share/equipo-runner-staging
55BACKUPS=$HOME/.local/share/equipo-cutover
56STATE=$BACKUPS/cutover.state
57SNAP=$BACKUPS/prior
58REPO=$(cd "$(dirname "$0")/.." && pwd)
59carlos_app=(--app "$APP" --account "$ACCOUNT" --console "$CONSOLE")
60
61mode=cutover
62version=${1:-}
63[[ $version == rollback ]] && mode=rollback
64execute=0
65[[ ${2:-} == --execute ]] && execute=1
66if [[ $mode == cutover && ! $version =~ ^[0-9a-f]{7,12}$ ]]; then
67 sed -n '2,41p' "$0"
68 exit 2
69fi
70
71die() { echo "refused: $*" >&2; exit 1; }
72step() {
73 printf '\n== %s\n' "$1"
74 if ((execute)) && [[ ${CUTOVER_YES:-} != 1 ]]; then
75 read -r -p "Run this step? Type yes: " answer
76 [[ $answer == yes ]] || { echo "stopped before: $1"; exit 1; }
77 fi
78}
79run() {
80 printf ' $ %s\n' "$*"
81 if ((execute)); then "$@"; fi
82}
83# Recorded facts and finished steps, one KEY=value per line.
84get() { [[ -f $STATE ]] && sed -n "s/^$1=//p" "$STATE" | tail -1 || true; }
85put() {
86 ((execute)) || return 0
87 printf '%s=%s\n' "$1" "$2" >>"$STATE" || die "could not record $1=$2 in $STATE"
88}
89done_step() { [[ $(get "done_$1") == 1 ]]; }
90finish() { put "done_$1" 1; }
91# env_value reads one key from a `carlos env list` listing on stdin.
92env_value() { awk -v k="$1" '$1 == k { print $2; found = 1 } END { if (!found) print "-unset-" }'; }
93# The runner scans every preview with the gitleaks beside its own
94# executable, held to the hash linked into the runner; without it every
95# preview fails (preview_scanner). $1 is a runner release directory: its
96# gitleaks runs, and its sha256 is the one linked into its equipo-runner.
97scanner_linked() {
98 local sum
99 [[ -f $1/gitleaks && ! -L $1/gitleaks && -x $1/gitleaks ]] || return 1
100 sum=$(sha256sum "$1/gitleaks" | cut -d ' ' -f 1) || return 1
101 grep -qaF "$sum" "$1/equipo-runner"
102}
103scanner() {
104 echo " $1/gitleaks: executable, and its sha256 is linked into $1/equipo-runner"
105 if ((execute)) && ! scanner_linked "$1"; then
106 die "$1/gitleaks is missing, cannot run, or is not the scanner equipo-runner was built with"
107 fi
108}
109# The runner renders pages with the renderer tree beside its own
110# executable, held to the pin linked into the runner (the sha256 of the
111# tree's MANIFEST). $1 is a runner release directory: the MANIFEST of its
112# renderer.tar.gz has the sha256 linked into its equipo-runner.
113renderer_linked() {
114 local sum
115 [[ -f $1/renderer.tar.gz && ! -L $1/renderer.tar.gz ]] || return 1
116 sum=$(tar -xzOf "$1/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || return 1
117 grep -qaF "$sum" "$1/equipo-runner"
118}
119renderer() {
120 echo " $1/renderer.tar.gz: its MANIFEST's sha256 is linked into $1/equipo-runner"
121 if ((execute)) && ! renderer_linked "$1"; then
122 die "$1/renderer.tar.gz is missing, or is not the renderer equipo-runner was built with"
123 fi
124}
125overlaps() { case "$1/" in "$2"/*) return 0 ;; esac; case "$2/" in "$1"/*) return 0 ;; esac; return 1; }
126
127if [[ $mode == rollback ]]; then
128 [[ -f $STATE ]] || die "no cutover was recorded ($STATE)"
129 [[ $(get rolled_back) != 1 ]] || die "already rolled back"
130 # On CARLOS the activator passes the app an absolute --db, so EQUIPO_DATABASE
131 # chose no fresh file: a deploy that began may have migrated the old
132 # database in place. It may have begun when the record says it started or
133 # finished, or when a record from before deploy_started existed passed
134 # step 4 without saying.
135 if [[ $(get deploy_started) == 1 ]] || done_step deploy || { done_step config && [[ -z $(get deploy_started) ]]; }; then
136 die "a deploy of $(get version) may have begun, and it migrates the production database in place (EQUIPO_DATABASE is ignored on CARLOS); $(get prev_version) cannot read it. Finish on the kept database: scripts/cutover-finish-thin.sh"
137 fi
138 stamp=$(get stamp)
139 [[ -n $stamp && -f $SNAP/service.env && -f $SNAP/equipo-runner.service ]] || die "the prior configuration was not recorded"
140 step "rollback 1 stop the runner"
141 run systemctl --user stop "$UNIT"
142 step "rollback 2 runner state, service.env and unit"
143 if [[ -e $RUNNER_ROOT/state.pre-thin-$stamp ]]; then
144 if [[ -e $RUNNER_ROOT/state ]]; then
145 run mv "$RUNNER_ROOT/state" "$RUNNER_ROOT/state.thin-rolled-back-$(date -u +%Y%m%dT%H%M%SZ)"
146 fi
147 run mv "$RUNNER_ROOT/state.pre-thin-$stamp" "$RUNNER_ROOT/state"
148 fi
149 run cp -p "$SNAP/service.env" "$RUNNER_ROOT/service.env"
150 run cp -p "$SNAP/equipo-runner.service" "$RUNNER_ROOT/equipo-runner.service"
151 run systemctl --user daemon-reload
152 step "rollback 3 instance configuration and the previous release"
153 prev_db=$(get prev_database)
154 prev_always=$(get prev_always_on)
155 if [[ $prev_db == -unset- ]]; then run carlos env unset "${carlos_app[@]}" EQUIPO_DATABASE; else run carlos env set "${carlos_app[@]}" --no-prompt "EQUIPO_DATABASE=$prev_db"; fi
156 if [[ $prev_always != -unset- ]]; then run carlos env set "${carlos_app[@]}" --no-prompt "EQUIPO_ALWAYS_ON=$prev_always"; fi
157 prev_origin=$(get prev_origin)
158 if [[ -n $prev_origin ]]; then run carlos env set "${carlos_app[@]}" --no-prompt "EQUIPO_ORIGIN=$prev_origin"; fi
159 run carlos promote "${carlos_app[@]}" "$(get prev_version)" edge
160 run bash -c "for i in \$(seq 60); do [ \"\$(curl -fsS $ORIGIN/api/version)\" = $(get prev_version) ] && exit 0; sleep 2; done; exit 1"
161 step "rollback 4 start the previous runner"
162 run systemctl --user start "$UNIT"
163 put rolled_back 1
164 ((execute)) || echo $'\nPlan only. A person runs it with --execute.'
165 exit 0
166fi
167
168instance=$REPO/releases/equipo-linux-arm64-$version
169runner_release=$REPO/releases/equipo-runner-linux-amd64-$version
170if [[ -f $STATE ]]; then
171 [[ $(get version) == "$version" ]] || die "a cutover to $(get version) is recorded; roll it back first"
172 [[ $(get rolled_back) != 1 ]] || die "this cutover was rolled back; move $STATE aside to start again"
173 if done_step verify; then die "the cutover to $version is complete"; fi
174fi
175
176step "1 preflight"
177run make -C "$REPO" staged-check RELEASE_VERSION="$version"
178run test -f "$instance"
179run test -x "$runner_release/equipo-runner"
180run test -x "$runner_release/equipo-tool-proxy"
181scanner "$runner_release"
182renderer "$runner_release"
183run test -f "$RUNNER_ROOT/equipo-runner.service"
184run test -f "$RUNNER_ROOT/service.env"
185run carlos apps list --account "$ACCOUNT" --console "$CONSOLE"
186if ((execute)) && ! done_step preflight; then
187 if ls -d "$RUNNER_ROOT"/state.pre-thin-* >/dev/null 2>&1; then
188 die "an unrecorded earlier attempt left state.pre-thin-*"
189 fi
190 [[ -d $RUNNER_ROOT/state ]] || die "no runner state to keep"
191 if [[ -f $STAGING_ROOT/service.env ]]; then
192 for key in EQUIPO_RUNNER_STATE_DIR EQUIPO_WORKSPACE_ROOT EQUIPO_SCRATCH_ROOT; do
193 mine=$(sed -n "s/^$key=\"\{0,1\}\([^\"]*\)\"\{0,1\}$/\1/p" "$RUNNER_ROOT/service.env")
194 theirs=$(sed -n "s/^$key=\"\{0,1\}\([^\"]*\)\"\{0,1\}$/\1/p" "$STAGING_ROOT/service.env")
195 if [[ -n $mine && -n $theirs ]] && overlaps "$mine" "$theirs"; then
196 die "$key is shared with the staging runner"
197 fi
198 done
199 fi
200 # Read every fact rollback needs before changing anything; a read that
201 # fails or looks wrong stops the cutover here, with nothing changed.
202 prev_version=$(curl -fsS "$ORIGIN/api/version") || die "could not read the served version"
203 [[ $prev_version =~ ^[A-Za-z0-9._-]{1,64}$ ]] || die "unexpected served version"
204 listing=$(carlos env list "${carlos_app[@]}") || die "could not read the instance configuration"
205 grep -q '^KEY' <<<"$listing" || die "unexpected configuration listing"
206 prev_always_on=$(env_value EQUIPO_ALWAYS_ON <<<"$listing")
207 prev_database=$(env_value EQUIPO_DATABASE <<<"$listing")
208 prev_origin=$(env_value EQUIPO_ORIGIN <<<"$listing")
209 [[ $prev_origin =~ ^https://[a-z0-9.-]+$ ]] || die "unexpected EQUIPO_ORIGIN"
210 [[ $PUBLIC_ORIGIN =~ ^https://[a-z0-9.-]+$ ]] || die "unexpected public origin"
211 curl -fsS -o /dev/null "$PUBLIC_ORIGIN/api/version" || die "$PUBLIC_ORIGIN does not serve this app yet: attach the domain first"
212 for value in "$prev_always_on" "$prev_database"; do
213 [[ $value =~ ^[A-Za-z0-9._-]{1,128}$ ]] || die "unexpected configuration value"
214 done
215 old_instance_id=$(sed -n 's/^EQUIPO_INSTANCE_ID="\{0,1\}\([^"]*\)"\{0,1\}$/\1/p' "$RUNNER_ROOT/service.env")
216 [[ $old_instance_id =~ ^[A-Z0-9]{26}$ ]] || die "the runner config has no instance id"
217 install -d -m 0700 "$BACKUPS" "$SNAP"
218 stamp=$(date -u +%Y%m%dT%H%M%SZ)
219 cp -p "$RUNNER_ROOT/service.env" "$SNAP/service.env"
220 cp -p "$RUNNER_ROOT/equipo-runner.service" "$SNAP/equipo-runner.service"
221 put version "$version"
222 put stamp "$stamp"
223 put prev_version "$prev_version"
224 put prev_always_on "$prev_always_on"
225 put prev_database "$prev_database"
226 put prev_origin "$prev_origin"
227 put old_instance_id "$old_instance_id"
228 finish preflight
229fi
230stamp=$(get stamp)
231stamp=${stamp:-PLAN}
232
233step "2 drain: stop the old runner (it finishes or releases in-flight work on SIGTERM)"
234run systemctl --user stop "$UNIT"
235
236step "3 export and verify the current database"
237if ! done_step export; then
238 backup=$BACKUPS/equipo-$stamp.db
239 run carlos db pull "${carlos_app[@]}" --out "$backup"
240 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 ('members','tasks','conversation_messages','runners')]; sys.exit(r!='ok')"
241 run sha256sum "$backup"
242 run chmod 0400 "$backup"
243 finish export
244fi
245
246step "4 configuration: fresh database file, no always-on"
247if ! done_step config; then
248 run carlos env set "${carlos_app[@]}" --no-prompt "EQUIPO_DATABASE=$NEW_DB" "EQUIPO_ORIGIN=$PUBLIC_ORIGIN"
249 [[ $(get prev_always_on) == -unset- ]] || run carlos env unset "${carlos_app[@]}" EQUIPO_ALWAYS_ON
250 put deploy_started 0
251 finish config
252fi
253
254step "5 deploy the staged instance with --swap hold"
255if ! done_step deploy; then
256 # Recorded before CARLOS is asked: from here rollback refuses the old version.
257 put deploy_started 1
258 run carlos deploy "${carlos_app[@]}" --channel edge --target linux-arm64 --swap hold --version "$version" --timeout 5m --no-prompt "$instance"
259 run bash -c "for i in \$(seq 60); do [ \"\$(curl -fsS $ORIGIN/api/version)\" = $version ] && exit 0; sleep 2; done; exit 1"
260 finish deploy
261fi
262
263step "6 identity: the new instance id, from the new database"
264printf ' $ carlos db query %s --sql "select id from teams"\n' "${carlos_app[*]}"
265if ((execute)) && ! done_step identity; then
266 new_id=
267 for _ in $(seq "${CUTOVER_ID_TRIES:-30}"); do
268 new_id=$(carlos db query "${carlos_app[@]}" --sql "select id from teams" 2>/dev/null | sed -n 2p || true)
269 [[ $new_id =~ ^[A-Z0-9]{26}$ && $new_id != "$(get old_instance_id)" ]] && break
270 new_id=
271 sleep "${CUTOVER_ID_WAIT:-5}"
272 done
273 [[ -n $new_id ]] || die "the replica shows no new instance id; roll back (scripts/cutover-thin.sh rollback --execute)"
274 put new_instance_id "$new_id"
275 finish identity
276fi
277new_id=$(get new_instance_id)
278
279step "7 install the staged runner beside the old one"
280if ! done_step runner; then
281 run install -d -m 0700 "$RUNNER_ROOT/releases/$version"
282 run install -m 0700 "$runner_release/equipo-runner" "$runner_release/equipo-tool-proxy" "$runner_release/gitleaks" "$RUNNER_ROOT/releases/$version/"
283 # The page renderer beside it, unpacked by the staged runner itself and
284 # held to the pin linked into it; verify-installed then renders a page.
285 run "$RUNNER_ROOT/releases/$version/equipo-runner" install-renderer "$runner_release/renderer.tar.gz"
286 run sed -i -e "s|^EQUIPO_TOOL_PROXY_PATH=.*|EQUIPO_TOOL_PROXY_PATH=\"$RUNNER_ROOT/releases/$version/equipo-tool-proxy\"|" -e "s|^EQUIPO_INSTANCE_ID=.*|EQUIPO_INSTANCE_ID=\"${new_id:-NEW_INSTANCE_ID}\"|" -e "s|^EQUIPO_ORIGIN=.*|EQUIPO_ORIGIN=\"$PUBLIC_ORIGIN\"|" "$RUNNER_ROOT/service.env"
287 run sed -i -e "s|^ExecStart=.*|ExecStart=$RUNNER_ROOT/releases/$version/equipo-runner run|" -e '/^RestartPreventExitStatus=/d' -e '/^RestartSec=/a RestartPreventExitStatus=3' "$RUNNER_ROOT/equipo-runner.service"
288 if ((execute)); then
289 [[ -e $RUNNER_ROOT/state.pre-thin-$stamp ]] && die "state.pre-thin-$stamp already exists"
290 fi
291 # The old credential belongs to the old database; keep the old state for rollback.
292 run mv "$RUNNER_ROOT/state" "$RUNNER_ROOT/state.pre-thin-$stamp"
293 run install -d -m 0700 "$RUNNER_ROOT/state"
294 run systemctl --user daemon-reload
295 finish runner
296fi
297
298step "8 enrol the runner with the new instance: open the printed link as the owner and enter the code"
299if ! done_step enrol; then
300 # Before anything of the new runner starts, it holds every program in
301 # its new configuration (bubblewrap, claude, codex and its code mode
302 # host, the staged tool proxy) and the gitleaks and renderer beside it
303 # to the digests of its release, and renders a fixture page.
304 run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$RUNNER_ROOT/releases/$version/equipo-runner' verify-installed"
305 run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$RUNNER_ROOT/releases/$version/equipo-runner' enrol"
306 run systemctl --user start "$UNIT"
307 finish enrol
308fi
309
310step "9 verify"
311run curl -fsS "$ORIGIN/api/version"
312run systemctl --user is-active "$UNIT"
313run carlos db query "${carlos_app[@]}" --sql "select count(*) from chat_turns"
314finish verify
315echo " Then, as the owner: sign in at $PUBLIC_ORIGIN/conversation, check the offline line"
316echo " is gone, say hello and see the reply stream. Sign native devices in again."
317echo " To undo: scripts/cutover-thin.sh rollback --execute"
318((execute)) || echo $'\nPlan only. A person runs it with --execute.'
319