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

add manage_network to manage networks in code #340

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions manifests/manage_network.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# @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 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 address_entry key value pairs for [Address] section of the unit file
#
define systemd::manage_network (
Enum['file', 'absent'] $ensure = 'file',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of module uses present and absent . The fact it a file is a hidden detail.

Stdlib::Absolutepath $path = '/etc/systemd/system',
String $owner = 'root',
String $group = 'root',
Stdlib::Filemode $mode = '0444',
Boolean $show_diff = true,
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,
restart_service => $restart_service,
content => epp('systemd/network.epp', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a new template the existing unit.epp could be extended to just support [Match], [Network] and [Address].

You would get manage_unit and manage_dropin of a network for free in the process. manage_unit .
manage_unit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but wouldn't it get to overloaded, with dropin, units and networks? the initial 3 *_entry are only a subset, netword offers a lot more. so i thought better have it separated. otherwise you will never get a PR done if you have to check alle the influence you change could have?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easier to deal with the interaction in a common layer.

Template does need refactoring to loop over the section rather than enumerate them but that just strengthens case for one template.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯ i also can add my stuff there, if this is whats needed to merge this in the end. i'm personally just not a big fand off such über-templates.

match_entry => $match_entry,
network_entry => $network_entry,
address_entry => $address_entry,
}),
}
}
32 changes: 32 additions & 0 deletions templates/network.epp
Original file line number Diff line number Diff line change
@@ -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 %>
<% } -%>
<% } -%>
<% } -%>
2 changes: 2 additions & 0 deletions types/network.pp
Original file line number Diff line number Diff line change
@@ -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)$/]