Files

55 lines
2.6 KiB
Python

"""One polling cycle with an injected canonical installer, no shell authority."""
from deployment_queue import REPOSITORY, REPOSITORY_ID, QueueError
class Controller:
def __init__(self, queue, source, installer):
self.queue, self.source, self.installer = queue, source, installer
def reconcile(self):
for row in self.queue.snapshot():
if row['phase'] != 'deploying':
continue
outcome, receipt = self.installer.receipt(row)
self.queue.reconcile(row['id'], digest=row['digest'], artifact=row['artifact'],
outcome=outcome, receipt=receipt)
def observe(self):
runs = self.source.discover()
# A disappeared pending run cannot be silently skipped. Keep its local
# reservation/order; a source error is actionable, never an expiry.
ids = {r['id'] for r in runs}
for row in self.queue.snapshot():
if row['phase'] in ('pending', 'ready') and row['id'] not in ids:
raise QueueError('observed_run_disappeared:' + str(row['id']))
self.queue.observe(runs, repository=REPOSITORY, repository_id=REPOSITORY_ID)
def tick(self):
self.reconcile()
self.observe()
row = self.queue.next_ready()
if row is None:
return {'status': 'waiting'}
run = self.source.successful_run(row)
baseline = self.installer.installed_source()
self.source.prove_lineage(baseline, row['sha'])
receipt, data = self.source.artifact(run)
# Staging validates the canonical payload and its relation to this run.
self.installer.stage(row, receipt, data)
self.installer.plan(receipt['artifact'])
# Recheck after potentially slow download/plan; a rerun/pending run may
# have appeared. Reserve still enforces the durable ordering atomically.
self.observe()
self.source.successful_run(row)
self.source.prove_lineage(baseline, row['sha'])
reserved = self.queue.reserve(row['id'], digest=receipt['sha256'], artifact=receipt['artifact'])
try:
self.installer.apply(receipt['artifact'])
finally:
# Even a clean rollback is terminal for this exact artifact. Missing
# or conflicting evidence blocks subsequent installs after restart.
outcome, canonical_receipt = self.installer.receipt(reserved)
self.queue.reconcile(row['id'], digest=receipt['sha256'], artifact=receipt['artifact'],
outcome=outcome, receipt=canonical_receipt)
return {'status': outcome, 'run': row['id'], 'source': row['sha']}