Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a private socket_service helper #199

Merged
merged 1 commit into from
May 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions manifests/service.pp
Original file line number Diff line number Diff line change
@@ -2,29 +2,20 @@
# @api private
class pulpcore::service {
include apache
$socket_service_ensure = bool2str($pulpcore::service_ensure, 'running', 'stopped')

systemd::unit_file { 'pulpcore-api.socket':
content => template('pulpcore/pulpcore-api.socket.erb'),
active => $pulpcore::service_ensure,
enable => $pulpcore::service_enable,
}

systemd::unit_file { 'pulpcore-api.service':
content => template('pulpcore/pulpcore-api.service.erb'),
active => $pulpcore::service_ensure,
enable => $pulpcore::service_enable,
pulpcore::socket_service { 'pulpcore-api':
ensure => $socket_service_ensure,
enable => $pulpcore::service_enable,
socket_content => template('pulpcore/pulpcore-api.socket.erb'),
service_content => template('pulpcore/pulpcore-api.service.erb'),
}

systemd::unit_file { 'pulpcore-content.socket':
content => template('pulpcore/pulpcore-content.socket.erb'),
active => $pulpcore::service_ensure,
enable => $pulpcore::service_enable,
}

systemd::unit_file { 'pulpcore-content.service':
content => template('pulpcore/pulpcore-content.service.erb'),
active => $pulpcore::service_ensure,
enable => $pulpcore::service_enable,
pulpcore::socket_service { 'pulpcore-content':
ensure => $socket_service_ensure,
enable => $pulpcore::service_enable,
socket_content => template('pulpcore/pulpcore-content.socket.erb'),
service_content => template('pulpcore/pulpcore-content.service.erb'),
}

systemd::unit_file { 'pulpcore-resource-manager.service':
61 changes: 61 additions & 0 deletions manifests/socket_service.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# @summary Create a systemd socket activated service
# @api private
#
# Systemd socket activated services have their own dependencies. This is a
# convenience wrapper around systemd::unit_file.
#
# @param name [Pattern['^[^/]+$']]
# The target unit file to create
# @param socket_content
# The content for the socket unit file. Required if ensure isn't absent.
# @param service_content
# The content for the service unit file. Required if ensure isn't absent.
# @param enable
# Whether to enable or disable the service. By default this is derived from
# $ensure but can be overridden for advanced use cases where the service is
# running during a migration but shouldn't be enabled on boot.
define pulpcore::socket_service (
Enum['running', 'stopped', 'present', 'absent'] $ensure = 'running',
Optional[String[1]] $socket_content = undef,
Optional[String[1]] $service_content = undef,
Optional[Boolean] $enable = undef,
) {
assert_type(Pattern['^[^/]+$'], $name)

if $ensure != 'absent' {
assert_type(NotUndef, $socket_content)
assert_type(NotUndef, $service_content)
}

$active = $ensure ? {
'running' => true,
'stopped' => false,
'absent' => false,
default => undef,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see present in the list but it is in the Enum of the parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what default is for. I agree it's a bit ugly, but puppet-lint wants a default statement and this is how I dealt with it.

$real_enable = pick($enable, $active)

$unit_file_ensure = bool2str($ensure == 'absent', 'absent', 'present')

systemd::unit_file { "${name}.socket":
ensure => $unit_file_ensure,
content => $socket_content,
active => $active,
enable => $real_enable,
}

systemd::unit_file { "${name}.service":
ensure => $unit_file_ensure,
content => $service_content,
active => $active,
enable => $real_enable,
}

if $active or $real_enable {
# Systemd needs both .socket and .service to be loaded when starting the
# service. The unit_file takes care of matching, this ensures the
# non-matching order.
File["/etc/systemd/system/${name}.socket"] -> Service["${name}.service"]
File["/etc/systemd/system/${name}.service"] -> Service["${name}.socket"]
}
}
8 changes: 8 additions & 0 deletions spec/classes/pulpcore_spec.rb
Original file line number Diff line number Diff line change
@@ -119,6 +119,14 @@

it 'configures services' do
is_expected.to contain_class('pulpcore::service')
is_expected.to contain_pulpcore__socket_service('pulpcore-api')
is_expected.to contain_systemd__unit_file('pulpcore-api.socket')
is_expected.to contain_systemd__unit_file('pulpcore-api.service')
is_expected.to contain_file('/etc/systemd/system/pulpcore-api.socket').that_comes_before('Service[pulpcore-api.service]')
is_expected.to contain_pulpcore__socket_service('pulpcore-content')
is_expected.to contain_systemd__unit_file('pulpcore-content.socket')
is_expected.to contain_systemd__unit_file('pulpcore-content.service')
is_expected.to contain_file('/etc/systemd/system/pulpcore-content.socket').that_comes_before('Service[pulpcore-content.service]')
is_expected.to contain_systemd__unit_file('pulpcore-resource-manager.service')
is_expected.to contain_systemd__unit_file('[email protected]')
is_expected.to contain_service("[email protected]").with_ensure(true)