Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bookworm DHCP search domain issue #96

Merged
merged 4 commits into from
Mar 20, 2024
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
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]
Loading