Skip to content

Commit

Permalink
Update to support Puppet 4
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
trevor-vaughan committed Jan 16, 2017
1 parent a032136 commit 0b13016
Show file tree
Hide file tree
Showing 20 changed files with 374 additions and 197 deletions.
4 changes: 3 additions & 1 deletion .fixtures.yml
@@ -1,4 +1,6 @@
---
fixtures:
repositories:
stdlib: https://github.com/puppetlabs/puppetlabs-stdlib
symlinks:
systemd: "#{source_dir}"
systemd: "#{source_dir}"
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--format documentation
--color
--fail-fast
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -25,7 +25,8 @@ group :development, :unit_tests do
end

group :system_tests do
gem 'beaker', :require => false
gem 'beaker', :git => 'https://github.com/trevor-vaughan/beaker.git', :branch => 'BKR-978-2.51.0'
#gem 'beaker', :require => false
gem 'beaker-rspec', '> 5', :require => false
gem 'beaker_spec_helper', :require => false
gem 'serverspec', :require => false
Expand Down
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -24,15 +24,16 @@ Let this module handle file creation and systemd reloading.
Or handle file creation yourself and trigger systemd.

```puppet
include ::systemd
include ::systemd::systemctl::daemon_reload
file { '/usr/lib/systemd/system/foo.service':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet:///modules/${module_name}/foo.service",
} ~>
Exec['systemctl-daemon-reload']
Class['systemd::systemctl::daemon_reload']
```

### tmpfiles
Expand All @@ -48,15 +49,16 @@ Let this module handle file creation and systemd reloading
Or handle file creation yourself and trigger systemd.

```puppet
include ::systemd
include ::systemd::tmpfiles
file { '/etc/tmpfiles.d/foo.conf':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet:///modules/${module_name}/foo.conf",
} ~>
Exec['systemd-tmpfiles-create']
Class['systemd::tmpfiles']
```

### service limits
Expand Down
31 changes: 13 additions & 18 deletions manifests/init.pp
@@ -1,24 +1,19 @@
# -- Class systemd
# This module allows triggering systemd commands once for all modules
# This module allows triggering systemd commands once for all modules
#
# @api public
#
# @param service_limits
# May be passed a resource hash suitable for passing directly into the
# ``create_resources()`` function as called on ``systemd::service_limits``
#
class systemd (
$service_limits = {}
Optional[Hash] $service_limits = undef
){

Exec {
refreshonly => true,
path => $::path,
}

exec {
'systemctl-daemon-reload':
command => 'systemctl daemon-reload',
}
include '::systemd::systemctl::daemon_reload'
include '::systemd::tmpfiles'

exec {
'systemd-tmpfiles-create':
command => 'systemd-tmpfiles --create',
if $service_limits {
create_resources('systemd::service_limits', $service_limits)
}

create_resources('systemd::service_limits', $service_limits, {})

}
88 changes: 62 additions & 26 deletions manifests/service_limits.pp
@@ -1,50 +1,86 @@
# -- Define: systemd::service_limits
# Creates a custom config file and reloads systemd
# Adds a set of custom limits to the service
#
# @api public
#
# @see systemd.exec(5)
#
# @attr name [Pattern['^.+\.(service|socket|mount|swap)$']]
# The name of the service that you will be modifying
#
# @param $ensure
# Whether to drop a file or remove it
#
# @param path
# The path to the main systemd settings directory
#
# @param limits
# A Hash of service limits matching the settings in ``systemd.exec(5)``
#
# * Mutually exclusive with ``$source``
#
# @param source
# A ``File`` resource compatible ``source``
#
# * Mutually exclusive with ``$limits``
#
# @param restart_service
# Restart the managed service after setting the limits
#
define systemd::service_limits(
$ensure = file,
$path = '/etc/systemd/system',
$limits = undef,
$source = undef,
$restart_service = true
Enum['file','absent'] $ensure = 'file',
Stdlib::Absolutepath $path = '/etc/systemd/system',
Optional[Systemd::ServiceLimits] $limits = undef,
Optional[String] $source = undef,
Boolean $restart_service = true
) {
include ::systemd

if $limits {
validate_hash($limits)
$content = template('systemd/limits.erb')
if $title !~ Pattern['^.+\.(service|socket|mount|swap)$'] {
fail('$name must match Pattern["^.+\.(service|socket|mount|swap)$"]')
}

if $limits and !empty($limits) {
$_content = template("${module_name}/limits.erb")
}
else {
$content = undef
$_content = undef
}

if $limits and $source {
if ($limits and !empty($limits)) and $source {
fail('You may not supply both limits and source parameters to systemd::service_limits')
} elsif $limits == undef and $source == undef {
}
elsif ($limits == undef or empty($limits)) and ($source == undef) {
fail('You must supply either the limits or source parameter to systemd::service_limits')
}

file { "${path}/${title}.d/":
ensure => 'directory',
owner => 'root',
group => 'root',
}
->
file { "${path}/${title}.d/limits.conf":
ensure_resource('file', "${path}/${title}.d/",
{
ensure => 'directory',
owner => 'root',
group => 'root',
mode => '0644',
}
)

file { "${path}/${title}.d/90-limits.conf":
ensure => $ensure,
content => $content,
content => $_content,
source => $source,
owner => 'root',
group => 'root',
mode => '0444',
notify => Exec['systemctl-daemon-reload'],
mode => '0644',
}

File["${path}/${title}.d/90-limits.conf"] ~> Class['systemd::systemctl::daemon_reload']

if $restart_service {
exec { "systemctl restart ${title}":
exec { "restart ${title} because limits":
command => "systemctl restart ${title}",
path => $::path,
refreshonly => true,
subscribe => File["${path}/${title}.d/limits.conf"],
require => Exec['systemctl-daemon-reload'],
}

File["${path}/${title}.d/90-limits.conf"] ~> Exec["restart ${title} because limits"]
Class['systemd::systemctl::daemon_reload'] -> Exec["restart ${title} because limits"]
}
}
10 changes: 10 additions & 0 deletions manifests/systemctl/daemon_reload.pp
@@ -0,0 +1,10 @@
# Reload the systemctl daemon
#
# @api public
class systemd::systemctl::daemon_reload {
exec { 'systemctl-daemon-reload':
command => 'systemctl daemon-reload',
refreshonly => true,
path => $::path,
}
}
47 changes: 38 additions & 9 deletions manifests/tmpfile.pp
@@ -1,20 +1,49 @@
# -- Define: systemd::tmpfile
# Creates a tmpfile and reloads systemd
# Creates a systemd tmpfile
#
# @api public
#
# @see systemd-tmpfiles(8)
#
# @attr name [String]
# The name of the tmpfile to create
#
# * May not contain ``/``
#
# @param $ensure
# Whether to drop a file or remove it
#
# @param path
# The path to the main systemd tmpfiles directory
#
# @param content
# The literal content to write to the file
#
# * Mutually exclusive with ``$source``
#
# @param source
# A ``File`` resource compatible ``source``
#
# * Mutually exclusive with ``$limits``
#
define systemd::tmpfile(
$ensure = file,
$path = '/etc/tmpfiles.d',
$content = undef,
$source = undef,
Enum['file','absent'] $ensure = 'file',
Stdlib::Absolutepath $path = '/etc/tmpfiles.d',
Optional[String] $content = undef,
Optional[String] $source = undef,
) {
include ::systemd

if name =~ Pattern['/'] {
fail('$name may not contain a forward slash "(/)"')
}

file { "${path}/${title}":
ensure => $ensure,
content => $content,
source => $source,
owner => 'root',
group => 'root',
mode => '0444',
notify => Exec['systemd-tmpfiles-create'],
mode => '0644',
notify => Class['systemd::tmpfiles'],
}
}
}
24 changes: 24 additions & 0 deletions manifests/tmpfiles.pp
@@ -0,0 +1,24 @@
# Update the systemd temp files
#
# @api public
#
# @see systemd-tmpfiles(8)
#
# @param operations
# The operations to perform on the systemd tempfiles
#
# * All operations may be combined but you'll probably only ever want to
# use ``create``
#
class systemd::tmpfiles (
Array[Enum['create','clean','remove']] $operations = ['create']
) {

$_ops = join($operations.map |$op| { "--${op}" }, ' ')

exec { 'systemd-tmpfiles':
command => "systemd-tmpfiles ${_ops}",
refreshonly => true,
path => $::path,
}
}
58 changes: 48 additions & 10 deletions manifests/unit_file.pp
@@ -1,22 +1,60 @@
# -- Define: systemd::unit_file
# Creates a unit file and reloads systemd
# Creates a systemd unit file
#
# @api public
#
# @see systemd.unit(5)
#
# @attr name [Pattern['^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']]
# The target unit file to create
#
# * Must not contain ``/``
#
# @attr path
# The main systemd configuration path
#
# @attr content
# The full content of the unit file
#
# * Mutually exclusive with ``$source``
#
# @attr source
# The ``File`` resource compatible ``source``
#
# * Mutually exclusive with ``$content``
#
# @attr target
# If set, will force the file to be a symlink to the given target
#
# * Mutually exclusive with both ``$source`` and ``$content``
#
define systemd::unit_file(
$ensure = file,
$path = '/etc/systemd/system',
$content = undef,
$source = undef,
$target = undef,
Enum['file', 'absent'] $ensure = 'file',
Stdlib::Absolutepath $path = '/etc/systemd/system',
Optional[String] $content = undef,
Optional[String] $source = undef,
Optional[Stdlib::Absolutepath] $target = undef,
) {
include ::systemd

if $title !~ Pattern['^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$'] {
fail('$name must match Pattern["^.+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$"]')
}

if $target {
$_ensure = 'link'
}
else {
$_ensure = $ensure
}

file { "${path}/${title}":
ensure => $ensure,
ensure => $_ensure,
content => $content,
source => $source,
target => $target,
owner => 'root',
group => 'root',
mode => '0444',
notify => Exec['systemctl-daemon-reload'],
mode => '0644',
notify => Class['systemd::systemctl::daemon_reload'],
}
}

0 comments on commit 0b13016

Please sign in to comment.