From c6d591e3d369ce6d13959374385934724e229003 Mon Sep 17 00:00:00 2001 From: Robert Waffen Date: Wed, 19 Apr 2023 18:49:19 +0200 Subject: [PATCH 1/2] add initial files to extent network --- manifests/manage_network.pp | 67 +++++++++++++++++++++++++++++++++++++ templates/network.epp | 0 types/network.pp | 2 ++ types/unit/link.pp | 7 ++++ types/unit/match.pp | 7 ++++ types/unit/network.pp | 7 ++++ 6 files changed, 90 insertions(+) create mode 100644 manifests/manage_network.pp create mode 100644 templates/network.epp create mode 100644 types/network.pp create mode 100644 types/unit/link.pp create mode 100644 types/unit/match.pp create mode 100644 types/unit/network.pp diff --git a/manifests/manage_network.pp b/manifests/manage_network.pp new file mode 100644 index 00000000..48fe81f2 --- /dev/null +++ b/manifests/manage_network.pp @@ -0,0 +1,67 @@ +# @summary Generate network file from template +# +# @api public +# +# @see systemd.network(5) +# +# @example network file +# systemd::manage_network { 'eth0': +# ensure => present, +# match_entry => 'eth0', +# network_entry => { +# address => '1.2.3.4', +# gateway => '7.8.9.0', +# dns => '1.1.1.1' +# } +# } +# +# @param name [Pattern['^[^/]+\.(network|netdev|link)$']] +# The target unit file to create +# +# @param ensure The state of the unit file to ensure +# @param path The main systemd configuration path +# @param owner The owner to set on the unit file +# @param group The group to set on the unit file +# @param mode The mode to set on the unit file +# @param show_diff Whether to show the diff when updating unit file +# @param enable If set, manage the unit enablement status +# @param active If set, will manage the state of the unit +# @param restart Specify a restart command manually. If left unspecified, a standard Puppet service restart happens +# @param daemon_reload +# call `systemd::daemon-reload` to ensure that the modified unit file is loaded +# +# @param match_entry key value pairs for [Match] section of the unit file +# @param network_entry key value pairs for [Network] section of the unit file +# @param link_entry key value pairs for [Link] section of the unit file +# +define systemd::manage_unit ( + Enum['present', 'absent'] $ensure = 'present', + Stdlib::Absolutepath $path = '/etc/systemd/system', + String $owner = 'root', + String $group = 'root', + Stdlib::Filemode $mode = '0444', + Boolean $show_diff = true, + Optional[Variant[Boolean, Enum['mask']]] $enable = undef, + Optional[Boolean] $active = undef, + Optional[String] $restart = undef, + Boolean $daemon_reload = true, + Optional[Systemd::Unit::Match] $match_entry = undef, + Optional[Systemd::Unit::Network] $network_entry = undef, + Optional[Systemd::Unit::Link] $link_entry = undef, +) { + assert_type(Systemd::Network, $name) + + systemd::network { $name: + ensure => $ensure, + path => $path, + owner => $owner, + group => $group, + mode => $mode, + show_diff => $show_diff, + content => epp('systemd/network.epp', { + match_entry => $match_entry, + network_entry => $network_entry, + link_entry => $link_entry, + }), + } +} diff --git a/templates/network.epp b/templates/network.epp new file mode 100644 index 00000000..e69de29b diff --git a/types/network.pp b/types/network.pp new file mode 100644 index 00000000..48bbcf77 --- /dev/null +++ b/types/network.pp @@ -0,0 +1,2 @@ +# @summary custom datatype that validates filenames/paths for valid systemd network files +type Systemd::Network = Pattern[/^[a-zA-Z0-9:\-_.\\@]+\.(network|netdev|link)$/] diff --git a/types/unit/link.pp b/types/unit/link.pp new file mode 100644 index 00000000..3e6c994d --- /dev/null +++ b/types/unit/link.pp @@ -0,0 +1,7 @@ +# @summary Possible keys for the [Link] section of a unit file +# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# +# +type Systemd::Unit::Network = Struct[ + { + } +] diff --git a/types/unit/match.pp b/types/unit/match.pp new file mode 100644 index 00000000..c76cf882 --- /dev/null +++ b/types/unit/match.pp @@ -0,0 +1,7 @@ +# @summary Possible keys for the [Match] section of a unit file +# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# +# +type Systemd::Unit::Network = Struct[ + { + } +] diff --git a/types/unit/network.pp b/types/unit/network.pp new file mode 100644 index 00000000..ae3e067f --- /dev/null +++ b/types/unit/network.pp @@ -0,0 +1,7 @@ +# @summary Possible keys for the [Network] section of a unit file +# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# +# +type Systemd::Unit::Network = Struct[ + { + } +] From dcd9234ba78de1b3e1a1bbaaeba564930b73a6c9 Mon Sep 17 00:00:00 2001 From: Robert Waffen Date: Tue, 25 Apr 2023 18:48:41 +0200 Subject: [PATCH 2/2] add template and add docu. --- README.md | 23 +++++++++++++++++++++ manifests/manage_network.pp | 41 +++++++++++++++---------------------- templates/network.epp | 32 +++++++++++++++++++++++++++++ types/unit/link.pp | 7 ------- types/unit/match.pp | 7 ------- types/unit/network.pp | 7 ------- 6 files changed, 72 insertions(+), 45 deletions(-) delete mode 100644 types/unit/link.pp delete mode 100644 types/unit/match.pp delete mode 100644 types/unit/network.pp diff --git a/README.md b/README.md index a08c304a..40b3a85a 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,29 @@ systemd::network { 'eth0.network': } ``` +### network files from parameters + +Create a network file from parameters + +```puppet +systemd::manage_network { 'myhome.network': + match_entry => { + 'Name' => 'enp1s0', + }, + network_entry => { + 'Address' => '10.1.1.1/24', + 'Gateway' => '10.1.1.1', + }, + address_entry => { + 'Address' => '10.1.1.2', + }, +} +``` + +The parameters `match_entry`, `network_entry` and `address_entry` populate the +`[Match]`, `[Network]` and `[Address]` sections of the generated network file. + + ### Services The default target is managed via the `default_target` parameter. If this is left at its default value (`undef`), the default-target will be unmanaged by puppet. diff --git a/manifests/manage_network.pp b/manifests/manage_network.pp index 48fe81f2..fcb3e0f5 100644 --- a/manifests/manage_network.pp +++ b/manifests/manage_network.pp @@ -24,44 +24,37 @@ # @param group The group to set on the unit file # @param mode The mode to set on the unit file # @param show_diff Whether to show the diff when updating unit file -# @param enable If set, manage the unit enablement status -# @param active If set, will manage the state of the unit -# @param restart Specify a restart command manually. If left unspecified, a standard Puppet service restart happens -# @param daemon_reload -# call `systemd::daemon-reload` to ensure that the modified unit file is loaded -# +# @param restart_service if netword should be restarted. # @param match_entry key value pairs for [Match] section of the unit file # @param network_entry key value pairs for [Network] section of the unit file -# @param link_entry key value pairs for [Link] section of the unit file +# @param address_entry key value pairs for [Address] section of the unit file # -define systemd::manage_unit ( - Enum['present', 'absent'] $ensure = 'present', +define systemd::manage_network ( + Enum['file', 'absent'] $ensure = 'file', Stdlib::Absolutepath $path = '/etc/systemd/system', String $owner = 'root', String $group = 'root', Stdlib::Filemode $mode = '0444', Boolean $show_diff = true, - Optional[Variant[Boolean, Enum['mask']]] $enable = undef, - Optional[Boolean] $active = undef, - Optional[String] $restart = undef, - Boolean $daemon_reload = true, - Optional[Systemd::Unit::Match] $match_entry = undef, - Optional[Systemd::Unit::Network] $network_entry = undef, - Optional[Systemd::Unit::Link] $link_entry = undef, + Boolean $restart_service = true, + Optional[Hash] $match_entry = undef, + Optional[Hash] $network_entry = undef, + Optional[Hash] $address_entry = undef, ) { assert_type(Systemd::Network, $name) systemd::network { $name: - ensure => $ensure, - path => $path, - owner => $owner, - group => $group, - mode => $mode, - show_diff => $show_diff, - content => epp('systemd/network.epp', { + ensure => $ensure, + path => $path, + owner => $owner, + group => $group, + mode => $mode, + show_diff => $show_diff, + restart_service => $restart_service, + content => epp('systemd/network.epp', { match_entry => $match_entry, network_entry => $network_entry, - link_entry => $link_entry, + address_entry => $address_entry, }), } } diff --git a/templates/network.epp b/templates/network.epp index e69de29b..f4a6e576 100644 --- a/templates/network.epp +++ b/templates/network.epp @@ -0,0 +1,32 @@ +<%- | + Optional[Hash] $match_entry, + Optional[Hash] $network_entry, + Optional[Hash] $address_entry, +| -%> +<% if $match_entry { -%> + +[Match] +<% $match_entry.each | $_key, $_value | { -%> +<% Array($_value, true).each | $_subvalue | { -%> +<%= $_key %>=<%= $_subvalue %> +<% } -%> +<% } -%> +<% } -%> +<% if $network_entry { -%> + +[Network] +<% $network_entry.each | $_key, $_value | { -%> +<% Array($_value, true).each | $_subvalue | { -%> +<%= $_key %>=<%= $_subvalue %> +<% } -%> +<% } -%> +<% } -%> +<% if $address_entry { -%> + +[Address] +<% $address_entry.each | $_key, $_value | { -%> +<% Array($_value, true).each | $_subvalue | { -%> +<%= $_key %>=<%= $_subvalue %> +<% } -%> +<% } -%> +<% } -%> diff --git a/types/unit/link.pp b/types/unit/link.pp deleted file mode 100644 index 3e6c994d..00000000 --- a/types/unit/link.pp +++ /dev/null @@ -1,7 +0,0 @@ -# @summary Possible keys for the [Link] section of a unit file -# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# -# -type Systemd::Unit::Network = Struct[ - { - } -] diff --git a/types/unit/match.pp b/types/unit/match.pp deleted file mode 100644 index c76cf882..00000000 --- a/types/unit/match.pp +++ /dev/null @@ -1,7 +0,0 @@ -# @summary Possible keys for the [Match] section of a unit file -# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# -# -type Systemd::Unit::Network = Struct[ - { - } -] diff --git a/types/unit/network.pp b/types/unit/network.pp deleted file mode 100644 index ae3e067f..00000000 --- a/types/unit/network.pp +++ /dev/null @@ -1,7 +0,0 @@ -# @summary Possible keys for the [Network] section of a unit file -# @see https://www.freedesktop.org/software/systemd/man/systemd.network.html# -# -type Systemd::Unit::Network = Struct[ - { - } -]