Skip to content

Commit

Permalink
Create manage_unit, manage_dropin types from hiera
Browse files Browse the repository at this point in the history
Units and dropins can now be created from hiera
records:

e.g.

```yaml
systemd::manage_dropins:
  myconf.conf:
    ensure: present
    unit: myservice.service
    service_entry:
      Type: oneshot
      ExecStart:
        - ''
        - '/usr/bin/doit.sh'

systemd::manage_units:
  myservice.service:
    unit_entry:
      Description: My Customisation
    service_entry:
      CPUWeight: 2000
```

* Fixes #435
  • Loading branch information
traylenator committed Mar 12, 2024
1 parent 2b8eebf commit 8fc8579
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ systemd::manage_unit { 'myrunner.service':
The parameters `unit_entry`, `service_entry` and `install_entry` populate the
`[Unit]`, `[Service]` and `[Install]` sections of the generated unit file.

Similarly units can be created from hiera yaml files

```yaml
systemd::manage_units:
myservice.service:
unit_entry:
Description: My Customisation
service_entry:
CPUWeight: 2000
```
### drop-in files
Drop-in files are used to add or alter settings of a unit without modifying the
Expand Down Expand Up @@ -141,6 +152,20 @@ systemd::manage_dropin { 'myconf.conf':
}
```

Dropins can also be created similarly via yaml

```yaml
systemd::manage_dropins:
myconf.conf:
ensure: present
unit: myservice.service
service_entry:
Type: oneshot
ExecStart:
- ''
- '/usr/bin/doit.sh'
```
The filename of the drop in. The full path is determined using the path, unit and this filename.
### modules-load.d
Expand Down
20 changes: 19 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ The following parameters are available in the `systemd` class:
* [`logind_settings`](#-systemd--logind_settings)
* [`loginctl_users`](#-systemd--loginctl_users)
* [`dropin_files`](#-systemd--dropin_files)
* [`manage_units`](#-systemd--manage_units)
* [`manage_dropins`](#-systemd--manage_dropins)
* [`manage_all_network_files`](#-systemd--manage_all_network_files)
* [`network_path`](#-systemd--network_path)
* [`manage_accounting`](#-systemd--manage_accounting)
Expand Down Expand Up @@ -511,7 +513,23 @@ Default value: `{}`

Data type: `Hash`

Configure dropin files via hiera with factory pattern
Configure dropin files via hiera and `systemd::dropin_file` with factory pattern

Default value: `{}`

##### <a name="-systemd--manage_units"></a>`manage_units`

Data type: `Hash`

Configure units via hiera and `systemd::manage_unit` with factory pattern

Default value: `{}`

##### <a name="-systemd--manage_dropins"></a>`manage_dropins`

Data type: `Hash`

Configure dropin files via hiera and `systemd::manage_dropin` with factory pattern

Default value: `{}`

Expand Down
22 changes: 21 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@
# `loginctl_user`.
#
# @param dropin_files
# Configure dropin files via hiera with factory pattern
# Configure dropin files via hiera and `systemd::dropin_file` with factory pattern
#
# @param manage_units
# Configure units via hiera and `systemd::manage_unit` with factory pattern
#
# @param manage_dropins
# Configure dropin files via hiera and `systemd::manage_dropin` with factory pattern
#
# @param manage_all_network_files
#
Expand Down Expand Up @@ -234,6 +240,8 @@
Stdlib::Absolutepath $network_path = '/etc/systemd/network',
Hash $loginctl_users = {},
Hash $dropin_files = {},
Hash $manage_units = {},
Hash $manage_dropins = {},
Hash $udev_rules = {},
Boolean $manage_coredump = false,
Systemd::CoredumpSettings $coredump_settings = {},
Expand Down Expand Up @@ -332,4 +340,16 @@
* => $resource,
}
}

$manage_units.each |$name, $resource| {
systemd::manage_unit { $name:
* => $resource,
}
}

$manage_dropins.each |$name, $resource| {
systemd::manage_dropin { $name:
* => $resource,
}
}
}
51 changes: 51 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,57 @@
it { is_expected.to contain_systemd__dropin_file('my-foo.conf').with_content('[Service]\nReadWritePaths=/') }
end

context 'when passing manage_units' do
let(:params) do
{
manage_units: {
'special.service' => {
'ensure' => 'present',
'unit_entry' => { 'Description' => 'My Special Unit' },
'service_entry' => { 'TimeoutStartSec' => '100h'},
},
},
}
end

it {
is_expected.to contain_systemd__manage_unit('special.service').
with_ensure('present').
with_unit_entry({ 'Description' => 'My Special Unit' }).
with_service_entry({ 'TimeoutStartSec' => '100h' })
}
end

context 'when passing manage_dropins' do
let(:params) do
{
manage_dropins: {
'foo.conf' => {
'unit' => 'special.slice',
'slice_entry' => { 'CPUQuota' => '999%' },
},
'bar.conf' => {
'unit' => 'special.timer',
'timer_entry' => { 'OnCalendar' => ['', 'Daily'] },
},

},
}
end

it {
is_expected.to contain_systemd__manage_dropin('foo.conf').
with_unit('special.slice').
with_slice_entry({ 'CPUQuota' => '999%' })
}

it {
is_expected.to contain_systemd__manage_dropin('bar.conf').
with_unit('special.timer').
with_timer_entry({ 'OnCalendar' => ['', 'Daily'] })
}
end

context 'with managed networkd directory' do
let :params do
{
Expand Down

0 comments on commit 8fc8579

Please sign in to comment.