From e3e75161d06387a979b8f4a04f406f9b54f8703e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 2 Jan 2022 04:31:50 +0100 Subject: Network rewrites. --- manifests/util/disable_networking.pp | 22 ++++++++++++++++++++++ manifests/util/enable_networkd.pp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 manifests/util/disable_networking.pp create mode 100644 manifests/util/enable_networkd.pp (limited to 'manifests/util') diff --git a/manifests/util/disable_networking.pp b/manifests/util/disable_networking.pp new file mode 100644 index 0000000..4a9b31b --- /dev/null +++ b/manifests/util/disable_networking.pp @@ -0,0 +1,22 @@ +define nspawn::util::disable_networking ( + String $machine = $name, + String $machine_path = "/var/lib/machines/${machine}", +) { + + + $cmd = if $facts['machined-info'][$machine]['State'] == 'running' { + [ 'systemctl', '-M', $machine, 'disable', 'networking' ] + } else { + [ 'systemd-nspawn', '-M', $machine, '--quiet', + 'systemctl', 'disable', 'networking' ] + } + + exec { "Disable networking on ${machine}": + command => $cmd, + path => [ '/bin', '/usr/bin', ], + # among others + # creates => "${machine_path}/etc/systemd/system/multi-user.target.wants/systemd-networkd.service", + } + +} + diff --git a/manifests/util/enable_networkd.pp b/manifests/util/enable_networkd.pp new file mode 100644 index 0000000..2b532b9 --- /dev/null +++ b/manifests/util/enable_networkd.pp @@ -0,0 +1,35 @@ +define nspawn::util::enable_networkd ( + String $machine = $name, + String $machine_path = "/var/lib/machines/${machine}", +) { + + # TODO only do this if the directory is empty + networking::networkd { "Initial networking on ${machine}": + filename => '20-puppet-initial', + match => { 'Name' => 'host0', }, + root => $machine_path, + network => { + 'DHCP' => 'ipv4', + 'IPv6AcceptRA' => 1, + }, + notify_ => false, + manage_directory => false, + } + + $running = $facts['machined-info'][$machine] != Undef or $facts['machined-info'][$machine]['State'] == 'running' + + $cmd = if $running { + [ 'systemctl', '-M', $machine, 'enable', 'systemd-networkd' ] + } else { + [ 'systemd-nspawn', '-M', $machine, '--quiet', + 'systemctl', 'enable', 'systemd-networkd' ] + } + + exec { "Enable systemd-networkd on ${machine}": + command => $cmd, + path => [ '/bin', '/usr/bin', ], + # among others + creates => "${machine_path}/etc/systemd/system/multi-user.target.wants/systemd-networkd.service", + } + +} -- cgit v1.2.3