Skip to content

Commit

Permalink
cable-guy: cleanup networks managed by NetworkManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani committed Jan 17, 2025
1 parent 1e4767a commit 7984dc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/services/cable_guy/api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def set_configuration(self, interface: NetworkInterface) -> None:
Args:
interface: NetworkInterface
"""
self.network_handler.cleanup_interface_connections(interface.name)
interfaces = self.get_ethernet_interfaces()
valid_names = [interface.name for interface in interfaces]
if interface.name not in valid_names:
Expand Down
34 changes: 32 additions & 2 deletions core/services/cable_guy/networksetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
from loguru import logger
from pyroute2 import IPRoute
from pyroute2.netlink.rtnl.ifaddrmsg import ifaddrmsg
from sdbus_block.networkmanager import NetworkDeviceGeneric, NetworkManager
from sdbus_block.networkmanager import (
ConnectionType,
NetworkConnectionSettings,
NetworkDeviceGeneric,
NetworkManager,
NetworkManagerSettings,
)

from typedefs import NetworkInterfaceMetric, NetworkInterfaceMetricApi

Expand Down Expand Up @@ -41,8 +47,32 @@ def remove_static_ip(self, interface_name: str, ip: str) -> None:
def trigger_dynamic_ip_acquisition(self, interface_name: str) -> None:
raise NotImplementedError("This Handler does not support setting interface priority")

def cleanup_interface_connections(self, interface_name: str) -> None:
pass


class BookwormHandler(AbstractNetworkHandler):
"""
While this class requires NetworkManager, it does NOT use NetworkManager for controlling the interfaces.
Instead it uses the Bookworm-specific NetworkManagerSettings API to remove the connections.
It then relies on IPRoute, dhclient, and dnsmasq to manage the interfaces.
"""

def cleanup_interface_connections(self, interface_name: str) -> None:
network_manager_settings = NetworkManagerSettings()
for connection_path in network_manager_settings.connections:
profile = NetworkConnectionSettings(connection_path).get_profile()
if profile.connection.interface_name == interface_name:
logger.info(
f"Removing connection {profile.connection.uuid} ({profile.connection.connection_id}) for interface {interface_name}"
)
try:
NetworkManagerSettings().delete_connection_by_uuid(profile.connection.uuid)
except Exception as e:
logger.error(
f"Failed to remove connection {profile.connection.uuid} ({profile.connection.connection_id}) for interface {interface_name}: {e}"
)

class NetworkManagerHandler(AbstractNetworkHandler):
def detect(self) -> bool:
try:
all_devices = {path: NetworkDeviceGeneric(path) for path in network_manager.devices}
Expand Down

0 comments on commit 7984dc7

Please sign in to comment.