dhcp

Provide an interface for manipulating DHCP configuration snippets for our dynamic/temporary DHCP system.

exception spicerack.dhcp.DHCPError[source]

Bases: SpicerackError

Base class for DHCP object errors.

exception spicerack.dhcp.DHCPRestartError[source]

Bases: DHCPError

Raised when the includes generator on target machine returns non-zero.

class spicerack.dhcp.DHCP(hosts: spicerack.remote.RemoteHosts, *, datacenter: str, lock: spicerack.locking.Lock | spicerack.locking.NoLock, dry_run: bool = True)[source]

Bases: object

A class which provides tools for manipulating DHCP configuration snippets by data center.

Create a DHCP instance.

Parameters:
config(dhcp_config: spicerack.dhcp.DHCPConfiguration) collections.abc.Iterator[None][source]

A context manager to perform actions while the given DHCP config is active.

Parameters:

dhcp_config (spicerack.dhcp.DHCPConfiguration) -- The DHCP configuration to use.

Return type:

collections.abc.Iterator[None]

push_configuration(configuration: spicerack.dhcp.DHCPConfiguration) None[source]

Push a specified file with specified content to DHCP server and call refresh_dhcp.

Parameters:

configuration (spicerack.dhcp.DHCPConfiguration) -- An instance which provides content and filename for a configuration.

Return type:

None

remove_configuration(configuration: spicerack.dhcp.DHCPConfiguration, force: bool = False) None[source]

Remove configuration from target DHCP server then call refresh_dhcp.

This will fail if contents do not match unless force is True.

Parameters:
  • configuration (spicerack.dhcp.DHCPConfiguration) -- An instance which provides content and filename for a configuration.

  • force (bool, default: False) -- If set to True, will remove filename regardless.

Return type:

None

class spicerack.dhcp.DHCPConfMac(hostname: str, ipv4: ipaddress.IPv4Address, mac: str, ttys: int, distro: str, media_type: str = 'installer', dhcp_filename: str = '', dhcp_options: dict[str, str] = <factory>, _template: str = '\\n    host {s.hostname} {{\\n        hardware ethernet {s.mac};\\n        fixed-address {s.ipv4};{s.rendered_dhcp_filename}{s.rendered_dhcp_options}\\n    }}\\n    ') None[source]

Bases: DHCPConfiguration

A configuration generator for host installation DHCP entries via MAC address.

Parameters:
  • hostname (str) -- the hostname to generate the DHCP matching block for.

  • ipv4 (ipaddress.IPv4Address) -- the IPv4 to be assigned to the host.

  • mac (str) -- the MAC address of the host's interface.

  • ttys (int) -- which ttyS to use for this host, accepted values are 0 and 1.

  • distro (str) -- the codename of the Debian distribution to use for the PXE

  • dhcp (installer (empty string or None is allowed and removes the related)

  • config). (default options from the)

  • media_type (str, default: 'installer') -- The media type to use e.g. installer, installer-11.0, rescue

  • dhcp_filename (str, default: '') -- the DHCP filename option to set.

  • dhcp_options (dict[str, str], default: <factory>) -- a dictionary of DHCP option settings to use.

property filename: str

Return the proposed filename based on this configuration.

property rendered_dhcp_filename: str

Return the DHCP filename config string.

Returns:

A string representing the DHCP filename config provided.

property rendered_dhcp_options: str

Return the DHCP options config strings.

Returns:

A string representing the DHCP options config provided.

class spicerack.dhcp.DHCPConfMgmt(datacenter: str, serial: str, manufacturer: str, fqdn: str, ipv4: ipaddress.IPv4Address, _template: str = '\\n    class "{s.fqdn}" {{\\n        match if (lcase(option host-name) = "{s.hostname}");\\n    }}\\n    pool {{\\n        allow members of "{s.fqdn}";\\n        range {s.ipv4} {s.ipv4};\\n    }}\\n    ') None[source]

Bases: DHCPConfiguration

A configuration for management network DHCP entries.

Parameters:
  • datacenter (str) -- the name of the Datacenter the host is.

  • serial (str) -- the vendor serial of the host.

  • manufacturer (str) -- the name of the manufacturer.

  • fqdn (str) -- the management console FQDN to use for this host.

  • ipv4 (ipaddress.IPv4Address) -- the IP address to give the management interface.

property filename: str

Return the filename that corresponds to this configuration.

property hostname: str

Return the hostname based on manufacturer and serial.

class spicerack.dhcp.DHCPConfOpt82(hostname: str, ipv4: ipaddress.IPv4Address, switch_hostname: str, switch_iface: str, vlan: str, ttys: int, distro: str, media_type: str = 'installer', dhcp_filename: str = '', dhcp_filename_exclude_vendor: str = '', dhcp_options: dict[str, str] = <factory>, _template: str = '\\n    host {s.hostname} {{\\n        host-identifier option agent.circuit-id "{s.switch_hostname}:{s.switch_iface}:{s.vlan}";\\n        fixed-address {s.ipv4};{s.rendered_dhcp_filename}{s.rendered_dhcp_options}\\n    }}\\n    ') None[source]

Bases: DHCPConfiguration

A configuration generator for host installation DHCP entries via DHCP Option 82.

Parameters:
  • hostname (str) -- the hostname to generate the DHCP matching block for.

  • ipv4 (ipaddress.IPv4Address) -- the IPv4 to be assigned to the host.

  • switch_hostname (str) -- the hostname of the switch the host is connected to.

  • switch_iface (str) -- the name of the switch interface the host is connected to.

  • vlan (str) -- the name of the VLAN the host is configured for.

  • ttys (int) -- which ttyS to use for this host, accepted values are 0 and 1.

  • distro (str) -- the codename of the Debian distribution to use for the PXE

  • dhcp (installer (empty string or None is allowed and removes the related)

  • config). (default options from the)

  • media_type (str, default: 'installer') -- the media type to use e.g. installer, installer-11.0, rescue.

  • dhcp_filename (str, default: '') -- the DHCP filename option to set.

  • dhcp_filename_exclude_vendor (str, default: '') -- vendor to exclude from sending over the filename, e.g. d-i

  • dhcp_options (dict[str, str], default: <factory>) -- a dictionary of DHCP option settings to use.

property filename: str

Return the proposed filename based on this configuration.

property rendered_dhcp_filename: str

Return the DHCP filename config string.

Returns:

A string representing the DHCP filename config provided.

property rendered_dhcp_options: str

Return the DHCP options config strings.

Returns:

A string representing the DHCP options config provided.

class spicerack.dhcp.DHCPConfiguration[source]

Bases: ABC

abstract An abstract class which defines the interface for the DHCP configuration generators.

abstract property filename: str

Return a string of the proposed filename for this configuration, from the automation directory.

spicerack.dhcp.DHCP_TARGET_PATH: str = '/etc/dhcp/automation'

The path to the top of the DHCPd automation directory.

spicerack.dhcp.MGMT_HOSTNAME_RE: str = '\\.mgmt\\.{dc}\\.wmnet'

A regular expression when formatted with a dc parameter will match a management hostname.