|
1 # = Define puppi::project::builder |
|
2 # |
|
3 # This is a shortcut define to build a puppi project for the deploy of web |
|
4 # applications based on different sources: a war file, a tar file, a source dir, |
|
5 # a list of files or a nexus maven repository |
|
6 # It uses different "core" defines (puppi::project, puppi:deploy (many), |
|
7 # puppi::rollback (many)) to build a full featured template project for |
|
8 # automatic deployments. |
|
9 # If you need to customize it, either change the template defined here or |
|
10 # build up your own custom ones. |
|
11 # |
|
12 # == Variables: |
|
13 # |
|
14 # [*source*] |
|
15 # The full URL of the main file to retrieve. |
|
16 # Format should be in URI standard (http:// file:// ssh:// rsync://). |
|
17 # |
|
18 # [*source_type*] |
|
19 # The type of file that is retrieved. Accepted values: tarball, zip, list, |
|
20 # war, dir, maven-metadata. |
|
21 # |
|
22 # [*deploy_root*] |
|
23 # The destination directory where the retrieved file(s) are deployed. |
|
24 # |
|
25 # [*init_source*] |
|
26 # (Optional) - The full URL to be used to retrieve, for the first time, |
|
27 # the project files. They are copied directly to the $deploy_root |
|
28 # Format should be in URI standard (http:// file:// ssh:// svn://). |
|
29 # |
|
30 # [*magicfix*] |
|
31 # (Optional) - A string that is used as prefix or suffix according to the |
|
32 # context and the scripts used in the deploy procedure. |
|
33 # |
|
34 # [*user*] |
|
35 # (Optional) - The user to be used for deploy operations. |
|
36 # |
|
37 # [*predeploy_customcommand*] |
|
38 # (Optional) - Full path with arguments of an eventual custom command to |
|
39 # execute before the deploy. The command is executed as $predeploy_user. |
|
40 # |
|
41 # [*predeploy_user*] |
|
42 # (Optional) - The user to be used to execute the $predeploy_customcommand. |
|
43 # By default is the same of $user. |
|
44 # |
|
45 # [*predeploy_priority*] |
|
46 # (Optional) - The priority (execution sequence number) that defines when, |
|
47 # during the deploy procedure, the $predeploy_customcommand is executed |
|
48 # Default: 39 (immediately before the copy of files on the deploy root). |
|
49 # |
|
50 # [*postdeploy_customcommand*] |
|
51 # (Optional) - Full path with arguments of an eventual custom command to |
|
52 # execute after the deploy. The command is executed as $postdeploy_user. |
|
53 # |
|
54 # [*postdeploy_user*] |
|
55 # (Optional) - The user to be used to execute the $postdeploy_customcommand. |
|
56 # By default is the same of $user. |
|
57 # |
|
58 # [*postdeploy_priority*] |
|
59 # (Optional) - The priority (execution sequence number) that defines when, |
|
60 # during the deploy procedure, the $postdeploy_customcommand is executed |
|
61 # Default: 41 (immediately after the copy of files on the deploy root). |
|
62 # |
|
63 # [*disable_services*] |
|
64 # (Optional) - The names (space separated) of the services you might want to |
|
65 # stop during deploy. By default is blank. Example: "apache puppet monit". |
|
66 # |
|
67 # [*firewall_src_ip*] |
|
68 # (Optional) - The IP address of a loadbalancer you might want to block out |
|
69 # during a deploy. |
|
70 # |
|
71 # [*firewall_dst_port*] |
|
72 # (Optional) - The local port to block from the loadbalancer during deploy |
|
73 # (Default all). |
|
74 # |
|
75 # [*firewall_delay*] |
|
76 # (Optional) - A delay time in seconds to wait after the block of |
|
77 # $firewall_src_ip. Should be at least as long as the loadbalancer check |
|
78 # interval for the services stopped during deploy (Default: 1). |
|
79 # |
|
80 # [*report_email*] |
|
81 # (Optional) - The (space separated) email(s) to notify of deploy/rollback |
|
82 # operations. If none is specified, no email is sent. |
|
83 # |
|
84 # [*backup*] |
|
85 # (Optional) - How backups of files are made. Default: "full". Options: |
|
86 # "full" - Make full backup of the deploy_root before making the deploy |
|
87 # "diff" - Backup only the files that are going to be deployed. Note that |
|
88 # in order to make reliable rollbacks of versions older that the latest |
|
89 # you've to individually rollback every intermediate deploy |
|
90 # "false" - Do not make backups. This disables the option to make rollbacks |
|
91 # |
|
92 # [*backup_rsync_options*] |
|
93 # (Optional) - The extra options to pass to rsync for backup operations. Use |
|
94 # it, for example, to exclude directories that you don't want to archive. |
|
95 # IE: "--exclude .snapshot --exclude cache --exclude www/cache". |
|
96 # |
|
97 # [*backup_retention*] |
|
98 # (Optional) - Number of backup archives to keep. (Default 5). |
|
99 # Lower the default value if your backups are too large and may fill up the |
|
100 # filesystem. |
|
101 # |
|
102 # [*run_checks*] |
|
103 # (Optional) - If you want to run local puppi checks before and after the |
|
104 # deploy procedure. Default: "true". |
|
105 # |
|
106 # [*always_deploy*] |
|
107 # (Optional) - If you always deploy what has been downloaded. Default="yes", |
|
108 # if set to "no" a checksum is made between the files previously downloaded |
|
109 # and the new files. If they are the same the deploy is not done. |
|
110 # |
|
111 # == Usage |
|
112 # A sample deploy of a zip with custom postdeploy command and mail notification |
|
113 # puppi::project::builder { "cms": |
|
114 # source => "http://repo.example42.com/deploy/cms/cms.zip", |
|
115 # source_type => "zip", |
|
116 # user => "root", |
|
117 # deploy_root => "/var/www", |
|
118 # postdeploy_customcommand => "chown -R www-data /var/www/files", |
|
119 # postdeploy_user => "root", |
|
120 # postdeploy_priority => "41", |
|
121 # report_email => "sysadmins@example42.com", |
|
122 # enable => "true", |
|
123 # } |
|
124 # |
|
125 # [*auto_deploy*] |
|
126 # (Optional) - If you want to automatically run this puppi deploy when |
|
127 # Puppet runs. Default: 'false' |
|
128 # |
|
129 define puppi::project::builder ( |
|
130 $source, |
|
131 $source_type, |
|
132 $deploy_root, |
|
133 $init_source = '', |
|
134 $user = 'root', |
|
135 $magicfix = '', |
|
136 $predeploy_customcommand = '', |
|
137 $predeploy_user = '', |
|
138 $predeploy_priority = '39', |
|
139 $postdeploy_customcommand = '', |
|
140 $postdeploy_user = '', |
|
141 $postdeploy_priority = '41', |
|
142 $disable_services = '', |
|
143 $firewall_src_ip = '', |
|
144 $firewall_dst_port = '0', |
|
145 $firewall_delay = '1', |
|
146 $report_email = '', |
|
147 $backup = 'full', |
|
148 $backup_rsync_options = '--exclude .snapshot', |
|
149 $backup_retention = '5', |
|
150 $run_checks = true, |
|
151 $always_deploy = true, |
|
152 $auto_deploy = false, |
|
153 $enable = true ) { |
|
154 |
|
155 require puppi |
|
156 require puppi::params |
|
157 |
|
158 # Autoinclude the puppi class |
|
159 include puppi |
|
160 |
|
161 # Set default values |
|
162 $predeploy_real_user = $predeploy_user ? { |
|
163 '' => $user, |
|
164 default => $predeploy_user, |
|
165 } |
|
166 |
|
167 $postdeploy_real_user = $postdeploy_user ? { |
|
168 '' => $user, |
|
169 default => $postdeploy_user, |
|
170 } |
|
171 |
|
172 $real_source_type = $source_type ? { |
|
173 'dir' => 'dir', |
|
174 'tarball' => 'tarball', |
|
175 'zip' => 'zip', |
|
176 'gz' => 'gz', |
|
177 'maven-metadata' => 'maven-metadata', |
|
178 'maven' => 'maven-metadata', |
|
179 'war' => 'war', |
|
180 'list' => 'list', |
|
181 } |
|
182 |
|
183 $real_always_deploy = any2bool($always_deploy) ? { |
|
184 false => 'no', |
|
185 true => 'yes', |
|
186 } |
|
187 |
|
188 $bool_run_checks = any2bool($run_checks) |
|
189 $bool_auto_deploy = any2bool($auto_deploy) |
|
190 |
|
191 $source_filename = url_parse($source,'filename') |
|
192 |
|
193 # Create Project |
|
194 puppi::project { $name: enable => $enable } |
|
195 |
|
196 |
|
197 ### INIT SEQUENCE |
|
198 if ($init_source != '') { |
|
199 puppi::initialize { "${name}-Deploy_Files": |
|
200 priority => '40' , |
|
201 command => 'get_file.sh' , |
|
202 arguments => "-s ${init_source} -d ${deploy_root}" , |
|
203 user => $user , |
|
204 project => $name , |
|
205 enable => $enable , |
|
206 } |
|
207 } |
|
208 |
|
209 |
|
210 ### DEPLOY SEQUENCE |
|
211 if ($bool_run_checks == true) { |
|
212 puppi::deploy { "${name}-Run_PRE-Checks": |
|
213 priority => '10' , |
|
214 command => 'check_project.sh' , |
|
215 arguments => $name , |
|
216 user => 'root' , |
|
217 project => $name , |
|
218 enable => $enable , |
|
219 } |
|
220 } |
|
221 |
|
222 # Here source file is retrieved |
|
223 puppi::deploy { "${name}-Retrieve_SourceFile": |
|
224 priority => '20' , |
|
225 command => 'get_file.sh' , |
|
226 arguments => "-s ${source} -t ${real_source_type} -a ${real_always_deploy}" , |
|
227 user => 'root' , |
|
228 project => $name , |
|
229 enable => $enable , |
|
230 } |
|
231 |
|
232 $args_magicfix = $magicfix ? { |
|
233 '' => '', |
|
234 default => "-m ${magicfix}" , |
|
235 } |
|
236 |
|
237 if ($real_source_type == 'tarball') { |
|
238 puppi::deploy { "${name}-PreDeploy_Tar": |
|
239 priority => '25' , |
|
240 command => 'predeploy.sh' , |
|
241 arguments => $args_magicfix, |
|
242 user => 'root' , |
|
243 project => $name , |
|
244 enable => $enable , |
|
245 } |
|
246 } |
|
247 |
|
248 if ($real_source_type == 'zip') { |
|
249 |
|
250 puppi::deploy { "${name}-PreDeploy_Zip": |
|
251 priority => '25' , |
|
252 command => 'predeploy.sh' , |
|
253 arguments => $args_magicfix, |
|
254 user => 'root' , |
|
255 project => $name , |
|
256 enable => $enable , |
|
257 } |
|
258 } |
|
259 |
|
260 if ($real_source_type == 'list') { |
|
261 puppi::deploy { "${name}-Extract_File_Metadata": |
|
262 priority => '22' , |
|
263 command => 'get_metadata.sh' , |
|
264 arguments => $args_magicfix, |
|
265 user => 'root' , |
|
266 project => $name , |
|
267 enable => $enable , |
|
268 } |
|
269 |
|
270 $clean_file_list_magicfix = $magicfix ? { |
|
271 '' => '', |
|
272 default => $magicfix, |
|
273 } |
|
274 |
|
275 puppi::deploy { "${name}-Clean_File_List": |
|
276 priority => '24' , |
|
277 command => 'clean_filelist.sh' , |
|
278 arguments => $clean_file_list_magicfix, |
|
279 user => 'root' , |
|
280 project => $name , |
|
281 enable => $enable , |
|
282 } |
|
283 puppi::deploy { "${name}-Retrieve_Files": |
|
284 priority => '25' , |
|
285 command => 'get_filesfromlist.sh' , |
|
286 arguments => $source , |
|
287 user => 'root' , |
|
288 project => $name , |
|
289 enable => $enable , |
|
290 } |
|
291 } |
|
292 |
|
293 if ($backup == 'full') or ($backup == 'diff') { |
|
294 puppi::deploy { "${name}-Backup_existing_Files": |
|
295 priority => '30' , |
|
296 command => 'archive.sh' , |
|
297 arguments => "-b ${deploy_root} -m ${backup} -o '${backup_rsync_options}' -n ${backup_retention}" , |
|
298 user => 'root' , |
|
299 project => $name , |
|
300 enable => $enable , |
|
301 } |
|
302 } |
|
303 |
|
304 if ($firewall_src_ip != '') { |
|
305 puppi::deploy { "${name}-Load_Balancer_Block": |
|
306 priority => '34' , |
|
307 command => 'firewall.sh' , |
|
308 arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" , |
|
309 user => 'root', |
|
310 project => $name , |
|
311 enable => $enable , |
|
312 } |
|
313 } |
|
314 |
|
315 if ($real_source_type == 'war') { |
|
316 puppi::deploy { "${name}-Remove_existing_WAR": |
|
317 priority => '35' , |
|
318 command => 'delete.sh' , |
|
319 arguments => "${deploy_root}/${source_filename}" , |
|
320 user => 'root' , |
|
321 project => $name , |
|
322 enable => $enable , |
|
323 } |
|
324 puppi::deploy { "${name}-Check_undeploy": |
|
325 priority => '36' , |
|
326 command => 'checkwardir.sh' , |
|
327 arguments => "-a ${deploy_root}/${source_filename}" , |
|
328 user => $user , |
|
329 project => $name , |
|
330 enable => $enable , |
|
331 } |
|
332 } |
|
333 |
|
334 if ($disable_services != '') { |
|
335 puppi::deploy { "${name}-Disable_extra_services": |
|
336 priority => '37' , |
|
337 command => 'service.sh' , |
|
338 arguments => "stop ${disable_services}" , |
|
339 user => 'root', |
|
340 project => $name , |
|
341 enable => $enable , |
|
342 } |
|
343 } |
|
344 |
|
345 if ($predeploy_customcommand != '') { |
|
346 puppi::deploy { "${name}-Run_Custom_PreDeploy_Script": |
|
347 priority => $predeploy_priority , |
|
348 command => 'execute.sh' , |
|
349 arguments => $predeploy_customcommand , |
|
350 user => $predeploy_real_user , |
|
351 project => $name , |
|
352 enable => $enable , |
|
353 } |
|
354 } |
|
355 |
|
356 # Here is done the deploy on $deploy_root |
|
357 puppi::deploy { "${name}-Deploy": |
|
358 priority => '40' , |
|
359 command => 'deploy.sh' , |
|
360 arguments => $deploy_root , |
|
361 user => $user , |
|
362 project => $name , |
|
363 enable => $enable , |
|
364 } |
|
365 |
|
366 if ($postdeploy_customcommand != '') { |
|
367 puppi::deploy { "${name}-Run_Custom_PostDeploy_Script": |
|
368 priority => $postdeploy_priority , |
|
369 command => 'execute.sh' , |
|
370 arguments => $postdeploy_customcommand , |
|
371 user => $postdeploy_real_user , |
|
372 project => $name , |
|
373 enable => $enable , |
|
374 } |
|
375 } |
|
376 |
|
377 if ($disable_services != '') { |
|
378 puppi::deploy { "${name}-Enable_extra_services": |
|
379 priority => '44' , |
|
380 command => 'service.sh' , |
|
381 arguments => "start ${disable_services}" , |
|
382 user => 'root', |
|
383 project => $name , |
|
384 enable => $enable , |
|
385 } |
|
386 } |
|
387 |
|
388 if ($real_source_type == 'war') { |
|
389 puppi::deploy { "${name}-Check_deploy": |
|
390 priority => '45' , |
|
391 command => 'checkwardir.sh' , |
|
392 arguments => "-p ${deploy_root}/${source_filename}" , |
|
393 user => $user , |
|
394 project => $name , |
|
395 enable => $enable , |
|
396 } |
|
397 } |
|
398 |
|
399 if ($firewall_src_ip != '') { |
|
400 puppi::deploy { "${name}-Load_Balancer_Unblock": |
|
401 priority => '46' , |
|
402 command => 'firewall.sh' , |
|
403 arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" , |
|
404 user => 'root', |
|
405 project => $name , |
|
406 enable => $enable , |
|
407 } |
|
408 } |
|
409 |
|
410 if ($bool_run_checks == true) { |
|
411 puppi::deploy { "${name}-Run_POST-Checks": |
|
412 priority => '80' , |
|
413 command => 'check_project.sh' , |
|
414 arguments => $name , |
|
415 user => 'root' , |
|
416 project => $name , |
|
417 enable => $enable , |
|
418 } |
|
419 } |
|
420 |
|
421 |
|
422 ### ROLLBACK PROCEDURE |
|
423 |
|
424 if ($firewall_src_ip != '') { |
|
425 puppi::rollback { "${name}-Load_Balancer_Block": |
|
426 priority => '34' , |
|
427 command => 'firewall.sh' , |
|
428 arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" , |
|
429 user => 'root', |
|
430 project => $name , |
|
431 enable => $enable , |
|
432 } |
|
433 } |
|
434 |
|
435 if ($real_source_type == 'war') { |
|
436 puppi::rollback { "${name}-Remove_existing_WAR": |
|
437 priority => '35' , |
|
438 command => 'delete.sh' , |
|
439 arguments => "${deploy_root}/${source_filename}" , |
|
440 user => 'root' , |
|
441 project => $name , |
|
442 enable => $enable , |
|
443 } |
|
444 puppi::rollback { "${name}-Check_undeploy": |
|
445 priority => '36' , |
|
446 command => 'checkwardir.sh' , |
|
447 arguments => "-a ${deploy_root}/${source_filename}" , |
|
448 user => $user , |
|
449 project => $name , |
|
450 enable => $enable , |
|
451 } |
|
452 } |
|
453 |
|
454 if ($disable_services != '') { |
|
455 puppi::rollback { "${name}-Disable_extra_services": |
|
456 priority => '37' , |
|
457 command => 'service.sh' , |
|
458 arguments => "stop ${disable_services}" , |
|
459 user => 'root', |
|
460 project => $name , |
|
461 enable => $enable , |
|
462 } |
|
463 } |
|
464 |
|
465 if ($predeploy_customcommand != '') { |
|
466 puppi::rollback { "${name}-Run_Custom_PreDeploy_Script": |
|
467 priority => $predeploy_priority , |
|
468 command => 'execute.sh' , |
|
469 arguments => $predeploy_customcommand , |
|
470 user => $predeploy_real_user , |
|
471 project => $name , |
|
472 enable => $enable , |
|
473 } |
|
474 } |
|
475 |
|
476 if ($backup == 'full') or ($backup == 'diff') { |
|
477 puppi::rollback { "${name}-Recover_Files_To_Deploy": |
|
478 priority => '40' , |
|
479 command => 'archive.sh' , |
|
480 arguments => "-r ${deploy_root} -m ${backup} -o '${backup_rsync_options}'" , |
|
481 user => $user , |
|
482 project => $name , |
|
483 enable => $enable , |
|
484 } |
|
485 } |
|
486 |
|
487 if ($postdeploy_customcommand != '') { |
|
488 puppi::rollback { "${name}-Run_Custom_PostDeploy_Script": |
|
489 priority => $postdeploy_priority , |
|
490 command => 'execute.sh' , |
|
491 arguments => $postdeploy_customcommand , |
|
492 user => $postdeploy_real_user , |
|
493 project => $name , |
|
494 enable => $enable , |
|
495 } |
|
496 } |
|
497 |
|
498 if ($disable_services != '') { |
|
499 puppi::rollback { "${name}-Enable_extra_services": |
|
500 priority => '44' , |
|
501 command => 'service.sh' , |
|
502 arguments => "start ${disable_services}" , |
|
503 user => 'root', |
|
504 project => $name , |
|
505 enable => $enable , |
|
506 } |
|
507 } |
|
508 |
|
509 if ($real_source_type == 'war') { |
|
510 puppi::rollback { "${name}-Check_deploy": |
|
511 priority => '45' , |
|
512 command => 'checkwardir.sh' , |
|
513 arguments => "-p ${deploy_root}/${source_filename}" , |
|
514 user => $user , |
|
515 project => $name , |
|
516 enable => $enable , |
|
517 } |
|
518 } |
|
519 |
|
520 if ($firewall_src_ip != '') { |
|
521 puppi::rollback { "${name}-Load_Balancer_Unblock": |
|
522 priority => '46' , |
|
523 command => 'firewall.sh' , |
|
524 arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" , |
|
525 user => 'root', |
|
526 project => $name , |
|
527 enable => $enable , |
|
528 } |
|
529 } |
|
530 |
|
531 if ($bool_run_checks == true) { |
|
532 puppi::rollback { "${name}-Run_POST-Checks": |
|
533 priority => '80' , |
|
534 command => 'check_project.sh' , |
|
535 arguments => $name , |
|
536 user => 'root' , |
|
537 project => $name , |
|
538 enable => $enable , |
|
539 } |
|
540 } |
|
541 |
|
542 |
|
543 ### REPORTING |
|
544 |
|
545 if ($report_email != '') { |
|
546 puppi::report { "${name}-Mail_Notification": |
|
547 priority => '20' , |
|
548 command => 'report_mail.sh' , |
|
549 arguments => $report_email , |
|
550 user => 'root', |
|
551 project => $name , |
|
552 enable => $enable , |
|
553 } |
|
554 } |
|
555 |
|
556 ### AUTO DEPLOY DURING PUPPET RUN |
|
557 if ($bool_auto_deploy == true) { |
|
558 puppi::run { $name: } |
|
559 } |
|
560 |
|
561 } |