36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'manifests/user_service.pp', line 36
define systemd::user_service (
Systemd::Unit $unit = $title,
Variant[Boolean,Enum['stopped','running']] $ensure = false,
Boolean $enable = false,
Boolean $global = false,
Optional[String[1]] $user = undef,
) {
$_ensure = $ensure ? {
'stopped' => false,
'running' => true,
default => $ensure
}
if ( $global and $user ) or ( ! $global and ! $user) {
fail('Exactly one of the "user" or "global" parameters must be defined')
}
if $global and $_ensure {
fail('Cannot ensure a service is running for all users globally')
}
if $global {
if $enable {
$_title = "Enable user service ${unit} globally"
$_command = ['systemctl', '--global', 'enable', $unit]
$_unless = [['systemctl', '--global', 'is-enabled', $unit]]
$_onlyif = undef
} else {
$_title = "Disable user service ${unit} globally"
$_command = ['systemctl', '--global', 'disable', $unit]
$_unless = undef
$_onlyif = [['systemctl', '--global', 'is-enabled', $unit]]
}
exec { $_title:
command => $_command,
unless => $_unless,
onlyif => $_onlyif,
path => $facts['path'],
}
} else { # per user services
$_systemctl_user = [
'systemd-run', '--pipe', '--wait', '--user', '--machine', "${user}@.host",
'systemctl', '--user',
]
# To accept notifies of this type.
exec { "try-reload-or-restart-${user}-${unit}":
command => $_systemctl_user + ['try-reload-or-restart', $unit],
refreshonly => true,
path => $facts['path'],
}
if $_ensure {
$_ensure_title = "Start user service ${unit} for user ${user}"
$_ensure_command = $_systemctl_user + ['start', $unit]
$_ensure_unless = [$_systemctl_user + ['is-active', $unit]]
$_ensure_onlyif = undef
# Don't reload just after starting
Exec["try-reload-or-restart-${user}-${unit}"] -> Exec[$_ensure_title]
} else {
$_ensure_title = "Stop user service ${unit} for user ${user}"
$_ensure_command = $_systemctl_user + ['stop', $unit]
$_ensure_unless = undef
$_ensure_onlyif = [$_systemctl_user + ['is-active', $unit]]
}
exec { $_ensure_title:
command => $_ensure_command,
unless => $_ensure_unless,
onlyif => $_ensure_onlyif,
path => $facts['path'],
}
if $enable {
$_enable_title = "Enable user service ${unit} for user ${user}"
$_enable_command = $_systemctl_user + ['enable', $unit]
$_enable_unless = [$_systemctl_user + ['is-enabled', $unit]]
$_enable_onlyif = undef
# Puppet does this for services so lets copy that logic
# don't enable if you can't start.
if $_ensure {
Exec[$_ensure_title] -> Exec[$_enable_title]
}
} else {
$_enable_title = "Disable user service ${unit} for user ${user}"
$_enable_command = $_systemctl_user + ['disable', $unit]
$_enable_unless = undef
$_enable_onlyif = [$_systemctl_user + ['is-enabled', $unit]]
}
exec { $_enable_title:
command => $_enable_command,
unless => $_enable_unless,
onlyif => $_enable_onlyif,
path => $facts['path'],
}
}
}
|