#!/usr/bin/env bash # Finish the thin v1 production cutover on the database production kept. # # scripts/cutover-finish-thin.sh # print the plan only (default) # scripts/cutover-finish-thin.sh --execute # a person runs it, step by step # scripts/cutover-finish-thin.sh rollback [--execute] # the runner as the cutover left it # # The 2026-11-19 cutover (scripts/cutover-thin.sh) stopped after step 5, # and its fresh database never happened: on CARLOS the activator passes the # app an absolute --db, so EQUIPO_DATABASE is ignored, and the new version # migrated the old database in place. That data is kept. This finishes # cutover steps 6 onward for it without the platform replica: every check # reads the instance or this machine. Agents must not run --execute. Steps # done are recorded in cutover.state (done_identity ... done_verify), so a # finished run is a finished cutover to scripts/upgrade-thin.sh. Every step # can be run again after a failure. # # 6 identity /install.sh, which the instance builds from its own database # (its team), names the recorded old instance id, the version # and the public origin, on both routes; staged runner bytes # 7 config EQUIPO_DATABASE unset, so the configuration says what runs; # then the instance serves that version and the same id again # 8 runner the runner's files recorded once; the staged runner (and # its page renderer, install-renderer) beside the old one; # service.env and unit written from that record # (scripts/runner-config.py); the old state moved aside # 9 enrol the written and loaded configuration checked, then # verify-installed, enrol (the owner approves the link) # 10 start the unit starts (recorded apart from enrol: the running # runner holds the state lock enrol needs) # 11 verify version and identity again; in this boot and this unit # invocation, the staged runner's process was welcomed by the # instance and has not dropped since, on two readings # # Rollback puts the runner's state, service.env and unit back as the # cutover left them and leaves the runner stopped. It never offers the # version before the cutover: that version cannot read the migrated schema. set -euo pipefail APP=equipo ACCOUNT=oficina CONSOLE=https://console.carloku.com HOST=equipo.bds.oncarlos.com ORIGIN=https://$HOST PUBLIC_ORIGIN=${EQUIPO_PUBLIC_ORIGIN:-https://equipo.oficina.works} UNIT=equipo-runner.service RUNNER_ROOT=$HOME/.local/share/equipo-runner BACKUPS=$HOME/.local/share/equipo-cutover STATE=$BACKUPS/cutover.state REPO=$(cd "$(dirname "$0")/.." && pwd) RELEASES=${EQUIPO_RELEASE_REPO:-$REPO}/releases PROC=${CUTOVER_PROC:-/proc} carlos_app=(--app "$APP" --account "$ACCOUNT" --console "$CONSOLE") mode=finish execute=0 case "${1:-} ${2:-}" in " ") ;; "--execute ") execute=1 ;; "rollback ") mode=rollback ;; "rollback --execute") mode=rollback execute=1 ;; *) sed -n '2,36p' "$0"; exit 2 ;; esac die() { echo "refused: $*" >&2; exit 1; } # A refusal read from the records stops --execute; the plan notes it and goes on. refuse() { if ((execute)); then die "$*"; fi echo " ! would refuse: $*" } step() { printf '\n== %s\n' "$1" if ((execute)) && [[ ${CUTOVER_YES:-} != 1 ]]; then read -r -p "Run this step? Type yes: " answer [[ $answer == yes ]] || { echo "stopped before: $1"; exit 1; } fi } run() { printf ' $ %s\n' "$*" if ((execute)); then "$@"; fi } # Recorded facts and finished steps, one KEY=value per line; the last wins. get() { [[ -f $STATE ]] && sed -n "s/^$1=//p" "$STATE" | tail -1 || true; } put() { ((execute)) || return 0 printf '%s=%s\n' "$1" "$2" >>"$STATE" || die "could not record $1=$2 in $STATE" } done_step() { [[ $(get "done_$1") == 1 ]]; } finish() { put "done_$1" 1; } env_value() { awk -v k="$1" '$1 == k { print $2; found = 1 } END { if (!found) print "-unset-" }'; } file_value() { sed -n "s/^$1=\"\{0,1\}\([^\"]*\)\"\{0,1\}$/\1/p" "$2"; } wait_serving() { run bash -c "for i in \$(seq 60); do [ \"\$(curl -fsS $ORIGIN/api/version)\" = $1 ] && exit 0; sleep 2; done; exit 1"; } version=$(get version) old_id=$(get old_instance_id) stamp=$(get stamp) snap=$BACKUPS/finish-$stamp kept=$RUNNER_ROOT/state.pre-thin-$stamp # The instance's own word, not the replica's: /install.sh ends with the # origin, version and instance id (its team's id, read from the database # it serves), on the platform route and on the public address. identity() { local origin said for origin in "$ORIGIN" "$PUBLIC_ORIGIN"; do said=$(curl -fsS "$origin/install.sh") || die "could not read $origin/install.sh" [[ $said == *"sh -s -- '$PUBLIC_ORIGIN' '$version' '$old_id'" ]] || die "$origin/install.sh does not name $PUBLIC_ORIGIN, $version and instance $old_id (it says: ${said##*sh -s -- })" done } # The runner scans every preview with the gitleaks beside its own # executable, held to the hash linked into the runner; without it every # preview fails (preview_scanner). $1 is a runner release directory: its # gitleaks runs, and its sha256 is the one linked into its equipo-runner. scanner_linked() { local sum [[ -f $1/gitleaks && ! -L $1/gitleaks && -x $1/gitleaks ]] || return 1 sum=$(sha256sum "$1/gitleaks" | cut -d ' ' -f 1) || return 1 grep -qaF "$sum" "$1/equipo-runner" } scanner() { echo " $1/gitleaks: executable, and its sha256 is linked into $1/equipo-runner" if ((execute)) && ! scanner_linked "$1"; then die "$1/gitleaks is missing, cannot run, or is not the scanner equipo-runner was built with" fi } # The runner renders pages with the renderer tree beside its own # executable, held to the pin linked into the runner (the sha256 of the # tree's MANIFEST). $1 is a runner release directory: the MANIFEST of its # renderer.tar.gz has the sha256 linked into its equipo-runner. renderer_linked() { local sum [[ -f $1/renderer.tar.gz && ! -L $1/renderer.tar.gz ]] || return 1 sum=$(tar -xzOf "$1/renderer.tar.gz" MANIFEST | sha256sum | cut -d ' ' -f 1) || return 1 grep -qaF "$sum" "$1/equipo-runner" } renderer() { echo " $1/renderer.tar.gz: its MANIFEST's sha256 is linked into $1/equipo-runner" if ((execute)) && ! renderer_linked "$1"; then die "$1/renderer.tar.gz is missing, or is not the renderer equipo-runner was built with" fi } # A hash from the green staging record of $version (empty if there is none). 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; } # The production unit and its state directory, not some other runner's. production_unit() { [[ $(readlink -f "$(systemctl --user show -p FragmentPath --value "$UNIT")") == "$RUNNER_ROOT/equipo-runner.service" ]] || die "$UNIT is not $RUNNER_ROOT/equipo-runner.service" [[ $(file_value EQUIPO_RUNNER_STATE_DIR "$RUNNER_ROOT/service.env") == "$RUNNER_ROOT/state" ]] || die "the runner state is not $RUNNER_ROOT/state" } # Connected now: in this boot and the unit's current invocation, the # process running the binary with sha256 $1 logged the instance's welcome, # and no stream drop after it, on two readings in a row of the same # invocation and process. connected() { local seen= inv pid last for _ in $(seq "${CUTOVER_TRIES:-30}"); do inv=$(systemctl --user show -p InvocationID --value "$UNIT") pid=$(systemctl --user show -p MainPID --value "$UNIT") last= if [[ $inv =~ ^[0-9a-f]{32}$ && $pid =~ ^[1-9][0-9]*$ ]]; then [[ $(sha256sum "$PROC/$pid/exe" | cut -d ' ' -f 1) == "$1" ]] || die "the running runner is not the staged $version runner" last=$(journalctl --user -b _SYSTEMD_INVOCATION_ID="$inv" _PID="$pid" -o cat --no-pager | grep -E 'msg=welcome|msg="runner stream' | tail -1 || true) fi if [[ $last == *msg=welcome* ]]; then [[ $seen == "$inv $pid" ]] && return 0 seen="$inv $pid" else seen= fi sleep "${CUTOVER_WAIT:-5}" done return 1 } # The old state was written by the old runner for the old origin; it is # kept for rollback. Each part is recorded, so a retry finds where it was. move_state() { if [[ $(get state_moved) != 1 ]]; then if [[ -e $kept && -e $RUNNER_ROOT/state ]]; then die "both state and state.pre-thin-$stamp exist and no move is recorded: look before going on" fi if [[ ! -e $kept ]]; then [[ -d $RUNNER_ROOT/state ]] || die "no runner state to keep" mv "$RUNNER_ROOT/state" "$kept" fi put state_moved 1 fi [[ -d $kept ]] || die "state.pre-thin-$stamp is missing" install -d -m 0700 "$RUNNER_ROOT/state" } if [[ $mode == rollback ]]; then [[ $(get kept_database) == 1 ]] || die "no finish of the cutover on the kept database is recorded ($STATE)" [[ -f $snap/service.env && -f $snap/equipo-runner.service ]] || die "the runner files from before the finish were not recorded" step "rollback 1 stop the runner" run systemctl --user stop "$UNIT" step "rollback 2 the runner's state, service.env and unit as the cutover left them" if [[ -e $kept ]]; then if [[ -e $RUNNER_ROOT/state ]]; then run mv "$RUNNER_ROOT/state" "$RUNNER_ROOT/state.finish-rolled-back-$(date -u +%Y%m%dT%H%M%SZ)" fi run mv "$kept" "$RUNNER_ROOT/state" fi run cp -p "$snap/service.env" "$RUNNER_ROOT/service.env" run cp -p "$snap/equipo-runner.service" "$RUNNER_ROOT/equipo-runner.service" run systemctl --user daemon-reload if ((execute)); then systemctl --user is-active --quiet "$UNIT" && die "$UNIT is still running" 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" [[ -d $RUNNER_ROOT/state && ! -e $kept ]] || die "the old runner state is not back in place" fi for s in identity unset runner enrol start verify; do put "done_$s" 0; done put state_moved 0 put kept_database 0 echo " The runner stays stopped and the instance stays on $version: nothing is promoted." echo " $(get prev_version) is not offered: $version migrated the kept database in place, and" echo " $(get prev_version) cannot read the migrated schema. EQUIPO_DATABASE is not set again: it was never used." echo " To finish again: scripts/cutover-finish-thin.sh --execute" ((execute)) || echo $'\nPlan only. A person runs it with --execute.' exit 0 fi [[ -f $STATE ]] || refuse "no cutover was recorded ($STATE)" [[ $(get rolled_back) != 1 ]] || refuse "the cutover was rolled back" done_step deploy || refuse "the cutover has not deployed its version yet; use scripts/cutover-thin.sh" if done_step verify; then die "the cutover to $version is complete"; fi [[ $old_id =~ ^[A-Z0-9]{26}$ ]] || refuse "cutover.state records no old instance id" runner_release=$RELEASES/equipo-runner-linux-amd64-$version new=$RUNNER_ROOT/releases/$version config=(--exec "$new/equipo-runner" "EQUIPO_TOOL_PROXY_PATH=$new/equipo-tool-proxy" "EQUIPO_INSTANCE_ID=$old_id" "EQUIPO_ORIGIN=$PUBLIC_ORIGIN") echo "Finish the cutover to ${version:--unknown-} on the kept database, instance ${old_id:--unknown-}." step "6 identity: the instance names the kept database's id (no replica)" run curl -fsS "$ORIGIN/api/version" echo " $ORIGIN/install.sh and $PUBLIC_ORIGIN/install.sh must end: sh -s -- '$PUBLIC_ORIGIN' '$version' '$old_id'" echo " the staged runner, tool proxy and gitleaks must have the hashes in $RELEASES/staged/$version.json" scanner "$runner_release" renderer "$runner_release" if ((execute)) && ! done_step identity; then [[ $(curl -fsS "$ORIGIN/api/version") == "$version" ]] || die "production does not serve $version" identity [[ $(sha256sum "$runner_release/equipo-runner" | cut -d ' ' -f 1) == "$(staged_sha runner)" ]] || die "equipo-runner is not the one staging passed for $version" [[ $(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" [[ $(sha256sum "$runner_release/gitleaks" | cut -d ' ' -f 1) == "$(staged_sha gitleaks)" ]] || die "gitleaks is not the one staging passed for $version" put new_instance_id "$old_id" put kept_database 1 finish identity fi step "7 config: unset EQUIPO_DATABASE (the activator's --db wins, so it was never used)" if ! done_step unset; then if ((execute)); then listing=$(carlos env list "${carlos_app[@]}") || die "could not read the instance configuration" grep -q '^KEY' <<<"$listing" || die "unexpected configuration listing" [[ $(env_value EQUIPO_DATABASE <<<"$listing") == -unset- ]] || run carlos env unset "${carlos_app[@]}" EQUIPO_DATABASE else run carlos env unset "${carlos_app[@]}" EQUIPO_DATABASE fi echo " the instance may restart for this; then it serves $version and names $old_id again" wait_serving "$version" if ((execute)); then identity; fi finish unset fi step "8 install the staged runner beside the old one" if ! done_step runner; then if ((execute)); then production_unit # Recorded once, before any edit: a retry never takes edited files for the originals. if [[ $(get finish_snapshot) != 1 ]]; then install -d -m 0700 "$snap" cp -p "$RUNNER_ROOT/service.env" "$RUNNER_ROOT/equipo-runner.service" "$snap/" put finish_snapshot 1 fi fi echo " $UNIT is $RUNNER_ROOT/equipo-runner.service with state $RUNNER_ROOT/state; its files recorded once in $snap" run install -d -m 0700 "$new" run install -m 0700 "$runner_release/equipo-runner" "$runner_release/equipo-tool-proxy" "$runner_release/gitleaks" "$new/" # The page renderer beside it, unpacked by the staged runner itself and # held to the pin linked into it; verify-installed then renders a page. run "$new/equipo-runner" install-renderer "$runner_release/renderer.tar.gz" run python3 "$REPO/scripts/runner-config.py" render "$snap" "$RUNNER_ROOT" "${config[@]}" if ((execute)); then move_state; else run mv "$RUNNER_ROOT/state" "$kept" run install -d -m 0700 "$RUNNER_ROOT/state" fi run systemctl --user daemon-reload finish runner fi step "9 enrol the runner: open the printed link as the owner and approve it" if ! done_step enrol; then run python3 "$REPO/scripts/runner-config.py" check "$RUNNER_ROOT" "${config[@]}" echo " the loaded unit starts $new/equipo-runner (systemctl show -p ExecStart)" if ((execute)); then production_unit [[ $(systemctl --user show -p ExecStart --value "$UNIT") == *"path=$new/equipo-runner ;"* ]] || die "the loaded $UNIT does not start $new/equipo-runner" fi run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$new/equipo-runner' verify-installed" run bash -c "set -a; . '$RUNNER_ROOT/service.env'; set +a; '$new/equipo-runner' enrol" # Recorded before the start: a running runner holds the state lock, so # enrol must never run again once the unit is up. finish enrol fi step "10 start the runner" if ! done_step start; then run systemctl --user start "$UNIT" finish start fi step "11 verify (no replica)" run curl -fsS "$ORIGIN/api/version" echo " install.sh names $old_id again; sha256 of $PROC//exe is the staged runner's" echo " $ journalctl --user -b _SYSTEMD_INVOCATION_ID= _PID= -o cat --no-pager" echo " the last stream line is msg=welcome (no later \"runner stream\" drop), on two readings of the same process" if ((execute)) && ! done_step verify; then [[ $(curl -fsS "$ORIGIN/api/version") == "$version" ]] || die "production does not serve $version" identity connected "$(staged_sha runner)" || die "the runner is not connected to the instance now; see journalctl --user -u $UNIT" finish verify fi echo " Then, as the owner: sign in at $PUBLIC_ORIGIN/conversation, check the offline line" echo " is gone, say hello and see the reply stream. If Machines lists the old runner, remove it." echo " To undo the runner change: scripts/cutover-finish-thin.sh rollback --execute" ((execute)) || echo $'\nPlan only. A person runs it with --execute.'