Files
wehub-resource-sync 4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

130 lines
4.1 KiB
Python

"""Gateway stack configuration and persisted deployment outputs."""
from __future__ import annotations
import json
import os
from collections.abc import Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from config.constants import OPENSRE_HOME_DIR
STACK_NAME = "opensre-gateway"
WEB_PROCESS_NAME = "opensre-web"
GATEWAY_PROCESS_NAME = "opensre-gateway"
_STACK_SUFFIX_ENV = "OPENSRE_STACK_SUFFIX"
_OUTPUTS_DIR = OPENSRE_HOME_DIR / "deployments"
_AMI_ID_FILE = _OUTPUTS_DIR / "gateway-id.txt"
@dataclass(frozen=True)
class GatewayStack:
"""Settings for the gateway EC2 deployment."""
stack_name: str
gateway_process_name: str
GATEWAY_STACK = GatewayStack(
stack_name=STACK_NAME,
gateway_process_name=GATEWAY_PROCESS_NAME,
)
def get_stack() -> GatewayStack:
"""Return the gateway stack config.
When ``OPENSRE_STACK_SUFFIX`` is set all resource names are suffixed with
``-<value>`` so each developer gets isolated AWS resources in a shared
account (same convention as the main EC2 stack).
"""
suffix = os.getenv(_STACK_SUFFIX_ENV, "").strip()
if suffix:
return GatewayStack(
stack_name=f"{STACK_NAME}-{suffix}",
gateway_process_name=f"{GATEWAY_PROCESS_NAME}-{suffix}",
)
return GATEWAY_STACK
# ── Outputs ───────────────────────────────────────────────────────────────────
def _outputs_path(*, path: Path | None = None) -> Path:
if path is not None:
return path
stack = get_stack()
return _OUTPUTS_DIR / f"{stack.stack_name}.json"
def save_outputs(outputs: Mapping[str, Any], *, path: Path | None = None) -> Path:
"""Persist deployment outputs to local user state."""
stack = get_stack()
payload = dict(outputs)
payload.setdefault("StackName", stack.stack_name)
output_path = _outputs_path(path=path)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(
json.dumps(payload, indent=2, default=str) + "\n",
encoding="utf-8",
)
return output_path
def outputs_exists(*, path: Path | None = None) -> bool:
return _outputs_path(path=path).exists()
def load_outputs(*, path: Path | None = None) -> dict[str, Any]:
output_path = _outputs_path(path=path)
if not output_path.exists():
stack = get_stack()
raise FileNotFoundError(
f"No outputs found for stack '{stack.stack_name}'. Deploy the stack first."
)
result = json.loads(output_path.read_text(encoding="utf-8"))
if not isinstance(result, dict):
raise ValueError("Deployment outputs file is malformed.")
return result
def delete_outputs(*, path: Path | None = None) -> None:
output_path = _outputs_path(path=path)
if output_path.exists():
output_path.unlink()
# ── AMI id ────────────────────────────────────────────────────────────────────
def _ami_id_path(*, path: Path | None = None) -> Path:
return path if path is not None else _AMI_ID_FILE
def save_ami_id(ami_id: str, *, path: Path | None = None) -> Path:
"""Persist the AMI id produced by ``make bake-gateway``."""
ami_path = _ami_id_path(path=path)
ami_path.parent.mkdir(parents=True, exist_ok=True)
ami_path.write_text(ami_id.strip() + "\n", encoding="utf-8")
return ami_path
def load_ami_id(*, path: Path | None = None) -> str:
"""Load the AMI id saved by the last ``make bake-gateway`` run."""
ami_path = _ami_id_path(path=path)
if not ami_path.exists():
raise FileNotFoundError(
f"No saved gateway AMI id found at {ami_path}. "
"Run `make bake-gateway` first, or set OPENSRE_GATEWAY_AMI_ID."
)
return ami_path.read_text(encoding="utf-8").strip()
def ami_id_exists(*, path: Path | None = None) -> bool:
return _ami_id_path(path=path).exists()