|
7 | 7 | from loguru import logger |
8 | 8 | from pyroute2 import IPRoute |
9 | 9 | from pyroute2.netlink.rtnl.ifaddrmsg import ifaddrmsg |
10 | | -from sdbus_block.networkmanager import NetworkDeviceGeneric, NetworkManager |
| 10 | +from sdbus_block.networkmanager import ( |
| 11 | + NetworkConnectionSettings, |
| 12 | + NetworkDeviceGeneric, |
| 13 | + NetworkManager, |
| 14 | + NetworkManagerSettings, |
| 15 | +) |
11 | 16 |
|
12 | 17 | from typedefs import NetworkInterfaceMetric, NetworkInterfaceMetricApi |
13 | 18 |
|
@@ -41,8 +46,32 @@ def remove_static_ip(self, interface_name: str, ip: str) -> None: |
41 | 46 | def trigger_dynamic_ip_acquisition(self, interface_name: str) -> None: |
42 | 47 | raise NotImplementedError("This Handler does not support setting interface priority") |
43 | 48 |
|
| 49 | + def cleanup_interface_connections(self, interface_name: str) -> None: |
| 50 | + pass |
| 51 | + |
| 52 | + |
| 53 | +class BookwormHandler(AbstractNetworkHandler): |
| 54 | + """ |
| 55 | + While this class requires NetworkManager, it does NOT use NetworkManager for controlling the interfaces. |
| 56 | + Instead it uses the Bookworm-specific NetworkManagerSettings API to remove the connections. |
| 57 | + It then relies on IPRoute, dhclient, and dnsmasq to manage the interfaces. |
| 58 | + """ |
| 59 | + |
| 60 | + def cleanup_interface_connections(self, interface_name: str) -> None: |
| 61 | + network_manager_settings = NetworkManagerSettings() |
| 62 | + for connection_path in network_manager_settings.connections: |
| 63 | + profile = NetworkConnectionSettings(connection_path).get_profile() |
| 64 | + if profile.connection.interface_name == interface_name: |
| 65 | + logger.info( |
| 66 | + f"Removing connection {profile.connection.uuid} ({profile.connection.connection_id}) for interface {interface_name}" |
| 67 | + ) |
| 68 | + try: |
| 69 | + NetworkManagerSettings().delete_connection_by_uuid(profile.connection.uuid) |
| 70 | + except Exception as e: |
| 71 | + logger.error( |
| 72 | + f"Failed to remove connection {profile.connection.uuid} ({profile.connection.connection_id}) for interface {interface_name}: {e}" |
| 73 | + ) |
44 | 74 |
|
45 | | -class NetworkManagerHandler(AbstractNetworkHandler): |
46 | 75 | def detect(self) -> bool: |
47 | 76 | try: |
48 | 77 | all_devices = {path: NetworkDeviceGeneric(path) for path in network_manager.devices} |
|
0 commit comments