Skip to content

Commit

Permalink
Support reload of instances of systemd --user
Browse files Browse the repository at this point in the history
This change increased the minimum required Puppet version to 6.24.0 or 7.9.0 [PUP-5704](https://puppet.atlassian.net/browse/PUP-5704) to support arrays to the command attribute of the exec type.

Support the calling the `systemd --user daemon-reload` for a particular user.

Example run:
```puppet
  notify{'junk':
    notify => Systemd::Daemon_reload['user_foobar'],
  }

  systemd::daemon_reload{'user_steve':
    user => 'steve',
  }
```

This results on a Fedora box:

```
Notice: /Stage[main]/Main/Notify[junk]/message: defined 'message' as 'junk'
Notice: /Stage[main]/Main/Systemd::Daemon_reload[user_steve]/Package[systemd-container]: Triggered 'refresh' from 1 event
Notice: /Stage[main]/Main/Systemd::Daemon_reload[user_steve]/Exec[systemd-user_steve-systemctl-user-steve-daemon-reload]: Triggered 'refresh' from 1 event
```
and a journal (debug on) for [email protected] of

```
Mar 29 10:32:11 fedora systemd[2062]: Created slice background.slice - User Background Tasks Slice.
Mar 29 10:32:11 fedora systemd[2062]: Starting systemd-tmpfiles-clean.service - Cleanup of User's Temporary Files and Directories...
Mar 29 10:32:11 fedora systemd[2062]: Reloading requested from client PID 4379 ('systemctl')...
Mar 29 10:32:11 fedora systemd[2062]: Reloading...
Mar 29 10:32:11 fedora systemd[2062]: Reloading finished in 179 ms.
Mar 29 10:32:11 fedora systemd[2062]: Finished systemd-tmpfiles-clean.service - Cleanup of User's Temporary Files and Directories.
...

Only recent versions of `systemd-run` support the --machine option or actually manage to connect to DBUS.
  • Loading branch information
traylenator committed Apr 2, 2024
1 parent 48e0752 commit 0d86eca
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
33 changes: 32 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand All @@ -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`

##### <a name="-systemd--daemon_reload--user"></a>`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`

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

Creates a drop-in file for a systemd unit
Expand Down
40 changes: 37 additions & 3 deletions manifests/daemon_reload.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Check warning on line 45 in manifests/daemon_reload.pp

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

double quoted string containing no variables (check: double_quoted_strings)
} else {
$_title = "${module_name}-${name}-systemctl-daemon-reload"
$_command = ['systemctl', 'daemon-reload']
}

exec { $_title:
command => $_command,
refreshonly => true,
path => $facts['path'],
}
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"requirements": [
{
"name": "puppet",
"version_requirement": ">= 7.0.0 < 9.0.0"
"version_requirement": ">= 7.9.0 < 9.0.0"
}
]
}
40 changes: 39 additions & 1 deletion spec/defines/daemon_reload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]', 'systemctl', '--user', 'daemon-reload']).
with_refreshonly(true)
}

end
end
end

context 'when disabled' do
Expand All @@ -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
Expand Down

0 comments on commit 0d86eca

Please sign in to comment.