Skip to content

Commit fe0081e

Browse files
committed
Introduce a private socket_service helper
This ensures the correct dependency ordering. This is submitted to camptocamp/systemd but we're currently still on 2.x so no update will be available. This also helps with the move to 3.x.
1 parent 7c07b93 commit fe0081e

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

manifests/service.pp

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,20 @@
22
# @api private
33
class pulpcore::service {
44
include apache
5+
$socket_service_ensure = bool2str($pulpcore::service_ensure, 'running', 'stopped')
56

6-
systemd::unit_file { 'pulpcore-api.socket':
7-
content => template('pulpcore/pulpcore-api.socket.erb'),
8-
active => $pulpcore::service_ensure,
9-
enable => $pulpcore::service_enable,
10-
}
11-
12-
systemd::unit_file { 'pulpcore-api.service':
13-
content => template('pulpcore/pulpcore-api.service.erb'),
14-
active => $pulpcore::service_ensure,
15-
enable => $pulpcore::service_enable,
7+
pulpcore::socket_service { 'pulpcore-api':
8+
ensure => $socket_service_ensure,
9+
enable => $pulpcore::service_enable,
10+
socket_content => template('pulpcore/pulpcore-api.socket.erb'),
11+
service_content => template('pulpcore/pulpcore-api.service.erb'),
1612
}
1713

18-
systemd::unit_file { 'pulpcore-content.socket':
19-
content => template('pulpcore/pulpcore-content.socket.erb'),
20-
active => $pulpcore::service_ensure,
21-
enable => $pulpcore::service_enable,
22-
}
23-
24-
systemd::unit_file { 'pulpcore-content.service':
25-
content => template('pulpcore/pulpcore-content.service.erb'),
26-
active => $pulpcore::service_ensure,
27-
enable => $pulpcore::service_enable,
14+
pulpcore::socket_service { 'pulpcore-content':
15+
ensure => $socket_service_ensure,
16+
enable => $pulpcore::service_enable,
17+
socket_content => template('pulpcore/pulpcore-content.socket.erb'),
18+
service_content => template('pulpcore/pulpcore-content.service.erb'),
2819
}
2920

3021
systemd::unit_file { 'pulpcore-resource-manager.service':

manifests/socket_service.pp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)