infra: flycontrol-edge.service and the FLY_CONTROL_VIA switch

Off by default. 05-deploy validates and writes FLY_CONTROL_VIA; the build
and package ship fly-control-edge; the edge gets the page's cpus. With the
control API on the bus, watchdog check 1 reads flysim's own /healthz and
check 1b restarts only the edge. Runbook: the live switch and rollback.
This commit is contained in:
acamilo 2026-10-01 23:33:59 +00:00
parent 29f9b7da3b
commit 8474d93eb0
10 changed files with 286 additions and 5 deletions

View file

@ -123,6 +123,9 @@ need pct
# /opt/fly/current, rather than half way through the deploy or at flysim's boot. # /opt/fly/current, rather than half way through the deploy or at flysim's boot.
FLY_FEED_VIA_EFFECTIVE="$(feed_via_normalize "${FLY_FEED_VIA:-}")" \ FLY_FEED_VIA_EFFECTIVE="$(feed_via_normalize "${FLY_FEED_VIA:-}")" \
|| die "05-deploy: FLY_FEED_VIA must be 'direct' or 'bus', got '${FLY_FEED_VIA}'" || die "05-deploy: FLY_FEED_VIA must be 'direct' or 'bus', got '${FLY_FEED_VIA}'"
# Who serves the control API (docs/design/flybus.md, "Control over the bus"): the same.
FLY_CONTROL_VIA_EFFECTIVE="$(control_via_normalize "${FLY_CONTROL_VIA:-}")" \
|| die "05-deploy: FLY_CONTROL_VIA must be 'direct' or 'bus', got '${FLY_CONTROL_VIA}'"
# CHROMIUM_PROFILE is validated here, not left to the launcher: a typo or a # CHROMIUM_PROFILE is validated here, not left to the launcher: a typo or a
# `vgl` on a container that never had VirtualGL installed would only show up as # `vgl` on a container that never had VirtualGL installed would only show up as
@ -585,6 +588,14 @@ trap 'rm -f "$tmp_fly_env" "$tmp_flypush_env"' EXIT
# feed counters (flysim's :9101, or flyedge's loopback :9102). # feed counters (flysim's :9101, or flyedge's loopback :9102).
# Validated and lowercased above (feed_via_normalize). # Validated and lowercased above (feed_via_normalize).
echo "FLY_FEED_VIA=${FLY_FEED_VIA_EFFECTIVE}" echo "FLY_FEED_VIA=${FLY_FEED_VIA_EFFECTIVE}"
# Who serves the control API on :7401 (docs/design/flybus.md, "Control over
# the bus"). "direct" is the default: flysim binds it. "bus" makes flysim
# register the control services on its embedded router and leave :7401 to
# flycontrol-edge.service, which this script never enables (runbook,
# "Control over the bus"). Written unconditionally; the watchdog reads it
# to know whose /healthz is flysim's own (check 1) and whether to watch
# the edge (check 1b). Validated and lowercased above.
echo "FLY_CONTROL_VIA=${FLY_CONTROL_VIA_EFFECTIVE}"
# How long a macro leaves a target alone after a walk to it aborted # How long a macro leaves a target alone after a walk to it aborted
# (macros.md section 12.1, the Viridian stall). Only written when it is set, # (macros.md section 12.1, the Viridian stall). Only written when it is set,
# because the default lives in the crate and a box that has not tuned it # because the default lives in the crate and a box that has not tuned it
@ -722,7 +733,8 @@ if [ -n "${CPUSET:-}" ]; then
log "05-deploy: cpuset partition — $(echo "$plan" | awk '$1 !~ /=$/ {printf "%s%s=%s", sep, $1, $2; sep=", "}')" log "05-deploy: cpuset partition — $(echo "$plan" | awk '$1 !~ /=$/ {printf "%s%s=%s", sep, $1, $2; sep=", "}')"
tmp_dropin="$(mktemp)" tmp_dropin="$(mktemp)"
# flyedge is off by default, but its drop-in is written with the rest so that the day # flyedge is off by default, but its drop-in is written with the rest so that the day
# it is enabled it serves the page from the page's CPUs, never from flysim's. # it is enabled it serves the page from the page's CPUs, never from flysim's; likewise
# flycontrol-edge (CTRL-01).
# flysim-session (SERVE-01) is the session runtime's standalone unit: flysim's cores. # flysim-session (SERVE-01) is the session runtime's standalone unit: flysim's cores.
# (`fly-runtime session` runs it AS flysim.service, which already has them.) # (`fly-runtime session` runs it AS flysim.service, which already has them.)
# flyshadow (SHADOW-01) is off by default too; when started it gets every CPU that is not # flyshadow (SHADOW-01) is off by default too; when started it gets every CPU that is not

View file

@ -326,9 +326,30 @@ is_enabled() {
# ============================================================================ # ============================================================================
# Check 1: flysim — /healthz 200 and hot-state mtime age < 30s. # Check 1: flysim — /healthz 200 and hot-state mtime age < 30s.
# ============================================================================ # ============================================================================
# FLY_CONTROL_VIA from fly.env, lowercased ("direct" when absent).
control_via() {
local via=""
if [ -f "$FLY_ENV_FILE" ]; then
via="$(awk -F= '/^FLY_CONTROL_VIA=/ { print $2; exit }' "$FLY_ENV_FILE" 2>/dev/null | tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
fi
echo "${via:-direct}"
}
# flysim's own /healthz: :7401 when flysim serves it, and with FLY_CONTROL_VIA=bus
# (docs/design/flybus.md, "Control over the bus") flysim's read-only listener,
# which flysim serves itself in both modes -- so an edge that is down is check
# 1b's business and never restarts the fly.
flysim_health_url() {
if [ "$(control_via)" = "bus" ]; then
echo "${FLY_METRICS_URL}/healthz"
else
echo "${FLY_CONTROL_URL}/healthz"
fi
}
check_flysim() { check_flysim() {
local ok=1 local ok=1
if ! curl -fsS -o /dev/null "${FLY_CONTROL_URL}/healthz"; then if ! curl -fsS -o /dev/null "$(flysim_health_url)"; then
ok=0 ok=0
fi fi
if [ -e "$FLY_STATE_HOT" ]; then if [ -e "$FLY_STATE_HOT" ]; then
@ -346,6 +367,24 @@ check_flysim() {
fi fi
} }
# ============================================================================
# Check 1b: flycontrol-edge -- only with FLY_CONTROL_VIA=bus. :7401/healthz
# answered by the edge while flysim's own /healthz is fine; when the edge does
# not answer, restart the edge (never flysim, never a reboot: the fly is fine
# and the edge holds no state). With flysim itself down, check 1 owns it.
# ============================================================================
check_control_edge() {
[ "$(control_via)" = "bus" ] || return 0
curl -fsS -o /dev/null "${FLY_METRICS_URL}/healthz" || return 0
if curl -fsS -o /dev/null "${FLY_CONTROL_URL}/healthz"; then
record_ok flycontrol-edge
else
record_fail flycontrol-edge
log_err "flycontrol-edge: :7401 does not answer while flysim is healthy; restarting the edge"
restart_unit flycontrol-edge
fi
}
# ============================================================================ # ============================================================================
# Check 2: flystage — flysim's frames_sent_total / feed_clients from the # Check 2: flystage — flysim's frames_sent_total / feed_clients from the
# read-only metrics listener (infra.md section 5; not superseded by the # read-only metrics listener (infra.md section 5; not superseded by the
@ -1227,6 +1266,7 @@ check_loop() {
main() { main() {
check_flysim check_flysim
check_control_edge
check_flystage check_flystage
check_flycast check_flycast
check_encoder check_encoder

View file

@ -85,6 +85,9 @@ log "building in $crate_dir for target-cpu=haswell (the host is E5-2660 v3, Hasw
# without a rebuild. It lands next to OUT_PATH, where package-release.sh # without a rebuild. It lands next to OUT_PATH, where package-release.sh
# looks for it. # looks for it.
RUSTFLAGS="-C target-cpu=haswell" cargo build --release --target "$CARGO_TARGET" --bin fly-edge RUSTFLAGS="-C target-cpu=haswell" cargo build --release --target "$CARGO_TARGET" --bin fly-edge
# fly-control-edge (FLY_CONTROL_VIA=bus, CTRL-01): the control API served from
# flysim's control services on the bus. Same reasons, same place.
RUSTFLAGS="-C target-cpu=haswell" cargo build --release --target "$CARGO_TARGET" --bin fly-control-edge
# flysim-session (SERVE-01): the live fly on the session runtime, a drop-in for flysim # flysim-session (SERVE-01): the live fly on the session runtime, a drop-in for flysim
# that `fly-runtime session` points flysim.service at; fly-shadow (SHADOW-01, # that `fly-runtime session` points flysim.service at; fly-shadow (SHADOW-01,
# infra/units/flyshadow.service): the session runtime shadowing the live fly; and # infra/units/flyshadow.service): the session runtime shadowing the live fly; and
@ -125,6 +128,12 @@ edge_out="$(dirname "$OUT_PATH")/fly-edge"
cp "$edge_built" "$edge_out" cp "$edge_built" "$edge_out"
chmod 0755 "$edge_out" chmod 0755 "$edge_out"
log "built $edge_out ($(du -h "$edge_out" | cut -f1))" log "built $edge_out ($(du -h "$edge_out" | cut -f1))"
control_edge_built="${crate_dir}/target/${CARGO_TARGET}/release/fly-control-edge"
[ -x "$control_edge_built" ] || die "expected binary not found after build: $control_edge_built"
control_edge_out="$(dirname "$OUT_PATH")/fly-control-edge"
cp "$control_edge_built" "$control_edge_out"
chmod 0755 "$control_edge_out"
log "built $control_edge_out ($(du -h "$control_edge_out" | cut -f1))"
for extra in flysim-session fly-shadow fly-session; do for extra in flysim-session fly-shadow fly-session; do
extra_built="${crate_dir}/target/${CARGO_TARGET}/release/${extra}" extra_built="${crate_dir}/target/${CARGO_TARGET}/release/${extra}"
[ -x "$extra_built" ] || die "expected binary not found after build: $extra_built" [ -x "$extra_built" ] || die "expected binary not found after build: $extra_built"

View file

@ -14,6 +14,8 @@
# fly-edge (the feed-bus edge, mode 0755, when build-flysim.sh # fly-edge (the feed-bus edge, mode 0755, when build-flysim.sh
# left one beside FLYSIM_BIN; flyedge.service stays # left one beside FLYSIM_BIN; flyedge.service stays
# inactive on a release without it) # inactive on a release without it)
# fly-control-edge (the control-bus edge, CTRL-01, likewise;
# flycontrol-edge.service stays inactive without it)
# flysim-session (the session runtime, SERVE-01, and fly-session, its # flysim-session (the session runtime, SERVE-01, and fly-session, its
# fly-shadow worker program; mode 0755, when build-flysim.sh left # fly-shadow worker program; mode 0755, when build-flysim.sh left
# fly-session them beside FLYSIM_BIN. `fly-runtime session` refuses # fly-session them beside FLYSIM_BIN. `fly-runtime session` refuses
@ -88,6 +90,13 @@ if [ -x "$EDGE_BIN" ]; then
else else
log "no fly-edge beside $FLYSIM_BIN; packaging without it (FLY_FEED_VIA=bus unavailable in this release)" log "no fly-edge beside $FLYSIM_BIN; packaging without it (FLY_FEED_VIA=bus unavailable in this release)"
fi fi
CONTROL_EDGE_BIN="$(dirname "$FLYSIM_BIN")/fly-control-edge"
if [ -x "$CONTROL_EDGE_BIN" ]; then
cp "$CONTROL_EDGE_BIN" "${release_dir}/fly-control-edge"
chmod 0755 "${release_dir}/fly-control-edge"
else
log "no fly-control-edge beside $FLYSIM_BIN; packaging without it (FLY_CONTROL_VIA=bus unavailable in this release)"
fi
for extra in flysim-session fly-shadow fly-session; do for extra in flysim-session fly-shadow fly-session; do
EXTRA_BIN="$(dirname "$FLYSIM_BIN")/${extra}" EXTRA_BIN="$(dirname "$FLYSIM_BIN")/${extra}"
if [ -x "$EXTRA_BIN" ]; then if [ -x "$EXTRA_BIN" ]; then

View file

@ -6,7 +6,7 @@
# <unit>` and, on the 5-consecutive-failure escalation, a container # <unit>` and, on the 5-consecutive-failure escalation, a container
# reboot. This is the minimum NOPASSWD surface for exactly what # reboot. This is the minimum NOPASSWD surface for exactly what
# bin/fly-watchdog's checks actually restart (flysim, flystage, flycast, # bin/fly-watchdog's checks actually restart (flysim, flystage, flycast,
# mediamtx, flypush), plus reboot. No `systemctl stop`, no # mediamtx, flypush, and flycontrol-edge when FLY_CONTROL_VIA=bus), plus reboot. No `systemctl stop`, no
# `enable`/`disable`, no arbitrary unit name, and no units this script # `enable`/`disable`, no arbitrary unit name, and no units this script
# does not itself restart (xvfb, pulse, flystage-web are never touched by # does not itself restart (xvfb, pulse, flystage-web are never touched by
# fly-watchdog's checks, so they are not granted here). # fly-watchdog's checks, so they are not granted here).
@ -15,6 +15,7 @@ fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flystage.service
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flycast.service fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flycast.service
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart mediamtx.service fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart mediamtx.service
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flypush.service fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flypush.service
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flycontrol-edge.service
fly ALL=(root) NOPASSWD: /usr/sbin/reboot fly ALL=(root) NOPASSWD: /usr/sbin/reboot
# fly-loop-recover.service (also User=fly): its ladder restarts flysim (granted above) and # fly-loop-recover.service (also User=fly): its ladder restarts flysim (granted above) and
# resets to a milestone archive through this one root wrapper, which validates the rung, # resets to a milestone archive through this one root wrapper, which validates the rung,

View file

@ -801,6 +801,71 @@ pct exec $CTID -- /opt/fly/bin/fly-runtime legacy
growth, a restore, a restart, legacy cancel), and the unit wiring has been run under a real growth, a restore, a restart, legacy cancel), and the unit wiring has been run under a real
systemd 257 user manager (state, timer enable/disable, `OnFailure`-style fallback, resume). systemd 257 user manager (state, timer enable/disable, `OnFailure`-style fallback, resume).
## Control over the bus (CTRL-01)
`FLY_CONTROL_VIA` picks who serves `http://127.0.0.1:7401`: `direct` (the default; flysim binds
it) or `bus`. In bus mode flysim registers the control API as RPC services on its embedded flybus
router, and `flycontrol-edge.service` serves the same HTTP bytes on `:7401` by calling them
(`docs/design/flybus.md`, "Control over the bus"). Clients do not change: the bridge, the stage's
health wait, the watchdog, `fly-runtime`, the probation and shadow guards, `fly-loop-recover`,
the stream check and `curl` still talk to `:7401`. It works under either runtime, independently
of `FLY_FEED_VIA`. Claim the container in the host log first, as for any host work.
**Prerequisites.** The release ships `fly-control-edge` (`ls /opt/fly/current/fly-control-edge`).
The env file has `FLY_CONTROL_VIA=bus`, deployed with `05-deploy.sh`. The deploy only rewrites
`/etc/fly/fly.env` and pushes the unit; it neither enables the edge nor restarts flysim, so the
box keeps serving `:7401` directly until step 2.
```
# 1. Before: what the switch must preserve.
pct exec $CTID -- grep -E '^FLY_(CONTROL|FEED)_VIA=' /etc/fly/fly.env # FLY_CONTROL_VIA=bus
pct exec $CTID -- curl -s 127.0.0.1:7401/status | jq '.frame, .status, .checkpoint'
pct exec $CTID -- curl -s -X POST 127.0.0.1:7401/checkpoint # a fresh generation to restart from
# 2. The switch: flysim stops binding :7401 and registers the services; the edge binds :7401 once
# they answer. Between the two, :7401 refuses connections, exactly as during any flysim restart.
pct exec $CTID -- systemctl enable flycontrol-edge.service
pct exec $CTID -- systemctl restart flysim.service
pct exec $CTID -- systemctl start flycontrol-edge.service
# 3. Verify.
pct exec $CTID -- ss -ltnp | grep ':7401' # users:(("fly-control-edg"...
pct exec $CTID -- curl -s 127.0.0.1:7401/healthz # {"status":"ok"}
pct exec $CTID -- curl -s 127.0.0.1:7401/status | jq '.frame, .status' # the frame moves
pct exec $CTID -- curl -s 127.0.0.1:9103/metrics | grep -E '^fly_control_edge_(bus_connected|calls_total|call_failures_total)'
pct exec $CTID -- journalctl -u flysim -b --since -5min | grep -E 'control services registered|bus listening'
pct exec $CTID -- ls /run/fly/bus/control-edge.sock /run/fly/bus/control/
pct exec $CTID -- curl -s 127.0.0.1:7410/health | jq . # the bridge: sim reachable
```
- **Healthy looks like:** `fly_control_edge_bus_connected 1`, `calls_total` rising (the bridge and
the watchdog poll it), `call_failures_total` flat, no `fly-watchdog:` restart lines, and the next
real channel-points sugar fulfilled as before. Do not fire a test sugar or chat line on the live
stream to prove it; both are visible on screen.
- **Watchdog.** With `FLY_CONTROL_VIA=bus`, check 1 reads flysim's own `/healthz` on `:9101`, so a
dead edge never restarts the fly. Check 1b restarts `flycontrol-edge` alone when `:7401` fails
while flysim is healthy (the sudoers file grants exactly that restart).
- **Restarts.** `systemctl restart flysim.service` (the unstick rule, `fly-runtime`, the ladder)
restarts the edge with it (`Requires=`). A crash-restart of flysim needs nothing: the edge
unbinds `:7401`, reconnects every 500 ms and binds again once the new services answer.
**Rollback** (any time, no release change):
```
pct exec $CTID -- sed -i 's/^FLY_CONTROL_VIA=.*/FLY_CONTROL_VIA=direct/' /etc/fly/fly.env
pct exec $CTID -- systemctl disable --now flycontrol-edge.service
pct exec $CTID -- systemctl restart flysim.service # binds :7401 itself again
pct exec $CTID -- curl -s 127.0.0.1:7401/healthz
# then set FLY_CONTROL_VIA=direct in the env file too, or the next 05-deploy.sh writes bus back.
```
- **Rolling back to a release from before CTRL-01 while in bus mode** is safe. The older flysim
does not know `FLY_CONTROL_VIA`, so it binds `:7401` itself. The edge unit's
`ConditionPathExists=` keeps it inactive on a release without the binary, and the older
watchdog reads `:7401` as before. Switch to direct first anyway, so the env file says what
runs.
- **If the edge cannot bind** (`fly_control_edge_bind_failures_total` rising, "the control port
cannot be bound" in its journal), flysim is still in direct mode. `fly.env` says bus but flysim
was not restarted after the deploy. Restart flysim (step 2).
## CPU partition (cpuset) ## CPU partition (cpuset)
`05-deploy.sh` derives the in-guest `AllowedCPUs=` drop-ins for every app unit `05-deploy.sh` derives the in-guest `AllowedCPUs=` drop-ins for every app unit

View file

@ -324,6 +324,13 @@ FLY_MACRO_MODE=raw
# the same bytes; enable that unit by hand (its header has the steps). # the same bytes; enable that unit by hand (its header has the steps).
# Watchdog check 2 follows this setting to the edge's counters by itself. # Watchdog check 2 follows this setting to the edge's counters by itself.
FLY_FEED_VIA=direct FLY_FEED_VIA=direct
# --- control path -------------------------------------------------------------
# Who serves http://127.0.0.1:7401 (docs/control-api.md; docs/design/flybus.md,
# "Control over the bus"). direct: flysim binds it, as always. bus: flysim
# registers the control services on its embedded bus (/run/fly/bus) and
# flycontrol-edge.service serves the same bytes; enable that unit by hand
# (infra/docs/runbook.md, "Control over the bus"). Independent of FLY_FEED_VIA.
FLY_CONTROL_VIA=direct
# How long a macro leaves a target alone after a walk to it aborted "blocked" or # How long a macro leaves a target alone after a walk to it aborted "blocked" or
# "timeout" (macros.md section 12.1). Session state, so a restart offers every # "timeout" (macros.md section 12.1). Session state, so a restart offers every
# target once more. Unset means the default, 10. # target once more. Unset means the default, 10.

View file

@ -168,6 +168,19 @@ feed_via_normalize() {
esac esac
} }
# control_via_normalize VALUE — FLY_CONTROL_VIA as feed_via_normalize does FLY_FEED_VIA:
# lowercased (empty means "direct"), or return 1 for anything but direct|bus. flysim refuses
# anything else at boot; 05-deploy.sh refuses it at deploy and writes the lowercased word, which
# the watchdog reads to know who serves :7401 (docs/design/flybus.md, "Control over the bus").
control_via_normalize() {
local via
via="$(printf '%s' "${1:-direct}" | tr '[:upper:]' '[:lower:]')"
case "$via" in
direct|bus) printf '%s\n' "$via" ;;
*) return 1 ;;
esac
}
cpuset_partition() { cpuset_partition() {
local cpuset="$1" rayon_threads="$2" encoder_cores="${3:-2}" local cpuset="$1" rayon_threads="$2" encoder_cores="${3:-2}"
local sim_cpus remainder remainder_count page_count page_cpus encoder_cpus local sim_cpus remainder remainder_count page_count page_cpus encoder_cpus
@ -196,7 +209,7 @@ cpuset_partition() {
# flysim.slice the sim's CPUs (flysim.service and flysim-session.service run in it); # flysim.slice the sim's CPUs (flysim.service and flysim-session.service run in it);
# flysim.service, flysim-session.service the same set (belt and braces: a unit outside the # flysim.service, flysim-session.service the same set (belt and braces: a unit outside the
# slice is still bounded); # slice is still bounded);
# xvfb, flystage, flystage-web, pulse, mediamtx, flyedge the page CPUs; # xvfb, flystage, flystage-web, pulse, mediamtx, flyedge, flycontrol-edge the page CPUs;
# flycast the encoder CPUs; # flycast the encoder CPUs;
# flyshadow page + encoder (never the sim's); # flyshadow page + encoder (never the sim's);
# FLY_SIM_CPUS / FLY_OTHER_CPUS "=": the lists /etc/fly/cpuset.env carries; OTHER is every CPU # FLY_SIM_CPUS / FLY_OTHER_CPUS "=": the lists /etc/fly/cpuset.env carries; OTHER is every CPU
@ -209,7 +222,7 @@ cpuset_dropin_plan() {
read -r sim page enc <<< "$(cpuset_partition "$cpuset" "$rayon" "$encoder")" || return 1 read -r sim page enc <<< "$(cpuset_partition "$cpuset" "$rayon" "$encoder")" || return 1
echo "flysim.slice $sim" echo "flysim.slice $sim"
for u in flysim flysim-session; do echo "${u}.service $sim"; done for u in flysim flysim-session; do echo "${u}.service $sim"; done
for u in xvfb flystage flystage-web pulse mediamtx flyedge; do echo "${u}.service $page"; done for u in xvfb flystage flystage-web pulse mediamtx flyedge flycontrol-edge; do echo "${u}.service $page"; done
echo "flycast.service $enc" echo "flycast.service $enc"
echo "flyshadow.service ${page},${enc}" echo "flyshadow.service ${page},${enc}"
echo "FLY_SIM_CPUS= $sim" echo "FLY_SIM_CPUS= $sim"

View file

@ -510,6 +510,60 @@ if grep -E '^(ALWAYS_ON_UNITS|APP_UNITS)=' "$INFRA_DIR/07-enable.sh" "$INFRA_DIR
else else
pass "07-enable.sh and verify.sh leave flyedge.service alone" pass "07-enable.sh and verify.sh leave flyedge.service alone"
fi fi
# ---------------------------------------------------------------------------
# 3b2b. The control bus edge (CTRL-01, docs/design/flybus.md, "Control over the
# bus"). flycontrol-edge.service is off unless the operator switches a
# container to FLY_CONTROL_VIA=bus, follows flysim when on, and the deploy
# writes the default. The watchdog's use of FLY_CONTROL_VIA is driven for real
# further down, next to check 2's.
# ---------------------------------------------------------------------------
echo "--- flycontrol-edge.service: off by default, after and bound to flysim ---"
CEDGE_UNIT="$INFRA_DIR/units/flycontrol-edge.service"
if [ ! -f "$CEDGE_UNIT" ]; then
fail "units/flycontrol-edge.service is missing"
else
grep -qE '^After=.*\bflysim\.service\b' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service orders itself After=flysim.service" \
|| fail "flycontrol-edge.service must be After=flysim.service: flysim owns the router"
grep -qE '^Requires=.*\bflysim\.service\b' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service Requires=flysim.service" \
|| fail "flycontrol-edge.service must Require flysim.service"
grep -qE '^ExecStart=/opt/fly/current/fly-control-edge$' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service runs the release's fly-control-edge" \
|| fail "flycontrol-edge.service ExecStart must be /opt/fly/current/fly-control-edge"
grep -qE '^ConditionPathExists=/opt/fly/current/fly-control-edge$' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service stays inactive on a release without fly-control-edge" \
|| fail "flycontrol-edge.service needs ConditionPathExists=/opt/fly/current/fly-control-edge"
grep -qE '^Environment=FLY_CONTROL_BIND=127\.0\.0\.1:7401$' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service binds the contract's loopback :7401" \
|| fail "flycontrol-edge.service FLY_CONTROL_BIND must be 127.0.0.1:7401 (docs/control-api.md)"
grep -qE '^Environment=FLY_CONTROL_EDGE_METRICS_ADDR=127\.0\.0\.1:' "$CEDGE_UNIT" \
&& pass "flycontrol-edge.service keeps its metrics on loopback" \
|| fail "flycontrol-edge.service FLY_CONTROL_EDGE_METRICS_ADDR must be a 127.0.0.1 address"
fi
if target_pulls "$INFRA_DIR/units/fly.target" | grep -qx 'flycontrol-edge.service'; then
fail "fly.target pulls flycontrol-edge.service in; it must stay off until the operator enables it"
else
pass "fly.target does not pull flycontrol-edge.service in"
fi
if grep -E '^(ALWAYS_ON_UNITS|APP_UNITS)=' "$INFRA_DIR/07-enable.sh" "$INFRA_DIR/verify.sh" | grep -q 'flycontrol-edge'; then
fail "07-enable.sh or verify.sh lists flycontrol-edge.service as always-on"
else
pass "07-enable.sh and verify.sh leave flycontrol-edge.service alone"
fi
grep -qE '^ echo "FLY_CONTROL_VIA=\$\{FLY_CONTROL_VIA_EFFECTIVE\}"$' "$INFRA_DIR/05-deploy.sh" \
&& pass "05-deploy.sh writes FLY_CONTROL_VIA into fly.env" \
|| fail "05-deploy.sh must write FLY_CONTROL_VIA (validated) into fly.env"
grep -qE '^fly ALL=\(root\) NOPASSWD: /usr/bin/systemctl restart flycontrol-edge\.service$' "$INFRA_DIR/config/fly-sudoers" \
&& pass "the watchdog may restart flycontrol-edge.service" \
|| fail "config/fly-sudoers must let the watchdog restart flycontrol-edge.service (check 1b)"
cv_out="$(bash -c "source '$INFRA_DIR/lib/common.sh' >/dev/null 2>&1; for v in '' direct Bus BUS http; do control_via_normalize \"\$v\" || echo refused; done" 2>&1)"
if [ "$(echo $cv_out)" = "direct direct bus bus refused" ]; then
pass "control_via_normalize: empty/direct -> direct, Bus/BUS -> bus, anything else refused"
else
fail "control_via_normalize: got '$(echo $cv_out)'"
fi
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# 3b3. The shadow run (SHADOW-01, infra/units/flyshadow.service). # 3b3. The shadow run (SHADOW-01, infra/units/flyshadow.service).
# #
@ -1397,6 +1451,27 @@ else
feed_url_case "BUS" "FLY_FEED_VIA=BUS" "" "http://edge" feed_url_case "BUS" "FLY_FEED_VIA=BUS" "" "http://edge"
feed_url_case "quoted bus" 'FLY_FEED_VIA="bus"' "" "http://edge" feed_url_case "quoted bus" 'FLY_FEED_VIA="bus"' "" "http://edge"
feed_url_case "explicit override wins" "FLY_FEED_VIA=bus" "http://other" "http://other" feed_url_case "explicit override wins" "FLY_FEED_VIA=bus" "http://other" "http://other"
# Check 1 reads flysim's own /healthz: :7401 when flysim serves it, flysim's read-only
# listener when the control edge does (FLY_CONTROL_VIA=bus), so an edge outage never
# restarts the fly (CTRL-01).
health_url_case() {
local label="$1" env_line="$2" want="$3" got
printf '%s\n' "$env_line" > "$fe_fixture/fly.env"
got="$(FLY_ENV_FILE="$fe_fixture/fly.env" FLY_CONTROL_URL=http://control \
FLY_METRICS_URL=http://sim \
WD_RUN_DIR="$fe_fixture/run" WD_STATE_DIR="$fe_fixture/state" \
TEXTFILE_DIR="$fe_fixture/textfile" \
bash -c "source '$fe_fixture/wd.sh'; flysim_health_url" 2>&1 || true)"
if [ "$got" = "$want" ]; then
pass "check 1 flysim health: $label -> $got"
else
fail "check 1 flysim health: $label: got '$got', want '$want'"
fi
}
health_url_case "direct" "FLY_CONTROL_VIA=direct" "http://control/healthz"
health_url_case "no FLY_CONTROL_VIA line" "FLY_FEED_VIA=bus" "http://control/healthz"
health_url_case "bus" "FLY_CONTROL_VIA=bus" "http://sim/healthz"
health_url_case "Bus" 'FLY_CONTROL_VIA="Bus"' "http://sim/healthz"
rm -rf "$fe_fixture" rm -rf "$fe_fixture"
fi fi

View file

@ -0,0 +1,50 @@
# infra/units/flycontrol-edge.service — pushed to /etc/systemd/system/flycontrol-edge.service.
#
# The control API (http://127.0.0.1:7401, docs/control-api.md) served from
# flysim's control services on the bus (docs/design/flybus.md, "Control over
# the bus"; services/flysim/crates/fly-control-edge). DISABLED BY DEFAULT: it
# is in no target's Wants=/Requires= and 07-enable.sh does not enable it.
# With FLY_CONTROL_VIA=direct (the default, written into /etc/fly/fly.env by
# 05-deploy.sh) flysim binds 127.0.0.1:7401 itself and this unit has nothing
# to do. The switch and the way back are infra/docs/runbook.md, "Control over
# the bus"; in short:
#
# on: FLY_CONTROL_VIA=bus in the env file, 05-deploy.sh, then
# systemctl enable flycontrol-edge.service; systemctl restart flysim.service
# (flysim stops binding :7401; the edge binds it once the services answer)
# back: FLY_CONTROL_VIA=direct, 05-deploy.sh, systemctl disable --now
# flycontrol-edge.service, systemctl restart flysim.service
#
# Ordering: flysim owns the router (/run/fly/bus, shared with the feed when
# that rides the bus too), so it starts first and the edge follows it.
# Requires= makes an explicit stop or restart of flysim (the unstick rule's
# restart and fly-runtime's switch included) stop or restart the edge with
# it. A crash-restart of flysim is covered by the
# edge itself: it unbinds :7401 and reconnects every 500 ms, so to every
# client it looks exactly like flysim restarting. The edge holds no state.
[Unit]
Description=flycontrol-edge: the control API served from flysim's control services on the bus
After=flysim.service
Requires=flysim.service
# A release that predates fly-control-edge has no binary; stay cleanly
# inactive rather than restart-looping.
ConditionPathExists=/opt/fly/current/fly-control-edge
[Service]
Type=simple
User=fly
# FLY_CONTROL_VIA, FLY_BUS_DIR and the rest of flysim's configuration: the
# edge reads the same file, so the two cannot disagree about the port or the bus.
EnvironmentFile=/etc/fly/fly.env
Environment=FLY_CONTROL_BIND=127.0.0.1:7401
Environment=FLY_BUS_DIR=/run/fly/bus
# Its own /metrics and /healthz. Loopback only.
Environment=FLY_CONTROL_EDGE_METRICS_ADDR=127.0.0.1:9103
ExecStart=/opt/fly/current/fly-control-edge
Restart=always
RestartSec=2
# Small JSON requests; an /events page is the largest thing it ever holds.
MemoryMax=128M
[Install]
WantedBy=fly.target