summaryrefslogtreecommitdiff
path: root/cloudinit/net/ephemeral.py
diff options
context:
space:
mode:
authorBrett Holman <brett.holman@canonical.com>2023-04-19 16:12:55 -0600
committerGitHub <noreply@github.com>2023-04-19 16:12:55 -0600
commit5942f4023e2581a43f31d547995095ca49954353 (patch)
tree510009a412671c4adb041c05fc803db1ea4e8c22 /cloudinit/net/ephemeral.py
parent9e4cb4f06b2e362889bcbda77ef6fad52afed52b (diff)
downloadcloud-init-git-5942f4023e2581a43f31d547995095ca49954353.tar.gz
[1/2] DHCP: Refactor dhcp client code (#2122)
Move isc-dhclient code to dhcp.py In support of the upcoming deprecation of isc-dhcp-client, this code refactors current dhcp code into classes in dhcp.py. The primary user-visible change should be the addition of the following log: dhcp.py[DEBUG]: DHCP client selected: dhclient This code lays groundwork to enable alternate implementations to live side by side in the codebase to be selected with distro-defined priority fallback. Note that maybe_perform_dhcp_discovery() now selects which dhcp client to call, and then runs the corresponding client's dhcp_discovery() method. Currently only class IscDhclient is implemented, however a yet-to-be-implemented class Dhcpcd exists to test fallback behavior and this will be implemented in part two of this series. Part of this refactor includes shifting dhclient service management from hardcoded calls to the distro-defined manage_service() method in the *BSDs. Future work is required in this area to support multiple clients via select_dhcp_client().
Diffstat (limited to 'cloudinit/net/ephemeral.py')
-rw-r--r--cloudinit/net/ephemeral.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/cloudinit/net/ephemeral.py b/cloudinit/net/ephemeral.py
index 1dfd1c42..130afa17 100644
--- a/cloudinit/net/ephemeral.py
+++ b/cloudinit/net/ephemeral.py
@@ -9,9 +9,9 @@ from typing import Any, Dict, List, Optional
import cloudinit.net as net
from cloudinit import subp
from cloudinit.net.dhcp import (
+ IscDhclient,
NoDHCPLeaseError,
maybe_perform_dhcp_discovery,
- parse_static_routes,
)
LOG = logging.getLogger(__name__)
@@ -305,6 +305,7 @@ class EphemeralIPv6Network:
class EphemeralDHCPv4:
def __init__(
self,
+ distro,
iface=None,
connectivity_url_data: Optional[Dict[str, Any]] = None,
dhcp_log_func=None,
@@ -314,6 +315,7 @@ class EphemeralDHCPv4:
self.lease = None
self.dhcp_log_func = dhcp_log_func
self.connectivity_url_data = connectivity_url_data
+ self.distro = distro
def __enter__(self):
"""Setup sandboxed dhcp context, unless connectivity_url can already be
@@ -351,7 +353,9 @@ class EphemeralDHCPv4:
"""
if self.lease:
return self.lease
- leases = maybe_perform_dhcp_discovery(self.iface, self.dhcp_log_func)
+ leases = maybe_perform_dhcp_discovery(
+ self.distro, self.iface, self.dhcp_log_func
+ )
if not leases:
raise NoDHCPLeaseError()
self.lease = leases[-1]
@@ -378,7 +382,7 @@ class EphemeralDHCPv4:
kwargs["prefix_or_mask"], kwargs["ip"]
)
if kwargs["static_routes"]:
- kwargs["static_routes"] = parse_static_routes(
+ kwargs["static_routes"] = IscDhclient.parse_static_routes(
kwargs["static_routes"]
)
if self.connectivity_url_data:
@@ -412,6 +416,7 @@ class EphemeralIPNetwork:
def __init__(
self,
+ distro,
interface,
ipv6: bool = False,
ipv4: bool = True,
@@ -421,13 +426,16 @@ class EphemeralIPNetwork:
self.ipv6 = ipv6
self.stack = contextlib.ExitStack()
self.state_msg: str = ""
+ self.distro = distro
def __enter__(self):
# ipv6 dualstack might succeed when dhcp4 fails
# therefore catch exception unless only v4 is used
try:
if self.ipv4:
- self.stack.enter_context(EphemeralDHCPv4(self.interface))
+ self.stack.enter_context(
+ EphemeralDHCPv4(self.distro, self.interface)
+ )
if self.ipv6:
self.stack.enter_context(EphemeralIPv6Network(self.interface))
# v6 link local might be usable