|
1 # = Define: puppi::runscript |
|
2 # |
|
3 # This define creates, executes and optionally crontabs a |
|
4 # simple whose content is directly managed by arguments. |
|
5 # The script content is provided either with $source or $content arguments. |
|
6 # It's placed in: |
|
7 # $destination_path , if provided, or in /usr/local/sbin/${name} |
|
8 # |
|
9 # Cron execution times are defined by the $cron argument (Default empty). |
|
10 # Automatic execution of the script via Puppet is managed by the $autorun |
|
11 # parameter (default: true). |
|
12 # Conditional execution of the script at subsequent puppet runs is |
|
13 # defined by the $refreshonly, $creates, $unless $onlyif parameters |
|
14 # that map the omonimous exec type arguments. |
|
15 # |
|
16 # == Parameters: |
|
17 # |
|
18 # [*source*] |
|
19 # String. Optional. Default: undef. Alternative to content. |
|
20 # Source of the script file to provide for execuution. |
|
21 # Sample: source => 'puppet:///modules/site/scripts/my_script', |
|
22 # |
|
23 # [*content*] |
|
24 # String. Optional. Default: undef. Alternative to source. |
|
25 # Content of the script file to provide for execuution. |
|
26 # This parameter is alternative to source. |
|
27 # Sample: content => 'template(site/scripts/my_script.erb'), |
|
28 # |
|
29 # [*destination_path*] |
|
30 # String. Optional. Default: '' |
|
31 # Path of the provided script. If not provided the script in saved in |
|
32 # /usr/local/sbin/${name} |
|
33 # |
|
34 # [*parameters*] |
|
35 # String. Optional. Default: '' |
|
36 # Optional parameters to pass to the script when executing it. |
|
37 # |
|
38 # [*autorun*] |
|
39 # Boolean. Default: true. |
|
40 # Define if to automatically execute the script when Puppet runs. |
|
41 # |
|
42 # [*refreshonly*] |
|
43 # Boolen. Optional. Default: true |
|
44 # Defines the logic of execution of the script when Puppet runs. |
|
45 # Maps to the omonymous Exec type argument. |
|
46 # |
|
47 # [*creates*] |
|
48 # String. Optional. Default: undef |
|
49 # Defines the logic of execution of the script when Puppet runs. |
|
50 # Maps to the omonymous Exec type argument. |
|
51 # |
|
52 # [*onlyif*] |
|
53 # String. Optional. Default: undef |
|
54 # Defines the logic of execution of the script when Puppet runs. |
|
55 # Maps to the omonymous Exec type argument. |
|
56 # |
|
57 # [*unless*] |
|
58 # String. Optional. Default: undef |
|
59 # Defines the logic of execution of the script when Puppet runs. |
|
60 # Maps to the omonymous Exec type argument. |
|
61 # |
|
62 # [*basedir*] |
|
63 # String. Optional. Default: /usr/local/sbin |
|
64 # Directory where the runscript scripts are created when destination_path |
|
65 # is empty. |
|
66 # |
|
67 # [*cron*] |
|
68 # String. Optional. Default: '' |
|
69 # Optional cron schedule to crontab the execution of the |
|
70 # script. Format must be in standard cron style. |
|
71 # Example: '0 4 * * *' . |
|
72 # By default no cron is scheduled. |
|
73 # |
|
74 # [*cron_user*] |
|
75 # String. Optional. Default: 'root' |
|
76 # When cron is enabled the user that executes the cron job. |
|
77 # |
|
78 # [*owner*] |
|
79 # Owner of the created script. Default: root. |
|
80 # |
|
81 # [*group*] |
|
82 # Group of the created script. Default: root. |
|
83 # |
|
84 # [*mode*] |
|
85 # Mode of the created script. Default: '7550'. |
|
86 # NOTE: Keep the execution flag! |
|
87 # |
|
88 # [*ensure*] |
|
89 # Define if the runscript script and eventual cron job |
|
90 # must be present or absent. Default: present. |
|
91 # |
|
92 # == Examples |
|
93 # |
|
94 # - Minimal setup |
|
95 # puppi::runscript { 'my_script': |
|
96 # source => 'puppet:///modules/site/scripts/my_script.sh', |
|
97 # destination_path => '/usr/local/bin/my_script.sh', |
|
98 # } |
|
99 # |
|
100 define puppi::runscript ( |
|
101 $source = undef, |
|
102 $content = undef, |
|
103 $destination_path = '', |
|
104 $parameters = '', |
|
105 $autorun = true, |
|
106 $refreshonly = true, |
|
107 $creates = undef, |
|
108 $onlyif = undef, |
|
109 $unless = undef, |
|
110 $basedir = '/usr/local/sbin', |
|
111 $cron = '', |
|
112 $cron_user = 'root', |
|
113 $owner = 'root', |
|
114 $group = 'root', |
|
115 $mode = '0755', |
|
116 $ensure = 'present' ) { |
|
117 |
|
118 $real_command = $destination_path ? { |
|
119 '' => "${basedir}/${name}", |
|
120 default => $destination_path, |
|
121 } |
|
122 |
|
123 file { "runscript_${name}": |
|
124 ensure => $ensure, |
|
125 path => $real_command, |
|
126 mode => $mode, |
|
127 owner => $owner, |
|
128 group => $group, |
|
129 content => $content, |
|
130 source => $source, |
|
131 } |
|
132 |
|
133 if $autorun == true { |
|
134 exec { "runscript_${name}": |
|
135 command => $real_command, |
|
136 refreshonly => $refreshonly, |
|
137 creates => $creates, |
|
138 onlyif => $onlyif, |
|
139 unless => $unless, |
|
140 subscribe => File["runscript_${name}"], |
|
141 } |
|
142 } |
|
143 |
|
144 if $cron != '' { |
|
145 file { "runscript_cron_${name}": |
|
146 ensure => $ensure, |
|
147 path => "/etc/cron.d/runscript_${name}", |
|
148 mode => '0644', |
|
149 owner => 'root', |
|
150 group => 'root', |
|
151 content => "${cron} ${cron_user} ${real_command} ${parameters}\n", |
|
152 } |
|
153 } |
|
154 } |