Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions openstack_network_agents/cli/dynamic_routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-FileCopyrightText: 2026 - Canonical Ltd
# SPDX-License-Identifier: Apache-2.0

import json
import logging
import subprocess
import time

import click
from snaphelpers import Snap

from openstack_network_agents.core.ndr_host import HostState, NDRHost, validate

logger = logging.getLogger(__name__)
_RECONCILE_ERRORS = (
KeyError,
TypeError,
ValueError,
RuntimeError,
OSError,
subprocess.SubprocessError,
)


@click.group()
def dynamic_routing() -> None:
"""Validate and reconcile local dynamic routing attachments."""


@dynamic_routing.command("validate")
@click.pass_obj
def validate_instances(snap: Snap) -> None:
"""Validate the JSON instance list on standard input."""
try:
instances = json.load(click.get_text_stream("stdin"))
result = validate(snap, instances)
except _RECONCILE_ERRORS as exc:
raise click.ClickException(str(exc)) from None
click.echo(json.dumps(result, sort_keys=True))


@dynamic_routing.command("reconcile")
@click.pass_obj
def reconcile_instances(snap: Snap) -> None:
"""Activate placement once the planned interface addresses are ready."""
host = NDRHost(snap)
try:
state = host.reconcile()
except _RECONCILE_ERRORS as exc:
raise click.ClickException(str(exc)) from None
click.echo(json.dumps({"ready": state.peerings_ready}))


@dynamic_routing.command("watch", hidden=True)
@click.pass_obj
def watch(snap: Snap) -> None:
"""Restore NDR host readiness independently of Pebble startup."""
host = NDRHost(snap)
state: HostState | None = None
while True:
try:
current = host.reconcile(polling=True)
if current != state:
logger.info("NDR host entered state %s", current)
state = current
except _RECONCILE_ERRORS as exc:
logger.warning("NDR host attachment not ready: %s", exc)
time.sleep(5)
14 changes: 14 additions & 0 deletions openstack_network_agents/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# SPDX-FileCopyrightText: 2025 - Canonical Ltd
# SPDX-License-Identifier: Apache-2.0

import os

import click
from snaphelpers import Snap

from openstack_network_agents.cli.dynamic_routing import dynamic_routing
from openstack_network_agents.cli.log import setup_root_logging
from openstack_network_agents.cli.nics import list_nics
from openstack_network_agents.cli.setup_bridge import setup_bridge
from openstack_network_agents.cli.show_bridge_setup import show_bridge_setup
from openstack_network_agents.core.dynamic_routing import DynamicRouting

CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}

Expand All @@ -18,13 +22,23 @@ def cli(verbose: bool):
"""Set of utilities for managing the agents."""


@cli.command(hidden=True)
@click.pass_obj
def start_pebble(snap: Snap) -> None:
"""Start the shared supervisor with feature startup guards applied."""
routing = DynamicRouting(snap.paths.common, snap.paths.snap)
routing.guard_supervisor_start()
os.execv(routing.pebble, [str(routing.pebble), "run"])


def main():
"""Register commands and run the CLI."""
snap = Snap()
setup_root_logging()
cli.add_command(setup_bridge)
cli.add_command(show_bridge_setup)
cli.add_command(list_nics)
cli.add_command(dynamic_routing)

cli(obj=snap)

Expand Down
Loading
Loading