From 8916582142286a1a49a3f8c14ecb917054fe8e8a Mon Sep 17 00:00:00 2001 From: Steve Traylen Date: Wed, 3 May 2023 15:23:30 +0200 Subject: [PATCH] No insistence on service_entry with absent manage_unit 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. --- REFERENCE.md | 11 ++++++++++- manifests/manage_unit.pp | 10 +++++++--- spec/defines/manage_unit_spec.rb | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 56ef372c..8e118222 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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', } ``` @@ -1168,10 +1175,12 @@ Default value: `true` ##### `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` + ##### `service_entry` Data type: `Optional[Systemd::Unit::Service]` diff --git a/manifests/manage_unit.pp b/manifests/manage_unit.pp index 7e73213c..7e79e6bc 100644 --- a/manifests/manage_unit.pp +++ b/manifests/manage_unit.pp @@ -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)$']] @@ -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, @@ -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: diff --git a/spec/defines/manage_unit_spec.rb b/spec/defines/manage_unit_spec.rb index ecce9b11..729f16c8 100644 --- a/spec/defines/manage_unit_spec.rb +++ b/spec/defines/manage_unit_spec.rb @@ -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' })