Skip to content

Commit

Permalink
No insistence on service_entry with absent manage_unit
Browse files Browse the repository at this point in the history
When removing a unit file with `systemd::manage_unit` with the
following:

```
systemd::manage_unit {'foo.service':
  ensure     => 'absent',
  unit_entry => {},
}
```

This is not possible and a `service_entry` must be specified as the unit name
is in the service namespace.

Remove insistence the `service_name` for the absent case.
  • Loading branch information
traylenator committed Jun 15, 2023
1 parent c647a4c commit 8916582
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
11 changes: 10 additions & 1 deletion REFERENCE.md
Expand Up @@ -1037,6 +1037,13 @@ systemd::manage_unit{'arcd@.service':
'ExecStart' => /usr/sbin/arcd /usr/libexec/arcd/arcd.pl,
'StandardInput' => 'socket',
}
```

##### Remove a unit file

```puppet
systemd::manage_unit { 'my.service':
ensure => 'absent',
}
```

Expand Down Expand Up @@ -1168,10 +1175,12 @@ Default value: `true`

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

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

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

Default value: `undef`

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

Data type: `Optional[Systemd::Unit::Service]`
Expand Down
10 changes: 7 additions & 3 deletions manifests/manage_unit.pp
Expand Up @@ -61,6 +61,10 @@
# 'ExecStart' => /usr/sbin/arcd /usr/libexec/arcd/arcd.pl,
# 'StandardInput' => 'socket',
# }
#
# @example Remove a unit file
# systemd::manage_unit { 'my.service':
# ensure => 'absent',
# }
#
# @param name [Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']]
Expand Down Expand Up @@ -103,7 +107,7 @@
Hash[String[1], Any] $service_parameters = {},
Boolean $daemon_reload = true,
Optional[Systemd::Unit::Install] $install_entry = undef,
Systemd::Unit::Unit $unit_entry,
Optional[Systemd::Unit::Unit] $unit_entry = undef,
Optional[Systemd::Unit::Service] $service_entry = undef,
Optional[Systemd::Unit::Timer] $timer_entry = undef,
Optional[Systemd::Unit::Path] $path_entry = undef,
Expand All @@ -119,8 +123,8 @@
fail("Systemd::Manage_unit[${name}]: path_entry is only valid for path units")
}

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

systemd::unit_file { $name:
Expand Down
18 changes: 18 additions & 0 deletions spec/defines/manage_unit_spec.rb
Expand Up @@ -42,6 +42,24 @@
with_content(%r{^Type=oneshot$})
}

context 'with no service_entry' do
let(:params) do
{
ensure: 'present',
}
end

it { is_expected.to compile.and_raise_error(%r{service_entry is required for service units}) }

context 'with ensure absent' do
let(:params) do
super().merge(ensure: 'absent')
end

it { is_expected.to contain_systemd__unit_file('foobar.service').with_ensure('absent') }
end
end

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

0 comments on commit 8916582

Please sign in to comment.