mirror of
https://github.com/Such-Software/adaptive-multi-protocol-gateway.git
synced 2026-12-01 12:24:03 +01:00
Add package deploy apply stage
This commit is contained in:
+169
-5
@@ -16,8 +16,10 @@ from .addresses import (
|
||||
from .config import GatewayConfig
|
||||
from .health import HealthCheck, health_plan
|
||||
from .install_plan import (
|
||||
InstallPackageAction,
|
||||
InstallStateCopy,
|
||||
InstallSupervisorAction,
|
||||
install_package_actions,
|
||||
install_state_copies,
|
||||
install_supervisor_actions,
|
||||
)
|
||||
@@ -61,6 +63,19 @@ class CommandRunResult:
|
||||
stderr: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PackageApplyResult:
|
||||
site_id: str
|
||||
protocol: str
|
||||
platform: str
|
||||
package: str
|
||||
status: str
|
||||
mode: str
|
||||
command: tuple[str, ...]
|
||||
return_code: int | None
|
||||
message: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StartApplyResult:
|
||||
site_id: str
|
||||
@@ -106,6 +121,62 @@ class HealthApplyResult:
|
||||
CommandRunner = Callable[[tuple[str, ...]], CommandRunResult]
|
||||
|
||||
|
||||
def apply_packages(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
dry_run: bool,
|
||||
platform_provider: PlatformProvider | None = None,
|
||||
daemon_probe: DaemonProbeFunc | None = None,
|
||||
command_runner: CommandRunner | None = None,
|
||||
) -> list[PackageApplyResult]:
|
||||
provider = platform_provider or detect_platform()
|
||||
actions = install_package_actions(
|
||||
config,
|
||||
platform_provider=provider,
|
||||
daemon_probe=daemon_probe,
|
||||
)
|
||||
runner = command_runner or _run_command
|
||||
results: list[PackageApplyResult] = []
|
||||
for action in actions:
|
||||
validation = _validate_package_action(action, dry_run=dry_run)
|
||||
if validation.status in {"blocked", "skipped"}:
|
||||
results.append(validation)
|
||||
continue
|
||||
if dry_run:
|
||||
results.append(
|
||||
_package_result(
|
||||
action,
|
||||
status="planned",
|
||||
mode="dry-run",
|
||||
return_code=None,
|
||||
message="would install managed daemon package",
|
||||
)
|
||||
)
|
||||
continue
|
||||
run = runner(action.command)
|
||||
if run.return_code != 0:
|
||||
results.append(
|
||||
_package_result(
|
||||
action,
|
||||
status="blocked",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message=_command_failure_message(run, label="package install command"),
|
||||
)
|
||||
)
|
||||
continue
|
||||
results.append(
|
||||
_package_result(
|
||||
action,
|
||||
status="installed",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message="installed managed daemon package",
|
||||
)
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
def apply_state(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
@@ -268,7 +339,7 @@ def apply_start(
|
||||
status="blocked",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message=_command_failure_message(run),
|
||||
message=_command_failure_message(run, label="start command"),
|
||||
)
|
||||
)
|
||||
continue
|
||||
@@ -333,7 +404,7 @@ def apply_health(
|
||||
status="blocked",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message=_command_failure_message(run),
|
||||
message=_command_failure_message(run, label="health check command"),
|
||||
)
|
||||
)
|
||||
continue
|
||||
@@ -350,6 +421,61 @@ def apply_health(
|
||||
return results
|
||||
|
||||
|
||||
def _validate_package_action(
|
||||
action: InstallPackageAction,
|
||||
*,
|
||||
dry_run: bool,
|
||||
) -> PackageApplyResult:
|
||||
mode = "dry-run" if dry_run else "live"
|
||||
if action.status == "ready":
|
||||
return _package_result(
|
||||
action,
|
||||
status="skipped",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message=action.message,
|
||||
)
|
||||
if action.status != "planned":
|
||||
return _package_result(
|
||||
action,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message=action.message,
|
||||
)
|
||||
if not _valid_package_name(action.package):
|
||||
return _package_result(
|
||||
action,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message=f"invalid package name: {action.package}",
|
||||
)
|
||||
if not action.command:
|
||||
return _package_result(
|
||||
action,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message="selected platform does not have an automatic package install command",
|
||||
)
|
||||
if not _allowed_package_command(action):
|
||||
return _package_result(
|
||||
action,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message="package install command is not allowlisted",
|
||||
)
|
||||
return _package_result(
|
||||
action,
|
||||
status="ready",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message="package install command passed safety checks",
|
||||
)
|
||||
|
||||
|
||||
def _validate_state_copy(
|
||||
config: GatewayConfig,
|
||||
copy: InstallStateCopy,
|
||||
@@ -614,6 +740,27 @@ def _address_result(
|
||||
)
|
||||
|
||||
|
||||
def _package_result(
|
||||
action: InstallPackageAction,
|
||||
*,
|
||||
status: str,
|
||||
mode: str,
|
||||
return_code: int | None,
|
||||
message: str,
|
||||
) -> PackageApplyResult:
|
||||
return PackageApplyResult(
|
||||
site_id=action.site_id,
|
||||
protocol=action.protocol,
|
||||
platform=action.platform,
|
||||
package=action.package,
|
||||
status=status,
|
||||
mode=mode,
|
||||
command=action.command,
|
||||
return_code=return_code,
|
||||
message=message,
|
||||
)
|
||||
|
||||
|
||||
def _health_result(
|
||||
check: HealthCheck,
|
||||
*,
|
||||
@@ -758,6 +905,23 @@ def _valid_service_name(service: str) -> bool:
|
||||
return all(char.isalnum() or char in {"-", "_", "."} for char in service)
|
||||
|
||||
|
||||
def _valid_package_name(package: str) -> bool:
|
||||
if not package or package == "-":
|
||||
return False
|
||||
return all(char.isalnum() or char in {"-", "_", ".", "+"} for char in package)
|
||||
|
||||
|
||||
def _allowed_package_command(action: InstallPackageAction) -> bool:
|
||||
package = action.package
|
||||
if action.platform == "android-termux":
|
||||
return action.command == ("pkg", "install", package)
|
||||
if action.platform == "linux-systemd":
|
||||
return action.command == ("sudo", "apt", "install", package)
|
||||
if action.platform == "macos-launchd":
|
||||
return action.command == ("brew", "install", package)
|
||||
return False
|
||||
|
||||
|
||||
def _chmod_supervisor_target(provider: PlatformProvider, target: Path) -> None:
|
||||
if provider.name not in {"android-termux", "linux-user"}:
|
||||
return
|
||||
@@ -790,11 +954,11 @@ def _run_command(command: tuple[str, ...]) -> CommandRunResult:
|
||||
)
|
||||
|
||||
|
||||
def _command_failure_message(run: CommandRunResult) -> str:
|
||||
def _command_failure_message(run: CommandRunResult, *, label: str = "command") -> str:
|
||||
detail = run.stderr or run.stdout
|
||||
if detail:
|
||||
return f"start command failed: {detail}"
|
||||
return "start command failed"
|
||||
return f"{label} failed: {detail}"
|
||||
return f"{label} failed"
|
||||
|
||||
|
||||
def format_command(command: tuple[str, ...]) -> str:
|
||||
|
||||
+42
-1
@@ -24,11 +24,13 @@ from .addresses import (
|
||||
from .apply import (
|
||||
AddressApplyResult,
|
||||
HealthApplyResult,
|
||||
PackageApplyResult,
|
||||
StateApplyResult,
|
||||
StartApplyResult,
|
||||
SupervisorApplyResult,
|
||||
apply_addresses,
|
||||
apply_health,
|
||||
apply_packages,
|
||||
apply_state,
|
||||
apply_start,
|
||||
apply_supervisor,
|
||||
@@ -164,7 +166,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
_add_target_selection(deploy_apply_parser)
|
||||
deploy_apply_parser.add_argument(
|
||||
"--stage",
|
||||
choices=("state", "supervisor", "start", "addresses", "health"),
|
||||
choices=("packages", "state", "supervisor", "start", "addresses", "health"),
|
||||
required=True,
|
||||
help="Deployment stage to apply.",
|
||||
)
|
||||
@@ -583,6 +585,29 @@ def _cmd_deploy(config, args) -> int:
|
||||
)
|
||||
return 1
|
||||
platform_provider = _platform_override(config, args)
|
||||
if args.stage == "packages":
|
||||
results = apply_packages(
|
||||
config,
|
||||
dry_run=args.dry_run,
|
||||
platform_provider=platform_provider,
|
||||
)
|
||||
for result in results:
|
||||
_print_package_apply_result(result)
|
||||
blocked = [result for result in results if result.status == "blocked"]
|
||||
installed = [result for result in results if result.status == "installed"]
|
||||
planned = [result for result in results if result.status == "planned"]
|
||||
skipped = [result for result in results if result.status == "skipped"]
|
||||
print(
|
||||
"AMPG_DEPLOY_APPLY_SUMMARY "
|
||||
f"stage=packages "
|
||||
f"mode={'dry-run' if args.dry_run else 'live'} "
|
||||
f"results={len(results)} "
|
||||
f"planned={len(planned)} "
|
||||
f"installed={len(installed)} "
|
||||
f"skipped={len(skipped)} "
|
||||
f"blocked={len(blocked)}"
|
||||
)
|
||||
return 1 if blocked else 0
|
||||
if args.stage == "state":
|
||||
results = apply_state(
|
||||
config,
|
||||
@@ -1171,6 +1196,22 @@ def _print_state_apply_result(result: StateApplyResult) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _print_package_apply_result(result: PackageApplyResult) -> None:
|
||||
return_code = "-" if result.return_code is None else str(result.return_code)
|
||||
print(
|
||||
"AMPG_DEPLOY_PACKAGE "
|
||||
f"site={result.site_id} "
|
||||
f"protocol={result.protocol} "
|
||||
f"platform={result.platform} "
|
||||
f"package=\"{_quote(result.package)}\" "
|
||||
f"mode={result.mode} "
|
||||
f"status={result.status} "
|
||||
f"return_code={return_code} "
|
||||
f"command=\"{_quote(format_command(result.command))}\" "
|
||||
f"message=\"{_quote(result.message)}\""
|
||||
)
|
||||
|
||||
|
||||
def _print_supervisor_apply_result(result: SupervisorApplyResult) -> None:
|
||||
print(
|
||||
"AMPG_DEPLOY_SUPERVISOR "
|
||||
|
||||
@@ -19,6 +19,7 @@ from .dns import dns_plan
|
||||
from .health import health_plan
|
||||
from .install_plan import (
|
||||
install_artifacts,
|
||||
install_package_actions,
|
||||
install_plan,
|
||||
install_state_copies,
|
||||
install_supervisor_actions,
|
||||
@@ -82,6 +83,12 @@ def deploy_plan(
|
||||
platform_provider=platform_provider,
|
||||
daemon_probe=daemon_probe,
|
||||
),
|
||||
_package_step(
|
||||
config,
|
||||
command_context,
|
||||
platform_provider=platform_provider,
|
||||
daemon_probe=daemon_probe,
|
||||
),
|
||||
_artifact_step(config, command_context, platform_provider=platform_provider),
|
||||
_apply_preflight_step(
|
||||
config,
|
||||
@@ -230,6 +237,52 @@ def _daemon_step(
|
||||
)
|
||||
|
||||
|
||||
def _package_step(
|
||||
config: GatewayConfig,
|
||||
commands: _CommandContext,
|
||||
*,
|
||||
platform_provider: PlatformProvider | None,
|
||||
daemon_probe: DaemonProbeFunc | None,
|
||||
) -> DeployStep:
|
||||
actions = install_package_actions(
|
||||
config,
|
||||
platform_provider=platform_provider,
|
||||
daemon_probe=daemon_probe,
|
||||
)
|
||||
if not actions:
|
||||
return DeployStep("packages", "ready", "-", "no managed package installs are needed")
|
||||
blocked = [action for action in actions if action.status == "blocked"]
|
||||
planned = [action for action in actions if action.status == "planned"]
|
||||
manual = [action for action in planned if not action.command]
|
||||
if blocked:
|
||||
return DeployStep(
|
||||
"packages",
|
||||
"blocked",
|
||||
commands.command("install-plan", platform=True),
|
||||
f"fix {len(blocked)} package planning blocker(s)",
|
||||
)
|
||||
if manual:
|
||||
return DeployStep(
|
||||
"packages",
|
||||
"review",
|
||||
commands.command("install-plan", platform=True),
|
||||
f"install {len(manual)} package(s) with the local package manager",
|
||||
)
|
||||
if planned:
|
||||
return DeployStep(
|
||||
"packages",
|
||||
"todo",
|
||||
commands.command("deploy apply --stage packages --dry-run", platform=True),
|
||||
f"install {len(planned)} managed daemon package(s)",
|
||||
)
|
||||
return DeployStep(
|
||||
"packages",
|
||||
"ready",
|
||||
commands.command("deploy apply --stage packages --dry-run", platform=True),
|
||||
"managed daemon packages are installed",
|
||||
)
|
||||
|
||||
|
||||
def _artifact_step(
|
||||
config: GatewayConfig,
|
||||
commands: _CommandContext,
|
||||
|
||||
+63
-4
@@ -66,6 +66,17 @@ class InstallSupervisorAction:
|
||||
message: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class InstallPackageAction:
|
||||
site_id: str
|
||||
protocol: str
|
||||
platform: str
|
||||
package: str
|
||||
status: str
|
||||
command: tuple[str, ...]
|
||||
message: str
|
||||
|
||||
|
||||
def install_plan(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
@@ -120,6 +131,47 @@ def install_artifacts(
|
||||
return artifacts
|
||||
|
||||
|
||||
def install_package_actions(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
platform_provider: PlatformProvider | None = None,
|
||||
daemon_probe: DaemonProbeFunc | None = None,
|
||||
) -> list[InstallPackageAction]:
|
||||
provider = platform_provider or detect_platform()
|
||||
steps = install_plan(
|
||||
config,
|
||||
platform_provider=provider,
|
||||
daemon_probe=daemon_probe,
|
||||
)
|
||||
actions: list[InstallPackageAction] = []
|
||||
for step in steps:
|
||||
if step.stage == "package":
|
||||
actions.append(
|
||||
InstallPackageAction(
|
||||
site_id=step.site_id,
|
||||
protocol=step.protocol,
|
||||
platform=step.platform,
|
||||
package=step.target,
|
||||
status=step.status,
|
||||
command=package_install_command(provider, step.target),
|
||||
message=step.message,
|
||||
)
|
||||
)
|
||||
elif step.status == "blocked":
|
||||
actions.append(
|
||||
InstallPackageAction(
|
||||
site_id=step.site_id,
|
||||
protocol=step.protocol,
|
||||
platform=step.platform,
|
||||
package=step.target,
|
||||
status="blocked",
|
||||
command=(),
|
||||
message=step.message,
|
||||
)
|
||||
)
|
||||
return actions
|
||||
|
||||
|
||||
def write_install_artifacts(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
@@ -513,13 +565,20 @@ def _package_name(provider: PlatformProvider, daemon: str) -> str:
|
||||
return daemon
|
||||
|
||||
|
||||
def _package_command(provider: PlatformProvider, package: str) -> str:
|
||||
def package_install_command(provider: PlatformProvider, package: str) -> tuple[str, ...]:
|
||||
if provider.name == "android-termux":
|
||||
return f"pkg install {package}"
|
||||
return ("pkg", "install", package)
|
||||
if provider.name == "linux-systemd":
|
||||
return f"sudo apt install {package}"
|
||||
return ("sudo", "apt", "install", package)
|
||||
if provider.name == "macos-launchd":
|
||||
return f"brew install {package}"
|
||||
return ("brew", "install", package)
|
||||
return ()
|
||||
|
||||
|
||||
def _package_command(provider: PlatformProvider, package: str) -> str:
|
||||
command = package_install_command(provider, package)
|
||||
if command:
|
||||
return shlex.join(command)
|
||||
if provider.name == "linux-user":
|
||||
return f"install {package} with the user package manager"
|
||||
return "-"
|
||||
|
||||
Reference in New Issue
Block a user