Skip to content

Commit

Permalink
Add timer_entry to manage_{dropin,unit}
Browse files Browse the repository at this point in the history
This makes it easier to deal with timer units. Right now it only
implements OnCalendar, but it should be easy to expand to all types.
  • Loading branch information
ekohl committed Mar 16, 2023
1 parent 8f77733 commit 798ff23
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
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,
}),
}
}
31 changes: 31 additions & 0 deletions spec/defines/manage_dropin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@
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(/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_unit('special.timer').
with_content(%r{^OnCalendar=soon$})
}
end
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(/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]],
}
]

0 comments on commit 798ff23

Please sign in to comment.