|
1 # == Define: concat |
|
2 # |
|
3 # Sets up so that you can use fragments to build a final config file, |
|
4 # |
|
5 # === Options: |
|
6 # |
|
7 # [*ensure*] |
|
8 # Present/Absent |
|
9 # [*path*] |
|
10 # The path to the final file. Use this in case you want to differentiate |
|
11 # between the name of a resource and the file path. Note: Use the name you |
|
12 # provided in the target of your fragments. |
|
13 # [*owner*] |
|
14 # Who will own the file |
|
15 # [*group*] |
|
16 # Who will own the file |
|
17 # [*mode*] |
|
18 # The mode of the final file |
|
19 # [*force*] |
|
20 # Enables creating empty files if no fragments are present |
|
21 # [*warn*] |
|
22 # Adds a normal shell style comment top of the file indicating that it is |
|
23 # built by puppet |
|
24 # [*force*] |
|
25 # [*backup*] |
|
26 # Controls the filebucketing behavior of the final file and see File type |
|
27 # reference for its use. Defaults to 'puppet' |
|
28 # [*replace*] |
|
29 # Whether to replace a file that already exists on the local system |
|
30 # [*order*] |
|
31 # [*ensure_newline*] |
|
32 # [*gnu*] |
|
33 # Deprecated |
|
34 # |
|
35 # === Actions: |
|
36 # * Creates fragment directories if it didn't exist already |
|
37 # * Executes the concatfragments.rb script to build the final file, this |
|
38 # script will create directory/fragments.concat. Execution happens only |
|
39 # when: |
|
40 # * The directory changes |
|
41 # * fragments.concat != final destination, this means rebuilds will happen |
|
42 # whenever someone changes or deletes the final file. Checking is done |
|
43 # using /usr/bin/cmp. |
|
44 # * The Exec gets notified by something else - like the concat::fragment |
|
45 # define |
|
46 # * Copies the file over to the final destination using a file resource |
|
47 # |
|
48 # === Aliases: |
|
49 # |
|
50 # * The exec can notified using Exec["concat_/path/to/file"] or |
|
51 # Exec["concat_/path/to/directory"] |
|
52 # * The final file can be referenced as File["/path/to/file"] or |
|
53 # File["concat_/path/to/file"] |
|
54 # |
|
55 define concat( |
|
56 $ensure = 'present', |
|
57 $path = $name, |
|
58 $owner = undef, |
|
59 $group = undef, |
|
60 $mode = '0644', |
|
61 $warn = false, |
|
62 $force = false, |
|
63 $backup = 'puppet', |
|
64 $replace = true, |
|
65 $order = 'alpha', |
|
66 $ensure_newline = false, |
|
67 $validate_cmd = undef, |
|
68 $gnu = undef |
|
69 ) { |
|
70 validate_re($ensure, '^present$|^absent$') |
|
71 validate_absolute_path($path) |
|
72 validate_string($owner) |
|
73 validate_string($group) |
|
74 validate_string($mode) |
|
75 if ! (is_string($warn) or $warn == true or $warn == false) { |
|
76 fail('$warn is not a string or boolean') |
|
77 } |
|
78 validate_bool($force) |
|
79 if ! concat_is_bool($backup) and ! is_string($backup) { |
|
80 fail('$backup must be string or bool!') |
|
81 } |
|
82 validate_bool($replace) |
|
83 validate_re($order, '^alpha$|^numeric$') |
|
84 validate_bool($ensure_newline) |
|
85 if $validate_cmd and ! is_string($validate_cmd) { |
|
86 fail('$validate_cmd must be a string') |
|
87 } |
|
88 if $gnu { |
|
89 warning('The $gnu parameter to concat is deprecated and has no effect') |
|
90 } |
|
91 |
|
92 include concat::setup |
|
93 |
|
94 $safe_name = regsubst($name, '[/:]', '_', 'G') |
|
95 $concatdir = $concat::setup::concatdir |
|
96 $fragdir = "${concatdir}/${safe_name}" |
|
97 $concat_name = 'fragments.concat.out' |
|
98 $script_command = $concat::setup::script_command |
|
99 $default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.' |
|
100 $bool_warn_message = 'Using stringified boolean values (\'true\', \'yes\', \'on\', \'false\', \'no\', \'off\') to represent boolean true/false as the $warn parameter to concat is deprecated and will be treated as the warning message in a future release' |
|
101 |
|
102 # lint:ignore:quoted_booleans |
|
103 case $warn { |
|
104 true: { |
|
105 $warn_message = $default_warn_message |
|
106 } |
|
107 # lint:ignore:quoted_booleans |
|
108 'true', 'yes', 'on': { |
|
109 # lint:endignore |
|
110 warning($bool_warn_message) |
|
111 $warn_message = $default_warn_message |
|
112 } |
|
113 false: { |
|
114 $warn_message = '' |
|
115 } |
|
116 # lint:ignore:quoted_booleans |
|
117 'false', 'no', 'off': { |
|
118 # lint:endignore |
|
119 warning($bool_warn_message) |
|
120 $warn_message = '' |
|
121 } |
|
122 default: { |
|
123 $warn_message = $warn |
|
124 } |
|
125 } |
|
126 # lint:endignore |
|
127 |
|
128 $warnmsg_escaped = regsubst($warn_message, '\'', '\'\\\'\'', 'G') |
|
129 $warnflag = $warnmsg_escaped ? { |
|
130 '' => '', |
|
131 default => "-w '${warnmsg_escaped}'" |
|
132 } |
|
133 |
|
134 $forceflag = $force ? { |
|
135 true => '-f', |
|
136 false => '', |
|
137 } |
|
138 |
|
139 $orderflag = $order ? { |
|
140 'numeric' => '-n', |
|
141 'alpha' => '', |
|
142 } |
|
143 |
|
144 $newlineflag = $ensure_newline ? { |
|
145 true => '-l', |
|
146 false => '', |
|
147 } |
|
148 |
|
149 File { |
|
150 backup => $backup, |
|
151 } |
|
152 |
|
153 # reset poisoned Exec defaults |
|
154 Exec { |
|
155 user => undef, |
|
156 group => undef, |
|
157 } |
|
158 |
|
159 if $ensure == 'present' { |
|
160 file { $fragdir: |
|
161 ensure => directory, |
|
162 mode => '0750', |
|
163 } |
|
164 |
|
165 file { "${fragdir}/fragments": |
|
166 ensure => directory, |
|
167 mode => '0750', |
|
168 force => true, |
|
169 ignore => ['.svn', '.git', '.gitignore'], |
|
170 notify => Exec["concat_${name}"], |
|
171 purge => true, |
|
172 recurse => true, |
|
173 } |
|
174 |
|
175 file { "${fragdir}/fragments.concat": |
|
176 ensure => present, |
|
177 mode => '0640', |
|
178 } |
|
179 |
|
180 file { "${fragdir}/${concat_name}": |
|
181 ensure => present, |
|
182 mode => '0640', |
|
183 } |
|
184 |
|
185 file { $name: |
|
186 ensure => present, |
|
187 owner => $owner, |
|
188 group => $group, |
|
189 mode => $mode, |
|
190 replace => $replace, |
|
191 path => $path, |
|
192 alias => "concat_${name}", |
|
193 source => "${fragdir}/${concat_name}", |
|
194 backup => $backup, |
|
195 } |
|
196 |
|
197 # Only newer versions of puppet 3.x support the validate_cmd parameter |
|
198 if $validate_cmd { |
|
199 File[$name] { |
|
200 validate_cmd => $validate_cmd, |
|
201 } |
|
202 } |
|
203 |
|
204 # remove extra whitespace from string interpolation to make testing easier |
|
205 $command = strip(regsubst("${script_command} -o \"${fragdir}/${concat_name}\" -d \"${fragdir}\" ${warnflag} ${forceflag} ${orderflag} ${newlineflag}", '\s+', ' ', 'G')) |
|
206 |
|
207 # make sure ruby is in the path for PE |
|
208 if defined('$is_pe') and str2bool("${::is_pe}") { # lint:ignore:only_variable_string |
|
209 if $::kernel == 'windows' { |
|
210 $command_path = "${::env_windows_installdir}/bin:${::path}" |
|
211 } else { |
|
212 $command_path = "/opt/puppetlabs/puppet/bin:/opt/puppet/bin:${::path}" |
|
213 } |
|
214 } elsif $::kernel == 'windows' { |
|
215 $command_path = $::path |
|
216 } else { |
|
217 $command_path = "/opt/puppetlabs/puppet/bin:${::path}" |
|
218 } |
|
219 |
|
220 # if puppet is running as root, this exec should also run as root to allow |
|
221 # the concatfragments.rb script to potentially be installed in path that |
|
222 # may not be accessible by a target non-root owner. |
|
223 exec { "concat_${name}": |
|
224 alias => "concat_${fragdir}", |
|
225 command => $command, |
|
226 notify => File[$name], |
|
227 subscribe => File[$fragdir], |
|
228 unless => "${command} -t", |
|
229 path => $command_path, |
|
230 require => [ |
|
231 File[$fragdir], |
|
232 File["${fragdir}/fragments"], |
|
233 File["${fragdir}/fragments.concat"], |
|
234 ], |
|
235 } |
|
236 } else { |
|
237 file { [ |
|
238 $fragdir, |
|
239 "${fragdir}/fragments", |
|
240 "${fragdir}/fragments.concat", |
|
241 "${fragdir}/${concat_name}" |
|
242 ]: |
|
243 ensure => absent, |
|
244 force => true, |
|
245 } |
|
246 |
|
247 file { $path: |
|
248 ensure => absent, |
|
249 backup => $backup, |
|
250 } |
|
251 |
|
252 # lint:ignore:quoted_booleans |
|
253 $absent_exec_command = $::kernel ? { |
|
254 'windows' => 'cmd.exe /c exit 0', |
|
255 # lint:ignore:quoted_booleans |
|
256 default => 'true', |
|
257 # lint:endignore |
|
258 } |
|
259 # lint:endignore |
|
260 |
|
261 $absent_exec_path = $::kernel ? { |
|
262 'windows' => $::path, |
|
263 default => '/bin:/usr/bin', |
|
264 } |
|
265 |
|
266 # Need to have an unless here for idempotency. |
|
267 exec { "concat_${name}": |
|
268 alias => "concat_${fragdir}", |
|
269 command => $absent_exec_command, |
|
270 unless => $absent_exec_command, |
|
271 path => $absent_exec_path, |
|
272 } |
|
273 } |
|
274 } |
|
275 |
|
276 # vim:sw=2:ts=2:expandtab:textwidth=79 |