Skip to content

Commit 040f1ac

Browse files
Shortcut for creating unit files / tmpfiles
This change allows creating unit files and reloading systemd with just a single resource. It's fully compatible with the manual behavior.
1 parent 6d47fd4 commit 040f1ac

File tree

6 files changed

+166
-4
lines changed

6 files changed

+166
-4
lines changed

.fixtures.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixtures:
3+
symlinks:
4+
systemd: "#{source_dir}"

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@
55

66
## Overview
77

8-
This module declares exec resources that you can use when you change systemd units or configuration files.
8+
This module declares exec resources to create global sync points for reloading systemd.
99

10-
## Examples
10+
## Usage and examples
1111

12-
### systemctl --daemon-reload
12+
There are two ways to use this module.
13+
14+
### unit files
15+
16+
Let this module handle file creation and systemd reloading.
17+
18+
```puppet
19+
::systemd::unit_file { 'foo.service':
20+
source => "puppet:///modules/${module_name}/foo.service",
21+
}
22+
```
23+
24+
Or handle file creation yourself and trigger systemd.
1325

1426
```puppet
1527
include ::systemd
@@ -23,7 +35,17 @@ file { '/usr/lib/systemd/system/foo.service':
2335
Exec['systemctl-daemon-reload']
2436
```
2537

26-
### systemd-tmpfiles --create
38+
### tmpfiles
39+
40+
Let this module handle file creation and systemd reloading
41+
42+
```puppet
43+
::systemd::tmpfile { 'foo.conf':
44+
source => "puppet:///modules/${module_name}/foo.conf",
45+
}
46+
```
47+
48+
Or handle file creation yourself and trigger systemd.
2749

2850
```puppet
2951
include ::systemd

manifests/tmpfile.pp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -- Define: systemd::tmpfile
2+
# Creates a tmpfile and reloads systemd
3+
define systemd::tmpfile(
4+
$ensure = file,
5+
$path = '/etc/tmpfiles.d',
6+
$content = undef,
7+
$source = undef,
8+
) {
9+
include ::systemd
10+
11+
file { "${path}/${title}":
12+
ensure => $ensure,
13+
content => $content,
14+
source => $source,
15+
owner => 'root',
16+
group => 'root',
17+
mode => '0444',
18+
notify => Exec['systemd-tmpfiles-create'],
19+
}
20+
}

manifests/unit_file.pp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -- Define: systemd::unit_file
2+
# Creates a unit file and reloads systemd
3+
define systemd::unit_file(
4+
$ensure = file,
5+
$path = '/etc/systemd/system',
6+
$content = undef,
7+
$source = undef,
8+
) {
9+
include ::systemd
10+
11+
file { "${path}/${title}":
12+
ensure => $ensure,
13+
content => $content,
14+
source => $source,
15+
owner => 'root',
16+
group => 'root',
17+
mode => '0444',
18+
notify => Exec['systemctl-daemon-reload'],
19+
}
20+
}

spec/defines/tmpfile_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper'
2+
3+
describe 'systemd::tmpfile' do
4+
5+
let(:facts) { {
6+
:path => '/usr/bin',
7+
} }
8+
9+
context 'default params' do
10+
11+
let(:title) { 'fancy.conf' }
12+
13+
it 'creates the tmpfile' do
14+
should contain_file('/etc/tmpfiles.d/fancy.conf').with({
15+
'ensure' => 'file',
16+
'owner' => 'root',
17+
'group' => 'root',
18+
'mode' => '0444',
19+
})
20+
end
21+
22+
it 'triggers systemd daemon-reload' do
23+
should contain_class('systemd')
24+
should contain_file('/etc/tmpfiles.d/fancy.conf').with_notify("Exec[systemd-tmpfiles-create]")
25+
end
26+
end
27+
28+
context 'with params' do
29+
let(:title) { 'fancy.conf' }
30+
31+
let(:params) { {
32+
:ensure => 'absent',
33+
:path => '/etc/tmpfiles.d/foo',
34+
:content => 'some-content',
35+
:source => 'some-source',
36+
} }
37+
38+
it 'creates the unit file' do
39+
should contain_file('/etc/tmpfiles.d/foo/fancy.conf').with({
40+
'ensure' => 'absent',
41+
'content' => 'some-content',
42+
'source' => 'some-source',
43+
})
44+
end
45+
46+
end
47+
48+
end

spec/defines/unit_file_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper'
2+
3+
describe 'systemd::unit_file' do
4+
5+
let(:facts) { {
6+
:path => '/usr/bin',
7+
} }
8+
9+
context 'default params' do
10+
11+
let(:title) { 'fancy.service' }
12+
13+
it 'creates the unit file' do
14+
should contain_file('/etc/systemd/system/fancy.service').with({
15+
'ensure' => 'file',
16+
'owner' => 'root',
17+
'group' => 'root',
18+
'mode' => '0444',
19+
})
20+
end
21+
22+
it 'triggers systemd daemon-reload' do
23+
should contain_class('systemd')
24+
should contain_file('/etc/systemd/system/fancy.service').with_notify("Exec[systemctl-daemon-reload]")
25+
end
26+
end
27+
28+
context 'with params' do
29+
let(:title) { 'fancy.service' }
30+
31+
let(:params) { {
32+
:ensure => 'absent',
33+
:path => '/usr/lib/systemd/system',
34+
:content => 'some-content',
35+
:source => 'some-source',
36+
} }
37+
38+
it 'creates the unit file' do
39+
should contain_file('/usr/lib/systemd/system/fancy.service').with({
40+
'ensure' => 'absent',
41+
'content' => 'some-content',
42+
'source' => 'some-source',
43+
})
44+
end
45+
46+
end
47+
48+
end

0 commit comments

Comments
 (0)