fix(ci): install durable Stage cleanup worker during guarded rollout

This commit is contained in:
devops committed 2026-10-24 13:03:38 +00:00
1 parent 104abf904f
commit b9bba1f129
4 files changed
+106 -5

No files matched your search

+6 -5
View File
@@ -2,11 +2,12 @@
This change replaces the SHA-pinned transient timer with a root-owned intent journal
and a fixed host systemd retry worker. No host service is installed or enabled by
the CI credential command. Deployment must first install the reviewed worker as
`/opt/services/canina/stage-principal-cleanup.py` (root:root 0755), install the two
supplied systemd units under `/etc/systemd/system`, daemon-reload, and enable/start
`canina-stage-principal-cleanup.timer`. This is a promotion prerequisite, NOT an
instruction to run these actions during review. The open wrapper refuses to operate
the CI credential command. The tracked `deploy.yml` → `ci-host-deploy.sh` →
`deploy.sh` path invokes `install-stage-principal-cleanup.py` from the verified
immutable release before `stage_rollout`. It atomically installs the root-owned
worker (0755) and supplied systemd units (0644), reloads systemd, enables/starts
the timer and verifies enabled/active state. Failure aborts rollout. No manual
sudo provisioning is required or permitted during review. The open wrapper refuses to operate
without an enabled and active timer. Never overwrite or delete the 0700 root-owned
`/opt/services/canina/stage-principal-cleanup` journal during deployments.
+3
View File
@@ -112,5 +112,8 @@ docker network inspect traefik_public >/dev/null
# Builds only app images; DB/Redis and support-bot remain untouched.
docker compose --progress plain build backend_stage frontend_stage
source scripts/rollout-stage.sh
# Install reboot-persistent cleanup from this immutable CI release before rollout.
# This preserves the root-private intent journal and fails closed on timer failure.
python3 scripts/install-stage-principal-cleanup.py
stage_rollout
echo 'Deployment for develop completed successfully!'
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""Install the reviewed retry worker only through the guarded host CI deploy."""
import os
from pathlib import Path
import subprocess
import tempfile
def install(root, source, run):
base = root / 'opt/services/canina'
units = root / 'etc/systemd/system'
journal = base / 'stage-principal-cleanup'
base.mkdir(parents=True, exist_ok=True)
units.mkdir(parents=True, exist_ok=True)
if journal.is_symlink():
raise ValueError('Unsafe journal')
journal.mkdir(mode=0o700, exist_ok=True)
st = journal.lstat()
if not journal.is_dir() or st.st_uid != os.getuid() or st.st_mode & 0o777 != 0o700:
raise ValueError('Unsafe journal')
# Never touch existing intents. Atomic replacement avoids truncated workers.
payloads = [('stage-principal-cleanup.py', base, 0o755)] + [
('canina-stage-principal-cleanup.' + suffix, units, 0o644)
for suffix in ('service', 'timer')]
for name, dest, mode in payloads:
fd, temporary = tempfile.mkstemp(prefix='.cleanup-install-', dir=dest)
try:
with os.fdopen(fd, 'wb') as output:
output.write((source / name).read_bytes())
os.fchmod(output.fileno(), mode)
output.flush()
os.fsync(output.fileno())
os.replace(temporary, dest / name)
directory = os.open(dest, os.O_RDONLY | os.O_DIRECTORY)
try:
os.fsync(directory)
finally:
os.close(directory)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
for command in [
['systemctl', 'daemon-reload'],
['systemctl', 'enable', '--now', 'canina-stage-principal-cleanup.timer'],
['systemctl', 'is-enabled', '--quiet', 'canina-stage-principal-cleanup.timer'],
['systemctl', 'is-active', '--quiet', 'canina-stage-principal-cleanup.timer'],
]:
run(command)
if __name__ == '__main__':
if (os.getuid() != 0 or Path('/.dockerenv').exists()
or Path('/run/.containerenv').exists()
or os.environ.get('GITHUB_ACTIONS', os.environ.get('GITEA_ACTIONS')) != 'true'):
raise SystemExit('Guarded host CI deployment required')
os.umask(0o077)
install(Path('/'), Path(__file__).resolve().parent,
lambda command: subprocess.run(command, check=True, timeout=60))
+39
View File
@@ -0,0 +1,39 @@
import pathlib, subprocess, tempfile, unittest, importlib.util
HERE=pathlib.Path(__file__).resolve().parent
class InstallTests(unittest.TestCase):
def test_ci_wires_installer_before_rollout(self):
deploy=(HERE/'deploy.sh').read_text()
self.assertIn('python3 scripts/install-stage-principal-cleanup.py',deploy)
self.assertLess(deploy.index('python3 scripts/install-stage-principal-cleanup.py'),deploy.index('stage_rollout\n'))
def test_disposable_install_preserves_intents_and_enables_timer(self):
spec=importlib.util.spec_from_file_location('installer',HERE/'install-stage-principal-cleanup.py')
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp); journal=root/'opt/services/canina/stage-principal-cleanup'
journal.mkdir(parents=True,mode=0o700); intent=journal/'owned.json';intent.write_text('pending')
commands=[]
m.install(root,HERE,lambda cmd:commands.append(cmd))
m.install(root,HERE,lambda cmd:commands.append(cmd))
self.assertEqual(intent.read_text(),'pending')
self.assertEqual((root/'opt/services/canina/stage-principal-cleanup.py').read_bytes(),(HERE/'stage-principal-cleanup.py').read_bytes())
for name in ('service','timer'):
self.assertEqual((root/f'etc/systemd/system/canina-stage-principal-cleanup.{name}').stat().st_mode & 0o777,0o644)
self.assertEqual(commands[:4],[['systemctl','daemon-reload'],['systemctl','enable','--now','canina-stage-principal-cleanup.timer'],['systemctl','is-enabled','--quiet','canina-stage-principal-cleanup.timer'],['systemctl','is-active','--quiet','canina-stage-principal-cleanup.timer']])
def test_disposable_host_commands_and_failure_propagation(self):
import os
from unittest.mock import patch
spec=importlib.util.spec_from_file_location('installer',HERE/'install-stage-principal-cleanup.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp);binary=root/'bin';binary.mkdir();log=root/'commands'
ctl=binary/'systemctl';ctl.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$COMMAND_LOG"\n[ "$1" != "$FAIL_COMMAND" ]\n');ctl.chmod(0o755)
with patch.dict(os.environ,{'PATH':str(binary)+':'+os.environ['PATH'],'COMMAND_LOG':str(log),'FAIL_COMMAND':'never'}):
m.install(root,HERE,lambda cmd:subprocess.run(cmd,check=True))
self.assertEqual(log.read_text().splitlines(),['daemon-reload','enable --now canina-stage-principal-cleanup.timer','is-enabled --quiet canina-stage-principal-cleanup.timer','is-active --quiet canina-stage-principal-cleanup.timer'])
with patch.dict(os.environ,{'FAIL_COMMAND':'enable'}),self.assertRaises(subprocess.CalledProcessError):
m.install(root,HERE,lambda cmd:subprocess.run(cmd,check=True))
def test_unsafe_journal_fails_before_commands(self):
spec=importlib.util.spec_from_file_location('installer',HERE/'install-stage-principal-cleanup.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp); journal=root/'opt/services/canina/stage-principal-cleanup';journal.mkdir(parents=True,mode=0o755)
with self.assertRaises(ValueError):m.install(root,HERE,lambda _:self.fail('must not enable'))
if __name__=='__main__': unittest.main()