Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reload of instances of systemd --user #443

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
} 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