Skip to content

Commit 17ccd4e

Browse files
authored
Merge pull request #446 from traylenator/userunit
Manage units running under `systemd --user` instance
2 parents fdf1274 + a46a7ff commit 17ccd4e

File tree

4 files changed

+390
-1
lines changed

4 files changed

+390
-1
lines changed

REFERENCE.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* [`systemd::tmpfile`](#systemd--tmpfile): Creates a systemd tmpfile
4141
* [`systemd::udev::rule`](#systemd--udev--rule): Adds a custom udev rule
4242
* [`systemd::unit_file`](#systemd--unit_file): Creates a systemd unit file
43+
* [`systemd::user_service`](#systemd--user_service): Manage a user service running under systemd --user
4344

4445
### Resource types
4546

@@ -2251,6 +2252,104 @@ restart (notify) the service when unit file changed
22512252

22522253
Default value: `true`
22532254

2255+
### <a name="systemd--user_service"></a>`systemd::user_service`
2256+
2257+
Manage a user service running under systemd --user
2258+
2259+
#### Examples
2260+
2261+
##### Enable a service for all users
2262+
2263+
```puppet
2264+
systemd::user_service { 'systemd-tmpfiles-clean.timer':
2265+
enable => true,
2266+
global => true,
2267+
}
2268+
```
2269+
2270+
##### Enable a particular user's service
2271+
2272+
```puppet
2273+
systemd::user_service { 'podman-auto-update.timer':
2274+
ensure => true,
2275+
enable => true,
2276+
user => 'steve',
2277+
}
2278+
```
2279+
2280+
##### Notify a user's service to restart it
2281+
2282+
```puppet
2283+
file{ '/home/steve/.config/systemd/user/podman-auto-update.timer':
2284+
ensure => file,
2285+
content => ...,
2286+
notify => Systemd::User_service['steve-podman-auto-update.timer']
2287+
}
2288+
2289+
systemd::user_service { 'steve-podman-auto-update.timer':
2290+
ensure => true,
2291+
enable => true,
2292+
unit => 'podman-auto-update.timer',
2293+
user => 'steve',
2294+
}
2295+
2296+
@param unit Unit name to work on
2297+
@param ensure Should the unit be started or stopped. Can only be true if user is specified.
2298+
@param enable Should the unit be enabled or disabled
2299+
@param user User name of user whose unit should be acted upon. Mutually exclusive with
2300+
@param global Act globally for all users. Mutually exclusibe with `user`.
2301+
```
2302+
2303+
#### Parameters
2304+
2305+
The following parameters are available in the `systemd::user_service` defined type:
2306+
2307+
* [`unit`](#-systemd--user_service--unit)
2308+
* [`ensure`](#-systemd--user_service--ensure)
2309+
* [`enable`](#-systemd--user_service--enable)
2310+
* [`global`](#-systemd--user_service--global)
2311+
* [`user`](#-systemd--user_service--user)
2312+
2313+
##### <a name="-systemd--user_service--unit"></a>`unit`
2314+
2315+
Data type: `Systemd::Unit`
2316+
2317+
2318+
2319+
Default value: `$title`
2320+
2321+
##### <a name="-systemd--user_service--ensure"></a>`ensure`
2322+
2323+
Data type: `Variant[Boolean,Enum['stopped','running']]`
2324+
2325+
2326+
2327+
Default value: `false`
2328+
2329+
##### <a name="-systemd--user_service--enable"></a>`enable`
2330+
2331+
Data type: `Boolean`
2332+
2333+
2334+
2335+
Default value: `false`
2336+
2337+
##### <a name="-systemd--user_service--global"></a>`global`
2338+
2339+
Data type: `Boolean`
2340+
2341+
2342+
2343+
Default value: `false`
2344+
2345+
##### <a name="-systemd--user_service--user"></a>`user`
2346+
2347+
Data type: `Optional[String[1]]`
2348+
2349+
2350+
2351+
Default value: `undef`
2352+
22542353
## Resource types
22552354

22562355
### <a name="loginctl_user"></a>`loginctl_user`

manifests/user_service.pp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# @summary Manage a user service running under systemd --user
2+
#
3+
# @example Enable a service for all users
4+
# systemd::user_service { 'systemd-tmpfiles-clean.timer':
5+
# enable => true,
6+
# global => true,
7+
# }
8+
#
9+
# @example Enable a particular user's service
10+
# systemd::user_service { 'podman-auto-update.timer':
11+
# ensure => true,
12+
# enable => true,
13+
# user => 'steve',
14+
# }
15+
#
16+
# @example Notify a user's service to restart it
17+
# file{ '/home/steve/.config/systemd/user/podman-auto-update.timer':
18+
# ensure => file,
19+
# content => ...,
20+
# notify => Systemd::User_service['steve-podman-auto-update.timer']
21+
# }
22+
#
23+
# systemd::user_service { 'steve-podman-auto-update.timer':
24+
# ensure => true,
25+
# enable => true,
26+
# unit => 'podman-auto-update.timer',
27+
# user => 'steve',
28+
# }
29+
#
30+
# @param unit Unit name to work on
31+
# @param ensure Should the unit be started or stopped. Can only be true if user is specified.
32+
# @param enable Should the unit be enabled or disabled
33+
# @param user User name of user whose unit should be acted upon. Mutually exclusive with
34+
# @param global Act globally for all users. Mutually exclusibe with `user`.
35+
#
36+
define systemd::user_service (
37+
Systemd::Unit $unit = $title,
38+
Variant[Boolean,Enum['stopped','running']] $ensure = false,
39+
Boolean $enable = false,
40+
Boolean $global = false,
41+
Optional[String[1]] $user = undef,
42+
) {
43+
$_ensure = $ensure ? {
44+
'stopped' => false,
45+
'running' => true,
46+
default => $ensure
47+
}
48+
49+
if ( $global and $user ) or ( ! $global and ! $user) {
50+
fail('Exactly one of the "user" or "global" parameters must be defined')
51+
}
52+
53+
if $global and $_ensure {
54+
fail('Cannot ensure a service is running for all users globally')
55+
}
56+
57+
if $global {
58+
if $enable {
59+
$_title = "Enable user service ${unit} globally"
60+
$_command = ['systemctl', '--global', 'enable', $unit]
61+
$_unless = [['systemctl', '--global', 'is-enabled', $unit]]
62+
$_onlyif = undef
63+
} else {
64+
$_title = "Disable user service ${unit} globally"
65+
$_command = ['systemctl', '--global', 'disable', $unit]
66+
$_unless = undef
67+
$_onlyif = [['systemctl', '--global', 'is-enabled', $unit]]
68+
}
69+
exec { $_title:
70+
command => $_command,
71+
unless => $_unless,
72+
onlyif => $_onlyif,
73+
path => $facts['path'],
74+
}
75+
} else { # per user services
76+
77+
$_systemctl_user = [
78+
'systemd-run', '--pipe', '--wait', '--user', '--machine', "${user}@.host",
79+
'systemctl', '--user',
80+
]
81+
82+
# To accept notifies of this type.
83+
exec { "try-reload-or-restart-${user}-${unit}":
84+
command => $_systemctl_user + ['try-reload-or-restart', $unit],
85+
refreshonly => true,
86+
path => $facts['path'],
87+
}
88+
89+
if $_ensure {
90+
$_ensure_title = "Start user service ${unit} for user ${user}"
91+
$_ensure_command = $_systemctl_user + ['start', $unit]
92+
$_ensure_unless = [$_systemctl_user + ['is-active', $unit]]
93+
$_ensure_onlyif = undef
94+
95+
# Don't reload just after starting
96+
Exec["try-reload-or-restart-${user}-${unit}"] -> Exec[$_ensure_title]
97+
} else {
98+
$_ensure_title = "Stop user service ${unit} for user ${user}"
99+
$_ensure_command = $_systemctl_user + ['stop', $unit]
100+
$_ensure_unless = undef
101+
$_ensure_onlyif = [$_systemctl_user + ['is-active', $unit]]
102+
}
103+
104+
exec { $_ensure_title:
105+
command => $_ensure_command,
106+
unless => $_ensure_unless,
107+
onlyif => $_ensure_onlyif,
108+
path => $facts['path'],
109+
}
110+
111+
if $enable {
112+
$_enable_title = "Enable user service ${unit} for user ${user}"
113+
$_enable_command = $_systemctl_user + ['enable', $unit]
114+
$_enable_unless = [$_systemctl_user + ['is-enabled', $unit]]
115+
$_enable_onlyif = undef
116+
117+
# Puppet does this for services so lets copy that logic
118+
# don't enable if you can't start.
119+
if $_ensure {
120+
Exec[$_ensure_title] -> Exec[$_enable_title]
121+
}
122+
} else {
123+
$_enable_title = "Disable user service ${unit} for user ${user}"
124+
$_enable_command = $_systemctl_user + ['disable', $unit]
125+
$_enable_unless = undef
126+
$_enable_onlyif = [$_systemctl_user + ['is-enabled', $unit]]
127+
}
128+
129+
exec { $_enable_title:
130+
command => $_enable_command,
131+
unless => $_enable_unless,
132+
onlyif => $_enable_onlyif,
133+
path => $facts['path'],
134+
}
135+
}
136+
}

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"requirements": [
101101
{
102102
"name": "puppet",
103-
"version_requirement": ">= 7.0.0 < 9.0.0"
103+
"version_requirement": ">= 7.9.0 < 9.0.0"
104104
}
105105
]
106106
}

0 commit comments

Comments
 (0)