Skip to content

Commit

Permalink
Merge pull request #96 from cisagov/bugfix/fix-bookworm-dhcp-searh-do…
Browse files Browse the repository at this point in the history
…main-issue

Fix Bookworm DHCP search domain issue
  • Loading branch information
jsf9k authored Mar 20, 2024
2 parents 1b76568 + b942e7c commit b7749f0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ repos:
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- types-pyyaml
- repo: https://github.com/asottile/pyupgrade
rev: v3.10.1
hooks:
Expand Down
41 changes: 41 additions & 0 deletions cloud_init.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ data "cloudinit_config" "cloud_init_tasks" {
merge_type = "list(append)+dict(recurse_array)+str()"
}

# TODO: Remove the following two parts when and if that becomes
# possible. See #96 for more details.

# Fix the DHCP options in the Canonical Netplan configuration
# created by cloud-init.
#
# The issue is that Netplan uses a default of false for
# dhcp4-overrides.use-domains, and cloud-init does not explicitly
# set this key or provide any way to do so.
#
# See these issues for more details:
# - cisagov/skeleton-packer#300
# - canonical/cloud-init#4764
part {
content = templatefile(
"${path.module}/cloudinit/fix-dhcp.tpl.py", {
netplan_config = "/etc/netplan/50-cloud-init.yaml"
})
content_type = "text/x-shellscript"
filename = "fix-dhcp.py"
merge_type = "list(append)+dict(recurse_array)+str()"
}

# Now that the DHCP options in the Canonical Netplan configuration
# created by cloud-init have been fixed, reapply the Netplan
# configuration.
#
# The issue is that Netplan uses a default of false for
# dhcp4-overrides.use-domains, and cloud-init does not explicitly
# set this key or provide any way to do so.
#
# See these issues for more details:
# - cisagov/skeleton-packer#300
# - canonical/cloud-init#4764
part {
content = file("${path.module}/cloudinit/fix-dhcp.yml")
content_type = "text/cloud-config"
filename = "fix-dhcp.yml"
merge_type = "list(append)+dict(recurse_array)+str()"
}

part {
filename = "openvpn-config.yml"
content_type = "text/cloud-config"
Expand Down
37 changes: 37 additions & 0 deletions cloudinit/fix-dhcp.tpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3

"""Append the necessary DHCP options to the Netplan configuration.
The Netplan configuration is created by cloud-init, but it needs to be
slightly modified and reapplied. This script takes care of the
modification.
See these issues for more details:
- cisagov/skeleton-packer#300
- canonical/cloud-init#4764
This file is a template. It should be processed by Terraform.
"""

# TODO: Remove this script when and if that becomes possible. See #96
# for more details.

# Third-Party Libraries
import yaml

# Inputs from Terraform
NETPLAN_CONFIG = "${netplan_config}"

with open(NETPLAN_CONFIG) as f:
# Load the current Netplan configuration
config = yaml.safe_load(f)
# Add a dhcp4-overrides section to each network
config["network"]["ethernets"] = {
k: v | {"dhcp4-overrides": {"use-domains": True}}
for (k, v) in config["network"]["ethernets"].items()
}

# Write the results back out to the Netplan configuration file
with open(NETPLAN_CONFIG, "w") as f:
f.write(yaml.dump(config))
10 changes: 10 additions & 0 deletions cloudinit/fix-dhcp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

# There is a Python script that fixes the DHCP4 options in the netplan
# configuration already generated by cloud-init. The following simply
# reapplies the Netplan configuration after the modification.
#
# TODO: Remove this code when and if that becomes possible. See #96
# for more details.
runcmd:
- [netplan, apply]

0 comments on commit b7749f0

Please sign in to comment.