Skip to content

Commit f1c49bf

Browse files
authored
Merge pull request #443 from traylenator/machinectl
Support reload of instances of systemd --user
2 parents 17ccd4e + 05c1f19 commit f1c49bf

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

REFERENCE.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,35 @@ Default value: `['create']`
678678

679679
Run systemctl daemon-reload
680680

681+
#### Examples
682+
683+
##### Force reload the system systemd
684+
685+
```puppet
686+
notify{ 'fake event to notify from':
687+
notify => Systemd::Daemon_reload['special']
688+
}
689+
systemd::daemon_reload{ 'special': }
690+
```
691+
692+
##### Force reload a systemd --user
693+
694+
```puppet
695+
notify{ 'fake event to notify from':
696+
notify => Systemd::Daemon_reload['steve_user']
697+
}
698+
systemd::daemon_reload{ 'steve_user':
699+
user => 'steve',
700+
}
701+
```
702+
681703
#### Parameters
682704

683705
The following parameters are available in the `systemd::daemon_reload` defined type:
684706

685707
* [`name`](#-systemd--daemon_reload--name)
686708
* [`enable`](#-systemd--daemon_reload--enable)
709+
* [`user`](#-systemd--daemon_reload--user)
687710

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

@@ -694,11 +717,19 @@ A globally unique name for the resource
694717
Data type: `Boolean`
695718

696719
Enable the reload exec
697-
698720
* Added in case users want to disable the reload globally using a resource collector
699721

700722
Default value: `true`
701723

724+
##### <a name="-systemd--daemon_reload--user"></a>`user`
725+
726+
Data type: `Optional[String[1]]`
727+
728+
Specify user name of `systemd --user` to reload. This not supported **below** Redhat 9,
729+
Ubuntu 22.04 or Debian 12.
730+
731+
Default value: `undef`
732+
702733
### <a name="systemd--dropin_file"></a>`systemd::dropin_file`
703734

704735
Creates a drop-in file for a systemd unit

manifests/daemon_reload.pp

+37-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,49 @@
77
#
88
# @param enable
99
# Enable the reload exec
10-
#
1110
# * Added in case users want to disable the reload globally using a resource collector
1211
#
12+
# @param user
13+
# Specify user name of `systemd --user` to reload. This not supported **below** Redhat 9,
14+
# Ubuntu 22.04 or Debian 12.
15+
#
16+
# @example Force reload the system systemd
17+
# notify{ 'fake event to notify from':
18+
# notify => Systemd::Daemon_reload['special']
19+
# }
20+
# systemd::daemon_reload{ 'special': }
21+
#
22+
# @example Force reload a systemd --user
23+
# notify{ 'fake event to notify from':
24+
# notify => Systemd::Daemon_reload['steve_user']
25+
# }
26+
# systemd::daemon_reload{ 'steve_user':
27+
# user => 'steve',
28+
# }
29+
#
1330
define systemd::daemon_reload (
1431
Boolean $enable = true,
32+
Optional[String[1]] $user = undef,
1533
) {
34+
include systemd
35+
1636
if $enable {
17-
exec { "${module_name}-${name}-systemctl-daemon-reload":
18-
command => 'systemctl daemon-reload',
37+
if $user {
38+
if ($facts['os']['family'] == 'RedHat' and versioncmp($facts['os']['release']['major'],'9') < 0 ) or
39+
($facts['os']['name'] == 'Debian' and versioncmp($facts['os']['release']['major'],'12') < 0 ) or
40+
($facts['os']['name'] == 'Ubuntu' and versioncmp($facts['os']['release']['major'],'22.04') < 0 ) {
41+
fail('systemd::daemon_reload_for a user is not supported below RedHat 9, Debian 12 or Ubuntu 22.04')
42+
}
43+
44+
$_title = "${module_name}-${name}-systemctl-user-${user}-daemon-reload"
45+
$_command = ['systemd-run', '--pipe', '--wait', '--user', '--machine', "${user}@.host", 'systemctl', '--user', 'daemon-reload']
46+
} else {
47+
$_title = "${module_name}-${name}-systemctl-daemon-reload"
48+
$_command = ['systemctl', 'daemon-reload']
49+
}
50+
51+
exec { $_title:
52+
command => $_command,
1953
refreshonly => true,
2054
path => $facts['path'],
2155
}

spec/defines/daemon_reload_spec.rb

+39-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,39 @@
1414
context 'with defaults' do
1515
it do
1616
expect(subject).to contain_exec("systemd-#{title}-systemctl-daemon-reload").
17-
with_command('systemctl daemon-reload').
17+
with_command(%w[systemctl daemon-reload]).
1818
with_refreshonly(true)
1919
end
20+
21+
context 'with a username specfied' do
22+
let(:params) do
23+
{ user: 'steve' }
24+
end
25+
26+
case [facts[:os]['name'], facts[:os]['family'], facts[:os]['release']['major']]
27+
when %w[Debian Debian 10],
28+
%w[Debian Debian 11],
29+
['Ubuntu', 'Debian', '20.04'],
30+
%w[VirtuozzoLinux RedHat 7],
31+
%w[AlmaLinux RedHat 8],
32+
%w[CentOS RedHat 7],
33+
%w[CentOS RedHat 8],
34+
%w[RedHat RedHat 7],
35+
%w[RedHat RedHat 8],
36+
%w[Rocky RedHat 8],
37+
%w[OracleLinux RedHat 8]
38+
it { is_expected.to compile.and_raise_error(%r{user is not supported below}) }
39+
else
40+
it { is_expected.to compile }
41+
42+
it {
43+
is_expected.to contain_exec('systemd-irregardless-systemctl-user-steve-daemon-reload').
44+
with_command(['systemd-run', '--pipe', '--wait', '--user', '--machine', '[email protected]', 'systemctl', '--user', 'daemon-reload']).
45+
with_refreshonly(true)
46+
}
47+
48+
end
49+
end
2050
end
2151

2252
context 'when disabled' do
@@ -27,6 +57,14 @@
2757
it do
2858
expect(subject).not_to contain_exec("systemd-#{title}-systemctl-daemon-reload")
2959
end
60+
61+
context 'with a username specfied' do
62+
let(:params) do
63+
super().merge(user: 'steve')
64+
end
65+
66+
it { is_expected.not_to contain_exec('systemd-irregardless-systemctl-user-steve-daemon-reload') }
67+
end
3068
end
3169
end
3270
end

0 commit comments

Comments
 (0)