| 1 | #!/usr/bin/env bash |
| 2 | # Finish the thin v1 production cutover on the database production kept. |
| 3 | # |
| 4 | # scripts/cutover-finish-thin.sh # print the plan only (default) |
| 5 | # scripts/cutover-finish-thin.sh --execute # a person runs it, step by step |
| 6 | # scripts/cutover-finish-thin.sh rollback [--execute] # the runner as the cutover left it |
| 7 | # |
| 8 | # The 2026-11-19 cutover (scripts/cutover-thin.sh) stopped after step 5, |
| 9 | # and its fresh database never happened: on CARLOS the activator passes the |
| 10 | # app an absolute --db, so EQUIPO_DATABASE is ignored, and the new version |
| 11 | # migrated the old database in place. That data is kept. This finishes |
| 12 | # cutover steps 6 onward for it without the platform replica: every check |
| 13 | # reads the instance or this machine. Agents must not run --execute. Steps |
| 14 | # done are recorded in cutover.state (done_identity ... done_verify), so a |
| 15 | # finished run is a finished cutover to scripts/upgrade-thin.sh. Every step |
| 16 | # can be run again after a failure. |
| 17 | # |
| 18 | # 6 identity /install.sh, which the instance builds from its own database |
| 19 | # (its team), names the recorded old instance id, the version |
| 20 | # and the public origin, on both routes; staged runner bytes |
| 21 | # 7 config EQUIPO_DATABASE unset, so the configuration says what runs; |
| 22 | # then the instance serves that version and the same id again |
| 23 | # 8 runner the runner's files recorded once; the staged runner (and |
| 24 | # its page renderer, install-renderer) beside the old one; |
| 25 | # service.env and unit written from that record |
| 26 | # (scripts/runner-config.py); the old state moved aside |
| 27 | # 9 enrol the written and loaded configuration checked, then |
| 28 | # verify-installed, enrol (the owner approves the link) |
| 29 | # 10 start the unit starts (recorded apart from enrol: the running |
| 30 | # runner holds the state lock enrol needs) |
| 31 | # 11 verify version and identity again; in this boot and this unit |
| 32 | # invocation, the staged runner's process was welcomed by the |
| 33 | # instance and has not dropped since, on two readings |
| 34 | # |
| 35 | # Rollback puts the runner's state, service.env and unit back as the |
| 36 | # cutover left them and leaves the runner stopped. It never offers the |
| 37 | # version before the cutover: that version cannot read the migrated schema. |
| 38 | set -euo pipefail |
| 39 | |
| 40 | APP=equipo |
| 41 | ACCOUNT=oficina |
| 42 | CONSOLE=https://console.carloku.com |
| 43 | HOST=equipo.bds.oncarlos.com |
| 44 | ORIGIN=https://$HOST |
| 45 | PUBLIC_ORIGIN=${EQUIPO_PUBLIC_ORIGIN:-https://equipo.oficina.works} |
| 46 | UNIT=equipo-runner.service |
| 47 | RUNNER_ROOT=$HOME/.local/share/equipo-runner |
| 48 | BACKUPS=$HOME/.local/share/equipo-cutover |
| 49 | STATE=$BACKUPS/cutover.state |
| 50 | REPO=$(cd "$(dirname "$0")/.." && pwd) |
| 51 | RELEASES=${EQUIPO_RELEASE_REPO:-$REPO}/releases |
| 52 | PROC=${CUTOVER_PROC:-/proc} |
| 53 | carlos_app=(--app "$APP" --account "$ACCOUNT" --console "$CONSOLE") |
| 54 | |
| 55 | mode=finish |
| 56 | execute=0 |
| 57 | case "${1:-} ${2:-}" in |
| 58 | " ") ;; |
| 59 | "--execute ") execute=1 ;; |
| 60 | "rollback ") mode=rollback ;; |
| 61 | "rollback --execute") mode=rollback execute=1 ;; |
| 62 | *) sed -n '2,36p' "$0"; exit 2 ;; |
| 63 | esac |
| 64 | |
| 65 | die() { echo "refused: $*" >&2; exit 1; } |
| 66 | # A refusal read from the records stops --execute; the plan notes it and goes on. |
| 67 | refuse() { |
| 68 | if ((execute)); then die "$*"; fi |
| 69 | echo " ! would refuse: $*" |
| 70 | } |
| 71 | step() { |
| 72 | printf '\n== %s\n' "$1" |
| 73 | if ((execute)) && [[ ${CUTOVER_YES:-} != 1 ]]; then |
| 74 | read -r -p "Run this step? Type yes: " answer |
| 75 | [[ $answer == yes ]] || { echo "stopped before: $1"; exit 1; } |
| 76 | fi |
| 77 | } |
| 78 | run() { |
| 79 | printf ' $ %s\n' "$*" |
| 80 | if ((execute)); then "$@"; fi |
| 81 | } |
| 82 | # Recorded facts and finished steps, one KEY=value per line; the last wins. |
| 83 | get() { [[ -f $STATE ]] && sed -n "s/^$1=//p" "$STATE" | tail -1 || true; } |
| 84 | put() { |
| 85 | ((execute)) || return 0 |
| 86 | printf '%s=%s\n' "$1" "$2" >>"$STATE" || die "could not record $1=$2 in $STATE" |
| 87 | } |
| 88 | done_step() { [[ $(get "done_$1") == 1 ]]; } |
| 89 | finish() { put "done_$1" 1; } |
| 90 | env_value() { awk -v k="$1" '$1 == k { print $2; found = 1 } END { if (!found) print "-unset-" }'; } |
| 91 | file_value() { sed -n "s/^$1=\"\{0,1\}\([^\"]*\)\"\{0,1\}$/\1/p" "$2"; } |
| 92 | wait_serving() { run bash -c "for i in \$(seq 60); do [ \"\$(curl -fsS $ORIGIN/api/version)\" = $1 ] && exit 0; sleep 2; done; exit 1"; } |
| 93 | |
| 94 | version=$(get version) |
| 95 | old_id=$(get old_instance_id) |
| 96 | stamp=$(get stamp) |
| 97 | snap=$BACKUPS/finish-$stamp |
| 98 | kept=$RUNNER_ROOT/state.pre-thin-$stamp |
| 99 | |
| 100 | # The instance's own word, not the replica's: /install.sh ends with the |
| 101 | # origin, version and instance id (its team's id, read from the database |
| 102 | # it serves), on the platform route and on the public address. |
| 103 | identity() { |
| 104 | local origin said |
| 105 | for origin in "$ORIGIN" "$PUBLIC_ORIGIN"; do |
| 106 | said=$(curl -fsS "$origin/install.sh") || die "could not read $origin/install.sh" |
| 107 | [[ $said == *"sh -s -- '$PUBLIC_ORIGIN' '$version' '$old_id'" ]] || |
| 108 | die "$origin/install.sh does not name $PUBLIC_ORIGIN, $version and instance $old_id (it says: ${said##*sh -s -- })" |
| 109 | done |
| 110 | } |
| 111 | |
| 112 | # The runner scans every preview with the gitleaks beside its own |
| 113 | # executable, held to the hash linked into the runner; without it every |
| 114 | # preview fails (preview_scanner). $1 is a runner release directory: its |
| 115 | # gitleaks runs, and its sha256 is the one linked into its equipo-runner. |
| 116 | scanner_linked() { |
| 117 | local sum |
| 118 | [[ -f $1/gitleaks && ! -L $1/gitleaks && -x $1/gitleaks ]] || return 1 |
| 119 | sum=$(sha256sum "$1/gitleaks" | cut -d ' ' -f 1) || return 1 |
| 120 | grep -qaF "$sum" "$1/equipo-runner" |
| 121 | } |
| 122 | scanner() { |
| 123 | echo " $1/gitleaks: executable, and its sha256 is linked into $1/equipo-runner" |
| 124 | if ((execute)) && ! scanner_linked "$1"; then |
| 125 | die "$1/gitleaks is missing, cannot run, or is not the scanner equipo-runner was built with" |
| 126 | fi |
| 127 | } |
| 128 | # The runner renders pages with the renderer tree beside its own |
| 129 | # executable, held to the pin linked into the runner (the sha256 of the |
| 130 | # tree's MANIFEST). $1 is a runner release directory: the MANIFEST of its |
| 131 | # renderer.tar.gz has the sha256 linked into its equipo-runner. |
| 132 | renderer_linked() { |
| 133 | local sum |
| 134 | [[ -f $1/renderer.tar.gz && ! -L $1/renderer.tar.gz ]] || return 1 |
| 135 | sum=$(tar -xzOf "$1/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || return 1 |
| 136 | grep -qaF "$sum" "$1/equipo-runner" |
| 137 | } |
| 138 | renderer() { |
| 139 | echo " $1/renderer.tar.gz: its MANIFEST's sha256 is linked into $1/equipo-runner" |
| 140 | if ((execute)) && ! renderer_linked "$1"; then |
| 141 | die "$1/renderer.tar.gz is missing, or is not the renderer equipo-runner was built with" |
| 142 | fi |
| 143 | } |
| 144 | # A hash from the green staging record of $version (empty if there is none). |
| 145 | staged_sha() { python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["sha256"][sys.argv[2]] if d.get("staging") == "PASS" else "")' "$RELEASES/staged/$version.json" "$1" 2>/dev/null || true; } |
| 146 | |
| 147 | # The production unit and its state directory, not some other runner's. |
| 148 | production_unit() { |
| 149 | [[ $(readlink -f "$(systemctl --user show -p FragmentPath --value "$UNIT")") == "$RUNNER_ROOT/equipo-runner.service" ]] || die "$UNIT is not $RUNNER_ROOT/equipo-runner.service" |
| 150 | [[ $(file_value EQUIPO_RUNNER_STATE_DIR "$RUNNER_ROOT/service.env") == "$RUNNER_ROOT/state" ]] || die "the runner state is not $RUNNER_ROOT/state" |
| 151 | } |
| 152 | |
| 153 | # Connected now: in this boot and the unit's current invocation, the |
| 154 | # process running the binary with sha256 $1 logged the instance's welcome, |
| 155 | # and no stream drop after it, on two readings in a row of the same |
| 156 | # invocation and process. |
| 157 | connected() { |
| 158 | local seen= inv pid last |
| 159 | for _ in $(seq "${CUTOVER_TRIES:-30}"); do |
| 160 | inv=$(systemctl --user show -p InvocationID --value "$UNIT") |
| 161 | pid=$(systemctl --user show -p MainPID --value "$UNIT") |
| 162 | last= |
| 163 | if [[ $inv =~ ^[0-9a-f]{32}$ && $pid =~ ^[1-9][0-9]*$ ]]; then |
| 164 | [[ $(sha256sum "$PROC/$pid/exe" | cut -d ' ' -f 1) == "$1" ]] || die "the running runner is not the staged $version runner" |
| 165 | last=$(journalctl --user -b _SYSTEMD_INVOCATION_ID="$inv" _PID="$pid" -o cat --no-pager | grep -E 'msg=welcome|msg="runner stream' | tail -1 || true) |
| 166 | fi |
| 167 | if [[ $last == *msg=welcome* ]]; then |
| 168 | [[ $seen == "$inv $pid" ]] && return 0 |
| 169 | seen="$inv $pid" |
| 170 | else |
| 171 | seen= |
| 172 | fi |
| 173 | sleep "${CUTOVER_WAIT:-5}" |
| 174 | done |
| 175 | return 1 |
| 176 | } |
| 177 | |
| 178 | # The old state was written by the old runner for the old origin; it is |
| 179 | # kept for rollback. Each part is recorded, so a retry finds where it was. |
| 180 | move_state() { |
| 181 | if [[ $(get state_moved) != 1 ]]; then |
| 182 | if [[ -e $kept && -e $RUNNER_ROOT/state ]]; then |
| 183 | die "both state and state.pre-thin-$stamp exist and no move is recorded: look before going on" |
| 184 | fi |
| 185 | if [[ ! -e $kept ]]; then |
| 186 | [[ -d $RUNNER_ROOT/state ]] || die "no runner state to keep" |
| 187 | mv "$RUNNER_ROOT/state" "$kept" |
| 188 | fi |
| 189 | put state_moved 1 |
| 190 | fi |
| 191 | [[ -d $kept ]] || die "state.pre-thin-$stamp is missing" |
| 192 | install -d -m 0700 "$RUNNER_ROOT/state" |
| 193 | } |
| 194 | |
| 195 | if [[ $mode == rollback ]]; then |
| 196 | [[ $(get kept_database) == 1 ]] || die "no finish of the cutover on the kept database is recorded ($STATE)" |
| 197 | [[ -f $snap/service.env && -f $snap/equipo-runner.service ]] || die "the runner files from before the finish were not recorded" |
| 198 | step "rollback 1 stop the runner" |
| 199 | run systemctl --user stop "$UNIT" |
| 200 | step "rollback 2 the runner's state, service.env and unit as the cutover left them" |
| 201 | if [[ -e $kept ]]; then |
| 202 | if [[ -e $RUNNER_ROOT/state ]]; then |
| 203 | run mv "$RUNNER_ROOT/state" "$RUNNER_ROOT/state.finish-rolled-back-$(date -u +%Y%m%dT%H%M%SZ)" |
| 204 | fi |
| 205 | run mv "$kept" "$RUNNER_ROOT/state" |
| 206 | fi |
| 207 | run cp -p "$snap/service.env" "$RUNNER_ROOT/service.env" |
| 208 | run cp -p "$snap/equipo-runner.service" "$RUNNER_ROOT/equipo-runner.service" |
| 209 | run systemctl --user daemon-reload |
| 210 | if ((execute)); then |
| 211 | systemctl --user is-active --quiet "$UNIT" && die "$UNIT is still running" |
| 212 | cmp -s "$snap/service.env" "$RUNNER_ROOT/service.env" && cmp -s "$snap/equipo-runner.service" "$RUNNER_ROOT/equipo-runner.service" || die "the runner files are not the recorded ones" |
| 213 | [[ -d $RUNNER_ROOT/state && ! -e $kept ]] || die "the old runner state is not back in place" |
| 214 | fi |
| 215 | for s in identity unset runner enrol start verify; do put "done_$s" 0; done |
| 216 | put state_moved 0 |
| 217 | put kept_database 0 |
| 218 | echo " The runner stays stopped and the instance stays on $version: nothing is promoted." |
| 219 | echo " $(get prev_version) is not offered: $version migrated the kept database in place, and" |
| 220 | echo " $(get prev_version) cannot read the migrated schema. EQUIPO_DATABASE is not set again: it was never used." |
| 221 | echo " To finish again: scripts/cutover-finish-thin.sh --execute" |
| 222 | ((execute)) || echo $'\nPlan only. A person runs it with --execute.' |
| 223 | exit 0 |
| 224 | fi |
| 225 | |
| 226 | [[ -f $STATE ]] || refuse "no cutover was recorded ($STATE)" |
| 227 | [[ $(get rolled_back) != 1 ]] || refuse "the cutover was rolled back" |
| 228 | done_step deploy || refuse "the cutover has not deployed its version yet; use scripts/cutover-thin.sh" |
| 229 | if done_step verify; then die "the cutover to $version is complete"; fi |
| 230 | [[ $old_id =~ ^[A-Z0-9]{26}$ ]] || refuse "cutover.state records no old instance id" |
| 231 | runner_release=$RELEASES/equipo-runner-linux-amd64-$version |
| 232 | new=$RUNNER_ROOT/releases/$version |
| 233 | config=(--exec "$new/equipo-runner" "EQUIPO_TOOL_PROXY_PATH=$new/equipo-tool-proxy" "EQUIPO_INSTANCE_ID=$old_id" "EQUIPO_ORIGIN=$PUBLIC_ORIGIN") |
| 234 | echo "Finish the cutover to ${version:--unknown-} on the kept database, instance ${old_id:--unknown-}." |
| 235 | |
| 236 | step "6 identity: the instance names the kept database's id (no replica)" |
| 237 | run curl -fsS "$ORIGIN/api/version" |
| 238 | echo " $ORIGIN/install.sh and $PUBLIC_ORIGIN/install.sh must end: sh -s -- '$PUBLIC_ORIGIN' '$version' '$old_id'" |
| 239 | echo " the staged runner, tool proxy and gitleaks must have the hashes in $RELEASES/staged/$version.json" |
| 240 | scanner "$runner_release" |
| 241 | renderer "$runner_release" |
| 242 | if ((execute)) && ! done_step identity; then |
| 243 | [[ $(curl -fsS "$ORIGIN/api/version") == "$version" ]] || die "production does not serve $version" |
| 244 | identity |
| 245 | [[ $(sha256sum "$runner_release/equipo-runner" | cut -d ' ' -f 1) == "$(staged_sha runner)" ]] || die "equipo-runner is not the one staging passed for $version" |
| 246 | [[ $(sha256sum "$runner_release/equipo-tool-proxy" | cut -d ' ' -f 1) == "$(staged_sha tool_proxy)" ]] || die "equipo-tool-proxy is not the one staging passed for $version" |
| 247 | [[ $(sha256sum "$runner_release/gitleaks" | cut -d ' ' -f 1) == "$(staged_sha gitleaks)" ]] || die "gitleaks is not the one staging passed for $version" |
| 248 | put new_instance_id "$old_id" |
| 249 | put kept_database 1 |
| 250 | finish identity |
| 251 | fi |
| 252 | |
| 253 | step "7 config: unset EQUIPO_DATABASE (the activator's --db wins, so it was never used)" |
| 254 | if ! done_step unset; then |
| 255 | if ((execute)); then |
| 256 | listing=$(carlos env list "${carlos_app[@]}") || die "could not read the instance configuration" |
| 257 | grep -q '^KEY' <<<"$listing" || die "unexpected configuration listing" |
| 258 | [[ $(env_value EQUIPO_DATABASE <<<"$listing") == -unset- ]] || run carlos env unset "${carlos_app[@]}" EQUIPO_DATABASE |
| 259 | else |
| 260 | run carlos env unset "${carlos_app[@]}" EQUIPO_DATABASE |
| 261 | fi |
| 262 | echo " the instance may restart for this; then it serves $version and names $old_id again" |
| 263 | wait_serving "$version" |
| 264 | if ((execute)); then identity; fi |
| 265 | finish unset |
| 266 | fi |
| 267 | |
| 268 | step "8 install the staged runner beside the old one" |
| 269 | if ! done_step runner; then |
| 270 | if ((execute)); then |
| 271 | production_unit |
| 272 | # Recorded once, before any edit: a retry never takes edited files for the originals. |
| 273 | if [[ $(get finish_snapshot) != 1 ]]; then |
| 274 | install -d -m 0700 "$snap" |
| 275 | cp -p "$RUNNER_ROOT/service.env" "$RUNNER_ROOT/equipo-runner.service" "$snap/" |
| 276 | put finish_snapshot 1 |
| 277 | fi |
| 278 | fi |
| 279 | echo " $UNIT is $RUNNER_ROOT/equipo-runner.service with state $RUNNER_ROOT/state; its files recorded once in $snap" |
| 280 | run install -d -m 0700 "$new" |
| 281 | run install -m 0700 "$runner_release/equipo-runner" "$runner_release/equipo-tool-proxy" "$runner_release/gitleaks" "$new/" |
| 282 | # The page renderer beside it, unpacked by the staged runner itself and |
| 283 | # held to the pin linked into it; verify-installed then renders a page. |
| 284 | run "$new/equipo-runner" install-renderer "$runner_release/renderer.tar.gz" |
| 285 | run python3 "$REPO/scripts/runner-config.py" render "$snap" "$RUNNER_ROOT" "${config[@]}" |
| 286 | if ((execute)); then move_state; else |
| 287 | run mv "$RUNNER_ROOT/state" "$kept" |
| 288 | run install -d -m 0700 "$RUNNER_ROOT/state" |
| 289 | fi |
| 290 | run systemctl --user daemon-reload |
| 291 | finish runner |
| 292 | fi |
| 293 | |
| 294 | step "9 enrol the runner: open the printed link as the owner and approve it" |
| 295 | if ! done_step enrol; then |
| 296 | run python3 "$REPO/scripts/runner-config.py" check "$RUNNER_ROOT" "${config[@]}" |
| 297 | echo " the loaded unit starts $new/equipo-runner (systemctl show -p ExecStart)" |
| 298 | if ((execute)); then |
| 299 | production_unit |
| 300 | [[ $(systemctl --user show -p ExecStart --value "$UNIT") == *"path=$new/equipo-runner ;"* ]] || die "the loaded $UNIT does not start $new/equipo-runner" |
| 301 | fi |
| 302 | run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$new/equipo-runner' verify-installed" |
| 303 | run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$new/equipo-runner' enrol" |
| 304 | # Recorded before the start: a running runner holds the state lock, so |
| 305 | # enrol must never run again once the unit is up. |
| 306 | finish enrol |
| 307 | fi |
| 308 | |
| 309 | step "10 start the runner" |
| 310 | if ! done_step start; then |
| 311 | run systemctl --user start "$UNIT" |
| 312 | finish start |
| 313 | fi |
| 314 | |
| 315 | step "11 verify (no replica)" |
| 316 | run curl -fsS "$ORIGIN/api/version" |
| 317 | echo " install.sh names $old_id again; sha256 of $PROC/<MainPID>/exe is the staged runner's" |
| 318 | echo " $ journalctl --user -b _SYSTEMD_INVOCATION_ID=<InvocationID> _PID=<MainPID> -o cat --no-pager" |
| 319 | echo " the last stream line is msg=welcome (no later \"runner stream\" drop), on two readings of the same process" |
| 320 | if ((execute)) && ! done_step verify; then |
| 321 | [[ $(curl -fsS "$ORIGIN/api/version") == "$version" ]] || die "production does not serve $version" |
| 322 | identity |
| 323 | connected "$(staged_sha runner)" || die "the runner is not connected to the instance now; see journalctl --user -u $UNIT" |
| 324 | finish verify |
| 325 | fi |
| 326 | echo " Then, as the owner: sign in at $PUBLIC_ORIGIN/conversation, check the offline line" |
| 327 | echo " is gone, say hello and see the reply stream. If Machines lists the old runner, remove it." |
| 328 | echo " To undo the runner change: scripts/cutover-finish-thin.sh rollback --execute" |
| 329 | ((execute)) || echo $'\nPlan only. A person runs it with --execute.' |
| 330 | |