Skip to content

Commit 81f3eb5

Browse files
authored
Add notify_service support to dropin_file (#191)
It is very common that a drop in file should notify a service that's managed elsewhere. This adds a parameter to do so which then uses a collector.
1 parent 3934a55 commit 81f3eb5

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

REFERENCE.md

+9
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ The following parameters are available in the `systemd::dropin_file` defined typ
374374
* [`group`](#group)
375375
* [`mode`](#mode)
376376
* [`show_diff`](#show_diff)
377+
* [`notify_service`](#notify_service)
377378
* [`unit`](#unit)
378379
* [`filename`](#filename)
379380
* [`ensure`](#ensure)
@@ -462,6 +463,14 @@ Whether to show the diff when updating dropin file
462463

463464
Default value: ``true``
464465

466+
##### <a name="notify_service"></a>`notify_service`
467+
468+
Data type: `Boolean`
469+
470+
Notify a service for the unit, if it exists
471+
472+
Default value: ``false``
473+
465474
##### <a name="unit"></a>`unit`
466475

467476
Data type: `Systemd::Unit`

manifests/dropin_file.pp

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
# @param show_diff
4141
# Whether to show the diff when updating dropin file
4242
#
43+
# @param notify_service
44+
# Notify a service for the unit, if it exists
45+
#
4346
define systemd::dropin_file (
4447
Systemd::Unit $unit,
4548
Systemd::Dropin $filename = $name,
@@ -53,6 +56,7 @@
5356
String $group = 'root',
5457
String $mode = '0444',
5558
Boolean $show_diff = true,
59+
Boolean $notify_service = false,
5660
) {
5761
include systemd
5862

@@ -65,8 +69,10 @@
6569
}
6670
}
6771

72+
$full_filename = "${path}/${unit}.d/${filename}"
73+
6874
if $ensure != 'absent' {
69-
ensure_resource('file', "${path}/${unit}.d", {
75+
ensure_resource('file', dirname($full_filename), {
7076
ensure => directory,
7177
owner => 'root',
7278
group => 'root',
@@ -76,7 +82,7 @@
7682
})
7783
}
7884

79-
file { "${path}/${unit}.d/${filename}":
85+
file { $full_filename:
8086
ensure => $_ensure,
8187
content => $content,
8288
source => $source,
@@ -87,4 +93,12 @@
8793
selinux_ignore_defaults => $selinux_ignore_defaults,
8894
show_diff => $show_diff,
8995
}
96+
97+
if $notify_service {
98+
File[$full_filename] ~> Service <| title == $unit or name == $unit |>
99+
if $unit =~ /\.service$/ {
100+
$short_service_name = regsubst($unit, /\.service$/, '')
101+
File[$full_filename] ~> Service <| title == $short_service_name or name == $short_service_name |>
102+
}
103+
}
90104
}

spec/defines/dropin_file_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@
3535
)
3636
}
3737

38+
context 'notifies services' do
39+
let(:params) do
40+
super().merge(notify_service: true)
41+
end
42+
let(:filename) { "/etc/systemd/system/#{params[:unit]}.d/#{title}" }
43+
let(:pre_condition) do
44+
<<-PUPPET
45+
service { ['test', 'test.service']:
46+
}
47+
PUPPET
48+
end
49+
50+
it { is_expected.to compile.with_all_deps }
51+
it { is_expected.to contain_service('test').that_subscribes_to("File[#{filename}]") }
52+
it { is_expected.to contain_service('test.service').that_subscribes_to("File[#{filename}]") }
53+
54+
context 'with overridden name' do
55+
let(:pre_condition) do
56+
<<-PUPPET
57+
service { 'myservice':
58+
name => 'test',
59+
}
60+
PUPPET
61+
end
62+
63+
it { is_expected.to compile.with_all_deps }
64+
it { is_expected.to contain_service('myservice').that_subscribes_to("File[#{filename}]") }
65+
end
66+
end
67+
3868
context 'with selinux_ignore_defaults set to true' do
3969
let(:params) do
4070
super().merge(selinux_ignore_defaults: true)

0 commit comments

Comments
 (0)