diff --git a/REFERENCE.md b/REFERENCE.md index 4c58eccb..6095afa0 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -658,12 +658,35 @@ Default value: `['create']` Run systemctl daemon-reload +#### Examples + +##### Force reload the system systemd + +```puppet +notify{ 'fake event to notify from': + notify => Systemd::Daemon_reload['special'] +} +systemd::daemon_reload{ 'special': } +``` + +##### Force reload a systemd --user + +```puppet +notify{ 'fake event to notify from': + notify => Systemd::Daemon_reload['steve_user'] +} +systemd::daemon_reload{ 'steve_user': + user => 'steve', +} +``` + #### Parameters The following parameters are available in the `systemd::daemon_reload` defined type: * [`name`](#-systemd--daemon_reload--name) * [`enable`](#-systemd--daemon_reload--enable) +* [`user`](#-systemd--daemon_reload--user) ##### `name` @@ -674,11 +697,19 @@ A globally unique name for the resource Data type: `Boolean` Enable the reload exec - * Added in case users want to disable the reload globally using a resource collector Default value: `true` +##### `user` + +Data type: `Optional[String[1]]` + +Specify user name of `systemd --user` to reload. This not supported **below** Redhat 9, +Ubuntu 22.04 or Debian 12. + +Default value: `undef` + ### `systemd::dropin_file` Creates a drop-in file for a systemd unit diff --git a/manifests/daemon_reload.pp b/manifests/daemon_reload.pp index b7c3bac2..7a578570 100644 --- a/manifests/daemon_reload.pp +++ b/manifests/daemon_reload.pp @@ -7,15 +7,49 @@ # # @param enable # Enable the reload exec -# # * Added in case users want to disable the reload globally using a resource collector # +# @param user +# Specify user name of `systemd --user` to reload. This not supported **below** Redhat 9, +# Ubuntu 22.04 or Debian 12. +# +# @example Force reload the system systemd +# notify{ 'fake event to notify from': +# notify => Systemd::Daemon_reload['special'] +# } +# systemd::daemon_reload{ 'special': } +# +# @example Force reload a systemd --user +# notify{ 'fake event to notify from': +# notify => Systemd::Daemon_reload['steve_user'] +# } +# systemd::daemon_reload{ 'steve_user': +# user => 'steve', +# } +# define systemd::daemon_reload ( Boolean $enable = true, + Optional[String[1]] $user = undef, ) { + include systemd + if $enable { - exec { "${module_name}-${name}-systemctl-daemon-reload": - command => 'systemctl daemon-reload', + if $user { + if ($facts['os']['family'] == 'RedHat' and versioncmp($facts['os']['release']['major'],'9') < 0 ) or + ($facts['os']['name'] == 'Debian' and versioncmp($facts['os']['release']['major'],'12') < 0 ) or + ($facts['os']['name'] == 'Ubuntu' and versioncmp($facts['os']['release']['major'],'22.04') < 0 ) { + fail('systemd::daemon_reload_for a user is not supported below RedHat 9, Debian 12 or Ubuntu 22.04') + } + + $_title = "${module_name}-${name}-systemctl-user-${user}-daemon-reload" + $_command = ['systemd-run', '--pipe', '--wait', '--user', '--machine', "${user}@.host", "systemctl", '--user', 'daemon-reload'] + } else { + $_title = "${module_name}-${name}-systemctl-daemon-reload" + $_command = ['systemctl', 'daemon-reload'] + } + + exec { $_title: + command => $_command, refreshonly => true, path => $facts['path'], } diff --git a/metadata.json b/metadata.json index 9e1f8837..edde66d2 100644 --- a/metadata.json +++ b/metadata.json @@ -100,7 +100,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 7.0.0 < 9.0.0" + "version_requirement": ">= 7.9.0 < 9.0.0" } ] } diff --git a/spec/defines/daemon_reload_spec.rb b/spec/defines/daemon_reload_spec.rb index e2dd5bbb..0b927e80 100644 --- a/spec/defines/daemon_reload_spec.rb +++ b/spec/defines/daemon_reload_spec.rb @@ -14,9 +14,39 @@ context 'with defaults' do it do expect(subject).to contain_exec("systemd-#{title}-systemctl-daemon-reload"). - with_command('systemctl daemon-reload'). + with_command(%w[systemctl daemon-reload]). with_refreshonly(true) end + + context 'with a username specfied' do + let(:params) do + { user: 'steve' } + end + + case [facts[:os]['name'], facts[:os]['family'], facts[:os]['release']['major']] + when %w[Debian Debian 10], + %w[Debian Debian 11], + ['Ubuntu', 'Debian', '20.04'], + %w[VirtuozzoLinux RedHat 7], + %w[AlmaLinux RedHat 8], + %w[CentOS RedHat 7], + %w[CentOS RedHat 8], + %w[RedHat RedHat 7], + %w[RedHat RedHat 8], + %w[Rocky RedHat 8], + %w[OracleLinux RedHat 8] + it { is_expected.to compile.and_raise_error(%r{user is not supported below}) } + else + it { is_expected.to compile } + + it { + is_expected.to contain_exec('systemd-irregardless-systemctl-user-steve-daemon-reload'). + with_command(['systemd-run', '--pipe', '--wait', '--user', '--machine', 'steve@.host', 'systemctl', '--user', 'daemon-reload']). + with_refreshonly(true) + } + + end + end end context 'when disabled' do @@ -27,6 +57,14 @@ it do expect(subject).not_to contain_exec("systemd-#{title}-systemctl-daemon-reload") end + + context 'with a username specfied' do + let(:params) do + super().merge(user: 'steve') + end + + it { is_expected.not_to contain_exec('systemd-irregardless-systemctl-user-steve-daemon-reload') } + end end end end