Skip to content

Commit 0ea30e4

Browse files
Update to support Puppet 4
* Optimized the limits files * Added a custom data type for service limits * Created a class for restarting the daemon so that other modules can use it cleanly * Updated tests Closes #17
1 parent a032136 commit 0ea30e4

19 files changed

+368
-193
lines changed

.fixtures.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
22
fixtures:
3+
repositories:
4+
stdlib: https://github.com/puppetlabs/puppetlabs-stdlib
35
symlinks:
4-
systemd: "#{source_dir}"
6+
systemd: "#{source_dir}"

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--fail-fast

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ group :development, :unit_tests do
2525
end
2626

2727
group :system_tests do
28-
gem 'beaker', :require => false
28+
gem 'beaker', :git => 'https://github.com/trevor-vaughan/beaker.git', :branch => 'BKR-978-2.51.0'
29+
#gem 'beaker', :require => false
2930
gem 'beaker-rspec', '> 5', :require => false
3031
gem 'beaker_spec_helper', :require => false
3132
gem 'serverspec', :require => false

manifests/init.pp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
# -- Class systemd
2-
# This module allows triggering systemd commands once for all modules
1+
# This module allows triggering systemd commands once for all modules
2+
#
3+
# @api public
4+
#
5+
# @param service_limits
6+
# May be passed a resource hash suitable for passing directly into the
7+
# ``create_resources()`` function as called on ``systemd::service_limits``
8+
#
39
class systemd (
4-
$service_limits = {}
10+
Optional[Hash] $service_limits = undef
511
){
612

7-
Exec {
8-
refreshonly => true,
9-
path => $::path,
10-
}
11-
12-
exec {
13-
'systemctl-daemon-reload':
14-
command => 'systemctl daemon-reload',
15-
}
13+
include '::systemd::systemctl::daemon_reload'
14+
include '::systemd::tmpfiles'
1615

17-
exec {
18-
'systemd-tmpfiles-create':
19-
command => 'systemd-tmpfiles --create',
16+
if $service_limits {
17+
create_resources('systemd::service_limits', $service_limits)
2018
}
21-
22-
create_resources('systemd::service_limits', $service_limits, {})
23-
2419
}

manifests/service_limits.pp

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,86 @@
1-
# -- Define: systemd::service_limits
2-
# Creates a custom config file and reloads systemd
1+
# Adds a set of custom limits to the service
2+
#
3+
# @api public
4+
#
5+
# @see systemd.exec(5)
6+
#
7+
# @attr name [Pattern['^.+\.(service|socket|mount|swap)$']]
8+
# The name of the service that you will be modifying
9+
#
10+
# @param $ensure
11+
# Whether to drop a file or remove it
12+
#
13+
# @param path
14+
# The path to the main systemd settings directory
15+
#
16+
# @param limits
17+
# A Hash of service limits matching the settings in ``systemd.exec(5)``
18+
#
19+
# * Mutually exclusive with ``$source``
20+
#
21+
# @param source
22+
# A ``File`` resource compatible ``source``
23+
#
24+
# * Mutually exclusive with ``$limits``
25+
#
26+
# @param restart_service
27+
# Restart the managed service after setting the limits
28+
#
329
define systemd::service_limits(
4-
$ensure = file,
5-
$path = '/etc/systemd/system',
6-
$limits = undef,
7-
$source = undef,
8-
$restart_service = true
30+
Enum['file','absent'] $ensure = 'file',
31+
Stdlib::Absolutepath $path = '/etc/systemd/system',
32+
Optional[Systemd::ServiceLimits] $limits = undef,
33+
Optional[String] $source = undef,
34+
Boolean $restart_service = true
935
) {
1036
include ::systemd
1137

12-
if $limits {
13-
validate_hash($limits)
14-
$content = template('systemd/limits.erb')
38+
if $title !~ Pattern['^.+\.(service|socket|mount|swap)$'] {
39+
fail('$name must match Pattern["^.+\.(service|socket|mount|swap)$"]')
40+
}
41+
42+
if $limits and !empty($limits) {
43+
$_content = template("${module_name}/limits.erb")
1544
}
1645
else {
17-
$content = undef
46+
$_content = undef
1847
}
1948

20-
if $limits and $source {
49+
if ($limits and !empty($limits)) and $source {
2150
fail('You may not supply both limits and source parameters to systemd::service_limits')
22-
} elsif $limits == undef and $source == undef {
51+
}
52+
elsif ($limits == undef or empty($limits)) and ($source == undef) {
2353
fail('You must supply either the limits or source parameter to systemd::service_limits')
2454
}
2555

26-
file { "${path}/${title}.d/":
27-
ensure => 'directory',
28-
owner => 'root',
29-
group => 'root',
30-
}
31-
->
32-
file { "${path}/${title}.d/limits.conf":
56+
ensure_resource('file', "${path}/${title}.d/",
57+
{
58+
ensure => 'directory',
59+
owner => 'root',
60+
group => 'root',
61+
mode => '0644',
62+
}
63+
)
64+
65+
file { "${path}/${title}.d/90-limits.conf":
3366
ensure => $ensure,
34-
content => $content,
67+
content => $_content,
3568
source => $source,
3669
owner => 'root',
3770
group => 'root',
38-
mode => '0444',
39-
notify => Exec['systemctl-daemon-reload'],
71+
mode => '0644',
4072
}
4173

74+
File["${path}/${title}.d/90-limits.conf"] ~> Class['systemd::systemctl::daemon_reload']
75+
4276
if $restart_service {
43-
exec { "systemctl restart ${title}":
77+
exec { "restart ${title} because limits":
78+
command => "systemctl restart ${title}",
4479
path => $::path,
4580
refreshonly => true,
46-
subscribe => File["${path}/${title}.d/limits.conf"],
47-
require => Exec['systemctl-daemon-reload'],
4881
}
82+
83+
File["${path}/${title}.d/90-limits.conf"] ~> Exec["restart ${title} because limits"]
84+
Class['systemd::systemctl::daemon_reload'] -> Exec["restart ${title} because limits"]
4985
}
5086
}

manifests/systemctl/daemon_reload.pp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Reload the systemctl daemon
2+
#
3+
# @api public
4+
class systemd::systemctl::daemon_reload {
5+
exec { 'systemctl-daemon-reload':
6+
command => 'systemctl daemon-reload',
7+
refreshonly => true,
8+
path => $::path,
9+
}
10+
}

manifests/tmpfile.pp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1-
# -- Define: systemd::tmpfile
2-
# Creates a tmpfile and reloads systemd
1+
# Creates a systemd tmpfile
2+
#
3+
# @api public
4+
#
5+
# @see systemd-tmpfiles(8)
6+
#
7+
# @attr name [String]
8+
# The name of the tmpfile to create
9+
#
10+
# * May not contain ``/``
11+
#
12+
# @param $ensure
13+
# Whether to drop a file or remove it
14+
#
15+
# @param path
16+
# The path to the main systemd tmpfiles directory
17+
#
18+
# @param content
19+
# The literal content to write to the file
20+
#
21+
# * Mutually exclusive with ``$source``
22+
#
23+
# @param source
24+
# A ``File`` resource compatible ``source``
25+
#
26+
# * Mutually exclusive with ``$limits``
27+
#
328
define systemd::tmpfile(
4-
$ensure = file,
5-
$path = '/etc/tmpfiles.d',
6-
$content = undef,
7-
$source = undef,
29+
Enum['file','absent'] $ensure = 'file',
30+
Stdlib::Absolutepath $path = '/etc/tmpfiles.d',
31+
Optional[String] $content = undef,
32+
Optional[String] $source = undef,
833
) {
934
include ::systemd
1035

36+
if name =~ Pattern['/'] {
37+
fail('$name may not contain a forward slash "(/)"')
38+
}
39+
1140
file { "${path}/${title}":
1241
ensure => $ensure,
1342
content => $content,
1443
source => $source,
1544
owner => 'root',
1645
group => 'root',
17-
mode => '0444',
18-
notify => Exec['systemd-tmpfiles-create'],
46+
mode => '0644',
47+
notify => Class['systemd::tmpfiles'],
1948
}
20-
}
49+
}

manifests/tmpfiles.pp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Update the systemd temp files
2+
#
3+
# @api public
4+
#
5+
# @see systemd-tmpfiles(8)
6+
#
7+
# @param operations
8+
# The operations to perform on the systemd tempfiles
9+
#
10+
# * All operations may be combined but you'll probably only ever want to
11+
# use ``create``
12+
#
13+
class systemd::tmpfiles (
14+
Array[Enum['create','clean','remove']] $operations = ['create']
15+
) {
16+
17+
$_ops = join($operations.map |$op| { "--${op}" }, ' ')
18+
19+
exec { 'systemd-tmpfiles':
20+
command => "systemd-tmpfiles ${_ops}",
21+
refreshonly => true,
22+
path => $::path,
23+
}
24+
}

manifests/unit_file.pp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
1-
# -- Define: systemd::unit_file
2-
# Creates a unit file and reloads systemd
1+
# Creates a systemd unit file
2+
#
3+
# @api public
4+
#
5+
# @see systemd.unit(5)
6+
#
7+
# @attr name [Pattern['^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']]
8+
# The target unit file to create
9+
#
10+
# * Must not contain ``/``
11+
#
12+
# @attr path
13+
# The main systemd configuration path
14+
#
15+
# @attr content
16+
# The full content of the unit file
17+
#
18+
# * Mutually exclusive with ``$source``
19+
#
20+
# @attr source
21+
# The ``File`` resource compatible ``source``
22+
#
23+
# * Mutually exclusive with ``$content``
24+
#
25+
# @attr target
26+
# If set, will force the file to be a symlink to the given target
27+
#
28+
# * Mutually exclusive with both ``$source`` and ``$content``
29+
#
330
define systemd::unit_file(
4-
$ensure = file,
5-
$path = '/etc/systemd/system',
6-
$content = undef,
7-
$source = undef,
8-
$target = undef,
31+
Enum['file', 'absent'] $ensure = 'file',
32+
Stdlib::Absolutepath $path = '/etc/systemd/system',
33+
Optional[String] $content = undef,
34+
Optional[String] $source = undef,
35+
Optional[Stdlib::Absolutepath] $target = undef,
936
) {
1037
include ::systemd
1138

39+
if $title !~ Pattern['^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$'] {
40+
fail('$name must match Pattern["^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$"]')
41+
}
42+
43+
if $target {
44+
$_ensure = 'link'
45+
}
46+
else {
47+
$_ensure = $ensure
48+
}
49+
1250
file { "${path}/${title}":
13-
ensure => $ensure,
51+
ensure => $_ensure,
1452
content => $content,
1553
source => $source,
1654
target => $target,
1755
owner => 'root',
1856
group => 'root',
19-
mode => '0444',
20-
notify => Exec['systemctl-daemon-reload'],
57+
mode => '0644',
58+
notify => Class['systemd::systemctl::daemon_reload'],
2159
}
2260
}

metadata.json

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
{
22
"name": "camptocamp-systemd",
3-
"version": "0.4.0",
3+
"version": "1.0.0",
44
"author": "camptocamp",
55
"summary": "Puppet Systemd module",
66
"license": "Apache-2.0",
77
"source": "https://github.com/camptocamp/puppet-systemd",
88
"project_page": "https://github.com/camptocamp/puppet-systemd",
99
"issues_url": "https://github.com/camptocamp/puppet-systemd/issues",
1010
"dependencies": [
11-
11+
{
12+
"name": "puppetlabs/stdlib",
13+
"version_requirement": ">= 4.13.1 < 5.0.0"
14+
}
1215
],
1316
"requirements": [
1417
{
15-
"name": "pe",
16-
"version_requirement": "3.x"
18+
"name": "puppet",
19+
"version_requirement": "4.x"
1720
},
1821
{
19-
"name": "puppet",
20-
"version_requirement": "3.x"
22+
"name": "pe",
23+
"version_requirement": ">= 2016.2.0"
2124
}
2225
],
2326
"operatingsystem_support": [
@@ -32,17 +35,12 @@
3235
"operatingsystemrelease": [
3336
"7"
3437
]
38+
},
39+
{
40+
"operatingsystem": "CentOS",
41+
"operatingsystemrelease": [
42+
"7"
43+
]
3544
}
36-
],
37-
"puppet_version": [
38-
"2.7",
39-
"3.0",
40-
"3.1",
41-
"3.2",
42-
"3.3",
43-
"3.4",
44-
"3.5",
45-
"3.6",
46-
"3.7"
4745
]
4846
}

0 commit comments

Comments
 (0)