|
| 1 | +# @summary Create a systemd socket activated service |
| 2 | +# @api private |
| 3 | +# |
| 4 | +# Systemd socket activated services have their own dependencies. This is a |
| 5 | +# convenience wrapper around systemd::unit_file. |
| 6 | +# |
| 7 | +# @param name [Pattern['^[^/]+$']] |
| 8 | +# The target unit file to create |
| 9 | +# @param socket_content |
| 10 | +# The content for the socket unit file. Required if ensure isn't absent. |
| 11 | +# @param service_content |
| 12 | +# The content for the service unit file. Required if ensure isn't absent. |
| 13 | +# @param enable |
| 14 | +# Whether to enable or disable the service. By default this is derived from |
| 15 | +# $ensure but can be overridden for advanced use cases where the service is |
| 16 | +# running during a migration but shouldn't be enabled on boot. |
| 17 | +define pulpcore::socket_service ( |
| 18 | + Enum['running', 'stopped', 'present', 'absent'] $ensure = 'running', |
| 19 | + Optional[String[1]] $socket_content = undef, |
| 20 | + Optional[String[1]] $service_content = undef, |
| 21 | + Optional[Boolean] $enable = undef, |
| 22 | +) { |
| 23 | + assert_type(Pattern['^[^/]+$'], $name) |
| 24 | + |
| 25 | + if $ensure != 'absent' { |
| 26 | + assert_type(NotUndef, $socket_content) |
| 27 | + assert_type(NotUndef, $service_content) |
| 28 | + } |
| 29 | + |
| 30 | + $active = $ensure ? { |
| 31 | + 'running' => true, |
| 32 | + 'stopped' => false, |
| 33 | + 'absent' => false, |
| 34 | + default => undef, |
| 35 | + } |
| 36 | + $real_enable = pick($enable, $active) |
| 37 | + |
| 38 | + $unit_file_ensure = bool2str($ensure == 'absent', 'absent', 'present') |
| 39 | + |
| 40 | + systemd::unit_file { "${name}.socket": |
| 41 | + ensure => $unit_file_ensure, |
| 42 | + content => $socket_content, |
| 43 | + active => $active, |
| 44 | + enable => $real_enable, |
| 45 | + } |
| 46 | + |
| 47 | + systemd::unit_file { "${name}.service": |
| 48 | + ensure => $unit_file_ensure, |
| 49 | + content => $service_content, |
| 50 | + active => $active, |
| 51 | + enable => $real_enable, |
| 52 | + } |
| 53 | + |
| 54 | + if $active or $real_enable { |
| 55 | + File["/etc/systemd/system/${name}.socket"] -> Service["${name}.service"] |
| 56 | + } |
| 57 | +} |
0 commit comments