28
|
1 |
# = Define puppi::project::mysql |
|
2 |
# |
|
3 |
# This is a shortcut define to build a puppi project for the |
|
4 |
# management of mysql queries, contained in the .sql file defined |
|
5 |
# as $source |
|
6 |
# |
|
7 |
# == Variables: |
|
8 |
# |
|
9 |
# [*source*] |
|
10 |
# The full URL of the main sql file to retrieve. |
|
11 |
# Format should be in URI standard (http:// file:// ssh:// rsync://). |
|
12 |
# |
|
13 |
# [*mysql_database*] |
|
14 |
# (Required) - The Mysql database to work on |
|
15 |
# Default: root |
|
16 |
# |
|
17 |
# [*mysql_user*] |
|
18 |
# (Optional) - The Mysql user to be used to run the queries |
|
19 |
# Default: root |
|
20 |
# |
|
21 |
# [*mysql_password*] |
|
22 |
# (Optional) - The password to use for the specified mysql user |
|
23 |
# Default: '' (blank) |
|
24 |
# |
|
25 |
# [*mysql_host*] |
|
26 |
# (Optional) - The Mysql server. Use ip or hostname |
|
27 |
# Default: localhost |
|
28 |
# |
|
29 |
# [*init_source*] |
|
30 |
# (Optional) - The full URL to be used to retrieve a sql file to |
|
31 |
# process only for the first time, |
|
32 |
# |
|
33 |
# [*predeploy_customcommand*] |
|
34 |
# (Optional) - Full path with arguments of an eventual custom command to |
|
35 |
# execute before the deploy. The command is executed as $predeploy_user. |
|
36 |
# |
|
37 |
# [*predeploy_user*] |
|
38 |
# (Optional) - The user to be used to execute the $predeploy_customcommand. |
|
39 |
# By default is the same of $user. |
|
40 |
# |
|
41 |
# [*predeploy_priority*] |
|
42 |
# (Optional) - The priority (execution sequence number) that defines when, |
|
43 |
# during the deploy procedure, the $predeploy_customcommand is executed |
|
44 |
# Default: 39 (immediately before the copy of files on the deploy root). |
|
45 |
# |
|
46 |
# [*postdeploy_customcommand*] |
|
47 |
# (Optional) - Full path with arguments of an eventual custom command to |
|
48 |
# execute after the deploy. The command is executed as $postdeploy_user. |
|
49 |
# |
|
50 |
# [*postdeploy_user*] |
|
51 |
# (Optional) - The user to be used to execute the $postdeploy_customcommand. |
|
52 |
# By default is the same of $user. |
|
53 |
# |
|
54 |
# [*postdeploy_priority*] |
|
55 |
# (Optional) - The priority (execution sequence number) that defines when, |
|
56 |
# during the deploy procedure, the $postdeploy_customcommand is executed |
|
57 |
# Default: 41 (immediately after the copy of files on the deploy root). |
|
58 |
# |
|
59 |
# [*disable_services*] |
|
60 |
# (Optional) - The names (space separated) of the services you might want to |
|
61 |
# stop during deploy. By default is blank. Example: "apache puppet monit". |
|
62 |
# |
|
63 |
# [*firewall_src_ip*] |
|
64 |
# (Optional) - The IP address of a loadbalancer you might want to block out |
|
65 |
# during a deploy. |
|
66 |
# |
|
67 |
# [*firewall_dst_port*] |
|
68 |
# (Optional) - The local port to block from the loadbalancer during deploy |
|
69 |
# (Default all). |
|
70 |
# |
|
71 |
# [*firewall_delay*] |
|
72 |
# (Optional) - A delay time in seconds to wait after the block of |
|
73 |
# $firewall_src_ip. Should be at least as long as the loadbalancer check |
|
74 |
# interval for the services stopped during deploy (Default: 1). |
|
75 |
# |
|
76 |
# [*report_email*] |
|
77 |
# (Optional) - The (space separated) email(s) to notify of deploy/rollback |
|
78 |
# operations. If none is specified, no email is sent. |
|
79 |
# |
|
80 |
# [*backup*] |
|
81 |
# (Optional) - If and how backups of existing databases are made. |
|
82 |
# Default: true |
|
83 |
# |
|
84 |
# [*backup_retention*] |
|
85 |
# (Optional) - Number of backup archives to keep. (Default 5). |
|
86 |
# Lower the default value if your backups are too large and may fill up the |
|
87 |
# filesystem. |
|
88 |
# |
|
89 |
# [*run_checks*] |
|
90 |
# (Optional) - If you want to run local puppi checks before and after the |
|
91 |
# deploy procedure. Default: "true". |
|
92 |
# |
|
93 |
# [*always_deploy*] |
|
94 |
# (Optional) - If you always deploy what has been downloaded. Default="yes", |
|
95 |
# if set to "no" a checksum is made between the files previously downloaded |
|
96 |
# and the new files. If they are the same the deploy is not done. |
|
97 |
# |
|
98 |
# [*auto_deploy*] |
|
99 |
# (Optional) - If you want to automatically run this puppi deploy when |
|
100 |
# Puppet runs. Default: 'false' |
|
101 |
# |
|
102 |
define puppi::project::mysql ( |
|
103 |
$source, |
|
104 |
$mysql_database, |
|
105 |
$mysql_user = 'root', |
|
106 |
$mysql_host = 'localhost', |
|
107 |
$mysql_password = '', |
|
108 |
$init_source = '', |
|
109 |
$predeploy_customcommand = '', |
|
110 |
$predeploy_user = '', |
|
111 |
$predeploy_priority = '39', |
|
112 |
$postdeploy_customcommand = '', |
|
113 |
$postdeploy_user = '', |
|
114 |
$postdeploy_priority = '41', |
|
115 |
$disable_services = '', |
|
116 |
$firewall_src_ip = '', |
|
117 |
$firewall_dst_port = '0', |
|
118 |
$firewall_delay = '1', |
|
119 |
$report_email = '', |
|
120 |
$backup = true, |
|
121 |
$backup_retention = '5', |
|
122 |
$run_checks = true, |
|
123 |
$always_deploy = true, |
|
124 |
$auto_deploy = false, |
|
125 |
$enable = true ) { |
|
126 |
|
|
127 |
require puppi |
|
128 |
require puppi::params |
|
129 |
|
|
130 |
# Set default values |
|
131 |
$predeploy_real_user = $predeploy_user ? { |
|
132 |
'' => 'root', |
|
133 |
default => $predeploy_user, |
|
134 |
} |
|
135 |
|
|
136 |
$postdeploy_real_user = $postdeploy_user ? { |
|
137 |
'' => 'root', |
|
138 |
default => $postdeploy_user, |
|
139 |
} |
|
140 |
|
|
141 |
$init_real_source = $init_source ? { |
|
142 |
'' => $source, |
|
143 |
default => $init_source, |
|
144 |
} |
|
145 |
|
|
146 |
$real_always_deploy = any2bool($always_deploy) ? { |
|
147 |
false => 'no', |
|
148 |
true => 'yes', |
|
149 |
} |
|
150 |
|
|
151 |
$bool_run_checks = any2bool($run_checks) |
|
152 |
$bool_backup = any2bool($backup) |
|
153 |
$real_source_type = 'mysql' |
|
154 |
$bool_auto_deploy = any2bool($auto_deploy) |
|
155 |
|
|
156 |
$source_filename = url_parse($source,'filename') |
|
157 |
|
|
158 |
### CREATE PROJECT |
|
159 |
puppi::project { $name: |
|
160 |
enable => $enable , |
|
161 |
} |
|
162 |
|
|
163 |
|
|
164 |
### INIT SEQUENCE |
|
165 |
if ($init_source != '') { |
|
166 |
puppi::initialize { "${name}-Deploy_Files": |
|
167 |
priority => '40' , |
|
168 |
command => 'get_file.sh' , |
|
169 |
arguments => "-s ${init_source} -t ${real_source_type}" , |
|
170 |
user => root , |
|
171 |
project => $name , |
|
172 |
enable => $enable , |
|
173 |
} |
|
174 |
puppi::initialize { "${name}-Run_SQL": |
|
175 |
priority => '42' , |
|
176 |
command => 'database.sh' , |
|
177 |
arguments => "-t mysql -a run -u ${mysql_user} -p '${mysql_password}' -d ${mysql_database} -h ${mysql_host}" , |
|
178 |
user => 'root' , |
|
179 |
project => $name , |
|
180 |
enable => $enable , |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
|
|
185 |
### DEPLOY SEQUENCE |
|
186 |
if ($bool_run_checks == true) { |
|
187 |
puppi::deploy { "${name}-Run_PRE-Checks": |
|
188 |
priority => '10' , |
|
189 |
command => 'check_project.sh' , |
|
190 |
arguments => $name , |
|
191 |
user => 'root' , |
|
192 |
project => $name , |
|
193 |
enable => $enable , |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
puppi::deploy { "${name}-Retrieve_SQLFile": |
|
198 |
priority => '20' , |
|
199 |
command => 'get_file.sh' , |
|
200 |
arguments => "-s ${source} -t ${real_source_type} -a ${real_always_deploy}" , |
|
201 |
user => 'root' , |
|
202 |
project => $name , |
|
203 |
enable => $enable , |
|
204 |
} |
|
205 |
|
|
206 |
if ($firewall_src_ip != '') { |
|
207 |
puppi::deploy { "${name}-Load_Balancer_Block": |
|
208 |
priority => '25' , |
|
209 |
command => 'firewall.sh' , |
|
210 |
arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" , |
|
211 |
user => 'root', |
|
212 |
project => $name , |
|
213 |
enable => $enable , |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 |
if ($bool_backup == true) { |
|
218 |
puppi::deploy { "${name}-Backup_Database": |
|
219 |
priority => '30' , |
|
220 |
command => 'database.sh' , |
|
221 |
arguments => "-t mysql -a dump -u ${mysql_user} -p '${mysql_password}' -d ${mysql_database} -h ${mysql_host}" , |
|
222 |
user => 'root' , |
|
223 |
project => $name , |
|
224 |
enable => $enable , |
|
225 |
} |
|
226 |
} |
|
227 |
|
|
228 |
if ($disable_services != '') { |
|
229 |
puppi::deploy { "${name}-Disable_extra_services": |
|
230 |
priority => '36' , |
|
231 |
command => 'service.sh' , |
|
232 |
arguments => "stop ${disable_services}" , |
|
233 |
user => 'root', |
|
234 |
project => $name , |
|
235 |
enable => $enable , |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
239 |
if ($predeploy_customcommand != '') { |
|
240 |
puppi::deploy { "${name}-Run_Custom_PreDeploy_Script": |
|
241 |
priority => $predeploy_priority , |
|
242 |
command => 'execute.sh' , |
|
243 |
arguments => $predeploy_customcommand , |
|
244 |
user => $predeploy_real_user , |
|
245 |
project => $name , |
|
246 |
enable => $enable , |
|
247 |
} |
|
248 |
} |
|
249 |
|
|
250 |
# Here is done the db change |
|
251 |
puppi::deploy { "${name}-Run_SQL": |
|
252 |
priority => '40' , |
|
253 |
command => 'database.sh' , |
|
254 |
arguments => "-t mysql -a run -u ${mysql_user} -p '${mysql_password}' -d ${mysql_database} -h ${mysql_host}" , |
|
255 |
user => 'root' , |
|
256 |
project => $name , |
|
257 |
enable => $enable , |
|
258 |
} |
|
259 |
|
|
260 |
if ($postdeploy_customcommand != '') { |
|
261 |
puppi::deploy { "${name}-Run_Custom_PostDeploy_Script": |
|
262 |
priority => $postdeploy_priority , |
|
263 |
command => 'execute.sh' , |
|
264 |
arguments => $postdeploy_customcommand , |
|
265 |
user => $postdeploy_real_user , |
|
266 |
project => $name , |
|
267 |
enable => $enable , |
|
268 |
} |
|
269 |
} |
|
270 |
|
|
271 |
if ($disable_services != '') { |
|
272 |
puppi::deploy { "${name}-Enable_extra_services": |
|
273 |
priority => '44' , |
|
274 |
command => 'service.sh' , |
|
275 |
arguments => "start ${disable_services}" , |
|
276 |
user => 'root', |
|
277 |
project => $name , |
|
278 |
enable => $enable , |
|
279 |
} |
|
280 |
} |
|
281 |
|
|
282 |
if ($firewall_src_ip != '') { |
|
283 |
puppi::deploy { "${name}-Load_Balancer_Unblock": |
|
284 |
priority => '46' , |
|
285 |
command => 'firewall.sh' , |
|
286 |
arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" , |
|
287 |
user => 'root', |
|
288 |
project => $name , |
|
289 |
enable => $enable , |
|
290 |
} |
|
291 |
} |
|
292 |
|
|
293 |
if ($bool_run_checks == true) { |
|
294 |
puppi::deploy { "${name}-Run_POST-Checks": |
|
295 |
priority => '80' , |
|
296 |
command => 'check_project.sh' , |
|
297 |
arguments => $name , |
|
298 |
user => 'root' , |
|
299 |
project => $name , |
|
300 |
enable => $enable , |
|
301 |
} |
|
302 |
} |
|
303 |
|
|
304 |
|
|
305 |
### ROLLBACK PROCEDURE |
|
306 |
|
|
307 |
if ($firewall_src_ip != '') { |
|
308 |
puppi::rollback { "${name}-Load_Balancer_Block": |
|
309 |
priority => '25' , |
|
310 |
command => 'firewall.sh' , |
|
311 |
arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" , |
|
312 |
user => 'root', |
|
313 |
project => $name , |
|
314 |
enable => $enable , |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 |
if ($bool_backup == true) { |
|
319 |
puppi::rollback { "${name}-Backup_Database_PreRollback": |
|
320 |
priority => '30' , |
|
321 |
command => 'database.sh' , |
|
322 |
arguments => "-t mysql -a dump -u ${mysql_user} -p '${mysql_password}' -d ${mysql_database} -h ${mysql_host}" , |
|
323 |
user => 'root' , |
|
324 |
project => $name , |
|
325 |
enable => $enable , |
|
326 |
} |
|
327 |
} |
|
328 |
|
|
329 |
if ($disable_services != '') { |
|
330 |
puppi::rollback { "${name}-Disable_extra_services": |
|
331 |
priority => '37' , |
|
332 |
command => 'service.sh' , |
|
333 |
arguments => "stop ${disable_services}" , |
|
334 |
user => 'root', |
|
335 |
project => $name , |
|
336 |
enable => $enable , |
|
337 |
} |
|
338 |
} |
|
339 |
|
|
340 |
if ($predeploy_customcommand != '') { |
|
341 |
puppi::rollback { "${name}-Run_Custom_PreDeploy_Script": |
|
342 |
priority => $predeploy_priority , |
|
343 |
command => 'execute.sh' , |
|
344 |
arguments => $predeploy_customcommand , |
|
345 |
user => $predeploy_real_user , |
|
346 |
project => $name , |
|
347 |
enable => $enable , |
|
348 |
} |
|
349 |
} |
|
350 |
|
|
351 |
if ($bool_backup == true) { |
|
352 |
puppi::rollback { "${name}-Recover_Database": |
|
353 |
priority => '40' , |
|
354 |
command => 'database.sh' , |
|
355 |
arguments => "-t mysql -a restore -u ${mysql_user} -p '${mysql_password}' -d ${mysql_database} -h ${mysql_host}" , |
|
356 |
user => 'root' , |
|
357 |
project => $name , |
|
358 |
enable => $enable , |
|
359 |
} |
|
360 |
} |
|
361 |
|
|
362 |
if ($postdeploy_customcommand != '') { |
|
363 |
puppi::rollback { "${name}-Run_Custom_PostDeploy_Script": |
|
364 |
priority => $postdeploy_priority , |
|
365 |
command => 'execute.sh' , |
|
366 |
arguments => $postdeploy_customcommand , |
|
367 |
user => $postdeploy_real_user , |
|
368 |
project => $name , |
|
369 |
enable => $enable , |
|
370 |
} |
|
371 |
} |
|
372 |
|
|
373 |
if ($disable_services != '') { |
|
374 |
puppi::rollback { "${name}-Enable_extra_services": |
|
375 |
priority => '44' , |
|
376 |
command => 'service.sh' , |
|
377 |
arguments => "start ${disable_services}" , |
|
378 |
user => 'root', |
|
379 |
project => $name , |
|
380 |
enable => $enable , |
|
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
if ($firewall_src_ip != '') { |
|
385 |
puppi::rollback { "${name}-Load_Balancer_Unblock": |
|
386 |
priority => '46' , |
|
387 |
command => 'firewall.sh' , |
|
388 |
arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" , |
|
389 |
user => 'root', |
|
390 |
project => $name , |
|
391 |
enable => $enable , |
|
392 |
} |
|
393 |
} |
|
394 |
|
|
395 |
if ($bool_run_checks == true) { |
|
396 |
puppi::rollback { "${name}-Run_POST-Checks": |
|
397 |
priority => '80' , |
|
398 |
command => 'check_project.sh' , |
|
399 |
arguments => $name , |
|
400 |
user => 'root' , |
|
401 |
project => $name , |
|
402 |
enable => $enable , |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
406 |
|
|
407 |
### REPORTING |
|
408 |
|
|
409 |
if ($report_email != '') { |
|
410 |
puppi::report { "${name}-Mail_Notification": |
|
411 |
priority => '20' , |
|
412 |
command => 'report_mail.sh' , |
|
413 |
arguments => $report_email , |
|
414 |
user => 'root', |
|
415 |
project => $name , |
|
416 |
enable => $enable , |
|
417 |
} |
|
418 |
} |
|
419 |
|
|
420 |
### AUTO DEPLOY DURING PUPPET RUN |
|
421 |
if ($bool_auto_deploy == true) { |
|
422 |
puppi::run { $name: } |
|
423 |
} |
|
424 |
|
|
425 |
} |