364 lines
9.4 KiB
Python
364 lines
9.4 KiB
Python
"""Coordinated deployment with conservative failure containment.
|
|
|
|
External service operations must be provided by trusted adapters.
|
|
This module does not directly execute Docker or database commands.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
from core.compatibility import require_compatible
|
|
from core.backup_evidence import BackupEvidence
|
|
from core.operation_guard import OperationGuard
|
|
from core.locking import DeploymentLock
|
|
|
|
|
|
class DeploymentError(RuntimeError):
|
|
"""Deployment failed or requires explicit recovery."""
|
|
|
|
|
|
def require(condition, message):
|
|
|
|
if not condition:
|
|
raise DeploymentError(message)
|
|
|
|
|
|
class DeploymentController:
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
site,
|
|
lock_path,
|
|
releases,
|
|
provenance,
|
|
journal,
|
|
backup,
|
|
services,
|
|
health,
|
|
runtime_check,
|
|
stopped_check,
|
|
backup_evidence,
|
|
operation_guard,
|
|
):
|
|
|
|
self.site = site
|
|
self.lock_path = Path(lock_path)
|
|
self.releases = releases
|
|
self.provenance = provenance
|
|
self.journal = journal
|
|
self.backup = backup
|
|
self.services = services
|
|
self.health = health
|
|
self.runtime_check = runtime_check
|
|
self.stopped_check = stopped_check
|
|
|
|
require(
|
|
isinstance(backup_evidence, BackupEvidence),
|
|
"A persistent backup evidence store is required",
|
|
)
|
|
|
|
self.backup_evidence = backup_evidence
|
|
|
|
require(
|
|
isinstance(operation_guard, OperationGuard),
|
|
"A shared operation guard is required",
|
|
)
|
|
|
|
self.operation_guard = operation_guard
|
|
|
|
def deploy(self, candidate):
|
|
|
|
with DeploymentLock(self.lock_path):
|
|
|
|
return self._deploy(candidate)
|
|
|
|
def _deploy(self, candidate):
|
|
|
|
transaction = None
|
|
quiesce_started = False
|
|
operation = "preflight"
|
|
|
|
try:
|
|
|
|
self.operation_guard.require_clear()
|
|
|
|
require(
|
|
self.journal.load() is None,
|
|
"Existing transaction requires review",
|
|
)
|
|
|
|
previous = self.releases.current()
|
|
|
|
require(
|
|
previous is not None,
|
|
"Initial deployment requires separate provisioning",
|
|
)
|
|
|
|
require(
|
|
previous != candidate,
|
|
"Candidate is already active",
|
|
)
|
|
|
|
old_path = self.releases.validate(previous)
|
|
|
|
new_path = self.releases.validate(candidate)
|
|
|
|
old_evidence = self.provenance.verify(old_path)
|
|
|
|
new_evidence = self.provenance.verify(new_path)
|
|
|
|
require_compatible(
|
|
old_evidence,
|
|
new_evidence,
|
|
)
|
|
|
|
require(
|
|
self.runtime_check() is True,
|
|
"Database runtime verification failed",
|
|
)
|
|
|
|
operation = "create transaction"
|
|
|
|
transaction = self.journal.create(
|
|
site=self.site,
|
|
previous=previous,
|
|
candidate=candidate,
|
|
revision=new_evidence["revision"],
|
|
provenance=new_evidence["fingerprint"],
|
|
previous_revision=old_evidence["revision"],
|
|
previous_provenance=old_evidence["fingerprint"],
|
|
)
|
|
|
|
def advance(phase):
|
|
|
|
nonlocal transaction
|
|
|
|
transaction = self.journal.transition(
|
|
transaction["transaction_id"],
|
|
transaction["phase"],
|
|
phase,
|
|
)
|
|
|
|
operation = "database backup"
|
|
|
|
backup = self.backup.create()
|
|
manifest = self.backup.verify(backup)
|
|
|
|
operation = "persist backup evidence"
|
|
|
|
evidence = self.backup_evidence.create(
|
|
transaction["transaction_id"],
|
|
manifest,
|
|
)
|
|
|
|
require(
|
|
self.backup_evidence.load(
|
|
transaction["transaction_id"]
|
|
) == evidence,
|
|
"Backup evidence readback failed",
|
|
)
|
|
|
|
advance("BACKED_UP")
|
|
|
|
operation = "quiesce background workers"
|
|
|
|
quiesce_started = True
|
|
|
|
self.services.quiesce()
|
|
|
|
require(
|
|
self.stopped_check() is True,
|
|
"Worker shutdown could not be verified",
|
|
)
|
|
|
|
advance("QUIESCED")
|
|
|
|
operation = "revalidate deployment"
|
|
|
|
require(
|
|
self.releases.current() == previous,
|
|
"Active release changed during deployment",
|
|
)
|
|
|
|
require(
|
|
self.runtime_check() is True,
|
|
"Database runtime verification changed",
|
|
)
|
|
|
|
require(
|
|
self.provenance.verify(
|
|
self.releases.validate(previous)
|
|
) == old_evidence,
|
|
"Previous release provenance changed",
|
|
)
|
|
|
|
require(
|
|
self.provenance.verify(
|
|
self.releases.validate(candidate)
|
|
) == new_evidence,
|
|
"Candidate provenance changed",
|
|
)
|
|
|
|
operation = "activate candidate"
|
|
|
|
advance("ACTIVATING")
|
|
|
|
self.releases.activate(candidate)
|
|
|
|
advance("ACTIVATED")
|
|
|
|
operation = "refresh application services"
|
|
|
|
self.services.refresh()
|
|
|
|
operation = "restore background workers"
|
|
|
|
self.services.resume()
|
|
|
|
operation = "verify application health"
|
|
|
|
report = self.health.verify(candidate)
|
|
|
|
require(
|
|
isinstance(report, dict)
|
|
and report.get("healthy") is True
|
|
and report.get("release") == candidate,
|
|
"Application health verification failed",
|
|
)
|
|
|
|
require(
|
|
self.releases.current() == candidate,
|
|
"Active release changed after verification",
|
|
)
|
|
|
|
operation = "finalise deployment"
|
|
|
|
advance("VERIFIED")
|
|
|
|
return transaction
|
|
|
|
except Exception:
|
|
|
|
containment_ok = True
|
|
journal_ok = True
|
|
|
|
if quiesce_started:
|
|
|
|
try:
|
|
|
|
self.services.quiesce()
|
|
|
|
containment_ok = (
|
|
self.stopped_check() is True
|
|
)
|
|
|
|
except Exception:
|
|
|
|
containment_ok = False
|
|
|
|
if transaction is not None:
|
|
|
|
try:
|
|
|
|
actual = self.journal.load()
|
|
|
|
if (
|
|
actual is None
|
|
or actual["transaction_id"]
|
|
!= transaction["transaction_id"]
|
|
):
|
|
|
|
journal_ok = False
|
|
|
|
elif actual["phase"] not in (
|
|
"VERIFIED",
|
|
"FAILED",
|
|
):
|
|
|
|
self.journal.transition(
|
|
actual["transaction_id"],
|
|
actual["phase"],
|
|
"FAILED",
|
|
)
|
|
|
|
elif actual["phase"] == "VERIFIED":
|
|
|
|
journal_ok = False
|
|
|
|
except Exception:
|
|
|
|
journal_ok = False
|
|
|
|
raise DeploymentError(
|
|
f"Deployment failed during {operation}; "
|
|
f"containment={containment_ok}; "
|
|
f"journal={journal_ok}; "
|
|
"explicit inspection required"
|
|
) from None
|
|
|
|
def recovery_plan(self):
|
|
|
|
with DeploymentLock(self.lock_path):
|
|
|
|
transaction = self.journal.load()
|
|
|
|
if transaction is None:
|
|
|
|
return {
|
|
"status": "CLEAR",
|
|
"automatic_rollback_allowed": False,
|
|
}
|
|
|
|
current = self.releases.current()
|
|
|
|
transaction_id = transaction["transaction_id"]
|
|
|
|
evidence_path = self.backup_evidence.path(
|
|
transaction_id
|
|
)
|
|
|
|
evidence_present = (
|
|
evidence_path.exists()
|
|
or evidence_path.is_symlink()
|
|
)
|
|
|
|
backup_id = None
|
|
|
|
if evidence_present:
|
|
|
|
try:
|
|
evidence = self.backup_evidence.load(
|
|
transaction_id
|
|
)
|
|
|
|
backup_id = evidence["backup"]["backup_id"]
|
|
|
|
except Exception:
|
|
raise DeploymentError(
|
|
"Invalid backup evidence; recovery blocked"
|
|
) from None
|
|
|
|
backup_required = any(
|
|
event["phase"] == "BACKED_UP"
|
|
for event in transaction["history"]
|
|
)
|
|
|
|
require(
|
|
not backup_required or evidence_present,
|
|
"Required backup evidence is missing",
|
|
)
|
|
|
|
return {
|
|
"backup_evidence_present": evidence_present,
|
|
"backup_id": backup_id,
|
|
"status": transaction["phase"],
|
|
"transaction_id": (
|
|
transaction["transaction_id"]
|
|
),
|
|
"previous": transaction["previous"],
|
|
"candidate": transaction["candidate"],
|
|
"current": current,
|
|
"automatic_rollback_allowed": False,
|
|
"explicit_review_required": True,
|
|
}
|