Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/fork-ci-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# is a note; one that was present and then deleted fails.
.github/scripts/fork-ci-tests.test.cjs
.github/scripts/fork-contracts.test.py
.github/scripts/fork-fleet-desk-agent.test.py
.github/scripts/fork-fleet-desk-key.test.py
.github/scripts/fork-fleet-desk.test.py
.github/scripts/fork-fleet.test.py
Expand Down
29 changes: 29 additions & 0 deletions .github/fork-fleet.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,35 @@ old `/run/fork-fleet-desk` first (`serve` refuses a lock file or directory left
by root, with a message that says so). A desk agent of an older wire version
gets exit 76 and a line that says to upgrade it.

**The desktop's side** (`fork-fleet-desk-agent.py`, as root on the desktop; on
WSL2 inside the distro, which runs systemd). Install it and
`fork-fleet-desk-wire.py` from the same pinned commit as step 3, beside the
launcher and the walls (`fork-fleet-walls.py`) in `/opt/fork-fleet`; the
launcher and its setup are as on a vm-pool host. Then, once per desktop:

```sh
install -d -m 755 /etc/fork-fleet
ssh-keygen -t ed25519 -N '' -C desk-POOL -f /etc/fork-fleet/desk-key # desk-key.pub goes to the always-on host
# The always-on host's own key, from that host (`cat /etc/ssh/ssh_host_ed25519_key.pub`), as one line;
# with another PORT the name is [HOST]:PORT. Nothing else is ever trusted.
echo 'HOST ssh-ed25519 AAAA...' > /etc/fork-fleet/desk-known_hosts
printf 'POOL=POOL\nHOST=HOST\n' > /etc/fork-fleet/desk.conf && chmod 600 /etc/fork-fleet/desk.conf
python3 -I /opt/fork-fleet/fork-fleet-desk-agent.py check
install -m 644 fork-fleet-desk-agent.service /etc/systemd/system/
systemctl daemon-reload && systemctl enable --now fork-fleet-desk-agent
```

The agent checks each verb again against the wire rules (anything else ends
the session), runs `start` only after `fork-fleet-walls.py check` passes, gives
the launcher and the walls only `PATH` and `LC_ALL` (they read their own
settings files), kills a verb at its limit (answer 75) and every running verb
when the session ends, and dials again with backoff and jitter (at once after
a sleep, also on WSL2, where only the wall clock jumps). One agent per desktop
(a lock in `/run/fork-fleet-desk-agent`); the other settings and their
defaults are in the script's header. `check-net` (step 6) never comes over the
link, so the always-on host cannot use it to probe the desktop's own network:
run it by hand on the desktop.

**What jobs need on the host.** The jobs run without sudo, so the host must
already have:

Expand Down
437 changes: 437 additions & 0 deletions .github/scripts/fork-fleet-desk-agent.py

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions .github/scripts/fork-fleet-desk-agent.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Fork only: the desk agent on a desktop (fork-fleet-desk-agent.py; .github/fork-fleet.md, "The desk link").
# Install as /etc/systemd/system/fork-fleet-desk-agent.service, root-owned 0644. A system unit, as root: the launcher
# and the walls it runs need root (namespaces, nft, transient units as the runner users).
# No Environment= on purpose: the launcher reads /etc/fork-fleet/launch.conf and the walls the real machine, never a
# variable (FORK_FLEET_LAUNCH_CONF and FORK_FLEET_WALLS_ROOT are test knobs; the agent passes neither to them).
[Unit]
Description=Fork fleet desk agent (the desktop's side of the desk link)
Wants=network-online.target
After=network-online.target

[Service]
Type=exec
ExecStart=/usr/bin/python3 -I /opt/fork-fleet/fork-fleet-desk-agent.py run
# The agent dials again by itself; this is for a crash. Exit 2 (unusable settings or files): fix them, then start it.
Restart=always
RestartSec=30
RestartPreventExitStatus=2
# Stopping the unit (or a crash) ends every verb it was running: all of its processes go, children included.
KillMode=control-group
TimeoutStopSec=20
UMask=0077
# A backstop for the agent and the verbs it runs (the launcher, the walls check); runners live in their own transient
# units, so this never limits a CI job. The agent itself reads the link in bounded pieces. No swap: a runaway is
# stopped, not spilled into the desktop's swap.
MemoryMax=512M
MemorySwapMax=0

[Install]
WantedBy=multi-user.target
502 changes: 502 additions & 0 deletions .github/scripts/fork-fleet-desk-agent.test.py

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions .github/scripts/fork-fleet-desk-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
serve -> agent, answer {"v":1, "hello": POOL}
serve -> agent {"v":1, "id": N, "verb": V, "args": [...], "stdin": S} one launcher call ("stdin": start only)
agent -> serve {"v":1, "id": N, "exit": E, "stdout": O, "stderr": R} its result; any order, once per id
serve -> agent {"v":1, "ping": N} the agent answers {"v":1, "pong": N} at once, also while verbs run
serve -> agent {"v":1, "ping": N} the agent answers {"v":1, "pong": N} at once, also while verbs run.
serve sends one whenever it has sent nothing for PING (15) s, however much the agent
sends: the agent ends the session after 45 s with no frame from serve.
launch -> serve (socket) a request without "id"; serve answers with an answer without "id".
Anything else ends the session (or drops that one socket client). A hello of another VERSION ends it with serve's
exit WRONG_VERSION (ssh passes it to the agent) and a line on ssh's stderr that says which side to upgrade.
Expand All @@ -38,8 +40,6 @@
NAME = re.compile(r'[a-z0-9][a-z0-9-]{0,62}-[0-9]{1,20}') # fork-fleet-launch.sh's NAME
RUNNER_VERSION = re.compile(r'[0-9]{1,5}\.[0-9]{1,5}\.[0-9]{1,5}') # fork-fleet-launch.sh's VERSION
MB = re.compile(r'[0-9]{1,7}')
URL = re.compile(r'https?://[A-Za-z0-9.-]+(:[0-9]{1,5})?(/[A-Za-z0-9._~/-]*)?')
HOST_PORT = re.compile(r'[0-9]{1,3}(\.[0-9]{1,3}){3}:[0-9]{1,5}')
START_LINE = re.compile(r'(ACTIONS_RUNNER_INPUT_JITCONFIG=[A-Za-z0-9+/=]{1,65536}|FORK_FLEET_(RUN|ATTEMPT)=[1-9][0-9]{0,19})')
# The launcher's verbs: (argument patterns, then a pattern every further argument matches or None, the verb's limit
# in seconds: the agent kills it then). fork-fleet.py's Host waits 120 s for its launch command: see "Time" above.
Expand All @@ -50,12 +50,13 @@
'fits': ((MB,), None, 30),
'stop': ((NAME,), None, 100),
'prune': ((), None, 100),
'check-net': ((URL, HOST_PORT), HOST_PORT, 300),
'refresh-net': ((), None, 100), # each dispatcher pass: the address lookups (10 s each) and the units' new deny lists
}
# The launcher's verbs that only its own machine runs (a root timer, the walls check or a person at the machine), never
# the link: `serve` and the agent refuse them like an unknown verb, and the contract test counts them as the launcher's.
LOCAL_ONLY = frozenset({'paths'})
# check-net connects, as root and from the machine's own network, to each HOST:PORT it is given and says which one it
# cannot reach: through the link that would let the always-on host map the desktop's home network.
LOCAL_ONLY = frozenset({'refresh', 'check-net', 'paths'})
MARGIN = 10 # seconds `launch` waits beyond a verb's limit, all told
SERVE_MARGIN = 5 # seconds `serve` waits beyond it before it answers UNKNOWN itself
UNKNOWN = 75 # launch's exit when the verb may or may not have run (EX_TEMPFAIL)
Expand Down Expand Up @@ -168,7 +169,8 @@ def check_request(frame, with_id):
if with_id:
_count(frame['id'])
verb, args = frame['verb'], frame['args']
if verb not in VERBS or not isinstance(args, list) or not all(isinstance(a, str) for a in args):
if not isinstance(verb, str) or verb not in VERBS or not isinstance(args, list) or \
not all(isinstance(a, str) for a in args): # str first: a list or an object is no key, and no crash
raise WireError(f'not a launcher verb: {str(verb)[:40]!r}')
fixed, rest, _ = VERBS[verb]
if len(args) < len(fixed) or (rest is None and len(args) > len(fixed)):
Expand Down
9 changes: 6 additions & 3 deletions .github/scripts/fork-fleet-desk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
The ssh forced command, as the desk-link user, never root (its authorized_keys: `restrict,command="FORCED
POOL" ssh-ed25519 ...`, written by fork-fleet-desk-key.py). Takes DIR/POOL.lock (one session per pool: a
second one exits 3 at once), reads the agent's hello, then listens on the socket DIR/POOL.sock and passes each
request to the agent and its answer back. Pings the agent every PING s; with no word from it in DEAD s, or on
request to the agent and its answer back. Pings the agent whenever it has heard nothing from it, or sent nothing
to it, for PING s (the agent drops a link that says nothing for DEAD s); with no word from it in DEAD s, or on
its end of the session, every request in flight is answered "outcome unknown" and the socket is removed.
python3 -I fork-fleet-desk.py launch POOL VERB ARGS...
A pool's `launch` command for `fork-fleet.py dispatch`: ["python3", "-I", ".../fork-fleet-desk.py", "launch",
Expand Down Expand Up @@ -200,6 +201,7 @@ def read_agent(self):

def send(self, frame):
self.outbuf += wire.encode(frame)
self.sent = self.now()
self.flush()

def flush(self):
Expand All @@ -210,7 +212,7 @@ def flush(self):
pass

def loop(self, listener):
self.heard = self.now()
self.heard = self.sent = self.now()
while True:
for key, _ in self.sel.select(min(1.0, self.ping / 4) if not self.outbuf else 0.05):
if key.data == 'agent':
Expand All @@ -224,7 +226,8 @@ def loop(self, listener):
t = self.now()
if t - self.heard > self.dead:
raise Refused(f'no word from the desktop in {self.dead} s')
if t - self.heard > self.ping * (self.pings + 1):
# also when only the agent talks (answers spaced under PING s): its side counts frames FROM serve
if t - self.heard > self.ping * (self.pings + 1) or t - self.sent > self.ping:
self.pings += 1
self.send({'v': wire.VERSION, 'ping': self.pings})
for client, (_, deadline) in list(self.waiting.items()):
Expand Down
43 changes: 41 additions & 2 deletions .github/scripts/fork-fleet-desk.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ def test_silent_desktop_ends_the_session(self):
self.assertTrue(any('ping' in f for f in agent.got))
self.assertEqual(self.launch('active')[0], wire.NO_SESSION)

def test_serve_pings_a_desktop_that_talks_but_hears_nothing(self):
"""The agent drops a link with no frame FROM serve in its DEAD s. Answers spaced under PING s (several verbs in
flight) used to keep serve quiet, so the agent killed a healthy verb: serve pings whenever it sent nothing."""
agent = Agent(lambda f: None)
t, _, _ = self.serve(agent, ping=0.3, dead=5)
stop = threading.Event()

def chatter(): # the desktop talks all the time (here: pongs), so serve always hears from it
while not stop.wait(0.05):
os.write(agent.to_serve_w, wire.encode({'v': 1, 'pong': 0}))
threading.Thread(target=chatter, daemon=True).start()
time.sleep(1.5)
stop.set()
pings = [f for f in agent.got if 'ping' in f]
self.assertGreaterEqual(len(pings), 3, agent.got) # about every 0.3 s, not never
agent.hang_up()
t.join(5)

def test_a_request_of_the_wrong_type_is_refused_and_the_session_lives(self):
agent = Agent(lambda f: wire.answer(0, 'ok\n'))
t, _, _ = self.serve(agent)
for bad in ({'verb': ['active'], 'args': []}, {'verb': {'a': 1}, 'args': []}, {'verb': 'active', 'args': 'x'}):
with socket.socket(socket.AF_UNIX) as c:
c.connect(self.sock)
c.sendall(json.dumps({'v': 1, **bad}).encode() + b'\n')
got = wire.check_answer(json.loads(c.makefile('rb').readline()), with_id=False)
self.assertEqual(got['exit'], 2, bad)
self.assertIn('not a launcher verb', got['stderr'])
self.assertEqual(self.launch('active'), (0, 'ok\n', '')) # serve is still there
for bad in (['start'], {'a': 1}, None, 7):
with self.assertRaises(wire.WireError):
wire.check_request({'v': 1, 'id': 1, 'verb': bad, 'args': []}, with_id=True)
self.assertEqual([f['verb'] for f in agent.got if 'id' in f], ['active']) # only the good one went on
agent.hang_up()
t.join(5)

def test_answering_pings_keeps_the_session_and_slow_answer_is_unknown(self):
agent = Agent(lambda f: None)
t, _, _ = self.serve(agent, ping=0.2, dead=0.6)
Expand Down Expand Up @@ -476,8 +512,11 @@ def shell_var(self, name):
def test_verbs_are_the_launchers(self):
case = self.LAUNCH[self.LAUNCH.index('case "${1-}:$#" in'):]
verbs = dict(re.findall(r'^ ([a-z-]+):([0-9]+|\*)\)', case, re.M))
self.assertEqual(set(verbs), set(wire.VERBS) | wire.LOCAL_ONLY)
# every launcher verb crosses the link or is local only (a local-only verb may name one the launcher has yet
# to get, as `refresh` before row D7)
self.assertEqual(set(verbs) - wire.LOCAL_ONLY, set(wire.VERBS))
self.assertFalse(set(wire.VERBS) & wire.LOCAL_ONLY)
self.assertIn('check-net', verbs) # the launcher has it, and the link never carries it (LOCAL_ONLY)
for verb, n in verbs.items():
if verb in wire.LOCAL_ONLY:
continue
Expand All @@ -497,7 +536,6 @@ def test_patterns_are_the_launchers_and_the_dispatchers(self):
self.assertEqual(wire.NAME.pattern, self.shell_var('NAME'))
self.assertEqual(wire.RUNNER_VERSION.pattern, self.shell_var('VERSION'))
self.assertEqual(wire.POOL.pattern, ff.POOL.pattern)
self.assertIn(f'$url =~ ^{wire.URL.pattern}$ ]]', self.LAUNCH)

def test_every_dispatcher_verb_answers_before_the_dispatcher_gives_up(self):
# The agent kills a verb at its limit, serve answers 75 after that, launch ends after that, and all of it
Expand All @@ -506,6 +544,7 @@ def test_every_dispatcher_verb_answers_before_the_dispatcher_gives_up(self):
src = ''.join(inspect.getsource(f) for f in vars(ff.Host).values() if inspect.isfunction(f))
verbs = set(re.findall(r"self\._(?:must|do)\('([a-z-]+)'", src))
self.assertEqual(verbs, {'start', 'ready', 'active', 'fits', 'stop', 'prune', 'refresh-net'})
self.assertEqual(verbs, set(wire.VERBS)) # the link carries what the dispatcher calls, nothing more
for verb in wire.VERBS:
self.assertLess(wire.limit(verb), wire.serve_seconds(verb), verb)
self.assertLess(wire.serve_seconds(verb), wire.seconds(verb), verb)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fork-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ jobs:
if: ${{ (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository) }}
needs: changes
runs-on: ubuntu-24.04
timeout-minutes: 5
timeout-minutes: 10
steps:
# scripts/: some tests are run by a script there (.github/fork-ci-tests.txt).
# fork-ci/: the composite actions and their tests. fork-kit/: host kits and their tests.
Expand Down
Loading