28
|
1 |
#!/bin/bash
|
|
2 |
configfile="<%= scope.lookupvar('puppi::params::basedir') %>/puppi.conf"
|
|
3 |
|
|
4 |
# This is the actual command used to run the different scripts
|
|
5 |
# Use cat for debugging noop purposes
|
|
6 |
# runcommand="cat"
|
|
7 |
runcommand=""
|
|
8 |
|
|
9 |
# Define defaults
|
|
10 |
verbosity="max"
|
|
11 |
show="yes"
|
|
12 |
|
|
13 |
# Define action tag
|
|
14 |
export tag=$(date +%Y%m%d-%H%M%S)
|
|
15 |
|
|
16 |
counter=0
|
|
17 |
|
|
18 |
# Load general configurations
|
|
19 |
if [ ! -f $configfile ] ; then
|
|
20 |
echo "Config file: $configfile not found"
|
|
21 |
exit 1
|
|
22 |
else
|
|
23 |
. $configfile
|
|
24 |
. $scriptsdir/functions
|
|
25 |
fi
|
|
26 |
|
|
27 |
# Main functions
|
|
28 |
check_host () {
|
|
29 |
for command in $(ls -1 $checksdir) ; do
|
|
30 |
title="$HOSTNAME check: $command"
|
|
31 |
code=$(cat "$checksdir/$command")
|
|
32 |
ask_interactive
|
|
33 |
output=$($runcommand "$checksdir/$command" 2>&1)
|
|
34 |
handle_result
|
|
35 |
done
|
|
36 |
}
|
|
37 |
|
|
38 |
check () {
|
|
39 |
for command in $(ls -1 $projectsdir/$project/check) ; do
|
|
40 |
title="$HOSTNAME check: $command"
|
|
41 |
code=$(cat "$projectsdir/$project/check/$command")
|
|
42 |
ask_interactive
|
|
43 |
output=$($runcommand "$projectsdir/$project/check/$command" 2>&1)
|
|
44 |
handle_result
|
|
45 |
done
|
|
46 |
|
|
47 |
check_host
|
|
48 |
# show_report
|
|
49 |
}
|
|
50 |
|
|
51 |
log () {
|
|
52 |
tailcommand="tail"
|
|
53 |
which colortail >/dev/null 2>&1 && tailcommand="colortail"
|
|
54 |
|
|
55 |
if [ "x$project" != "xdefault" ] ; then
|
|
56 |
if [ $logsdir/$project ] ; then
|
|
57 |
alllog="$alllog $(cat $logsdir/$project)"
|
|
58 |
else
|
|
59 |
echo "WARNING: $logsdir/$project does not exist!"
|
|
60 |
exit 1
|
|
61 |
fi
|
|
62 |
else
|
|
63 |
if [ "$interactive" = "yes" ] ; then
|
|
64 |
echo "Choose one or more log topics to show. Select the last number (done) to end selection"
|
|
65 |
PS3="Type one number to add a log topic to the show list."
|
|
66 |
all_choices=""
|
|
67 |
select choice in $( ls $logsdir ) done
|
|
68 |
do
|
|
69 |
echo "You selected $choice [$REPLY]"
|
|
70 |
[[ $choice == "done" ]] && break
|
|
71 |
all_choices="$all_choices $choice"
|
|
72 |
echo "Your choices: $all_choices"
|
|
73 |
done
|
|
74 |
for log in $all_choices ; do
|
|
75 |
alllog="$alllog $(cat $logsdir/$log)"
|
|
76 |
done
|
|
77 |
else
|
|
78 |
for log in $(ls $logsdir) ; do
|
|
79 |
alllog="$alllog $(cat $logsdir/$log)"
|
|
80 |
done
|
|
81 |
fi
|
|
82 |
fi
|
|
83 |
|
|
84 |
# So, show something
|
|
85 |
if [ ! -z "$counts" ] ; then
|
|
86 |
if [ ! -z "$greppattern" ] ; then
|
|
87 |
$tailcommand -n $counts $alllog | grep $greppattern
|
|
88 |
else
|
|
89 |
$tailcommand -n $counts $alllog
|
|
90 |
fi
|
|
91 |
else
|
|
92 |
if [ ! -z "$greppattern" ] ; then
|
|
93 |
$tailcommand -f $alllog | grep $greppattern
|
|
94 |
else
|
|
95 |
$tailcommand -f $alllog
|
|
96 |
fi
|
|
97 |
fi
|
|
98 |
}
|
|
99 |
|
|
100 |
info () {
|
|
101 |
if [ "x$project" != "xdefault" ] ; then
|
|
102 |
if [ $infodir/$project ] ; then
|
|
103 |
$infodir/$project
|
|
104 |
else
|
|
105 |
echo "WARNING: $infodir/$project does not exist!"
|
|
106 |
exit 1
|
|
107 |
fi
|
|
108 |
else
|
|
109 |
if [ "$interactive" = "yes" ] ; then
|
|
110 |
echo "Choose one or more topics to show. Select the last number (done) to end selection"
|
|
111 |
PS3="Type one number to add an info topic to the show list."
|
|
112 |
all_choices=""
|
|
113 |
select choice in $( ls $infodir ) done
|
|
114 |
do
|
|
115 |
echo "You selected $choice [$REPLY]"
|
|
116 |
[[ $choice == "done" ]] && break
|
|
117 |
all_choices="$all_choices $choice"
|
|
118 |
echo "Your choices: $all_choices"
|
|
119 |
done
|
|
120 |
for info in $all_choices ; do
|
|
121 |
if [ ! -z "$greppattern" ] ; then
|
|
122 |
$infodir/$info | grep $greppattern
|
|
123 |
else
|
|
124 |
$infodir/$info
|
|
125 |
fi
|
|
126 |
done
|
|
127 |
else
|
|
128 |
for info in $(ls $infodir) ; do
|
|
129 |
if [ ! -z "$greppattern" ] ; then
|
|
130 |
$infodir/$info | grep $greppattern
|
|
131 |
else
|
|
132 |
$infodir/$info
|
|
133 |
fi
|
|
134 |
done
|
|
135 |
fi
|
|
136 |
fi
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
todo () {
|
|
141 |
for todo in $(ls $tododir) ; do
|
|
142 |
$tododir/$todo
|
|
143 |
done
|
|
144 |
}
|
|
145 |
|
|
146 |
|
|
147 |
rollback () {
|
|
148 |
if [ ! -z $rollbackversion ] ; then
|
|
149 |
save_runtime_config "rollbackversion=$rollbackversion" || initerr=1
|
|
150 |
else
|
|
151 |
echo "Choose deploy to rollback:"
|
|
152 |
ls -1 $archivedir/$project
|
|
153 |
read rollbackversion
|
|
154 |
save_runtime_config "rollbackversion=$rollbackversion" || initerr=1
|
|
155 |
fi
|
|
156 |
|
|
157 |
for command in $(ls -1 $projectsdir/$project/rollback) ; do
|
|
158 |
title="$HOSTNAME Rollback: $command"
|
|
159 |
code=$(cat "$projectsdir/$project/rollback/$command")
|
|
160 |
ask_interactive
|
|
161 |
output=$($runcommand "$projectsdir/$project/rollback/$command" 2>&1)
|
|
162 |
handle_result
|
|
163 |
done
|
|
164 |
|
|
165 |
send_reports
|
|
166 |
show_report
|
|
167 |
[ "$result" = "OK" ] && exit 0
|
|
168 |
}
|
|
169 |
|
|
170 |
deploy () {
|
|
171 |
for command in $(ls -1 $projectsdir/$project/deploy) ; do
|
|
172 |
title="$HOSTNAME Deploy: $command"
|
|
173 |
code=$(cat "$projectsdir/$project/deploy/$command")
|
|
174 |
ask_interactive
|
|
175 |
output=$($runcommand "$projectsdir/$project/deploy/$command" 2>&1)
|
|
176 |
handle_result
|
|
177 |
[ "$EXITCRIT" = "1" ] && [ "$force" != "yes" ] && break
|
|
178 |
if [ "$DONTDEPLOY" = "1" ] ; then
|
|
179 |
echo "No need to deploy: source file has not changed"
|
|
180 |
echo "Type 'rm $archivedir/$project/md5sum' and run puppi again to force deployment"
|
|
181 |
exit 0
|
|
182 |
fi
|
|
183 |
done
|
|
184 |
|
|
185 |
send_reports
|
|
186 |
show_report
|
|
187 |
[ "$result" = "OK" ] && exit 0
|
|
188 |
}
|
|
189 |
|
|
190 |
initialize () {
|
|
191 |
for command in $(ls -1 $projectsdir/$project/initialize) ; do
|
|
192 |
title="$HOSTNAME Init: $command"
|
|
193 |
code=$(cat "$projectsdir/$project/initialize/$command")
|
|
194 |
ask_interactive
|
|
195 |
output=$($runcommand "$projectsdir/$project/initialize/$command" 2>&1)
|
|
196 |
handle_result
|
|
197 |
[ "$EXITCRIT" = "1" ] && [ "$force" != "yes" ] && break
|
|
198 |
done
|
|
199 |
|
|
200 |
send_reports
|
|
201 |
show_report
|
|
202 |
[ "$result" = "OK" ] && exit 0
|
|
203 |
}
|
|
204 |
|
|
205 |
configure () {
|
|
206 |
for command in $(ls -1 $projectsdir/$project/configure) ; do
|
|
207 |
title="$HOSTNAME Init: $command"
|
|
208 |
code=$(cat "$projectsdir/$project/configure/$command")
|
|
209 |
ask_interactive
|
|
210 |
output=$($runcommand "$projectsdir/$project/configure/$command" 2>&1)
|
|
211 |
handle_result
|
|
212 |
[ "$EXITCRIT" = "1" ] && [ "$force" != "yes" ] && break
|
|
213 |
done
|
|
214 |
|
|
215 |
send_reports
|
|
216 |
show_report
|
|
217 |
[ "$result" = "OK" ] && exit 0
|
|
218 |
}
|
|
219 |
|
|
220 |
save_summary () {
|
|
221 |
tagend=$(date +%Y%m%d-%H%M%S)
|
|
222 |
|
|
223 |
result="OK"
|
|
224 |
if [ "$EXITWARN" = "1" ] ; then
|
|
225 |
result="WARNING"
|
|
226 |
fi
|
|
227 |
if [ "$EXITCRIT" = "1" ] ; then
|
|
228 |
result="ERROR"
|
|
229 |
fi
|
|
230 |
|
|
231 |
echo "Report for $action on $project" > $logdir/$project/$tag/summary
|
|
232 |
echo "Job start: $tag" >> $logdir/$project/$tag/summary
|
|
233 |
echo "Job end: $tagend" >> $logdir/$project/$tag/summary
|
|
234 |
echo "Job result: $result" >> $logdir/$project/$tag/summary
|
|
235 |
echo "Actions executed:" >> $logdir/$project/$tag/summary
|
|
236 |
cd $logdir/$project/$tag/
|
|
237 |
for message in $(ls -1 $logdir/$project/$tag/ | grep -v summary ) ; do
|
|
238 |
msg_title=$(head -1 $message)
|
|
239 |
msg_code=$(head -2 $message | tail -1)
|
|
240 |
msg_result=$(head -3 $message | tail -1)
|
|
241 |
echo "[$msg_result] $msg_title : $msg_code" >> $logdir/$project/$tag/summary
|
|
242 |
done
|
|
243 |
|
|
244 |
# Copy runtime config in archive
|
|
245 |
cp $workdir/$project/config $logdir/$project/$tag/
|
|
246 |
|
|
247 |
# Write runtime config on Summary
|
|
248 |
echo >> $logdir/$project/$tag/summary
|
|
249 |
echo "RUNTIME CONFIGURATION" >> $logdir/$project/$tag/summary
|
|
250 |
cat $workdir/$project/config | grep -vE "^#|^$" >> $logdir/$project/$tag/summary
|
|
251 |
|
|
252 |
}
|
|
253 |
|
|
254 |
send_reports () {
|
|
255 |
if [[ "x$report" == "xyes" ]] ; then
|
|
256 |
save_summary
|
|
257 |
for command in $(ls -1 $projectsdir/$project/report) ; do
|
|
258 |
title="Reporting: $command"
|
|
259 |
code=$(cat "$projectsdir/$project/report/$command")
|
|
260 |
echo -n $title
|
|
261 |
output=$($runcommand "$projectsdir/$project/report/$command" 2>&1)
|
|
262 |
# handle_result # This breaks the overall exit code when deploy fails
|
|
263 |
done
|
|
264 |
fi
|
|
265 |
}
|
|
266 |
|
|
267 |
show_report () {
|
|
268 |
echo
|
|
269 |
echo "REPORT FOR PUPPI - STATUS $result"
|
|
270 |
echo "Summary of operations is: $logdir/$project/$tag/summary "
|
|
271 |
echo "Details are in: $logdir/$project/$tag/"
|
|
272 |
echo "Temporary workdir has been: $workdir/$project/ (Will be rewritten at the next puppi run)"
|
|
273 |
echo "Runtime config file is: $workdir/$project/config"
|
|
274 |
echo "Files have been archived in: $archivedir/$project/$tag"
|
|
275 |
test "$testmode" = "yes" && echo "This was a TEST RUN! Nothing has been done for real."
|
|
276 |
}
|
|
277 |
|
|
278 |
|
|
279 |
create_runtime_conf () {
|
|
280 |
if [[ ( ! -e $projectsdir/$project ) && ( ! -e $infodir/$project ) && ( ! -e $logsdir/$project ) ]] ; then
|
|
281 |
showhelp
|
|
282 |
exit 1
|
|
283 |
fi
|
|
284 |
|
|
285 |
initerr=0
|
|
286 |
|
|
287 |
# When project is unset we set it to default
|
|
288 |
[ ! -z "$project" ] || export project="default"
|
|
289 |
|
|
290 |
# Clean up and Create runtime configuration file
|
|
291 |
# command="00-$project-RuntimeConfig-Initialization"
|
|
292 |
# title="Puppi setup: $command"
|
|
293 |
# code="rm -rf $workdir/$project && touch $workdir/$project/config [...]"
|
|
294 |
# echo -n $title
|
|
295 |
|
|
296 |
echo $workdir | grep tmp >/dev/null 2>&1 || ( echo "Workdir must contain string tmp" ; exit 1 )
|
|
297 |
rm -rf $workdir/$project || initerr=1
|
|
298 |
|
|
299 |
mkdir -p $workdir/$project || initerr=1
|
|
300 |
touch $workdir/$project/config || initerr=1
|
|
301 |
|
|
302 |
test -r "$projectsdir/$project/config" && cp $projectsdir/$project/config $workdir/$project/
|
|
303 |
chmod 644 $workdir/$project/config || initerr=1
|
|
304 |
|
|
305 |
save_runtime_config "project=$project" || initerr=1
|
|
306 |
save_runtime_config "tag=$tag" || initerr=1
|
|
307 |
save_runtime_config "action=$action" || initerr=1
|
|
308 |
|
|
309 |
storedir=$workdir/$project/store || initerr=1
|
|
310 |
mkdir -p $storedir || initerr=1
|
|
311 |
save_runtime_config "storedir=$storedir" || initerr=1
|
|
312 |
|
|
313 |
predeploydir=$workdir/$project/deploy || initerr=1
|
|
314 |
mkdir -p $predeploydir || initerr=1
|
|
315 |
save_runtime_config "predeploydir=$predeploydir" || initerr=1
|
|
316 |
|
|
317 |
save_runtime_config "force=$force" || initerr=1
|
|
318 |
save_runtime_config "testmode=$testmode" || initerr=1
|
|
319 |
save_runtime_config "interactive=$interactive" || initerr=1
|
|
320 |
save_runtime_config "debug=$debug" || initerr=1
|
|
321 |
save_runtime_config "report=$report" || initerr=1
|
|
322 |
save_runtime_config "show=$show" || initerr=1
|
|
323 |
save_runtime_config "counts=$counts" || initerr=1
|
|
324 |
save_runtime_config "greppattern=$greppattern" || initerr=1
|
|
325 |
|
|
326 |
for oopt in $(echo $options) ; do
|
|
327 |
save_runtime_config "$(echo $oopt)" || initerr=1
|
|
328 |
done
|
|
329 |
|
|
330 |
echo $initerr | grep "0" 2>&1 > /dev/null
|
|
331 |
# handle_result
|
|
332 |
}
|
|
333 |
|
|
334 |
|
|
335 |
showhelp () {
|
|
336 |
echo "Usage: puppi <command> [project|topic] [options]"
|
|
337 |
echo " "
|
|
338 |
echo "Available commands:"
|
|
339 |
echo "check [project] [-s <yes|no|fail>] - Run puppi checks host or project wide"
|
|
340 |
echo "log [topic] [-i] [-g <pattern>] - Show system and application specific logs"
|
|
341 |
echo "info [topic] [-i] [-g <pattern>] - Show informations about the system"
|
|
342 |
echo "todo - Show todo's checklist of the system"
|
|
343 |
echo "init <project> [-i] [-f] [-t] - First time project initialization and setup"
|
|
344 |
echo "configure <project> [-i] [-f] [-t] - Project configuration deployment."
|
|
345 |
echo "deploy <project> [-i] [-f] [-t] [-o ...] - Deploy the specified project"
|
|
346 |
echo "rollback <project> [state] [-i] [-f] [-t] - Rollback the specified project. "
|
|
347 |
echo " "
|
|
348 |
echo "Available options:"
|
|
349 |
echo "-f - Force puppi commands execution flow also on CRITICAL errors"
|
|
350 |
echo "-i - Interactively ask confirmation for every step"
|
|
351 |
echo "-t - Test mode. Just show the commands that should be executed"
|
|
352 |
echo "-d <yes|full> - Debug mode. Show debug of what is done."
|
|
353 |
echo "-r <yes|no|fail> - Enable reporting: yes/no/only on failures. Default depends on action"
|
|
354 |
echo "-s <yes|no|fail> - Show output: yes/no/only for failures. Default: yes"
|
|
355 |
echo "-g <pattern> - Grep command output with the selected pattern"
|
|
356 |
echo "-o \"parameter=value parameter2=value2\" - Set manual options to override defaults"
|
|
357 |
echo " "
|
|
358 |
echo "Available deploy projects:"
|
|
359 |
ls -1 $projectsdir
|
|
360 |
echo
|
|
361 |
echo "Available info topics:"
|
|
362 |
ls $infodir
|
|
363 |
echo
|
|
364 |
echo "Available log topics:"
|
|
365 |
ls $logsdir
|
|
366 |
}
|
|
367 |
|
|
368 |
# Check Input
|
|
369 |
if [ "$#" = "0" ] ; then
|
|
370 |
showhelp
|
|
371 |
exit
|
|
372 |
fi
|
|
373 |
|
|
374 |
while [ $# -gt 0 ]; do
|
|
375 |
case "$1" in
|
|
376 |
deploy|init|configure)
|
|
377 |
report="yes"
|
|
378 |
export action=$1
|
|
379 |
if [ -n "$2" ] ; then
|
|
380 |
echo "$2" | egrep -q "^-.$"
|
|
381 |
if [ "$?" != "0" ] ; then
|
|
382 |
export project=$(shell_filter_strict $2)
|
|
383 |
shift 2
|
|
384 |
else
|
|
385 |
shift
|
|
386 |
fi
|
|
387 |
else
|
|
388 |
showhelp
|
|
389 |
exit
|
|
390 |
shift
|
|
391 |
fi
|
|
392 |
;;
|
|
393 |
rollback)
|
|
394 |
report="yes"
|
|
395 |
export action=$1
|
|
396 |
if [ -n "$3" ] ; then
|
|
397 |
echo "$3" | egrep -q "^-.$"
|
|
398 |
if [ "$?" != "0" ] ; then
|
|
399 |
export project=$(shell_filter_strict $2)
|
|
400 |
export rollbackversion=$(shell_filter_strict $3)
|
|
401 |
shift 3
|
|
402 |
else
|
|
403 |
shift 2
|
|
404 |
fi
|
|
405 |
elif [ -n "$2" ] ; then
|
|
406 |
echo "$2" | egrep -q "^-.$"
|
|
407 |
if [ "$?" != "0" ] ; then
|
|
408 |
export project=$(shell_filter_strict $2)
|
|
409 |
shift 2
|
|
410 |
else
|
|
411 |
shift
|
|
412 |
fi
|
|
413 |
else
|
|
414 |
showhelp
|
|
415 |
exit
|
|
416 |
shift
|
|
417 |
fi
|
|
418 |
;;
|
|
419 |
check)
|
|
420 |
report="no"
|
|
421 |
export action="checkhost"
|
|
422 |
if [ -n "$2" ] ; then
|
|
423 |
echo "$2" | egrep -q "^-.$"
|
|
424 |
if [ "$?" != "0" ] ; then
|
|
425 |
export project=$(shell_filter_strict $2)
|
|
426 |
export action="check"
|
|
427 |
shift 2
|
|
428 |
else
|
|
429 |
shift
|
|
430 |
fi
|
|
431 |
else
|
|
432 |
shift
|
|
433 |
fi
|
|
434 |
;;
|
|
435 |
log)
|
|
436 |
report="no"
|
|
437 |
export action="log"
|
|
438 |
if [ -n "$2" ] ; then
|
|
439 |
echo "$2" | egrep -q "^-.$"
|
|
440 |
if [ "$?" != "0" ] ; then
|
|
441 |
export project=$(shell_filter_strict $2)
|
|
442 |
shift 2
|
|
443 |
else
|
|
444 |
shift
|
|
445 |
fi
|
|
446 |
else
|
|
447 |
shift
|
|
448 |
fi
|
|
449 |
;;
|
|
450 |
info)
|
|
451 |
report="no"
|
|
452 |
export action="info"
|
|
453 |
if [ -n "$2" ] ; then
|
|
454 |
echo "$2" | egrep -q "^-.$"
|
|
455 |
if [ "$?" != "0" ] ; then
|
|
456 |
export project=$(shell_filter_strict $2)
|
|
457 |
shift 2
|
|
458 |
else
|
|
459 |
shift
|
|
460 |
fi
|
|
461 |
else
|
|
462 |
shift
|
|
463 |
fi
|
|
464 |
;;
|
|
465 |
todo)
|
|
466 |
report="no"
|
|
467 |
export action="todo"
|
|
468 |
shift ;;
|
|
469 |
-i)
|
|
470 |
interactive="yes"
|
|
471 |
shift ;;
|
|
472 |
-f)
|
|
473 |
force="yes"
|
|
474 |
shift ;;
|
|
475 |
-t)
|
|
476 |
testmode="yes"
|
|
477 |
runcommand="cat"
|
|
478 |
shift ;;
|
|
479 |
-o)
|
|
480 |
options="$2"
|
|
481 |
shift 2;;
|
|
482 |
-d)
|
|
483 |
debug="$(shell_filter_strict $2)"
|
|
484 |
shift 2;;
|
|
485 |
-r)
|
|
486 |
report="$(shell_filter_strict $2)"
|
|
487 |
shift 2;;
|
|
488 |
-s)
|
|
489 |
show="$(shell_filter_strict $2)"
|
|
490 |
shift 2;;
|
|
491 |
-c)
|
|
492 |
counts="$(shell_filter_strict $2)"
|
|
493 |
shift 2;;
|
|
494 |
-g)
|
|
495 |
greppattern="$(shell_filter_strict $2)"
|
|
496 |
# greppattern="$(shell_filter $2)" # This allows partial regexp usage
|
|
497 |
# greppattern="$2" # This allows full regexp usage but is highly insecure if you sudo puppi
|
|
498 |
shift 2;;
|
|
499 |
*)
|
|
500 |
showhelp
|
|
501 |
exit
|
|
502 |
;;
|
|
503 |
esac
|
|
504 |
done
|
|
505 |
|
|
506 |
# Action!
|
|
507 |
case $action in
|
|
508 |
check) check ;;
|
|
509 |
checkhost) check_host ;;
|
|
510 |
log) create_runtime_conf ; log ;;
|
|
511 |
info) create_runtime_conf ; info ;;
|
|
512 |
todo) create_runtime_conf ; todo ;;
|
|
513 |
rollback) create_runtime_conf ; rollback ;;
|
|
514 |
deploy) create_runtime_conf ; deploy ;;
|
|
515 |
init) create_runtime_conf ; initialize ;;
|
|
516 |
configure) create_runtime_conf ; configure ;;
|
|
517 |
esac
|