|
| 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 | +} |
0 commit comments