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 timer_entry to manage_{dropin,unit} #322

Merged
merged 2 commits into from
Mar 20, 2023
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
38 changes: 37 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* [`Systemd::Unit::Install`](#Systemd--Unit--Install): Possible keys for the [Install] section of a unit file
* [`Systemd::Unit::Service`](#Systemd--Unit--Service): Possible keys for the [Service] section of a unit file
* [`Systemd::Unit::Service::Exec`](#Systemd--Unit--Service--Exec): Possible strings for ExecStart, ExecStartPrep, ...
* [`Systemd::Unit::Timer`](#Systemd--Unit--Timer): Possible keys for the [Timer] section of a unit file
* [`Systemd::Unit::Unit`](#Systemd--Unit--Unit): Possible keys for the [Unit] section of a unit file

## Classes
Expand Down Expand Up @@ -798,6 +799,7 @@ The following parameters are available in the `systemd::manage_dropin` defined t
* [`unit_entry`](#-systemd--manage_dropin--unit_entry)
* [`service_entry`](#-systemd--manage_dropin--service_entry)
* [`install_entry`](#-systemd--manage_dropin--install_entry)
* [`timer_entry`](#-systemd--manage_dropin--timer_entry)

##### <a name="-systemd--manage_dropin--unit"></a>`unit`

Expand Down Expand Up @@ -909,6 +911,14 @@ key value pairs for [Install] section of the unit file

Default value: `undef`

##### <a name="-systemd--manage_dropin--timer_entry"></a>`timer_entry`

Data type: `Optional[Systemd::Unit::Timer]`

key value pairs for [Timer] section of the unit file

Default value: `undef`

### <a name="systemd--manage_unit"></a>`systemd::manage_unit`

Generate unit file from template
Expand Down Expand Up @@ -955,6 +965,7 @@ The following parameters are available in the `systemd::manage_unit` defined typ
* [`unit_entry`](#-systemd--manage_unit--unit_entry)
* [`service_entry`](#-systemd--manage_unit--service_entry)
* [`install_entry`](#-systemd--manage_unit--install_entry)
* [`timer_entry`](#-systemd--manage_unit--timer_entry)

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

Expand Down Expand Up @@ -1066,10 +1077,12 @@ key value pairs for [Unit] section of the unit file.

##### <a name="-systemd--manage_unit--service_entry"></a>`service_entry`

Data type: `Systemd::Unit::Service`
Data type: `Optional[Systemd::Unit::Service]`

key value pairs for [Service] section of the unit file.

Default value: `undef`

##### <a name="-systemd--manage_unit--install_entry"></a>`install_entry`

Data type: `Optional[Systemd::Unit::Install]`
Expand All @@ -1078,6 +1091,14 @@ key value pairs for [Install] section of the unit file.

Default value: `undef`

##### <a name="-systemd--manage_unit--timer_entry"></a>`timer_entry`

Data type: `Optional[Systemd::Unit::Timer]`

key value pairs for [Timer] section of the unit file

Default value: `undef`

### <a name="systemd--modules_load"></a>`systemd::modules_load`

Creates a modules-load.d drop file
Expand Down Expand Up @@ -2194,6 +2215,21 @@ Possible strings for ExecStart, ExecStartPrep, ...

Alias of `Variant[Enum[''], Pattern[/^[@\-:]*(\+|!|!!)?[@\-:]*\/.*/], Pattern[/^[@\-:]*(\+|!|!!)?[@\-:]*[^\/]*(\s.*)?$/]]`

### <a name="Systemd--Unit--Timer"></a>`Systemd::Unit::Timer`

Possible keys for the [Timer] section of a unit file

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.timer.html

Alias of

```puppet
Struct[{
Optional['OnCalendar'] => Variant[String,Array[String,1]],
}]
```

### <a name="Systemd--Unit--Unit"></a>`Systemd::Unit::Unit`

Possible keys for the [Unit] section of a unit file
Expand Down
7 changes: 7 additions & 0 deletions manifests/manage_dropin.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# @param unit_entry key value pairs for [Unit] section of the unit file
# @param service_entry key value pairs for [Service] section of the unit file
# @param install_entry key value pairs for [Install] section of the unit file
# @param timer_entry key value pairs for [Timer] section of the unit file
#
define systemd::manage_dropin (
Systemd::Unit $unit,
Expand All @@ -44,7 +45,12 @@
Optional[Systemd::Unit::Install] $install_entry = undef,
Optional[Systemd::Unit::Unit] $unit_entry = undef,
Optional[Systemd::Unit::Service] $service_entry = undef,
Optional[Systemd::Unit::Timer] $timer_entry = undef,
) {
if $timer_entry and $unit !~ Pattern['^[^/]+\.timer'] {
fail("Systemd::Manage_unit[${name}]: timer_entry is only valid for timer units")
}

systemd::dropin_file { $name:
ensure => $ensure,
filename => $filename,
Expand All @@ -61,6 +67,7 @@
'unit_entry' => $unit_entry,
'service_entry' => $service_entry,
'install_entry' => $install_entry,
'timer_entry' => $timer_entry,
}),
}
}
13 changes: 12 additions & 1 deletion manifests/manage_unit.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# @param unit_entry key value pairs for [Unit] section of the unit file.
# @param service_entry key value pairs for [Service] section of the unit file.
# @param install_entry key value pairs for [Install] section of the unit file.
# @param timer_entry key value pairs for [Timer] section of the unit file
#
define systemd::manage_unit (
Enum['present', 'absent'] $ensure = 'present',
Expand All @@ -55,10 +56,19 @@
Boolean $daemon_reload = true,
Optional[Systemd::Unit::Install] $install_entry = undef,
Systemd::Unit::Unit $unit_entry,
Systemd::Unit::Service $service_entry,
Optional[Systemd::Unit::Service] $service_entry = undef,
Optional[Systemd::Unit::Timer] $timer_entry = undef,
) {
assert_type(Systemd::Unit, $name)

if $timer_entry and $name !~ Pattern['^[^/]+\.timer'] {
fail("Systemd::Manage_unit[${name}]: timer_entry is only valid for timer units")
}

if $name =~ Pattern['^[^/]+\.service'] and !$service_entry {
fail("Systemd::Manage_unit[${name}]: service_entry is only required for service units")
}

systemd::unit_file { $name:
ensure => $ensure,
path => $path,
Expand All @@ -75,6 +85,7 @@
'unit_entry' => $unit_entry,
'service_entry' => $service_entry,
'install_entry' => $install_entry,
'timer_entry' => $timer_entry,
}),
}
}
94 changes: 66 additions & 28 deletions spec/defines/manage_dropin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,86 @@

let(:title) { 'foobar.conf' }

context 'drop file chaning Type and resetting ExecStart' do
context 'on a service' do
let(:params) do
{
unit: 'special.service',
service_entry: {
Type: 'oneshot',
ExecStart: ['', '/usr/bin/doit.sh'],
},
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_systemd__dropin_file('foobar.conf') }
context 'drop file chaning Type and resetting ExecStart' do
let(:params) do
super().merge(
service_entry: {
Type: 'oneshot',
ExecStart: ['', '/usr/bin/doit.sh'],
}
)
end

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^\[Service\]$})
}
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_systemd__dropin_file('foobar.conf') }

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
without_content(%r{^\[Unit\]$})
}
it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^\[Service\]$})
}

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
without_content(%r{^\[Install\]$})
}
it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
without_content(%r{^\[Unit\]$})
}

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^ExecStart=$})
}
it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
without_content(%r{^\[Install\]$})
}

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^ExecStart=/usr/bin/doit.sh$})
}
it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^ExecStart=$})
}

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^ExecStart=/usr/bin/doit.sh$})
}

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^Type=oneshot$})
}
end

context 'with a timer entry' do
let(:params) do
super().merge(
timer_entry: {
'OnCalendar' => 'soon',
}
)
end

it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) }
end
end

context 'on a timer' do
let(:params) do
{
unit: 'special.timer',
timer_entry: {
'OnCalendar' => 'soon',
}
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^Type=oneshot$})
with_unit('special.timer').
with_content(%r{^OnCalendar=soon$})
}
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/defines/manage_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@
is_expected.to contain_systemd__unit_file('foobar.service').
with_content(%r{^Type=oneshot$})
}

context 'with a timer entry' do
let(:params) do
super().merge(timer_entry: { 'OnCalendar' => 'something' })
end

it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) }
end
end

context 'on a timer' do
let(:title) { 'winter.timer' }

let(:params) do
{
unit_entry: {
Description: 'Winter is coming',
},
timer_entry: {
'OnCalendar' => 'soon',
}
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__unit_file('winter.timer').
with_content(%r{^OnCalendar=soon$})
}
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions templates/unit_file.epp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Optional[Hash] $unit_entry,
Optional[Hash] $service_entry,
Optional[Hash] $install_entry,
Optional[Hash] $timer_entry,
| -%>
<% if $unit_entry { -%>

Expand All @@ -21,6 +22,15 @@
<% } -%>
<% } -%>
<% } -%>
<% if $timer_entry { -%>

[Timer]
<% $timer_entry.each | $_key, $_value | { -%>
<% Array($_value, true).each | $_subvalue | { -%>
<%= $_key %>=<%= $_subvalue %>
<% } -%>
<% } -%>
<% } -%>
<% if $install_entry { -%>

[Install]
Expand Down
8 changes: 8 additions & 0 deletions types/unit/timer.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @summary Possible keys for the [Timer] section of a unit file
# @see https://www.freedesktop.org/software/systemd/man/systemd.timer.html
#
type Systemd::Unit::Timer = Struct[
{
Optional['OnCalendar'] => Variant[String,Array[String,1]],
}
]