host: carlos-edge-drain.service runs the ingress drain before tenants stop
The drain unit (W6 spec §5.2): BindsTo= and After= the edge, a oneshot whose ExecStop is -/opt/carlos/carlos __ingress-drain with TimeoutStopSec=150, the edge's hardening with no capabilities and only AF_UNIX. The edge Upholds= it; the twelve tenant units bound to the edge gain Before= it, so their stops begin only after the drain has finished. It is enabled with the others (WantedBy=carlos-edge.service) and never started by bootstrap or arm: BindsTo= would start the edge with it. The apply's edge restart after daemon-reload starts it. A rollback to a set without it disables, stops and removes it as any added unit. carlos-system-update.service and carlos-system-recover.service allow 150 s more (17min 30s, 12min 30s) for the drain an apply or a runtime recovery now waits on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/host/bootstrap.go+8 −1internal/host/gvisor_fixture_rootrunner_linux_test.go+1 −1internal/host/gvisor_hardening_rootrunner_linux_test.go+1 −1internal/host/gvisor_units_test.go+1 −0internal/host/host_test.go+3 −2internal/host/units/carlos-edge-drain.service+58 −0internal/host/units/carlos-edge.service+5 −0internal/host/units/carlos-instance-cmd@.service+2 −0internal/host/units/carlos-instance-gen@.service+2 −0internal/host/units/carlos-instance-gvisor@.service+1 −0internal/host/units/carlos-instance-rootfs-cmd@.service+2 −0internal/host/units/carlos-instance-rootfs-gen@.service+2 −0internal/host/units/carlos-instance-rootfs-tty@.service+2 −0internal/host/units/carlos-instance-rootfs-worker@.service+2 −0internal/host/units/carlos-instance-rootfs@.service+2 −0internal/host/units/carlos-instance-seed@.service+2 −0internal/host/units/carlos-instance-sidecar@.service+2 −0internal/host/units/carlos-instance-tty@.service+2 −0internal/host/units/carlos-instance@.service+2 −0internal/host/units/carlos-system-recover.service+3 −1internal/host/units/carlos-system-update.service+4 −2internal/host/units_ingress_drain_test.go+207 −0
CI failed — run details
Intent
<task-notification> <task-id>ad045bc75bb93dab3</task-id> <tool-use-id>toolu_01QEyzw7TA8vNWb8ctAsQrtd</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/a373aecd-faa9-4b83-8736-ff726fb2d3af/tasks/ad045bc75bb93dab3.output</output-file> <status>completed</status> <summary>Agent "Implement W6 Task 8 (tofu+hostenv)" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE. Fixed the finding: an ARN with a malformed account or region is now refused, so Seed writes nothing and Assemble returns nothing. Commits: 580c5bb7 (this fix), on top of 2657b58e (Task 8). Branch w6-ingress-drain points at 580c5bb7. Fix: the one validator, `ParseTargetGroups` in internal/ingresshealth, now checks the box's account is 12 digits and its region is an AWS region code before comparing entries. internal/hostenv already calls it, so only a comment changed there. Tests: the new cases failed before the fix and pass after. They cover malformed account and region, a pair of entries with matching malformed fields, and a us-gov region that must still pass. Each hostenv refusal checks that Seed writes no parameters and Assemble returns nothing. `go test ./internal/ingresshealth/... ./internal/hostenv/` passes in full, `go test ./internal/host/ -run Ingress` passes, and vet is clean. Concerns: - The validator also runs when a box arms its drain. A box whose own account or region doesn't fit the format would now report not_armed with the reason. Real AWS values fit. - Every git command warns about `.git/packed-refs.lock`: an empty file dated 21:09:54, before my session's first commit at about 21:40. I didn't create it and left it alone. Commits still land, but something in the main checkout may be stuck; it needs checking by whoever owns it. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/w6-build/.superpowers/sdd/2026-11-10-fixed-edge-ingress-drain/task-8-report.md (fix round 1 section appended).</result> <usage><subagent_tokens>156204</subagent_tokens><tool_uses>6</tool_uses><duration_ms>153379</duration_ms></usage> </task-notification>
Attributed to this commit: 27.3 g CO₂e (3,360,495 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on w6-ingress-drain, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/host/bootstrap.go b/internal/host/bootstrap.go| index f200a3331..fc9044183 100644 |
| --- a/internal/host/bootstrap.go |
| +++ b/internal/host/bootstrap.go |
| @@ -171,7 +171,14 @@ var enabledUnits = []string{"carlos-edge.service", relayUnitName, "carlos-adopt. |
| "data-instances.mount", "run-carlos-instances.mount", "run-carlos-logs.mount", |
| // The root instance-control channel (design §5.2); socket-activated, so |
| // only the socket is enabled and started. |
| - "carlos-instance-control.socket"} |
| + "carlos-instance-control.socket", |
| + // The ingress drain (W6 spec §5.2). Enabled, never started here: its |
| + // BindsTo= would start the edge with it. The edge starts it (Upholds=). |
| + ingressDrainUnit} |
| + |
| +// ingressDrainUnit runs the ingress drain in its stop, before every tenant |
| +// unit's stop and the edge's (W6 spec §5.2). |
| +const ingressDrainUnit = "carlos-edge-drain.service" |
| // inertUnits are installed everywhere and deliberately never armed by |
| // bootstrap or by a self-update apply: restore-verify until an operator |
diff --git a/internal/host/gvisor_fixture_rootrunner_linux_test.go b/internal/host/gvisor_fixture_rootrunner_linux_test.go| index a5c434d4a..ae67f0c77 100644 |
| --- a/internal/host/gvisor_fixture_rootrunner_linux_test.go |
| +++ b/internal/host/gvisor_fixture_rootrunner_linux_test.go |
| @@ -835,7 +835,7 @@ func (f *gvisorFixture) bisectVariant(v gvisorBisectVariant, number int) (result |
| } |
| var lines []string |
| for _, line := range strings.Split(gvisorRunSkeleton, "\n") { |
| - if strings.HasPrefix(line, "BindsTo=") || strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") { |
| + if strings.HasPrefix(line, "BindsTo=") || strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") || strings.HasPrefix(line, "Before=") { |
| continue |
| } |
| if strings.HasPrefix(line, "Slice=") { |
diff --git a/internal/host/gvisor_hardening_rootrunner_linux_test.go b/internal/host/gvisor_hardening_rootrunner_linux_test.go| index 1f8eb4d33..5352e88a7 100644 |
| --- a/internal/host/gvisor_hardening_rootrunner_linux_test.go |
| +++ b/internal/host/gvisor_hardening_rootrunner_linux_test.go |
| @@ -68,7 +68,7 @@ func (f *gvisorFixture) start(kind string) string { |
| } |
| var lines []string |
| for _, line := range strings.Split(skeleton, "\n") { |
| - if strings.HasPrefix(line, "BindsTo=") || strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") { |
| + if strings.HasPrefix(line, "BindsTo=") || strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") || strings.HasPrefix(line, "Before=") { |
| continue |
| } |
| if strings.HasPrefix(line, "Slice=") { |
diff --git a/internal/host/gvisor_units_test.go b/internal/host/gvisor_units_test.go| index 792fa7c3b..8859845d5 100644 |
| --- a/internal/host/gvisor_units_test.go |
| +++ b/internal/host/gvisor_units_test.go |
| @@ -80,6 +80,7 @@ Description=carlos gVisor demo instance %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=exec |
| User=root |
diff --git a/internal/host/host_test.go b/internal/host/host_test.go| index f1a3b8b89..196120ca6 100644 |
| --- a/internal/host/host_test.go |
| +++ b/internal/host/host_test.go |
| @@ -45,6 +45,7 @@ var expectedUnits = []string{ |
| "carlos-demo-build@.service", |
| "carlos-demo-builds.slice", |
| "carlos-demo-snapshot@.service", |
| + "carlos-edge-drain.service", |
| "carlos-edge.service", |
| "carlos-hibernate-verify.service", |
| "carlos-hibernate-verify.timer", |
| @@ -1380,7 +1381,7 @@ func TestSystemUnitsAreShapedLikeTheStageRelay(t *testing.T) { |
| } |
| } |
| u := read("carlos-system-update.service") |
| - for _, want := range []string{"Type=oneshot", "StartLimitIntervalSec=0", "TimeoutStartSec=15min", "OnFailure=carlos-system-recover.service", |
| + for _, want := range []string{"Type=oneshot", "StartLimitIntervalSec=0", "TimeoutStartSec=17min 30s", "OnFailure=carlos-system-recover.service", |
| "EnvironmentFile=-/etc/carlos/host.env", "system apply", "carlos.prev", "MemoryMax=256M", |
| "floor.min-release", "carlos.prev.allowed", floorGateCall, "seeds-floor.min-release", "carlos.prev.seeds-allowed", seedsFloorGateCall} { |
| if !strings.Contains(u, want) { |
| @@ -1396,7 +1397,7 @@ func TestSystemUnitsAreShapedLikeTheStageRelay(t *testing.T) { |
| } |
| } |
| r := read("carlos-system-recover.service") |
| - for _, want := range []string{"Type=oneshot", "Before=carlos-edge.service", "WantedBy=multi-user.target", "TimeoutStartSec=10min", "system apply --recover", "carlos.prev", |
| + for _, want := range []string{"Type=oneshot", "Before=carlos-edge.service", "WantedBy=multi-user.target", "TimeoutStartSec=12min 30s", "system apply --recover", "carlos.prev", |
| "floor.min-release", "carlos.prev.allowed", floorGateCall, "seeds-floor.min-release", "carlos.prev.seeds-allowed", seedsFloorGateCall} { |
| if !strings.Contains(r, want) { |
| t.Errorf("recover unit lacks %q", want) |
diff --git a/internal/host/units/carlos-edge-drain.service b/internal/host/units/carlos-edge-drain.service| new file mode 100644 |
| index 000000000..d0dc27532 |
| --- /dev/null |
| +++ b/internal/host/units/carlos-edge-drain.service |
| @@ -0,0 +1,58 @@ |
| +# The ingress drain on every systemd stop of the edge (W6 spec §5.2): a |
| +# manual stop or restart, a reboot, a poweroff. Its stop asks the agent to |
| +# take the box out of the load balancer and waits for the answer, and it |
| +# runs before anything else in the edge's stop: |
| +# - every tenant unit bound to the edge is ordered Before= this one, so |
| +# systemd begins their stops only once this stop has finished; |
| +# - this unit is After= the edge, so its stop finishes before the edge's. |
| +# The edge's Upholds= starts it whenever the edge is active, after a boot, |
| +# a restart and an automatic restart alike. |
| +[Unit] |
| +Description=carlos edge ingress drain (load balancer health, W6) |
| +BindsTo=carlos-edge.service |
| +After=carlos-edge.service |
| + |
| +[Service] |
| +Type=oneshot |
| +RemainAfterExit=yes |
| +ExecStart=/bin/true |
| +# "-": nothing here ever holds up the stop. __ingress-drain exits 0 in every |
| +# case, and at once when the agent's socket does not answer (the edge has |
| +# crashed, or a binary from before W6 is on disk during a rollback). |
| +ExecStop=-/opt/carlos/carlos __ingress-drain |
| +# The 90 s cap, the 10 s settle, the 30 s connection wait, and slack. The |
| +# edge's own TimeoutStopSec=120 is separate. |
| +TimeoutStopSec=150 |
| + |
| +# --- the edge's hardening (carlos-edge.service), with no capabilities --- |
| +# It runs as the agent's user: the drain socket serves only that user. |
| +User=carlos |
| +Group=carlos |
| +CapabilityBoundingSet= |
| +NoNewPrivileges=yes |
| +ProtectSystem=strict |
| +ProtectHome=yes |
| +PrivateTmp=yes |
| +PrivateDevices=yes |
| +ProtectClock=yes |
| +ProtectHostname=yes |
| +ProtectKernelTunables=yes |
| +ProtectKernelModules=yes |
| +ProtectKernelLogs=yes |
| +ProtectProc=invisible |
| +# It dials one unix socket and nothing else. |
| +RestrictAddressFamilies=AF_UNIX |
| +RestrictNamespaces=yes |
| +RestrictRealtime=yes |
| +LockPersonality=yes |
| +MemoryDenyWriteExecute=yes |
| +SystemCallFilter=@system-service |
| +SystemCallErrorNumber=EPERM |
| +SystemCallArchitectures=native |
| +Slice=carlos.slice |
| + |
| +[Install] |
| +# Enabled with the others, so the edge's start also pulls this unit in on |
| +# a systemd too old for Upholds= (249). Upholds= is what keeps it started |
| +# whenever the edge is active. |
| +WantedBy=carlos-edge.service |
diff --git a/internal/host/units/carlos-edge.service b/internal/host/units/carlos-edge.service| index 1b2d7a031..de0b8e94f 100644 |
| --- a/internal/host/units/carlos-edge.service |
| +++ b/internal/host/units/carlos-edge.service |
| @@ -16,6 +16,11 @@ After=network-online.target data-instances.mount run-carlos-instances.mount run- |
| # Control's start probe needs the edge's cgroup. Let the agent's first |
| # request socket-activate control after the edge starts (region HA T1/DR-4). |
| Wants=network-online.target |
| +# The ingress drain (W6 spec §5.2): carlos-edge-drain.service runs the drain |
| +# in its stop, ahead of every tenant's stop and this unit's own. Upholds= |
| +# (systemd 249) starts it whenever the edge is active: after a boot, a |
| +# restart and an automatic restart. |
| +Upholds=carlos-edge-drain.service |
| [Service] |
| # Optional host-wide config (0600, root-owned): CARLOS_HIBERNATE_*, |
diff --git a/internal/host/units/carlos-instance-cmd@.service b/internal/host/units/carlos-instance-cmd@.service| index 364976f52..a987dfeb5 100644 |
| --- a/internal/host/units/carlos-instance-cmd@.service |
| +++ b/internal/host/units/carlos-instance-cmd@.service |
| @@ -13,6 +13,8 @@ Description=carlos exec-backed instance %I (cmd) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=oneshot |
diff --git a/internal/host/units/carlos-instance-gen@.service b/internal/host/units/carlos-instance-gen@.service| index e73106010..357b73447 100644 |
| --- a/internal/host/units/carlos-instance-gen@.service |
| +++ b/internal/host/units/carlos-instance-gen@.service |
| @@ -15,6 +15,8 @@ Description=carlos exec-backed instance generation %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-instance-gvisor@.service b/internal/host/units/carlos-instance-gvisor@.service| index 9a760bf57..11efa6926 100644 |
| --- a/internal/host/units/carlos-instance-gvisor@.service |
| +++ b/internal/host/units/carlos-instance-gvisor@.service |
| @@ -3,6 +3,7 @@ Description=carlos gVisor demo instance %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=exec |
| User=root |
diff --git a/internal/host/units/carlos-instance-rootfs-cmd@.service b/internal/host/units/carlos-instance-rootfs-cmd@.service| index 205ed6eb1..0c12358e2 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-cmd@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-cmd@.service |
| @@ -14,6 +14,8 @@ Description=carlos rootfs instance %I (cmd) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=oneshot |
diff --git a/internal/host/units/carlos-instance-rootfs-gen@.service b/internal/host/units/carlos-instance-rootfs-gen@.service| index 61f43a960..18d839479 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-gen@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-gen@.service |
| @@ -17,6 +17,8 @@ Description=carlos rootfs instance generation %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-instance-rootfs-tty@.service b/internal/host/units/carlos-instance-rootfs-tty@.service| index 06232d086..ca360a163 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-tty@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-tty@.service |
| @@ -13,6 +13,8 @@ Description=carlos rootfs instance %I (tty) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=oneshot |
diff --git a/internal/host/units/carlos-instance-rootfs-worker@.service b/internal/host/units/carlos-instance-rootfs-worker@.service| index 1ef4735ae..a003845bb 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-worker@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-worker@.service |
| @@ -20,6 +20,8 @@ Description=carlos rootfs worker %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-instance-rootfs@.service b/internal/host/units/carlos-instance-rootfs@.service| index 088bd51ae..a5c768d3f 100644 |
| --- a/internal/host/units/carlos-instance-rootfs@.service |
| +++ b/internal/host/units/carlos-instance-rootfs@.service |
| @@ -16,6 +16,8 @@ Description=carlos rootfs instance %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-instance-seed@.service b/internal/host/units/carlos-instance-seed@.service| index 11152d49a..deb07691d 100644 |
| --- a/internal/host/units/carlos-instance-seed@.service |
| +++ b/internal/host/units/carlos-instance-seed@.service |
| @@ -14,6 +14,8 @@ Description=carlos exec-backed instance %I (seed) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=oneshot |
diff --git a/internal/host/units/carlos-instance-sidecar@.service b/internal/host/units/carlos-instance-sidecar@.service| index e82694a2f..b6f0f169c 100644 |
| --- a/internal/host/units/carlos-instance-sidecar@.service |
| +++ b/internal/host/units/carlos-instance-sidecar@.service |
| @@ -14,6 +14,8 @@ Description=carlos exec-backed instance %I (sidecar) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-instance-tty@.service b/internal/host/units/carlos-instance-tty@.service| index 094ca5cb8..79036d4c1 100644 |
| --- a/internal/host/units/carlos-instance-tty@.service |
| +++ b/internal/host/units/carlos-instance-tty@.service |
| @@ -13,6 +13,8 @@ Description=carlos exec-backed instance %I (tty) |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=oneshot |
diff --git a/internal/host/units/carlos-instance@.service b/internal/host/units/carlos-instance@.service| index db2369f0f..0a36229b9 100644 |
| --- a/internal/host/units/carlos-instance@.service |
| +++ b/internal/host/units/carlos-instance@.service |
| @@ -14,6 +14,8 @@ Description=carlos exec-backed instance %I |
| BindsTo=carlos-edge.service |
| Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount |
| +# Stops only after the ingress drain's stop has finished (W6 spec §5.2). |
| +Before=carlos-edge-drain.service |
| [Service] |
| Type=simple |
diff --git a/internal/host/units/carlos-system-recover.service b/internal/host/units/carlos-system-recover.service| index 36bc1d8f6..d6adb580e 100644 |
| --- a/internal/host/units/carlos-system-recover.service |
| +++ b/internal/host/units/carlos-system-recover.service |
| @@ -36,7 +36,9 @@ DefaultDependencies=yes |
| [Service] |
| Type=oneshot |
| -TimeoutStartSec=10min |
| +# 10min, and 150 s more for the ingress drain (W6 spec §5.2): a runtime |
| +# recovery restarts the edge, and the drain unit's stop runs first. |
| +TimeoutStartSec=12min 30s |
| EnvironmentFile=-/etc/carlos/host.env |
| ExecStart=/bin/sh -c 'if [ -x /opt/carlos/system/guard/carlos ] && { [ -e /var/lib/carlos-claims/authority/exec/census.json ] || [ -L /var/lib/carlos-claims/authority/exec/census.json ] || [ -e /opt/carlos/system/guard/moves ] || [ -L /opt/carlos/system/guard/moves ]; }; then exec /opt/carlos/system/guard/carlos system guard-exec recover; fi; if [ -f /opt/carlos/system/router/journal.json ] && [ -x /opt/carlos/carlos.prev ] && [ ! -L /data/.moves/floor.min-release ] && { [ ! -e /data/.moves/floor.min-release ] || [ -f /data/.moves/floor.min-release ]; } && { ! [ -s /data/.moves/floor.min-release ] || /opt/carlos/carlos.prev system floor-allows-prev /data/.moves/floor.min-release /opt/carlos/carlos.prev.allowed; } && [ ! -L /data/.moves/seeds-floor.min-release ] && { [ ! -e /data/.moves/seeds-floor.min-release ] || [ -f /data/.moves/seeds-floor.min-release ]; } && { ! [ -s /data/.moves/seeds-floor.min-release ] || /opt/carlos/carlos.prev system floor-allows-prev /data/.moves/seeds-floor.min-release /opt/carlos/carlos.prev.seeds-allowed; } && /opt/carlos/carlos system exec-sandbox-allows-prev /opt/carlos/carlos.prev; then exec /opt/carlos/carlos.prev system apply --recover; fi; exec /opt/carlos/carlos system apply --recover' |
diff --git a/internal/host/units/carlos-system-update.service b/internal/host/units/carlos-system-update.service| index c64e4c46c..63267bad0 100644 |
| --- a/internal/host/units/carlos-system-update.service |
| +++ b/internal/host/units/carlos-system-update.service |
| @@ -32,7 +32,9 @@ |
| # account: it must write /opt/carlos and /etc/systemd/system, which only |
| # root can do. StartLimitIntervalSec=0 because the path unit can fire in |
| # bursts. TimeoutStartSec bounds a hung apply; OnFailure= makes a killed or |
| -# timed-out apply recover without waiting for a touch. |
| +# timed-out apply recover without waiting for a touch. It was 15min; the |
| +# ingress drain added 150 s (W6 spec §5.2-5.3): the apply's own ask, or |
| +# the drain unit's stop inside the edge restart, holds up to that long. |
| [Unit] |
| Description=carlos platform self-update (root apply) |
| After=network-online.target |
| @@ -42,7 +44,7 @@ OnFailure=carlos-system-recover.service |
| [Service] |
| Type=oneshot |
| -TimeoutStartSec=15min |
| +TimeoutStartSec=17min 30s |
| EnvironmentFile=-/etc/carlos/host.env |
| ExecStart=/bin/sh -c 'if [ -x /opt/carlos/system/guard/carlos ] && { [ -e /var/lib/carlos-claims/authority/exec/census.json ] || [ -L /var/lib/carlos-claims/authority/exec/census.json ] || [ -e /opt/carlos/system/guard/moves ] || [ -L /opt/carlos/system/guard/moves ]; }; then exec /opt/carlos/system/guard/carlos system guard-exec apply; fi; if [ -f /opt/carlos/system/router/journal.json ] && [ -x /opt/carlos/carlos.prev ] && [ ! -L /data/.moves/floor.min-release ] && { [ ! -e /data/.moves/floor.min-release ] || [ -f /data/.moves/floor.min-release ]; } && { ! [ -s /data/.moves/floor.min-release ] || /opt/carlos/carlos.prev system floor-allows-prev /data/.moves/floor.min-release /opt/carlos/carlos.prev.allowed; } && [ ! -L /data/.moves/seeds-floor.min-release ] && { [ ! -e /data/.moves/seeds-floor.min-release ] || [ -f /data/.moves/seeds-floor.min-release ]; } && { ! [ -s /data/.moves/seeds-floor.min-release ] || /opt/carlos/carlos.prev system floor-allows-prev /data/.moves/seeds-floor.min-release /opt/carlos/carlos.prev.seeds-allowed; }; then exec /opt/carlos/carlos.prev system apply; fi; exec /opt/carlos/carlos system apply' |
diff --git a/internal/host/units_ingress_drain_test.go b/internal/host/units_ingress_drain_test.go| new file mode 100644 |
| index 000000000..e988d1e00 |
| --- /dev/null |
| +++ b/internal/host/units_ingress_drain_test.go |
| @@ -0,0 +1,207 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// unitValues is every value a unit gives key (section-qualified, as |
| +// "[Unit]Before"), split on whitespace, in file order. An empty assignment |
| +// resets the list, as in systemd. |
| +func unitValues(body, key string) []string { |
| + var out []string |
| + section := "" |
| + for _, line := range strings.Split(body, "\n") { |
| + line = strings.TrimSpace(line) |
| + if line == "" || strings.HasPrefix(line, "#") { |
| + continue |
| + } |
| + if strings.HasPrefix(line, "[") { |
| + section = line |
| + continue |
| + } |
| + k, v, ok := strings.Cut(line, "=") |
| + if !ok || section+strings.TrimSpace(k) != key { |
| + continue |
| + } |
| + if strings.TrimSpace(v) == "" { |
| + out = nil |
| + continue |
| + } |
| + out = append(out, strings.Fields(v)...) |
| + } |
| + return out |
| +} |
| + |
| +// Spec §9 "Units": systemd stops a unit only after the stops ordered before |
| +// it have finished, so a tenant unit that misses Before= would start its |
| +// stop while the drain is still running and drop the requests the drain |
| +// exists to keep. |
| +func TestTenantUnitsStopAfterTheIngressDrain(t *testing.T) { |
| + names, err := unitNames() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !slices.Contains(names, ingressDrainUnit) { |
| + t.Fatalf("%s is not embedded", ingressDrainUnit) |
| + } |
| + tenants := 0 |
| + for _, name := range names { |
| + if name == ingressDrainUnit { |
| + continue |
| + } |
| + body := readUnit(t, name) |
| + if !slices.Contains(unitValues(body, "[Unit]BindsTo"), systemEdgeUnit+".service") { |
| + continue |
| + } |
| + tenants++ |
| + if !slices.Contains(unitValues(body, "[Unit]Before"), ingressDrainUnit) { |
| + t.Errorf("%s is bound to the edge and lacks Before=%s", name, ingressDrainUnit) |
| + } |
| + } |
| + // Twelve today; the floor catches a parser that finds none. |
| + if tenants < 12 { |
| + t.Errorf("found %d tenant units bound to the edge, want at least 12", tenants) |
| + } |
| + edge := readUnit(t, systemEdgeUnit+".service") |
| + if !slices.Contains(unitValues(edge, "[Unit]Upholds"), ingressDrainUnit) { |
| + t.Errorf("%s lacks Upholds=%s", systemEdgeUnit, ingressDrainUnit) |
| + } |
| +} |
| + |
| +func TestIngressDrainUnitShape(t *testing.T) { |
| + u := readUnit(t, ingressDrainUnit) |
| + for key, want := range map[string]string{ |
| + "[Unit]BindsTo": "carlos-edge.service", |
| + "[Unit]After": "carlos-edge.service", |
| + "[Service]Type": "oneshot", |
| + "[Service]RemainAfterExit": "yes", |
| + "[Service]ExecStart": "/bin/true", |
| + "[Service]ExecStop": "-/opt/carlos/carlos __ingress-drain", |
| + "[Service]TimeoutStopSec": "150", |
| + "[Service]User": "carlos", |
| + "[Service]Group": "carlos", |
| + // Only the drain socket is dialled. |
| + "[Service]RestrictAddressFamilies": "AF_UNIX", |
| + "[Install]WantedBy": "carlos-edge.service", |
| + } { |
| + if got := strings.Join(unitValues(u, key), " "); got != want { |
| + t.Errorf("%s %s = %q, want %q", ingressDrainUnit, key, got, want) |
| + } |
| + } |
| + // An empty bounding set, written out, and no ambient capability. |
| + if !hasDirective(u, "CapabilityBoundingSet=") || len(unitValues(u, "[Service]CapabilityBoundingSet")) != 0 { |
| + t.Errorf("%s must carry an empty CapabilityBoundingSet=", ingressDrainUnit) |
| + } |
| + if strings.Contains(u, "AmbientCapabilities") { |
| + t.Errorf("%s must not grant AmbientCapabilities", ingressDrainUnit) |
| + } |
| + // The edge's hardening, line for line. |
| + edge := readUnit(t, systemEdgeUnit+".service") |
| + for _, key := range []string{"NoNewPrivileges", "ProtectSystem", "ProtectHome", "PrivateTmp", "PrivateDevices", |
| + "ProtectClock", "ProtectHostname", "ProtectKernelTunables", "ProtectKernelModules", "ProtectKernelLogs", |
| + "ProtectProc", "RestrictNamespaces", "RestrictRealtime", "LockPersonality", "MemoryDenyWriteExecute", |
| + "SystemCallFilter", "SystemCallErrorNumber", "SystemCallArchitectures"} { |
| + want := unitValues(edge, "[Service]"+key) |
| + if len(want) == 0 { |
| + t.Fatalf("%s has no %s to copy", systemEdgeUnit, key) |
| + } |
| + if got := unitValues(u, "[Service]"+key); !slices.Equal(got, want) { |
| + t.Errorf("%s %s = %v, want the edge's %v", ingressDrainUnit, key, got, want) |
| + } |
| + } |
| + // It writes nothing, so it is given nowhere to write. |
| + if len(unitValues(u, "[Service]ReadWritePaths")) != 0 { |
| + t.Errorf("%s needs no writable path", ingressDrainUnit) |
| + } |
| +} |
| + |
| +// The drain unit is enabled with the others (its WantedBy= adds it to the |
| +// edge's start) and never started by bootstrap or an apply's arm: BindsTo= |
| +// would pull the edge up with it, and bootstrap must not start the edge. |
| +// The apply starts it by restarting the edge after daemon-reload |
| +// (Upholds=). Group members install and enable it like any box. |
| +func TestIngressDrainUnitIsEnabledNotArmed(t *testing.T) { |
| + plan := currentBootstrapPlan() |
| + if !slices.Contains(plan.Enabled, ingressDrainUnit) { |
| + t.Errorf("plan does not enable %s: %v", ingressDrainUnit, plan.Enabled) |
| + } |
| + if slices.Contains(plan.Armed, ingressDrainUnit) { |
| + t.Errorf("plan starts %s, which would start the edge: %v", ingressDrainUnit, plan.Armed) |
| + } |
| + if groupAutomaticUnit(ingressDrainUnit) || !slices.Contains(groupManualUnits(plan.Enabled), ingressDrainUnit) { |
| + t.Errorf("group members must install and enable %s", ingressDrainUnit) |
| + } |
| +} |
| + |
| +// Spec §5.3: apply installs the drain unit with the others, and the edge |
| +// restart after daemon-reload is what starts it; a rollback to a build |
| +// without it disables, stops and removes it, as any unit the newer set |
| +// added. |
| +func TestApplyInstallsTheIngressDrainUnitBeforeTheEdgeRestart(t *testing.T) { |
| + f, _ := applyHappy(t) |
| + staged := filepath.Join(f.a.stagingDir(), "carlos") |
| + f.answer["plan:"+staged] = strings.Replace(f.answer["plan:"+staged], `"carlos-new-only.path"`, `"carlos-new-only.path","`+ingressDrainUnit+`"`, 1) |
| + f.answer["units:"+staged] += " " + ingressDrainUnit |
| + unitFile := filepath.Join(f.a.etcDir, "systemd", "system", ingressDrainUnit) |
| + installedAtReload := false |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "daemon-reload" && !installedAtReload { |
| + _, err := os.Stat(unitFile) |
| + installedAtReload = err == nil |
| + } |
| + return orig(args...) |
| + } |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !installedAtReload { |
| + t.Fatalf("%s not installed by the first daemon-reload", ingressDrainUnit) |
| + } |
| + joined := strings.Join(f.calls, "\n") |
| + reload := strings.Index(joined, "systemctl daemon-reload") |
| + restart := strings.Index(joined, "systemctl restart carlos-edge") |
| + enable := strings.Index(joined, "systemctl enable ") |
| + if !(reload >= 0 && reload < restart && restart < enable) { |
| + t.Fatalf("want daemon-reload, restart of the edge, then enable:\n%s", joined) |
| + } |
| + if !strings.Contains(joined[enable:], ingressDrainUnit) { |
| + t.Errorf("%s not enabled:\n%s", ingressDrainUnit, joined) |
| + } |
| + for _, c := range f.calls { |
| + if strings.HasPrefix(c, "systemctl start ") && strings.Contains(c, ingressDrainUnit) { |
| + t.Errorf("apply started %s itself, which would start a stopped edge: %s", ingressDrainUnit, c) |
| + } |
| + } |
| +} |
| + |
| +func TestRollbackRemovesTheIngressDrainUnitTheNewerSetAdded(t *testing.T) { |
| + f, _ := applyHappy(t) |
| + staged := filepath.Join(f.a.stagingDir(), "carlos") |
| + f.answer["units:"+staged] += " " + ingressDrainUnit |
| + // v2 never comes up; v1 does, so the gate failure is a rollback. |
| + f.edgeUp = false |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "restart" { |
| + if b, _ := os.ReadFile(systemBinaryPath(f.a.optDir)); strings.Contains(string(b), "carlos v1") { |
| + f.edgeUp = true |
| + } |
| + } |
| + return orig(args...) |
| + } |
| + if err := f.a.Run(context.Background(), false); err == nil { |
| + t.Fatal("a failed gate must exit non-zero") |
| + } |
| + if !f.called("systemctl disable --now " + ingressDrainUnit) { |
| + t.Errorf("rollback did not disable and stop %s:\n%s", ingressDrainUnit, strings.Join(f.calls, "\n")) |
| + } |
| + if _, err := os.Stat(filepath.Join(f.a.etcDir, "systemd", "system", ingressDrainUnit)); err == nil { |
| + t.Errorf("%s survived the rollback", ingressDrainUnit) |
| + } |
| +} |