-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredeploy.sh
More file actions
1356 lines (1289 loc) · 70.5 KB
/
Copy pathredeploy.sh
File metadata and controls
1356 lines (1289 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ═════════════════════════════════════════════════════════════════════════════
# redeploy.sh — ONE-COMMAND, idempotent (re)deploy of the Archon Autopilot
# backend onto an Alibaba Cloud ECS host with an existing pgvector service.
#
# WHY THIS SHAPE (see deploy/DEPLOY_STATE.md):
# • The MemoryAgent already runs on the box via docker compose (backend on host
# port 9000 + a `pgvector/pgvector:pg16` container on a compose network).
# • The Autopilot must NOT start a second Postgres and must NOT take port 9000.
# So this script:
# - serves the Autopilot on host port 9100 (container 9000 → host 9100),
# - JOINS the MemoryAgent's internal data network for pgvector DNS plus
# its edge network for outbound Qwen/DashScope traffic,
# - isolates its data in a SEPARATE Postgres database and dedicated
# `autopilot_app` runtime role
# (its own agent_memory + ap_workitems tables), so it never collides with
# the MemoryAgent's `agent_memory` in the default `postgres` database.
# It runs the backend with `docker run` (NOT compose) so the repo's
# docker-compose.yml stays a clean, self-contained LOCAL-DEV stack.
#
# Order is fail-closed: create+migrate the `autopilot` DB FIRST, then serve the
# new image, then prove it with a real /intake + /pending round-trip.
#
# RUN IT — on the box, in the repository:
# ssh -i <key.pem> <deployer>@<ecs-host>
# cd <autopilot-checkout>
# git fetch origin main && git switch main && git merge --ff-only origin/main
# sudo bash deploy/bootstrap-buildx.sh
# sudo DOCKER_CONFIG="$PWD/.artifacts/docker-config" \
# EXPECTED_RELEASE=<exact-40-character-final-main-sha> bash deploy/redeploy.sh
#
# Production requires a .env with DASHSCOPE_API_KEY and REVIEWER_TOKEN next to
# the repository. Missing real-Qwen or reviewer credentials fail closed.
#
# FLAGS:
# --no-smoke skip the /intake + /pending smoke (health-only). Not recommended.
# -h|--help show this help.
#
# CONFIG (env-overridable):
# EXPECTED_RELEASE (required exact 40-character lowercase Git commit)
# APP_DIR (repository root) · IMAGE (archon-qwen-autopilot:latest)
# CONTAINER (archon-autopilot) · HOST_PORT (9100) · CONTAINER_PORT (9000)
# DATA_NETWORK / EDGE_NETWORK (auto-detected MemoryAgent compose networks)
# MIGRATION_ENV_FILE (.env.migration, admin DSN + app-role password; never runtime)
# BASE_URL (http://127.0.0.1:9100; display only) · PUBLIC_BASE_URL (optional)
# LEDGER_HOST_DIR (/var/lib/archon-autopilot/ledger)
# LEDGER_CONTAINER_PATH (/var/lib/archon-ledger/ledger.jsonl)
# DOCKER_CONTROL_TIMEOUT_SECONDS (45) · DOCKER_JOB_TIMEOUT_SECONDS (900)
# ═════════════════════════════════════════════════════════════════════════════
set -euo pipefail
# Never let an inherited/caller-supplied `bash -x` serialize credentials or
# private env-file contents into Cloud Assistant / CI logs.
set +x
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
APP_DIR="${APP_DIR:-$SCRIPT_ROOT}"
IMAGE="${IMAGE:-archon-qwen-autopilot:latest}"
CONTAINER="${CONTAINER:-archon-autopilot}"
HOST_PORT="${HOST_PORT:-9100}"
CONTAINER_PORT="${CONTAINER_PORT:-9000}"
MIGRATION_ENV_FILE="${MIGRATION_ENV_FILE:-.env.migration}"
BASE_URL="${BASE_URL:-http://127.0.0.1:${HOST_PORT}}"
PUBLIC_BASE_URL="${PUBLIC_BASE_URL:-}"
LEDGER_HOST_DIR="${LEDGER_HOST_DIR:-/var/lib/archon-autopilot/ledger}"
LEDGER_CONTAINER_PATH="${LEDGER_CONTAINER_PATH:-/var/lib/archon-ledger/ledger.jsonl}"
GLOBAL_RELEASE_LOCK_FILE="${GLOBAL_RELEASE_LOCK_FILE:-/run/lock/archon-autopilot-redeploy.lock}"
RELEASE_GATE_ROOT="${RELEASE_GATE_ROOT:-/var/lib/archon-autopilot/release-gates}"
GATE_CONTAINER_DIR="/run/archon-release-gate"
DOCKER_CONTROL_TIMEOUT_SECONDS="${DOCKER_CONTROL_TIMEOUT_SECONDS:-45}"
DOCKER_JOB_TIMEOUT_SECONDS="${DOCKER_JOB_TIMEOUT_SECONDS:-900}"
DO_SMOKE=1
BACKUP_CONTAINER="${CONTAINER}-rollback"
STAGING_CONTAINER="${CONTAINER}-candidate"
BOOTSTRAP_CONTAINER="${CONTAINER}-bootstrap"
SMOKE_CLEANUP_CONTAINER="${CONTAINER}-smoke-cleanup"
ENDPOINT_VERIFY_CONTAINER="${CONTAINER}-endpoint-verify"
HAD_PREVIOUS_CONTAINER=0
BACKUP_PRESERVED=0
REPLACEMENT_ACTIVE=0
RELEASE_COMPLETE=0
ROLLBACK_IN_PROGRESS=0
PREVIOUS_CONTAINER_ID=""
PREVIOUS_IMAGE_ID=""
CANDIDATE_CONTAINER_ID=""
STAGING_CONTAINER_ID=""
BUILT_IMAGE_ID=""
IID_FILE=""
BUILD_CONTEXT=""
DATABASE_ENV_FILE=""
RUNTIME_SOURCE_ENV_FILE=""
RUNTIME_BASE_ENV_FILE=""
RUNTIME_OVERRIDE_ENV_FILE=""
GATE_ENV_FILE=""
ENDPOINT_ENV_FILE=""
GATE_HOST_DIR=""
GATE_TOKEN=""
RUNTIME_ATTESTATION=""
GATE_CLOSED=0
SMOKE_VENDOR=""
SMOKE_ID=""
SMOKE_CLEANUP_REQUIRED=0
RUNTIME_BASE_KEYS=()
RUNTIME_OVERRIDE_KEYS=(
DASHSCOPE_API_KEY
REVIEWER_TOKEN
DASHSCOPE_BASE_URL
DATABASE_URL
PORT
LEDGER_JSONL_PATH
TRUST_PROXY_HOPS
TRUST_PROXY_ADDRESSES
DEPLOYMENT_CONFIG_ATTESTATION
)
env_path_value() {
local path="$1" name="$2"
sed -n "s/^${name}=//p" "$path" 2>/dev/null | tail -1 | tr -d '\r'
}
env_file_value() {
env_path_value .env "$1"
}
runtime_override_key() {
local candidate="$1" runtime_key
for runtime_key in "${RUNTIME_OVERRIDE_KEYS[@]}"; do
[ "$candidate" = "$runtime_key" ] && return 0
done
return 1
}
materialize_runtime_base_env() {
local source="$1" destination="$2" line key
: >"$destination" || return 1
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
''|'#'*) printf '%s\n' "$line" >>"$destination" || return 1 ;;
*)
[[ "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)= ]] || return 1
key="${BASH_REMATCH[1]}"
runtime_override_key "$key" && continue
printf '%s\n' "$line" >>"$destination" || return 1
;;
esac
done <"$source"
}
docker_with_timeout() {
local seconds="$1"
shift
timeout --kill-after=5s "${seconds}s" docker "$@"
}
docker_control() {
docker_with_timeout "$DOCKER_CONTROL_TIMEOUT_SECONDS" "$@"
}
docker_job() {
docker_with_timeout "$DOCKER_JOB_TIMEOUT_SECONDS" "$@"
}
docker_inspect_value() {
local format="$1"
local target="$2"
local attempt output
for attempt in 1 2 3; do
if output="$(docker_control inspect --format "$format" "$target" 2>/dev/null)"; then
printf '%s' "$output"
return 0
fi
[ "$attempt" -eq 3 ] || sleep 1
done
return 1
}
docker_container_inventory() {
local attempt output
for attempt in 1 2 3; do
if output="$(docker_control container ls -a --format '{{.Names}}' 2>/dev/null)"; then
printf '%s' "$output"
return 0
fi
[ "$attempt" -eq 3 ] || sleep 1
done
return 1
}
container_name_present() {
local inventory="$1"
local expected="$2"
local existing
while IFS= read -r existing; do
[ "$existing" = "$expected" ] && return 0
done <<<"$inventory"
return 1
}
valid_container_id() {
[[ "$1" =~ ^[0-9a-f]{64}$ ]]
}
valid_image_id() {
[[ "$1" =~ ^sha256:[0-9a-f]{64}$ ]]
}
random_hex() {
local bytes="$1" value
value="$(od -An -N"$bytes" -tx1 /dev/urandom 2>/dev/null | tr -d ' \r\n')" || return 1
[[ "$value" =~ ^[0-9a-f]+$ ]] && [ "${#value}" -eq $((bytes * 2)) ] || return 1
printf '%s' "$value"
}
container_absent() {
local expected="$1" inventory
inventory="$(docker_container_inventory)" || return 1
! container_name_present "$inventory" "$expected"
}
remove_container_reconciled() {
local container_id="$1" expected_name="$2"
docker_control rm -f "$container_id" >/dev/null 2>&1 || true
container_absent "$expected_name"
}
# Run a detached, named one-shot container and bound the actual job, not merely
# the attached Docker client. A timeout is reconciled by immutable ID; a running
# job is force-removed and can never keep mutating state after this function fails.
run_named_job() {
local name="$1" seconds="$2"
shift 2
local output="" run_status=0 job_id="" wait_output="" wait_status=0
local running="" exit_code="" job_image="" job_restart=""
container_absent "$name" || return 1
set +e
output="$(docker_control run -d --name "$name" --restart no "$@" 2>/dev/null)"
run_status=$?
set -e
if [ "$run_status" -eq 0 ] && valid_container_id "$output"; then
job_id="$output"
else
job_id="$(docker_inspect_value '{{.Id}}' "$name" || true)"
fi
if ! valid_container_id "$job_id"; then
docker_control rm -f "$name" >/dev/null 2>&1 || true
return 1
fi
job_image="$(docker_inspect_value '{{.Image}}' "$job_id" || true)"
job_restart="$(docker_inspect_value '{{.HostConfig.RestartPolicy.Name}}' "$job_id" || true)"
if [ "$job_image" != "$BUILT_IMAGE_ID" ] || [ "$job_restart" != "no" ]; then
remove_container_reconciled "$job_id" "$name" || true
return 1
fi
set +e
wait_output="$(docker_with_timeout "$seconds" wait "$job_id" 2>/dev/null)"
wait_status=$?
set -e
if [ "$wait_status" -eq 0 ]; then
exit_code="$(printf '%s' "$wait_output" | tr -d '\r\n')"
else
running="$(docker_inspect_value '{{.State.Running}}' "$job_id" || true)"
if [ "$running" = "false" ]; then
exit_code="$(docker_inspect_value '{{.State.ExitCode}}' "$job_id" || true)"
else
remove_container_reconciled "$job_id" "$name" || true
return 1
fi
fi
[[ "$exit_code" =~ ^[0-9]+$ ]] || { remove_container_reconciled "$job_id" "$name" || true; return 1; }
remove_container_reconciled "$job_id" "$name" || return 1
[ "$exit_code" -eq 0 ]
}
container_env_value() {
local container_id="$1"
local key="$2"
local environment matches count
environment="$(docker_inspect_value '{{range .Config.Env}}{{println .}}{{end}}' "$container_id")" \
|| return 1
matches="$(printf '%s\n' "$environment" | sed -n "s/^${key}=//p")"
count="$(printf '%s\n' "$environment" | grep -c "^${key}=" || true)"
[ "$count" = "1" ] || return 1
printf '%s' "$matches"
}
verify_runtime_contract() {
local container_id="$1" phase="$2" expected_restart="$3"
local expected_name gate_dir gate_token port_binding port_count mount_count
local networks edge_priority ledger_mount gate_mount security_options cap_drop
[ "$phase" = "staging" ] || [ "$phase" = "final" ] || return 1
expected_name="$STAGING_CONTAINER"
gate_dir="$GATE_CONTAINER_DIR"
gate_token="$GATE_TOKEN"
[ "$phase" = "staging" ] || expected_name="$CONTAINER"
[ "$(docker_inspect_value '{{.Id}}' "$container_id" || true)" = "$container_id" ] || return 1
[ "$(docker_inspect_value '{{.Name}}' "$container_id" || true)" = "/$expected_name" ] || return 1
[ "$(docker_inspect_value '{{.Image}}' "$container_id" || true)" = "$BUILT_IMAGE_ID" ] || return 1
[ "$(docker_inspect_value '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$container_id" || true)" = "$EXPECTED_RELEASE" ] || return 1
[ "$(docker_inspect_value '{{.State.Running}}' "$container_id" || true)" = "true" ] || return 1
[ "$(docker_inspect_value '{{.HostConfig.RestartPolicy.Name}}' "$container_id" || true)" = "$expected_restart" ] || return 1
[ "$(docker_inspect_value '{{.HostConfig.ReadonlyRootfs}}' "$container_id" || true)" = "true" ] || return 1
[ "$(docker_inspect_value '{{.HostConfig.Memory}}' "$container_id" || true)" = "536870912" ] || return 1
[ "$(docker_inspect_value '{{.HostConfig.NanoCpus}}' "$container_id" || true)" = "1000000000" ] || return 1
[ "$(docker_inspect_value '{{.HostConfig.PidsLimit}}' "$container_id" || true)" = "128" ] || return 1
security_options="$(docker_inspect_value '{{range .HostConfig.SecurityOpt}}{{println .}}{{end}}' "$container_id" || true)"
[ "$security_options" = "no-new-privileges:true" ] || [ "$security_options" = "no-new-privileges" ] || return 1
cap_drop="$(docker_inspect_value '{{range .HostConfig.CapDrop}}{{println .}}{{end}}' "$container_id" || true)"
[ "$cap_drop" = "ALL" ] || return 1
networks="$(docker_inspect_value '{{range $name, $_ := .NetworkSettings.Networks}}{{println $name}}{{end}}' "$container_id" || true)"
[ "$(printf '%s\n' "$networks" | grep -c . || true)" = "2" ] || return 1
container_name_present "$networks" "$DATA_NETWORK" || return 1
container_name_present "$networks" "$EDGE_NETWORK" || return 1
edge_priority="$(docker_inspect_value "{{with (index .NetworkSettings.Networks \"$EDGE_NETWORK\")}}{{.GwPriority}}{{end}}" "$container_id" || true)"
[ "$edge_priority" = "1" ] || return 1
[ "$(container_env_value "$container_id" DATABASE_URL || true)" = "$DATABASE_URL" ] || return 1
[ "$(container_env_value "$container_id" DASHSCOPE_API_KEY || true)" = "$DASHSCOPE_API_KEY" ] || return 1
[ "$(container_env_value "$container_id" REVIEWER_TOKEN || true)" = "$REVIEWER_TOKEN" ] || return 1
[ "$(container_env_value "$container_id" DASHSCOPE_BASE_URL || true)" = "$DASHSCOPE_BASE_URL" ] || return 1
[ "$(container_env_value "$container_id" PORT || true)" = "$CONTAINER_PORT" ] || return 1
[ "$(container_env_value "$container_id" LEDGER_JSONL_PATH || true)" = "$LEDGER_CONTAINER_PATH" ] || return 1
[ "$(container_env_value "$container_id" TRUST_PROXY_HOPS || true)" = "1" ] || return 1
[ -z "$(container_env_value "$container_id" TRUST_PROXY_ADDRESSES || true)" ] || return 1
[ "$(container_env_value "$container_id" DEPLOYMENT_GATE_DIR || true)" = "$gate_dir" ] || return 1
[ "$(container_env_value "$container_id" DEPLOYMENT_GATE_TOKEN || true)" = "$gate_token" ] || return 1
[ "$(container_env_value "$container_id" DEPLOYMENT_CONFIG_ATTESTATION || true)" = "$RUNTIME_ATTESTATION" ] || return 1
[ "$(container_env_value "$container_id" NODE_ENV || true)" = "production" ] || return 1
[ "$(container_env_value "$container_id" HOME || true)" = "/tmp" ] || return 1
local runtime_key
for runtime_key in "${RUNTIME_BASE_KEYS[@]}"; do
runtime_override_key "$runtime_key" && continue
[ "$(container_env_value "$container_id" "$runtime_key" || true)" = "$(env_path_value "$RUNTIME_BASE_ENV_FILE" "$runtime_key")" ] \
|| return 1
done
ledger_mount="$(docker_inspect_value '{{range .Mounts}}{{if eq .Destination "/var/lib/archon-ledger"}}{{printf "%s|%t" .Source .RW}}{{end}}{{end}}' "$container_id" || true)"
[ "$ledger_mount" = "$LEDGER_HOST_DIR|true" ] || return 1
mount_count="$(docker_inspect_value '{{len .Mounts}}' "$container_id" || true)"
gate_mount="$(docker_inspect_value "{{range .Mounts}}{{if eq .Destination \"$GATE_CONTAINER_DIR\"}}{{printf \"%s|%t\" .Source .RW}}{{end}}{{end}}" "$container_id" || true)"
port_count="$(docker_inspect_value '{{len .HostConfig.PortBindings}}' "$container_id" || true)"
port_binding="$(docker_inspect_value "{{with (index .HostConfig.PortBindings \"${CONTAINER_PORT}/tcp\")}}{{(index . 0).HostIp}}|{{(index . 0).HostPort}}{{end}}" "$container_id" || true)"
if [ "$phase" = "staging" ]; then
[ "$mount_count" = "2" ] && [ "$gate_mount" = "$GATE_HOST_DIR|false" ] \
&& [ "$port_count" = "0" ] && [ -z "$port_binding" ] || return 1
else
[ "$mount_count" = "2" ] && [ "$gate_mount" = "$GATE_HOST_DIR|false" ] \
&& [ "$port_count" = "1" ] && [ "$port_binding" = "127.0.0.1|$HOST_PORT" ] || return 1
fi
return 0
}
start_runtime_container() {
local phase="$1" name output="" status=0 container_id=""
local -a args=("${RUNTIME_COMMON_ARGS[@]}")
args+=(
--mount "type=bind,src=${GATE_HOST_DIR},dst=${GATE_CONTAINER_DIR},readonly"
--env-file "$GATE_ENV_FILE"
)
if [ "$phase" = "staging" ]; then
name="$STAGING_CONTAINER"
elif [ "$phase" = "final" ]; then
name="$CONTAINER"
args+=( -p "127.0.0.1:${HOST_PORT}:${CONTAINER_PORT}" )
else
return 1
fi
container_absent "$name" || return 1
set +e
output="$(docker_control run -d --name "$name" --restart no "${args[@]}" "$BUILT_IMAGE_ID" 2>/dev/null)"
status=$?
set -e
if [ "$status" -eq 0 ] && valid_container_id "$output"; then
container_id="$output"
else
container_id="$(docker_inspect_value '{{.Id}}' "$name" || true)"
fi
if ! valid_container_id "$container_id"; then
docker_control rm -f "$name" >/dev/null 2>&1 || true
return 1
fi
printf '%s' "$container_id"
}
container_probe() {
local container_id="$1"
local route="$2"
local expected="$3"
local authenticated="${4:-false}"
local seconds="${5:-15}"
local inner_ms=$((seconds * 1000 - 1000))
docker_with_timeout "$seconds" exec \
-e "DEPLOY_PROBE_ROUTE=$route" \
-e "DEPLOY_PROBE_EXPECTED=$expected" \
-e "DEPLOY_PROBE_AUTHENTICATED=$authenticated" \
-e "DEPLOY_PROBE_TIMEOUT_MS=$inner_ms" \
"$container_id" node --input-type=module -e '
const port = process.env.PORT || "9000";
const headers = process.env.DEPLOY_PROBE_AUTHENTICATED === "true"
? { authorization: `Bearer ${process.env.REVIEWER_TOKEN || ""}` } : {};
const response = await fetch(`http://127.0.0.1:${port}${process.env.DEPLOY_PROBE_ROUTE}`, {
headers, signal: AbortSignal.timeout(Number(process.env.DEPLOY_PROBE_TIMEOUT_MS))
});
const body = await response.json().catch(() => ({}));
const expected = process.env.DEPLOY_PROBE_EXPECTED;
const deep = expected === "ready-deep";
const ok = response.ok && body.status === (deep ? "ready" : expected)
&& (!deep || body.qwen?.probed === true);
if (!ok) process.exit(1);
' >/dev/null 2>&1
}
wait_for_probe() {
local container_id="$1"
local route="$2"
local expected="$3"
local authenticated="${4:-false}"
local per_try_seconds="${5:-8}"
local attempts="${6:-20}"
local attempt
for attempt in $(seq 1 "$attempts"); do
if container_probe "$container_id" "$route" "$expected" "$authenticated" "$per_try_seconds"; then
return 0
fi
[ "$attempt" -eq "$attempts" ] || sleep 2
done
return 1
}
cleanup_private_artifacts() {
if [ -n "$IID_FILE" ]; then
rm -f -- "$IID_FILE" >/dev/null 2>&1 || true
IID_FILE=""
fi
if [ -n "$RUNTIME_OVERRIDE_ENV_FILE" ]; then
case "$RUNTIME_OVERRIDE_ENV_FILE" in
"$APP_DIR"/.git/autopilot-runtime-env.*) rm -f -- "$RUNTIME_OVERRIDE_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
RUNTIME_OVERRIDE_ENV_FILE=""
fi
if [ -n "$RUNTIME_BASE_ENV_FILE" ]; then
case "$RUNTIME_BASE_ENV_FILE" in
"$APP_DIR"/.git/autopilot-base-env.*) rm -f -- "$RUNTIME_BASE_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
RUNTIME_BASE_ENV_FILE=""
fi
if [ -n "$RUNTIME_SOURCE_ENV_FILE" ]; then
case "$RUNTIME_SOURCE_ENV_FILE" in
"$APP_DIR"/.git/autopilot-source-env.*) rm -f -- "$RUNTIME_SOURCE_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
RUNTIME_SOURCE_ENV_FILE=""
fi
if [ -n "$DATABASE_ENV_FILE" ]; then
case "$DATABASE_ENV_FILE" in
"$APP_DIR"/.git/autopilot-database-env.*) rm -f -- "$DATABASE_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
DATABASE_ENV_FILE=""
fi
if [ -n "$GATE_ENV_FILE" ]; then
case "$GATE_ENV_FILE" in
"$APP_DIR"/.git/autopilot-gate-env.*) rm -f -- "$GATE_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
GATE_ENV_FILE=""
fi
if [ -n "$ENDPOINT_ENV_FILE" ]; then
case "$ENDPOINT_ENV_FILE" in
"$APP_DIR"/.git/autopilot-endpoint-env.*) rm -f -- "$ENDPOINT_ENV_FILE" >/dev/null 2>&1 || true ;;
esac
ENDPOINT_ENV_FILE=""
fi
if [ -n "$BUILD_CONTEXT" ]; then
case "$BUILD_CONTEXT" in
"$APP_DIR"/.git/autopilot-build-context.*) rm -rf -- "$BUILD_CONTEXT" >/dev/null 2>&1 || true ;;
esac
BUILD_CONTEXT=""
fi
}
close_release_gate() {
local temporary
[ -n "$GATE_HOST_DIR" ] && [ -d "$GATE_HOST_DIR" ] && [ ! -L "$GATE_HOST_DIR" ] || return 1
temporary="$GATE_HOST_DIR/.closed.$$"
printf 'closed\n' >"$temporary" || return 1
chmod 0644 "$temporary" || return 1
mv -f -- "$temporary" "$GATE_HOST_DIR/closed" || return 1
GATE_CLOSED=1
}
open_release_gate() {
[ -n "$GATE_HOST_DIR" ] && [ -d "$GATE_HOST_DIR" ] && [ ! -L "$GATE_HOST_DIR" ] || return 1
[ -f "$GATE_HOST_DIR/contract" ] && [ ! -L "$GATE_HOST_DIR/contract" ] || return 1
printf 'archon-release-gate-v1\n' | cmp -s - "$GATE_HOST_DIR/contract" || return 1
rm -f -- "$GATE_HOST_DIR/closed" || return 1
[ "$(find "$GATE_HOST_DIR" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null)" = "contract" ] || return 1
GATE_CLOSED=0
}
cleanup_release_gate_dir() {
[ -n "$GATE_HOST_DIR" ] || return 0
case "$GATE_HOST_DIR" in
"$RELEASE_GATE_ROOT"/*)
rm -f -- "$GATE_HOST_DIR/closed" "$GATE_HOST_DIR/contract" >/dev/null 2>&1 || true
rmdir -- "$GATE_HOST_DIR" >/dev/null 2>&1 || return 1
GATE_HOST_DIR=""
;;
*) return 1 ;;
esac
}
cleanup_smoke_residue() {
[ "$SMOKE_CLEANUP_REQUIRED" -eq 1 ] || return 0
[[ "$SMOKE_VENDOR" =~ ^__deploy_[0-9a-f_]+$ ]] || return 1
run_named_job "$SMOKE_CLEANUP_CONTAINER" 180 \
--network "$DATA_NETWORK" \
--env-file "$DATABASE_ENV_FILE" \
-e DEPLOY_SMOKE_VENDOR="$SMOKE_VENDOR" \
-e DEPLOY_SMOKE_ID="$SMOKE_ID" \
--read-only --tmpfs /tmp:size=32m,mode=1777 \
--security-opt no-new-privileges:true --cap-drop ALL \
--memory 256m --cpus 0.5 --pids-limit 64 \
"$BUILT_IMAGE_ID" node --input-type=module -e '
import pg from "pg";
const vendor = process.env.DEPLOY_SMOKE_VENDOR || "";
const expectedId = process.env.DEPLOY_SMOKE_ID || "";
if (!/^__deploy_[0-9a-f_]+$/.test(vendor)) throw new Error("invalid cleanup marker");
if (expectedId && !/^[0-9a-f-]{36}$/i.test(expectedId)) throw new Error("invalid cleanup identity");
const client = new pg.Client({ connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 10000, query_timeout: 30000, statement_timeout: 25000 });
await client.connect();
try {
await client.query("BEGIN");
const work = await client.query(
"DELETE FROM ap_workitems WHERE item->\x27invoice\x27->>\x27vendor\x27 = $1 RETURNING id", [vendor]);
const memory = await client.query("DELETE FROM agent_memory WHERE vendor = $1 RETURNING id", [vendor]);
const residual = await client.query(
`SELECT
(SELECT count(*)::int FROM ap_workitems WHERE item->\x27invoice\x27->>\x27vendor\x27 = $1) AS work_count,
(SELECT count(*)::int FROM agent_memory WHERE vendor = $1) AS memory_count`, [vendor]);
const exactWork = expectedId
? work.rowCount === 1 && work.rows[0]?.id === expectedId
: (work.rowCount ?? 0) <= 1;
if (!exactWork || (memory.rowCount ?? 0) > 1
|| residual.rows[0]?.work_count !== 0 || residual.rows[0]?.memory_count !== 0) {
throw new Error("cleanup proof mismatch");
}
await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK").catch(() => {});
throw error;
} finally {
await client.end().catch(() => {});
}
' >/dev/null 2>&1 || return 1
SMOKE_CLEANUP_REQUIRED=0
return 0
}
usage() { sed -n '2,45p' "$0" | sed 's/^# \{0,1\}//'; }
for arg in "$@"; do
case "$arg" in
--no-smoke) DO_SMOKE=0 ;;
-h|--help) usage; exit 0 ;;
*) echo "ERROR: unknown arg '$arg'"; usage; exit 2 ;;
esac
done
log() { printf '\n\033[1m==> %s\033[0m\n' "$*"; }
ok() { printf ' \033[32m✓ %s\033[0m\n' "$*"; }
warn() { printf ' \033[33m! %s\033[0m\n' "$*"; }
die() { printf '\n\033[31mABORT: %s\033[0m\n' "$*" >&2; exit 1; }
rollback_release() {
local exit_code=$?
local restored=0 previous_running="" previous_name="" candidate_id="" staging_id=""
local candidate_quiet=0 staging_quiet=0 cleanup_ok=1
trap - EXIT
trap '' HUP INT TERM
if [ "$exit_code" -eq 0 ] || [ "$REPLACEMENT_ACTIVE" -ne 1 ] || [ "$RELEASE_COMPLETE" -eq 1 ]; then
cleanup_private_artifacts
exit "$exit_code"
fi
if [ "$ROLLBACK_IN_PROGRESS" -eq 1 ]; then
cleanup_private_artifacts
exit "$exit_code"
fi
ROLLBACK_IN_PROGRESS=1
warn "candidate release failed; closing traffic and reconciling by immutable identity"
if [ -n "$GATE_HOST_DIR" ]; then
close_release_gate \
|| warn "release gate could not be re-closed; the candidate must remain unavailable pending manual recovery"
fi
# Staging shares the data/edge networks but is protected by the same closed
# application gate. Reconcile it first so no failed/signal-interrupted probe
# can outlive the release transaction or retain the gate mount.
staging_id="$STAGING_CONTAINER_ID"
if ! valid_container_id "$staging_id"; then
staging_id="$(docker_inspect_value '{{.Id}}' "$STAGING_CONTAINER" || true)"
fi
if valid_container_id "$staging_id"; then
if remove_container_reconciled "$staging_id" "$STAGING_CONTAINER"; then
staging_quiet=1
STAGING_CONTAINER_ID=""
else
warn "staging candidate could not be removed/proved absent; its application gate remains closed"
fi
elif container_absent "$STAGING_CONTAINER"; then
staging_quiet=1
else
warn "staging candidate identity is ambiguous; its application gate remains closed"
fi
if [ -z "$CANDIDATE_CONTAINER_ID" ]; then
candidate_id="$(docker_inspect_value '{{.Id}}' "$CONTAINER" || true)"
if valid_container_id "$candidate_id" && [ "$candidate_id" != "$PREVIOUS_CONTAINER_ID" ]; then
CANDIDATE_CONTAINER_ID="$candidate_id"
fi
fi
if [ -n "$CANDIDATE_CONTAINER_ID" ]; then
docker_control stop --time 5 "$CANDIDATE_CONTAINER_ID" >/dev/null 2>&1 || true
if remove_container_reconciled "$CANDIDATE_CONTAINER_ID" "$CONTAINER"; then
candidate_quiet=1
else
warn "candidate could not be removed/proved absent; old traffic remains fail-closed"
fi
else
candidate_id="$(docker_inspect_value '{{.Id}}' "$CONTAINER" || true)"
if [ "$candidate_id" = "$PREVIOUS_CONTAINER_ID" ] && [ "$HAD_PREVIOUS_CONTAINER" -eq 1 ]; then
candidate_quiet=1
elif container_absent "$CONTAINER"; then
candidate_quiet=1
elif [ "$HAD_PREVIOUS_CONTAINER" -eq 0 ]; then
docker_control rm -f "$CONTAINER" >/dev/null 2>&1 || true
container_absent "$CONTAINER" && candidate_quiet=1
else
warn "production-name container identity is ambiguous; old traffic remains fail-closed"
fi
fi
if [ "$staging_quiet" -eq 1 ] && [ "$candidate_quiet" -eq 1 ] && ! cleanup_smoke_residue; then
cleanup_ok=0
warn "deployment smoke residue could not be transactionally removed and proved zero; old queue remains offline"
fi
if [ "$staging_quiet" -eq 1 ] && [ "$candidate_quiet" -eq 1 ] && [ "$cleanup_ok" -eq 1 ] && [ "$HAD_PREVIOUS_CONTAINER" -eq 1 ]; then
docker_control start "$PREVIOUS_CONTAINER_ID" >/dev/null 2>&1 || true
previous_running="$(docker_inspect_value '{{.State.Running}}' "$PREVIOUS_CONTAINER_ID" || true)"
if [ "$previous_running" = "true" ] \
&& wait_for_probe "$PREVIOUS_CONTAINER_ID" /ready ready false 10 6; then
previous_name="$(docker_inspect_value '{{.Name}}' "$PREVIOUS_CONTAINER_ID" || true)"
if [ "$previous_name" != "/$CONTAINER" ]; then
docker_control rename "$PREVIOUS_CONTAINER_ID" "$CONTAINER" >/dev/null 2>&1 || true
fi
previous_name="$(docker_inspect_value '{{.Name}}' "$PREVIOUS_CONTAINER_ID" || true)"
if [ "$previous_name" = "/$CONTAINER" ]; then
restored=1
ok "pre-release container restored under the production name and DB-ready"
else
warn "pre-release service is DB-ready, but name reconciliation remains manual"
fi
else
warn "automatic restore could not prove the pre-release container DB-ready; manual recovery required"
fi
elif [ "$HAD_PREVIOUS_CONTAINER" -eq 0 ] && [ "$staging_quiet" -eq 1 ] && [ "$candidate_quiet" -eq 1 ] && [ "$cleanup_ok" -eq 1 ]; then
restored=1
warn "failed first-deploy candidate was removed and zero-residual cleanup proved; no old release existed"
fi
if [ "$staging_quiet" -eq 1 ] && [ "$candidate_quiet" -eq 1 ] && [ "$cleanup_ok" -eq 1 ]; then
cleanup_release_gate_dir || warn "closed release-gate directory remains for explicit stale-state cleanup"
fi
[ "$restored" -eq 1 ] || warn "release remains failed and requires operator inspection"
cleanup_private_artifacts
exit "$exit_code"
}
# ── Preflight ─────────────────────────────────────────────────────────────────
log "Preflight"
command -v docker >/dev/null 2>&1 || die "docker not found."
command -v git >/dev/null 2>&1 || die "git not found (needed for exact-release verification)."
command -v flock >/dev/null 2>&1 || die "flock not found (needed to serialize production releases)."
command -v timeout >/dev/null 2>&1 || die "GNU timeout not found (needed to bound Docker operations)."
command -v tar >/dev/null 2>&1 || die "tar not found (needed for immutable Git build context)."
command -v sha256sum >/dev/null 2>&1 || die "sha256sum not found (needed for schema identity)."
command -v cmp >/dev/null 2>&1 || die "cmp not found (needed for byte-exact release-gate attestation)."
command -v od >/dev/null 2>&1 || die "od not found (needed for release nonces)."
command -v find >/dev/null 2>&1 || die "find not found (needed for exact gate-directory attestation)."
command -v realpath >/dev/null 2>&1 || die "realpath not found (needed for canonical host-path attestation)."
command -v stat >/dev/null 2>&1 || die "stat not found (needed for file-identity attestation)."
cd "$APP_DIR" 2>/dev/null || die "app dir '$APP_DIR' not found. Sync code there first (see header)."
APP_DIR="$(pwd -P)" || die "could not resolve the physical app directory."
[ -f Dockerfile ] || die "no Dockerfile in $APP_DIR — is this the autopilot repo?"
[ -d .git ] && [ ! -L .git ] \
|| die "production release requires a normal Git checkout with a real in-project .git directory."
ok "app dir: $APP_DIR"
# The release build is bound to the exact, hash-pinned Buildx artifact installed
# by deploy/bootstrap-buildx.sh. A same-version global/spoofed plugin is not an
# acceptable substitute: both its canonical project location and bytes are part
# of the reviewed release input.
BUILDX_SHA256='d41ece72044243b4f58b343441ae37446d9c29a7d6b5e11c61847bbcf8f7dfda'
BUILDX_ARTIFACT_ROOT="$APP_DIR/.artifacts"
EXPECTED_DOCKER_CONFIG="$BUILDX_ARTIFACT_ROOT/docker-config"
BUILDX_PLUGIN_DIR="$EXPECTED_DOCKER_CONFIG/cli-plugins"
BUILDX_PLUGIN="$BUILDX_PLUGIN_DIR/docker-buildx"
[ -n "${DOCKER_CONFIG:-}" ] \
|| die "DOCKER_CONFIG must select the project-contained hash-pinned Buildx installation."
[ "$(realpath -m -- "$DOCKER_CONFIG")" = "$EXPECTED_DOCKER_CONFIG" ] \
|| die "DOCKER_CONFIG must equal the canonical project-contained .artifacts/docker-config path."
[ -d "$BUILDX_ARTIFACT_ROOT" ] && [ ! -L "$BUILDX_ARTIFACT_ROOT" ] \
|| die "Buildx artifact root must be a non-symlink directory."
IFS=: read -r buildx_root_uid buildx_root_gid buildx_root_mode \
<<<"$(stat -Lc '%u:%g:%a' "$BUILDX_ARTIFACT_ROOT" 2>/dev/null)"
[ "$buildx_root_uid:$buildx_root_gid" = 0:0 ] \
&& [[ "$buildx_root_mode" =~ ^[0-7]{3,4}$ ]] \
&& (( (8#$buildx_root_mode & 0022) == 0 )) \
|| die "Buildx artifact root must be root-owned and not group/world writable."
attest_exact_buildx_artifact() {
local buildx_directory
[ -z "${DOCKER_CLI_PLUGIN_EXTRA_DIRS:-}" ] \
&& [ ! -e "$EXPECTED_DOCKER_CONFIG/config.json" ] && [ ! -L "$EXPECTED_DOCKER_CONFIG/config.json" ] \
&& [ "$(find "$EXPECTED_DOCKER_CONFIG" -mindepth 1 -maxdepth 1 -printf '%f\n')" = cli-plugins ] \
&& [ "$(find "$BUILDX_PLUGIN_DIR" -mindepth 1 -maxdepth 1 -printf '%f\n')" = docker-buildx ] \
|| return 1
for buildx_directory in "$EXPECTED_DOCKER_CONFIG" "$BUILDX_PLUGIN_DIR"; do
[ -d "$buildx_directory" ] && [ ! -L "$buildx_directory" ] \
&& [ "$(realpath -- "$buildx_directory")" = "$buildx_directory" ] \
&& [ "$(stat -Lc '%u:%g:%a' "$buildx_directory" 2>/dev/null)" = 0:0:700 ] \
|| return 1
done
[ -f "$BUILDX_PLUGIN" ] && [ ! -L "$BUILDX_PLUGIN" ] \
&& [ "$(realpath -- "$BUILDX_PLUGIN")" = "$BUILDX_PLUGIN" ] \
&& [ "$(stat -Lc '%u:%g:%a:%h' "$BUILDX_PLUGIN" 2>/dev/null)" = 0:0:755:1 ] \
&& [ "$(sha256sum "$BUILDX_PLUGIN" | awk '{print $1}')" = "$BUILDX_SHA256" ]
}
attest_exact_buildx_artifact \
|| die "Buildx artifact/layout failed canonical path, closed-config, ownership, mode, link-count, or SHA-256 attestation."
BUILDX_VERSION_OUTPUT="$(docker buildx version 2>/dev/null)" \
|| die "Docker Buildx v0.35.0 is required; run deploy/bootstrap-buildx.sh and pass its project-contained DOCKER_CONFIG."
[[ "$BUILDX_VERSION_OUTPUT" =~ ^github\.com/docker/buildx[[:space:]]+v0\.35\.0([[:space:]]|$) ]] \
|| die "Docker Buildx must be exactly v0.35.0; run deploy/bootstrap-buildx.sh and pass its project-contained DOCKER_CONFIG."
attest_exact_buildx_artifact \
|| die "Buildx artifact/layout changed while the exact version was being attested."
ok "Docker Buildx v0.35.0 exact project-contained artifact attested"
[[ "${EXPECTED_RELEASE:-}" =~ ^[0-9a-f]{40}$ ]] \
|| die "EXPECTED_RELEASE must be the exact 40-character lowercase final-main Git commit."
[[ "$CONTAINER" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]] \
|| die "CONTAINER must be a valid single-line Docker object name."
for private_name in "$BACKUP_CONTAINER" "$STAGING_CONTAINER" "$BOOTSTRAP_CONTAINER" "$SMOKE_CLEANUP_CONTAINER" "$ENDPOINT_VERIFY_CONTAINER"; do
[[ "$private_name" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]] \
|| die "derived private container name is invalid."
done
[[ "$HOST_PORT" =~ ^[1-9][0-9]{0,4}$ ]] && [ "$HOST_PORT" -le 65535 ] \
|| die "HOST_PORT must be an integer from 1 to 65535."
[[ "$CONTAINER_PORT" =~ ^[1-9][0-9]{0,4}$ ]] && [ "$CONTAINER_PORT" -le 65535 ] \
|| die "CONTAINER_PORT must be an integer from 1 to 65535."
[[ "$DOCKER_CONTROL_TIMEOUT_SECONDS" =~ ^[1-9][0-9]{0,2}$ ]] \
|| die "DOCKER_CONTROL_TIMEOUT_SECONDS must be an integer from 1 to 999."
[[ "$DOCKER_JOB_TIMEOUT_SECONDS" =~ ^[1-9][0-9]{0,3}$ ]] \
|| die "DOCKER_JOB_TIMEOUT_SECONDS must be an integer from 1 to 9999."
[ "$BASE_URL" = "http://127.0.0.1:${HOST_PORT}" ] \
|| die "BASE_URL must be the exact loopback listener http://127.0.0.1:${HOST_PORT}."
[[ "$GLOBAL_RELEASE_LOCK_FILE" =~ ^/[A-Za-z0-9_./-]+$ ]] \
|| die "GLOBAL_RELEASE_LOCK_FILE must be an absolute canonical host path."
[[ "$RELEASE_GATE_ROOT" =~ ^/[A-Za-z0-9_./-]+$ ]] \
|| die "RELEASE_GATE_ROOT must be an absolute canonical host path."
[ "$(realpath -m -- "$GLOBAL_RELEASE_LOCK_FILE")" = "$GLOBAL_RELEASE_LOCK_FILE" ] \
|| die "GLOBAL_RELEASE_LOCK_FILE must contain no symlink, dot-segment, or duplicate-separator aliases."
[ "$(realpath -m -- "$RELEASE_GATE_ROOT")" = "$RELEASE_GATE_ROOT" ] \
|| die "RELEASE_GATE_ROOT must contain no symlink, dot-segment, or duplicate-separator aliases."
[[ "$LEDGER_HOST_DIR" =~ ^/[A-Za-z0-9_./-]+$ ]] \
&& [ "$(realpath -m -- "$LEDGER_HOST_DIR")" = "$LEDGER_HOST_DIR" ] \
|| die "LEDGER_HOST_DIR must be an absolute canonical non-aliased host path."
[[ "$LEDGER_CONTAINER_PATH" =~ ^/[A-Za-z0-9_./-]+$ ]] \
&& [ "$(realpath -m -- "$LEDGER_CONTAINER_PATH")" = "$LEDGER_CONTAINER_PATH" ] \
|| die "LEDGER_CONTAINER_PATH must be an absolute canonical container path."
[ "$LEDGER_CONTAINER_PATH" = "/var/lib/archon-ledger/ledger.jsonl" ] \
|| die "LEDGER_CONTAINER_PATH must use the attested durable ledger mount path."
[ -d "$(dirname "$GLOBAL_RELEASE_LOCK_FILE")" ] \
|| die "global release-lock parent directory does not exist."
[ ! -L "$GLOBAL_RELEASE_LOCK_FILE" ] \
|| die "global release lock must not be a symlink."
[ ! -e "$GLOBAL_RELEASE_LOCK_FILE" ] || [ -f "$GLOBAL_RELEASE_LOCK_FILE" ] \
|| die "global release lock must be absent or a regular file."
exec 9>"$GLOBAL_RELEASE_LOCK_FILE" || die "could not open the host-global deployment lock."
chown 0:0 "$GLOBAL_RELEASE_LOCK_FILE" || die "could not assign the host-global deployment lock to root."
chmod 0600 "$GLOBAL_RELEASE_LOCK_FILE" || die "could not restrict the host-global deployment lock."
LOCK_FD_STATE="$(stat -Lc '%d:%i:%u:%g:%a' "/proc/$$/fd/9" 2>/dev/null)" \
|| die "could not attest the opened global release-lock identity."
LOCK_PATH_STATE="$(stat -Lc '%d:%i:%u:%g:%a' "$GLOBAL_RELEASE_LOCK_FILE" 2>/dev/null)" \
|| die "could not attest the global release-lock path identity."
[ -f "/proc/$$/fd/9" ] && [ "$LOCK_FD_STATE" = "$LOCK_PATH_STATE" ] && [[ "$LOCK_FD_STATE" == *":0:0:600" ]] \
|| die "global release-lock path/descriptor identity or ownership is unsafe."
flock -n 9 || die "another Autopilot deployment is already in progress from this or another checkout."
[ "$(stat -Lc '%d:%i' "/proc/$$/fd/9" 2>/dev/null)" = "$(stat -Lc '%d:%i' "$GLOBAL_RELEASE_LOCK_FILE" 2>/dev/null)" ] \
|| die "global release-lock path changed while acquiring the lock."
ok "exclusive host-global deployment lock acquired"
HEAD_RELEASE="$(git rev-parse --verify 'HEAD^{commit}' 2>/dev/null)" \
|| die "could not resolve the checked-out Git commit."
[ "$HEAD_RELEASE" = "$EXPECTED_RELEASE" ] \
|| die "checked-out HEAD does not match EXPECTED_RELEASE; refusing to deploy an ambiguous revision."
ORIGIN_MAIN_RELEASE="$(git rev-parse --verify 'refs/remotes/origin/main^{commit}' 2>/dev/null)" \
|| die "could not resolve refs/remotes/origin/main; fetch final main before deployment."
[ "$ORIGIN_MAIN_RELEASE" = "$EXPECTED_RELEASE" ] \
|| die "fetched origin/main does not match EXPECTED_RELEASE; fetch final main before deployment."
# Lowercase `git ls-files -v` markers hide assume-unchanged files; `S` hides
# skip-worktree files. Reject both so index flags cannot conceal a modified build
# input. The Docker allowlist admits the complete `src` tree, so reject ignored
# untracked files there as well: Git's normal status intentionally hides them,
# but Docker would still copy them into the build context.
INDEX_STATE="$(git ls-files -v)" || die "could not enumerate Git index flags."
if printf '%s\n' "$INDEX_STATE" | grep -E '^[a-zS] ' >/dev/null; then
die "Git assume-unchanged/skip-worktree flags are forbidden on a release checkout."
fi
git update-index -q --really-refresh >/dev/null 2>&1 \
|| die "tracked working tree is dirty; commit or restore tracked changes before deployment."
WORKTREE_STATUS="$(git status --porcelain=v1 --untracked-files=normal 2>/dev/null)" \
|| die "could not inspect the release working tree."
[ -z "$WORKTREE_STATUS" ] \
|| die "non-ignored working tree is dirty; remove untracked build inputs and restore tracked changes."
IGNORED_DOCKER_INPUTS="$(git ls-files --others --ignored --exclude-standard -- src/ 2>/dev/null)" \
|| die "could not enumerate ignored files inside Docker's source allowlist."
[ -z "$IGNORED_DOCKER_INPUTS" ] \
|| die "ignored untracked files exist inside Docker's source allowlist; remove them before deployment."
ok "exact expected release and complete reviewed Docker build-input set verified (value not printed)"
# MemoryAgent intentionally separates DB traffic from internet egress. Picking
# one arbitrary `*memoryagent*` network is unsafe: `edge` cannot resolve `db`,
# while `data` is internal and cannot reach DashScope. Resolve both by their
# Compose suffix and attach the runtime to both; migration needs only `data`.
NETWORK_INVENTORY="$(docker_control network ls --format '{{.Name}}')" \
|| die "could not enumerate Docker networks."
if [ -z "${DATA_NETWORK:-}" ]; then
DATA_NETWORK_MATCHES="$(printf '%s\n' "$NETWORK_INVENTORY" | grep -iE '^memoryagent.*_data$' || true)"
[ "$(printf '%s\n' "$DATA_NETWORK_MATCHES" | grep -c . || true)" = "1" ] \
|| die "automatic data-network discovery requires exactly one *memoryagent*_data match."
DATA_NETWORK="$DATA_NETWORK_MATCHES"
fi
if [ -z "${EDGE_NETWORK:-}" ]; then
EDGE_NETWORK_MATCHES="$(printf '%s\n' "$NETWORK_INVENTORY" | grep -iE '^memoryagent.*_edge$' || true)"
[ "$(printf '%s\n' "$EDGE_NETWORK_MATCHES" | grep -c . || true)" = "1" ] \
|| die "automatic edge-network discovery requires exactly one *memoryagent*_edge match."
EDGE_NETWORK="$EDGE_NETWORK_MATCHES"
fi
[ -n "${DATA_NETWORK:-}" ] \
|| die "could not find the MemoryAgent data network (*memoryagent*_data). Set DATA_NETWORK to override."
[ -n "${EDGE_NETWORK:-}" ] \
|| die "could not find the MemoryAgent edge network (*memoryagent*_edge). Set EDGE_NETWORK to override."
[[ "$DATA_NETWORK" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]] \
|| die "DATA_NETWORK resolved to an invalid Docker object name."
[[ "$EDGE_NETWORK" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]] \
|| die "EDGE_NETWORK resolved to an invalid Docker object name."
[ "$DATA_NETWORK" != "$EDGE_NETWORK" ] \
|| die "data and edge networks must be distinct security zones."
docker_control network inspect "$DATA_NETWORK" >/dev/null 2>&1 || die "data network '$DATA_NETWORK' does not exist."
docker_control network inspect "$EDGE_NETWORK" >/dev/null 2>&1 || die "edge network '$EDGE_NETWORK' does not exist."
[ "$(docker_control network inspect -f '{{.Internal}}' "$DATA_NETWORK")" = "true" ] \
|| die "data network '$DATA_NETWORK' must be internal/private."
[ "$(docker_control network inspect -f '{{.Internal}}' "$EDGE_NETWORK")" = "false" ] \
|| die "edge network '$EDGE_NETWORK' must be non-internal for deterministic Qwen egress."
docker_control network connect --help 2>&1 | grep -q -- '--gw-priority' \
|| die "Docker Engine with network gateway priority support is required for deterministic Qwen egress."
ok "reusing MemoryAgent data network: $DATA_NETWORK"
ok "reusing MemoryAgent edge/egress network: $EDGE_NETWORK"
# Load DASHSCOPE_API_KEY (+ DASHSCOPE_BASE_URL) from a .env next to compose, if present.
[ -f .env ] || die "no .env — production refuses silent Fake Qwen/in-memory operation"
[ ! -L .env ] || die ".env must be a regular non-symlink file with exact mode 0600."
[ "$(stat -c '%a' -- .env 2>/dev/null || true)" = "600" ] \
|| die ".env must be a regular non-symlink file with exact mode 0600."
[ -f .env.example ] && [ ! -L .env.example ] \
|| die ".env.example must be a committed regular file; it is the runtime-key allowlist."
! grep -q $'\r' .env || die ".env must use canonical LF line endings."
declare -A ALLOWED_RUNTIME_ENV=()
declare -A SEEN_RUNTIME_ENV=()
while IFS= read -r allowed_key; do
[ -n "$allowed_key" ] || continue
[ -z "${ALLOWED_RUNTIME_ENV[$allowed_key]+x}" ] \
|| die ".env.example contains a duplicate runtime key."
ALLOWED_RUNTIME_ENV["$allowed_key"]=1
done < <(sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' .env.example)
[ "${#ALLOWED_RUNTIME_ENV[@]}" -gt 0 ] || die ".env.example runtime-key allowlist is empty."
while IFS= read -r runtime_line || [ -n "$runtime_line" ]; do
case "$runtime_line" in
''|'#'*) continue ;;
esac
[[ "$runtime_line" =~ ^([A-Za-z_][A-Za-z0-9_]*)= ]] \
|| die ".env contains a non-canonical line; only KEY=value, comments, and empty lines are allowed."
runtime_key="${BASH_REMATCH[1]}"
[ -n "${ALLOWED_RUNTIME_ENV[$runtime_key]+x}" ] \
|| die ".env contains a key outside the committed runtime allowlist: $runtime_key"
[ -z "${SEEN_RUNTIME_ENV[$runtime_key]+x}" ] \
|| die ".env contains a duplicate runtime key: $runtime_key"
SEEN_RUNTIME_ENV["$runtime_key"]=1
RUNTIME_BASE_KEYS+=("$runtime_key")
done <.env
case "$(env_file_value ALLOW_FAKE_QWEN)" in ''|false) ;; *) die "production .env must not enable ALLOW_FAKE_QWEN." ;; esac
case "$(env_file_value ALLOW_IN_MEMORY_STORE)" in ''|false) ;; *) die "production .env must not enable ALLOW_IN_MEMORY_STORE." ;; esac
case "$(env_file_value READY_REQUIRE_QWEN)" in ''|true) ;; *) die "production .env must keep READY_REQUIRE_QWEN enabled." ;; esac
case "$(env_file_value READY_REQUIRE_DATABASE)" in ''|true) ;; *) die "production .env must keep READY_REQUIRE_DATABASE enabled." ;; esac
unset ALLOWED_RUNTIME_ENV SEEN_RUNTIME_ENV allowed_key runtime_line runtime_key
ok ".env is mode 0600, canonical, duplicate-free, and restricted to committed runtime keys"
REVIEWER_TOKEN="${REVIEWER_TOKEN:-$(env_file_value REVIEWER_TOKEN)}"
DASHSCOPE_API_KEY="${DASHSCOPE_API_KEY:-$(env_file_value DASHSCOPE_API_KEY)}"
DASHSCOPE_BASE_URL="${DASHSCOPE_BASE_URL:-$(env_file_value DASHSCOPE_BASE_URL)}"
DATABASE_URL="${DATABASE_URL:-$(env_file_value DATABASE_URL)}"
DASHSCOPE_BASE_URL="${DASHSCOPE_BASE_URL:-https://dashscope-intl.aliyuncs.com/compatible-mode/v1}"
[ -n "${DASHSCOPE_API_KEY:-}" ] || die "DASHSCOPE_API_KEY is empty; production authenticity is fail-closed."
[ "${#REVIEWER_TOKEN}" -ge 32 ] || die "REVIEWER_TOKEN must be configured with at least 32 characters."
[ -n "$DATABASE_URL" ] || die "DATABASE_URL must be the dedicated autopilot_app runtime DSN in .env."
[ -f "$MIGRATION_ENV_FILE" ] && [ ! -L "$MIGRATION_ENV_FILE" ] \
|| die "missing or symlinked $MIGRATION_ENV_FILE — create a regular file from .env.migration.example."
[ -z "$(env_file_value MIGRATION_DATABASE_URL)" ] \
|| die "MIGRATION_DATABASE_URL must not be stored in runtime .env; keep it only in $MIGRATION_ENV_FILE."
[ "$(stat -c '%a' -- "$MIGRATION_ENV_FILE" 2>/dev/null || true)" = "600" ] \
|| die "$MIGRATION_ENV_FILE must have exact mode 0600."
for secret_value in "$DASHSCOPE_API_KEY" "$REVIEWER_TOKEN" "$DATABASE_URL"; do
[[ "$secret_value" != *$'\n'* && "$secret_value" != *$'\r'* ]] \
|| die "runtime credential values must be single-line."
done
[[ "$DASHSCOPE_BASE_URL" != *$'\n'* && "$DASHSCOPE_BASE_URL" != *$'\r'* ]] \
|| die "DASHSCOPE_BASE_URL must be single-line."
[ "$REVIEWER_TOKEN" = "${REVIEWER_TOKEN#${REVIEWER_TOKEN%%[![:space:]]*}}" ] \
&& [ "$REVIEWER_TOKEN" = "${REVIEWER_TOKEN%${REVIEWER_TOKEN##*[![:space:]]}}" ] \
|| die "REVIEWER_TOKEN must not have leading or trailing whitespace."
DATABASE_ENV_FILE="$(mktemp "$APP_DIR/.git/autopilot-database-env.XXXXXX")" \
|| die "could not create the private database env override."
RUNTIME_SOURCE_ENV_FILE="$(mktemp "$APP_DIR/.git/autopilot-source-env.XXXXXX")" \
|| die "could not create the private raw runtime env snapshot."
RUNTIME_BASE_ENV_FILE="$(mktemp "$APP_DIR/.git/autopilot-base-env.XXXXXX")" \
|| die "could not create the private filtered runtime base env."
RUNTIME_OVERRIDE_ENV_FILE="$(mktemp "$APP_DIR/.git/autopilot-runtime-env.XXXXXX")" \
|| die "could not create the private runtime env override."
ENDPOINT_ENV_FILE="$(mktemp "$APP_DIR/.git/autopilot-endpoint-env.XXXXXX")" \
|| die "could not create the private endpoint env override."
chmod 0600 "$DATABASE_ENV_FILE" "$RUNTIME_SOURCE_ENV_FILE" "$RUNTIME_BASE_ENV_FILE" "$RUNTIME_OVERRIDE_ENV_FILE" "$ENDPOINT_ENV_FILE" \
|| die "could not restrict private env overrides."
trap cleanup_private_artifacts EXIT
ENV_SHA_BEFORE="$(sha256sum .env | awk '{print $1}')" || die "could not hash runtime .env before snapshot."
cp -- .env "$RUNTIME_SOURCE_ENV_FILE" || die "could not snapshot raw runtime .env."
ENV_SHA_AFTER="$(sha256sum .env | awk '{print $1}')" || die "could not hash runtime .env after snapshot."
ENV_SNAPSHOT_SHA="$(sha256sum "$RUNTIME_SOURCE_ENV_FILE" | awk '{print $1}')" \
|| die "could not attest runtime .env snapshot."
[ "$ENV_SHA_BEFORE" = "$ENV_SHA_AFTER" ] && [ "$ENV_SHA_AFTER" = "$ENV_SNAPSHOT_SHA" ] \
|| die "runtime .env changed while the release snapshot was created."
materialize_runtime_base_env "$RUNTIME_SOURCE_ENV_FILE" "$RUNTIME_BASE_ENV_FILE" \
|| die "could not materialize the filtered runtime base env."
RUNTIME_ATTESTATION="$(random_hex 32)" || die "could not generate runtime config attestation."
printf 'DATABASE_URL=%s\n' "$DATABASE_URL" >"$DATABASE_ENV_FILE" \
|| die "could not write the private database env override."
printf 'DASHSCOPE_BASE_URL=%s\n' "$DASHSCOPE_BASE_URL" >"$ENDPOINT_ENV_FILE" \
|| die "could not write the private endpoint env override."
printf '%s\n' \
"DASHSCOPE_API_KEY=$DASHSCOPE_API_KEY" \
"REVIEWER_TOKEN=$REVIEWER_TOKEN" \
"DASHSCOPE_BASE_URL=$DASHSCOPE_BASE_URL" \
"DATABASE_URL=$DATABASE_URL" \
"PORT=$CONTAINER_PORT" \
"LEDGER_JSONL_PATH=$LEDGER_CONTAINER_PATH" \
"TRUST_PROXY_HOPS=1" \
"TRUST_PROXY_ADDRESSES=" \
"DEPLOYMENT_CONFIG_ATTESTATION=$RUNTIME_ATTESTATION" \
>"$RUNTIME_OVERRIDE_ENV_FILE" \
|| die "could not write the private runtime env override."
for runtime_key in "${RUNTIME_OVERRIDE_KEYS[@]}"; do
[ "$(grep -c "^${runtime_key}=" "$RUNTIME_BASE_ENV_FILE" || true)" = "0" ] \
|| die "filtered runtime base env retained override-owned key $runtime_key."
[ "$(grep -c "^${runtime_key}=" "$RUNTIME_OVERRIDE_ENV_FILE" || true)" = "1" ] \
|| die "runtime override must materialize key $runtime_key exactly once."
done
ok "raw runtime .env attested and override-owned keys materialized exactly once"
ok "production Qwen + reviewer credentials are configured (values not printed)"
ok "runtime/migration inputs and private argv-free overrides are mode 0600 and credential-separated (values not printed)"
# Reconcile the serving state before any build or database mutation. A stale
# rollback object is an unfinished transaction and must be handled explicitly.
CONTAINER_INVENTORY="$(docker_container_inventory)" \
|| die "could not obtain a reliable Docker container inventory; no release state was changed."
for private_name in "$BACKUP_CONTAINER" "$STAGING_CONTAINER" "$BOOTSTRAP_CONTAINER" "$SMOKE_CLEANUP_CONTAINER" "$ENDPOINT_VERIFY_CONTAINER"; do
container_name_present "$CONTAINER_INVENTORY" "$private_name" \
&& die "stale private release container '$private_name' exists; inspect/recover it before another deployment."
done
if container_name_present "$CONTAINER_INVENTORY" "$CONTAINER"; then
HAD_PREVIOUS_CONTAINER=1
PREVIOUS_CONTAINER_ID="$(docker_inspect_value '{{.Id}}' "$CONTAINER")" \
|| die "could not capture the pre-release container identity."
valid_container_id "$PREVIOUS_CONTAINER_ID" \
|| die "pre-release container returned an invalid immutable identity."
PREVIOUS_IMAGE_ID="$(docker_inspect_value '{{.Image}}' "$PREVIOUS_CONTAINER_ID")" \
|| die "could not capture the pre-release image identity."
valid_image_id "$PREVIOUS_IMAGE_ID" \
|| die "pre-release container returned an invalid immutable image identity."
[ "$(docker_inspect_value '{{.State.Running}}' "$PREVIOUS_CONTAINER_ID")" = "true" ] \