53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
"""Egress apply for the smolmachines backend.
|
|
|
|
The smolmachines sidecar bundle runs as a sidecar smolVM. Route-file
|
|
inspection and reload signalling therefore go through ``smolvm machine
|
|
exec`` instead of Docker.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from ...egress import EGRESS_ROUTES_IN_CONTAINER
|
|
from ...log import warn
|
|
from ..egress_apply import EgressApplicator, EgressApplyError
|
|
from . import sidecar_bundle as _bundle
|
|
from . import smolvm as _smolvm
|
|
|
|
|
|
def fetch_current_routes(slug: str) -> str:
|
|
machine = _bundle.bundle_machine_name(slug)
|
|
result = _smolvm.machine_exec(machine, ["cat", EGRESS_ROUTES_IN_CONTAINER])
|
|
if result.returncode != 0:
|
|
raise EgressApplyError(
|
|
f"could not read routes.yaml from {machine}: "
|
|
f"{(result.stderr or '').strip() or 'sidecar VM not running?'}"
|
|
)
|
|
return result.stdout
|
|
|
|
|
|
class SmolmachinesEgressApplicator(EgressApplicator):
|
|
def _signal_bundle_reload(self, slug: str) -> None:
|
|
machine = _bundle.bundle_machine_name(slug)
|
|
result = _smolvm.machine_exec(machine, ["sh", "-c", "kill -HUP 1"])
|
|
if result.returncode != 0:
|
|
last_error = (result.stderr or "").strip() or (result.stdout or "").strip()
|
|
warn(
|
|
f"egress: routes updated on disk for {slug}, but bundle reload failed: "
|
|
f"{last_error or 'smolvm exec failed'}"
|
|
)
|
|
raise EgressApplyError(
|
|
f"could not reload egress bundle {machine}: "
|
|
f"{last_error or 'smolvm exec failed'}"
|
|
)
|
|
|
|
|
|
applicator = SmolmachinesEgressApplicator()
|
|
|
|
|
|
__all__ = [
|
|
"SmolmachinesEgressApplicator",
|
|
"EgressApplyError",
|
|
"applicator",
|
|
"fetch_current_routes",
|
|
]
|