Skip to content

Commit

Permalink
Add support for drop-in files (#39)
Browse files Browse the repository at this point in the history
* Add support for drop-in files

The drop-in directory must not created more then once, even if multiple
drop-in files per unit are created. Also drop-in files for units, which are
not deployed via puppet, should be supported. The only solution is to use
ensure_resource from puppetlabs/stdlib. Everything else is the same as for a
unit_file, except the name of the drop-in file, which has to end with '.conf'.

* Add spec tests for dropin_file

* Update README with the drop-in file feature

* Typo Fixes

* Fix broken spec test
  • Loading branch information
countsudoku authored and raphink committed Aug 17, 2017
1 parent 047b31d commit 0e4281a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ file { '/usr/lib/systemd/system/foo.service':
~> Class['systemd::systemctl::daemon_reload']
```

### drop-in files

Drop-in files are used to add or alter settings of a unit without modifying the
unit itself. As for the unit files, the module can handle the file and
directory creation and systemd reloading:

```puppet
::systemd::dropin_file { 'foo.conf':
unit => 'foo.service',
source => "puppet:///modules/${module_name}/foo.conf",
}
```

Or handle file and directory creation yourself and trigger systemd:

```puppet
include ::systemd::systemctl::daemon_reload
file { '/etc/systemd/system/foo.service.d':
ensure => directory,
owner => 'root',
group => 'root',
}
file { '/etc/systemd/system/foo.service.d/foo.conf':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet:///modules/${module_name}/foo.conf",
}
~> Class['systemd::systemctl::daemon_reload']
```

### tmpfiles

Let this module handle file creation and systemd reloading
Expand Down
69 changes: 69 additions & 0 deletions manifests/dropin_file.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Creates a drop-in file for a systemd unit
#
# @api public
#
# @see systemd.unit(5)
#
# @attr name [Pattern['^.+\.conf$']]
# 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::dropin_file(
Systemd::Unit $unit,
Enum['present', 'absent', 'file'] $ensure = 'present',
Stdlib::Absolutepath $path = '/etc/systemd/system',
Optional[String] $content = undef,
Optional[String] $source = undef,
Optional[Stdlib::Absolutepath] $target = undef,
) {
include ::systemd

assert_type(Systemd::Dropin, $name)

if $target {
$_ensure = 'link'
} else {
$_ensure = $ensure ? {
'present' => 'file',
default => $ensure,
}
}

if $ensure != 'absent' {
ensure_resource('file', "${path}/${unit}.d", {
ensure => directory,
owner => 'root',
group => 'root',
})
}

file { "${path}/${unit}.d/${name}":
ensure => $_ensure,
content => $content,
source => $source,
target => $target,
owner => 'root',
group => 'root',
mode => '0444',
notify => Class['systemd::systemctl::daemon_reload'],
}
}
42 changes: 42 additions & 0 deletions spec/defines/dropin_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe 'systemd::dropin_file' do
context 'supported operating systems' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) { facts }

let(:title) { 'test.conf' }

let(:params) {{
:unit => 'test.service',
:content => 'random stuff'
}}

it { is_expected.to compile.with_all_deps }

it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d").with(
:ensure => 'directory',
) }

it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").with(
:ensure => 'file',
:content => /#{params[:content]}/,
:mode => '0444'
) }

it { is_expected.to create_file("/etc/systemd/system/#{params[:unit]}.d/#{title}").that_notifies('Class[systemd::systemctl::daemon_reload]') }

context 'with a bad unit type' do
let(:title) { 'test.badtype' }

it {
expect{
is_expected.to compile.with_all_deps
}.to raise_error(/expects a match for Systemd::Dropin/)
}
end
end
end
end
end
1 change: 1 addition & 0 deletions types/dropin.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Systemd::Dropin = Pattern['^.+\.conf$']

0 comments on commit 0e4281a

Please sign in to comment.