Skip to content
Merged
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
23 changes: 23 additions & 0 deletions cloudinit/distros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from io import StringIO
from typing import Any, Mapping, MutableMapping, Optional, Type

import cloudinit.net.netops.iproute2 as iproute2
from cloudinit import importer
from cloudinit import log as logging
from cloudinit import (
Expand Down Expand Up @@ -104,6 +105,7 @@ class Distro(persistence.CloudInitPickleMixin, metaclass=abc.ABCMeta):
# This is used by self.shutdown_command(), and can be overridden in
# subclasses
shutdown_options_map = {"halt": "-H", "poweroff": "-P", "reboot": "-r"}
net_ops = iproute2.Iproute2

_ci_pkl_version = 1
prefer_fqdn = False
Expand All @@ -118,6 +120,7 @@ def __init__(self, name, cfg, paths):
self.name = name
self.networking: Networking = self.networking_cls()
self.dhcp_client_priority = [dhcp.IscDhclient, dhcp.Dhcpcd]
self.net_ops = iproute2.Iproute2

def _unpickle(self, ci_pkl_version: int) -> None:
"""Perform deserialization fixes for Distro."""
Expand Down Expand Up @@ -998,6 +1001,26 @@ def do_as(self, command: list, user: str, cwd: str = "", **kwargs):
**kwargs,
)

@staticmethod
def build_dhclient_cmd(
path: str,
lease_file: str,
pid_file: str,
interface: str,
config_file: str,
) -> list:
return [
path,
"-1",
"-v",
"-lf",
lease_file,
"-pf",
pid_file,
"-sf",
"/bin/true",
] + (["-cf", config_file, interface] if config_file else [interface])
Comment thread
holmanb marked this conversation as resolved.


def _apply_hostname_transformations_to_url(url: str, transformations: list):
"""
Expand Down
3 changes: 3 additions & 0 deletions cloudinit/distros/bsd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import platform
from typing import List, Optional

import cloudinit.net.netops.bsd_netops as bsd_netops
from cloudinit import distros, helpers
from cloudinit import log as logging
from cloudinit import net, subp, util
Expand All @@ -27,6 +28,7 @@ class BSD(distros.Distro):
# There is no update/upgrade on OpenBSD
pkg_cmd_update_prefix: Optional[List[str]] = None
pkg_cmd_upgrade_prefix: Optional[List[str]] = None
net_ops = bsd_netops.BsdNetOps # type: ignore

def __init__(self, name, cfg, paths):
super().__init__(name, cfg, paths)
Expand All @@ -36,6 +38,7 @@ def __init__(self, name, cfg, paths):
self._runner = helpers.Runners(paths)
cfg["ssh_svcname"] = "sshd"
self.osfamily = platform.system().lower()
self.net_ops = bsd_netops

def _read_system_hostname(self):
sys_hostname = self._read_hostname(self.hostname_conf_fn)
Expand Down
12 changes: 12 additions & 0 deletions cloudinit/distros/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,15 @@ def update_package_sources(self):
["update"],
freq=PER_INSTANCE,
)

@staticmethod
def build_dhclient_cmd(
path: str,
lease_file: str,
pid_file: str,
interface: str,
config_file: str,
) -> list:
return [path, "-l", lease_file, "-p", pid_file] + (
["-c", config_file, interface] if config_file else [interface]
)
31 changes: 13 additions & 18 deletions cloudinit/net/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def dhcp_discovery(
# scripts in /etc/dhcp/dhclient*hooks.d.
pid_file = "/run/dhclient.pid"
lease_file = "/run/dhclient.lease"
config_file = None

# this function waits for these files to exist, clean previous runs
# to avoid false positive in wait_for_files
Expand All @@ -236,23 +237,11 @@ def dhcp_discovery(
# Generally dhclient relies on dhclient-script PREINIT action to bring
# the link up before attempting discovery. Since we are using
# -sf /bin/true, we need to do that "link up" ourselves first.
subp.subp(["ip", "link", "set", "dev", interface, "up"], capture=True)
distro.net_ops.link_up(interface)
# For INFINIBAND port the dhlient must be sent with
# dhcp-client-identifier. So here we are checking if the interface is
# INFINIBAND or not. If yes, we are generating the the client-id to be
# used with the dhclient
cmd = [
self.dhclient_path,
"-1",
"-v",
"-lf",
lease_file,
"-pf",
pid_file,
interface,
"-sf",
"/bin/true",
]
if is_ib_interface(interface):
dhcp_client_identifier = (
"20:%s" % get_interface_mac(interface)[36:]
Expand All @@ -263,13 +252,19 @@ def dhcp_discovery(
% (interface, dhcp_client_identifier)
)
tmp_dir = temp_utils.get_tmp_ancestor(needs_exe=True)
file_name = os.path.join(tmp_dir, interface + "-dhclient.conf")
util.write_file(file_name, interface_dhclient_content)
cmd.append("-cf")
cmd.append(file_name)
config_file = os.path.join(tmp_dir, interface + "-dhclient.conf")
util.write_file(config_file, interface_dhclient_content)

try:
out, err = subp.subp(cmd, capture=True)
out, err = subp.subp(
distro.build_dhclient_cmd(
self.dhclient_path,
lease_file,
pid_file,
interface,
config_file,
)
)
except subp.ProcessExecutionError as error:
LOG.debug(
"dhclient exited with code: %s stderr: %r stdout: %r",
Expand Down
Loading