331 lines
8.6 KiB
Python
331 lines
8.6 KiB
Python
"""Read-only framework inspection and provisioning planning."""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
from core.configuration import load_config, validate_config
|
|
from core.compatibility import canonical_hash
|
|
from core.releases import ReleaseManager
|
|
from core.retention import plan_retention, plan_site_retention
|
|
from core.site_policy import validate_site, validate_registry
|
|
from core.preflight import site_preflight
|
|
|
|
|
|
class InspectionError(RuntimeError):
|
|
"""Inspection failed or supplied data is unsafe."""
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise InspectionError(message)
|
|
|
|
|
|
def unique_keys(items):
|
|
result = {}
|
|
|
|
for key, value in items:
|
|
require(
|
|
key not in result,
|
|
"Duplicate JSON field",
|
|
)
|
|
result[key] = value
|
|
|
|
return result
|
|
|
|
|
|
def read_json(path):
|
|
with Path(path).open("rb") as handle:
|
|
raw = handle.read(1024 * 1024 + 1)
|
|
|
|
require(
|
|
len(raw) <= 1024 * 1024,
|
|
"JSON policy exceeds size limit",
|
|
)
|
|
|
|
return json.loads(raw, object_pairs_hook=unique_keys)
|
|
|
|
|
|
def capabilities():
|
|
return {
|
|
"format_version": 1,
|
|
"production_ready": False,
|
|
"deployment_execution_enabled": False,
|
|
"migration_execution_enabled": False,
|
|
"provisioning_execution_enabled": False,
|
|
"database_restore_enabled": False,
|
|
"permanent_deletion_enabled": False,
|
|
"commands": [
|
|
"validate",
|
|
"capabilities",
|
|
"inventory",
|
|
"retention-plan",
|
|
"provision-plan",
|
|
"site-policy",
|
|
"registry-validate",
|
|
"preflight",
|
|
],
|
|
}
|
|
|
|
|
|
def inventory_report(app):
|
|
app = Path(app).absolute()
|
|
|
|
require(
|
|
app.is_dir()
|
|
and not app.is_symlink()
|
|
and app.resolve() == app,
|
|
"Invalid application directory",
|
|
)
|
|
|
|
releases = ReleaseManager(app)
|
|
|
|
inventory = releases.inventory()
|
|
current = releases.current()
|
|
|
|
require(
|
|
current in inventory,
|
|
"Active release missing from inventory",
|
|
)
|
|
|
|
failed = [
|
|
release
|
|
for release in inventory
|
|
if os.path.lexists(
|
|
releases.release_path(release) / "BAD_RELEASE"
|
|
)
|
|
]
|
|
|
|
require(
|
|
current not in failed,
|
|
"Active release is marked failed",
|
|
)
|
|
|
|
require(
|
|
releases.current() == current
|
|
and releases.inventory() == inventory,
|
|
"Release inventory changed during inspection",
|
|
)
|
|
|
|
report = {
|
|
"format_version": 1,
|
|
"status": "INSPECTED",
|
|
"application_path": str(app),
|
|
"current": current,
|
|
"inventory": inventory,
|
|
"failed": failed,
|
|
"transaction_protections_verified": False,
|
|
"runtime_verified": False,
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
report["fingerprint"] = canonical_hash(report)
|
|
|
|
return report
|
|
|
|
|
|
def provisioning_plan(config):
|
|
config = validate_config(config)
|
|
policy = validate_site(config)
|
|
|
|
site = config["site"]
|
|
root = Path(site["root"])
|
|
database = config["database"]
|
|
docker = config["docker"]
|
|
|
|
result = {
|
|
"format_version": 1,
|
|
"status": "PLAN_ONLY",
|
|
"site": site["identifier"],
|
|
"domain": site["domain"],
|
|
"root": str(root),
|
|
"origin_port": site["origin_port"],
|
|
"repository": {
|
|
"url": config["repository"]["url"],
|
|
"branch": config["repository"]["branch"],
|
|
},
|
|
"directories": [
|
|
str(root),
|
|
str(root / "app"),
|
|
str(root / "app/releases"),
|
|
str(root / "app/shared"),
|
|
str(root / "deployment"),
|
|
str(root / "backups"),
|
|
str(root / "deployment/state"),
|
|
str(root / "deployment/approvals"),
|
|
str(root / "deployment/archives"),
|
|
],
|
|
"database": {
|
|
"driver": database["driver"],
|
|
"container": database["container"],
|
|
"name": database["name"],
|
|
"username": database["username"],
|
|
},
|
|
"docker": {
|
|
"project": docker["project"],
|
|
"services": [
|
|
docker["php_service"],
|
|
docker["web_service"],
|
|
docker["queue_service"],
|
|
docker["scheduler_service"],
|
|
],
|
|
"networks": [
|
|
docker["backend_network"],
|
|
docker["proxy_network"],
|
|
],
|
|
},
|
|
"retention": config["retention"],
|
|
"configuration_fingerprint": canonical_hash(config),
|
|
"static_policy_verified": True,
|
|
"site_policy_fingerprint": policy["fingerprint"],
|
|
"resource_conflicts_checked": False,
|
|
"filesystem_permissions_verified": False,
|
|
"secrets_required": True,
|
|
"database_creation_permitted": False,
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
result["fingerprint"] = canonical_hash(result)
|
|
|
|
return result
|
|
|
|
|
|
def parser():
|
|
result = argparse.ArgumentParser(
|
|
prog="absdev-deploy",
|
|
description="ABSDEV deployment framework (inspection only)",
|
|
)
|
|
|
|
commands = result.add_subparsers(
|
|
dest="command",
|
|
required=True,
|
|
)
|
|
|
|
validate = commands.add_parser(
|
|
"validate",
|
|
help="Validate site configuration",
|
|
)
|
|
validate.add_argument("--config", required=True)
|
|
|
|
commands.add_parser(
|
|
"capabilities",
|
|
help="Show enabled framework capabilities",
|
|
)
|
|
|
|
inventory = commands.add_parser(
|
|
"inventory",
|
|
help="Inspect application release inventory",
|
|
)
|
|
inventory.add_argument("--app", required=True)
|
|
|
|
retention = commands.add_parser(
|
|
"retention-plan",
|
|
help="Evaluate a supplied retention policy",
|
|
)
|
|
retention_source = retention.add_mutually_exclusive_group(required=True)
|
|
retention_source.add_argument("--policy")
|
|
retention_source.add_argument("--runtime-root")
|
|
retention.add_argument("--keep", type=int)
|
|
retention.add_argument("--site")
|
|
|
|
provision = commands.add_parser(
|
|
"provision-plan",
|
|
help="Generate a read-only provisioning plan",
|
|
)
|
|
provision.add_argument("--config", required=True)
|
|
|
|
policy = commands.add_parser(
|
|
"site-policy",
|
|
help="Validate static site isolation policy",
|
|
)
|
|
policy.add_argument("--config", required=True)
|
|
|
|
registry = commands.add_parser(
|
|
"registry-validate",
|
|
help="Validate supplied site registry",
|
|
)
|
|
registry.add_argument("--registry", required=True)
|
|
|
|
preflight = commands.add_parser(
|
|
"preflight",
|
|
help="Check prospective site against supplied registry",
|
|
)
|
|
preflight.add_argument("--config", required=True)
|
|
preflight.add_argument("--registry", required=True)
|
|
|
|
return result
|
|
|
|
|
|
def main(argv=None):
|
|
args = parser().parse_args(argv)
|
|
|
|
try:
|
|
if args.command == "capabilities":
|
|
report = capabilities()
|
|
|
|
elif args.command == "inventory":
|
|
report = inventory_report(args.app)
|
|
|
|
elif args.command == "retention-plan":
|
|
if args.runtime_root:
|
|
report = plan_site_retention(args.runtime_root, keep=args.keep, site=args.site)
|
|
else:
|
|
if args.keep is not None or args.site is not None:
|
|
raise InspectionError("--keep and --site require --runtime-root")
|
|
report = plan_retention(read_json(args.policy))
|
|
|
|
elif args.command == "provision-plan":
|
|
report = provisioning_plan(
|
|
load_config(args.config)
|
|
)
|
|
|
|
elif args.command == "site-policy":
|
|
report = validate_site(
|
|
load_config(args.config)
|
|
)
|
|
|
|
elif args.command == "registry-validate":
|
|
report = validate_registry(
|
|
read_json(args.registry)
|
|
)
|
|
|
|
elif args.command == "preflight":
|
|
report = site_preflight(
|
|
load_config(args.config),
|
|
read_json(args.registry),
|
|
)
|
|
|
|
elif args.command == "validate":
|
|
load_config(args.config)
|
|
|
|
report = {
|
|
"status": "VALID",
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
else:
|
|
raise InspectionError("Unsupported command")
|
|
|
|
print(json.dumps(
|
|
report,
|
|
indent=2,
|
|
sort_keys=True,
|
|
allow_nan=False,
|
|
))
|
|
|
|
return 0
|
|
|
|
except Exception as error:
|
|
# Avoid exposing configuration contents,
|
|
# credentials or external command output.
|
|
|
|
print(
|
|
f"Inspection failed: {type(error).__name__}",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
return 1
|