Browse code

Re-order stack.sh 3: logging and error traps

Part 3 of a series

Re-order the setup and check bits in the top portion of stack.sh to
have a logical flow with similar things done together.

No behaviour changes are intended aside from the order of execution.
Any such changes are bugs.

* Move logging and error configuration earlier to cover initial project setup.

Change-Id: Ib16bbe20f224b1cf5e86c7a2fda0d9472c108873

Dean Troyer authored on 2014/08/03 06:07:03
Showing 2 changed files
... ...
@@ -43,7 +43,7 @@ function check_rpc_backend {
43 43
     local rpc_backend_cnt=0
44 44
     for svc in qpid zeromq rabbit; do
45 45
         is_service_enabled $svc &&
46
-        ((rpc_backend_cnt++))
46
+        (( rpc_backend_cnt++ )) || true
47 47
     done
48 48
     if [ "$rpc_backend_cnt" -gt 1 ]; then
49 49
         echo "ERROR: only one rpc backend may be enabled,"
... ...
@@ -177,9 +177,6 @@ if [[ ,${ENABLED_SERVICES}, =~ ,"swift", ]]; then
177 177
     exit 1
178 178
 fi
179 179
 
180
-# Set up logging level
181
-VERBOSE=$(trueorfalse True $VERBOSE)
182
-
183 180
 # Configure sudo
184 181
 # --------------
185 182
 
... ...
@@ -285,6 +282,182 @@ if [ -z "`grep ^127.0.0.1 /etc/hosts | grep $LOCAL_HOSTNAME`" ]; then
285 285
 fi
286 286
 
287 287
 
288
+# Configure Logging
289
+# -----------------
290
+
291
+# Set up logging level
292
+VERBOSE=$(trueorfalse True $VERBOSE)
293
+
294
+# Draw a spinner so the user knows something is happening
295
+function spinner {
296
+    local delay=0.75
297
+    local spinstr='/-\|'
298
+    printf "..." >&3
299
+    while [ true ]; do
300
+        local temp=${spinstr#?}
301
+        printf "[%c]" "$spinstr" >&3
302
+        local spinstr=$temp${spinstr%"$temp"}
303
+        sleep $delay
304
+        printf "\b\b\b" >&3
305
+    done
306
+}
307
+
308
+function kill_spinner {
309
+    if [ ! -z "$LAST_SPINNER_PID" ]; then
310
+        kill >/dev/null 2>&1 $LAST_SPINNER_PID
311
+        printf "\b\b\bdone\n" >&3
312
+    fi
313
+}
314
+
315
+# Echo text to the log file, summary log file and stdout
316
+# echo_summary "something to say"
317
+function echo_summary {
318
+    if [[ -t 3 && "$VERBOSE" != "True" ]]; then
319
+        kill_spinner
320
+        echo -n -e $@ >&6
321
+        spinner &
322
+        LAST_SPINNER_PID=$!
323
+    else
324
+        echo -e $@ >&6
325
+    fi
326
+}
327
+
328
+# Echo text only to stdout, no log files
329
+# echo_nolog "something not for the logs"
330
+function echo_nolog {
331
+    echo $@ >&3
332
+}
333
+
334
+if [[ is_fedora && $DISTRO == "rhel6" ]]; then
335
+    # poor old python2.6 doesn't have argparse by default, which
336
+    # outfilter.py uses
337
+    is_package_installed python-argparse || install_package python-argparse
338
+fi
339
+
340
+# Set up logging for ``stack.sh``
341
+# Set ``LOGFILE`` to turn on logging
342
+# Append '.xxxxxxxx' to the given name to maintain history
343
+# where 'xxxxxxxx' is a representation of the date the file was created
344
+TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
345
+if [[ -n "$LOGFILE" || -n "$SCREEN_LOGDIR" ]]; then
346
+    LOGDAYS=${LOGDAYS:-7}
347
+    CURRENT_LOG_TIME=$(date "+$TIMESTAMP_FORMAT")
348
+fi
349
+
350
+if [[ -n "$LOGFILE" ]]; then
351
+    # First clean up old log files.  Use the user-specified ``LOGFILE``
352
+    # as the template to search for, appending '.*' to match the date
353
+    # we added on earlier runs.
354
+    LOGDIR=$(dirname "$LOGFILE")
355
+    LOGFILENAME=$(basename "$LOGFILE")
356
+    mkdir -p $LOGDIR
357
+    find $LOGDIR -maxdepth 1 -name $LOGFILENAME.\* -mtime +$LOGDAYS -exec rm {} \;
358
+    LOGFILE=$LOGFILE.${CURRENT_LOG_TIME}
359
+    SUMFILE=$LOGFILE.${CURRENT_LOG_TIME}.summary
360
+
361
+    # Redirect output according to config
362
+
363
+    # Set fd 3 to a copy of stdout. So we can set fd 1 without losing
364
+    # stdout later.
365
+    exec 3>&1
366
+    if [[ "$VERBOSE" == "True" ]]; then
367
+        # Set fd 1 and 2 to write the log file
368
+        exec 1> >( $TOP_DIR/tools/outfilter.py -v -o "${LOGFILE}" ) 2>&1
369
+        # Set fd 6 to summary log file
370
+        exec 6> >( $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
371
+    else
372
+        # Set fd 1 and 2 to primary logfile
373
+        exec 1> >( $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
374
+        # Set fd 6 to summary logfile and stdout
375
+        exec 6> >( $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
376
+    fi
377
+
378
+    echo_summary "stack.sh log $LOGFILE"
379
+    # Specified logfile name always links to the most recent log
380
+    ln -sf $LOGFILE $LOGDIR/$LOGFILENAME
381
+    ln -sf $SUMFILE $LOGDIR/$LOGFILENAME.summary
382
+else
383
+    # Set up output redirection without log files
384
+    # Set fd 3 to a copy of stdout. So we can set fd 1 without losing
385
+    # stdout later.
386
+    exec 3>&1
387
+    if [[ "$VERBOSE" != "True" ]]; then
388
+        # Throw away stdout and stderr
389
+        exec 1>/dev/null 2>&1
390
+    fi
391
+    # Always send summary fd to original stdout
392
+    exec 6> >( $TOP_DIR/tools/outfilter.py -v >&3 )
393
+fi
394
+
395
+# Set up logging of screen windows
396
+# Set ``SCREEN_LOGDIR`` to turn on logging of screen windows to the
397
+# directory specified in ``SCREEN_LOGDIR``, we will log to the the file
398
+# ``screen-$SERVICE_NAME-$TIMESTAMP.log`` in that dir and have a link
399
+# ``screen-$SERVICE_NAME.log`` to the latest log file.
400
+# Logs are kept for as long specified in ``LOGDAYS``.
401
+if [[ -n "$SCREEN_LOGDIR" ]]; then
402
+
403
+    # We make sure the directory is created.
404
+    if [[ -d "$SCREEN_LOGDIR" ]]; then
405
+        # We cleanup the old logs
406
+        find $SCREEN_LOGDIR -maxdepth 1 -name screen-\*.log -mtime +$LOGDAYS -exec rm {} \;
407
+    else
408
+        mkdir -p $SCREEN_LOGDIR
409
+    fi
410
+fi
411
+
412
+
413
+# Configure Error Traps
414
+# ---------------------
415
+
416
+# Kill background processes on exit
417
+trap exit_trap EXIT
418
+function exit_trap {
419
+    local r=$?
420
+    jobs=$(jobs -p)
421
+    # Only do the kill when we're logging through a process substitution,
422
+    # which currently is only to verbose logfile
423
+    if [[ -n $jobs && -n "$LOGFILE" && "$VERBOSE" == "True" ]]; then
424
+        echo "exit_trap: cleaning up child processes"
425
+        kill 2>&1 $jobs
426
+    fi
427
+
428
+    # Kill the last spinner process
429
+    kill_spinner
430
+
431
+    if [[ $r -ne 0 ]]; then
432
+        echo "Error on exit"
433
+        if [[ -z $LOGDIR ]]; then
434
+            $TOP_DIR/tools/worlddump.py
435
+        else
436
+            $TOP_DIR/tools/worlddump.py -d $LOGDIR
437
+        fi
438
+    fi
439
+
440
+    exit $r
441
+}
442
+
443
+# Exit on any errors so that errors don't compound
444
+trap err_trap ERR
445
+function err_trap {
446
+    local r=$?
447
+    set +o xtrace
448
+    if [[ -n "$LOGFILE" ]]; then
449
+        echo "${0##*/} failed: full log in $LOGFILE"
450
+    else
451
+        echo "${0##*/} failed"
452
+    fi
453
+    exit $r
454
+}
455
+
456
+# Begin trapping error exit codes
457
+set -o errexit
458
+
459
+# Print the commands being run so that we can see the command that triggers
460
+# an error.  It is also useful for following along as the install occurs.
461
+set -o xtrace
462
+
463
+
288 464
 # Common Configuration
289 465
 # --------------------
290 466
 
... ...
@@ -494,179 +667,6 @@ if is_service_enabled s-proxy; then
494 494
 fi
495 495
 
496 496
 
497
-# Configure logging
498
-# -----------------
499
-
500
-# Draw a spinner so the user knows something is happening
501
-function spinner {
502
-    local delay=0.75
503
-    local spinstr='/-\|'
504
-    printf "..." >&3
505
-    while [ true ]; do
506
-        local temp=${spinstr#?}
507
-        printf "[%c]" "$spinstr" >&3
508
-        local spinstr=$temp${spinstr%"$temp"}
509
-        sleep $delay
510
-        printf "\b\b\b" >&3
511
-    done
512
-}
513
-
514
-function kill_spinner {
515
-    if [ ! -z "$LAST_SPINNER_PID" ]; then
516
-        kill >/dev/null 2>&1 $LAST_SPINNER_PID
517
-        printf "\b\b\bdone\n" >&3
518
-    fi
519
-}
520
-
521
-# Echo text to the log file, summary log file and stdout
522
-# echo_summary "something to say"
523
-function echo_summary {
524
-    if [[ -t 3 && "$VERBOSE" != "True" ]]; then
525
-        kill_spinner
526
-        echo -n -e $@ >&6
527
-        spinner &
528
-        LAST_SPINNER_PID=$!
529
-    else
530
-        echo -e $@ >&6
531
-    fi
532
-}
533
-
534
-# Echo text only to stdout, no log files
535
-# echo_nolog "something not for the logs"
536
-function echo_nolog {
537
-    echo $@ >&3
538
-}
539
-
540
-if [[ is_fedora && $DISTRO == "rhel6" ]]; then
541
-    # poor old python2.6 doesn't have argparse by default, which
542
-    # outfilter.py uses
543
-    is_package_installed python-argparse || install_package python-argparse
544
-fi
545
-
546
-# Set up logging for ``stack.sh``
547
-# Set ``LOGFILE`` to turn on logging
548
-# Append '.xxxxxxxx' to the given name to maintain history
549
-# where 'xxxxxxxx' is a representation of the date the file was created
550
-TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
551
-if [[ -n "$LOGFILE" || -n "$SCREEN_LOGDIR" ]]; then
552
-    LOGDAYS=${LOGDAYS:-7}
553
-    CURRENT_LOG_TIME=$(date "+$TIMESTAMP_FORMAT")
554
-fi
555
-
556
-if [[ -n "$LOGFILE" ]]; then
557
-    # First clean up old log files.  Use the user-specified ``LOGFILE``
558
-    # as the template to search for, appending '.*' to match the date
559
-    # we added on earlier runs.
560
-    LOGDIR=$(dirname "$LOGFILE")
561
-    LOGFILENAME=$(basename "$LOGFILE")
562
-    mkdir -p $LOGDIR
563
-    find $LOGDIR -maxdepth 1 -name $LOGFILENAME.\* -mtime +$LOGDAYS -exec rm {} \;
564
-    LOGFILE=$LOGFILE.${CURRENT_LOG_TIME}
565
-    SUMFILE=$LOGFILE.${CURRENT_LOG_TIME}.summary
566
-
567
-    # Redirect output according to config
568
-
569
-    # Set fd 3 to a copy of stdout. So we can set fd 1 without losing
570
-    # stdout later.
571
-    exec 3>&1
572
-    if [[ "$VERBOSE" == "True" ]]; then
573
-        # Set fd 1 and 2 to write the log file
574
-        exec 1> >( $TOP_DIR/tools/outfilter.py -v -o "${LOGFILE}" ) 2>&1
575
-        # Set fd 6 to summary log file
576
-        exec 6> >( $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
577
-    else
578
-        # Set fd 1 and 2 to primary logfile
579
-        exec 1> >( $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
580
-        # Set fd 6 to summary logfile and stdout
581
-        exec 6> >( $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
582
-    fi
583
-
584
-    echo_summary "stack.sh log $LOGFILE"
585
-    # Specified logfile name always links to the most recent log
586
-    ln -sf $LOGFILE $LOGDIR/$LOGFILENAME
587
-    ln -sf $SUMFILE $LOGDIR/$LOGFILENAME.summary
588
-else
589
-    # Set up output redirection without log files
590
-    # Set fd 3 to a copy of stdout. So we can set fd 1 without losing
591
-    # stdout later.
592
-    exec 3>&1
593
-    if [[ "$VERBOSE" != "True" ]]; then
594
-        # Throw away stdout and stderr
595
-        exec 1>/dev/null 2>&1
596
-    fi
597
-    # Always send summary fd to original stdout
598
-    exec 6> >( $TOP_DIR/tools/outfilter.py -v >&3 )
599
-fi
600
-
601
-# Set up logging of screen windows
602
-# Set ``SCREEN_LOGDIR`` to turn on logging of screen windows to the
603
-# directory specified in ``SCREEN_LOGDIR``, we will log to the the file
604
-# ``screen-$SERVICE_NAME-$TIMESTAMP.log`` in that dir and have a link
605
-# ``screen-$SERVICE_NAME.log`` to the latest log file.
606
-# Logs are kept for as long specified in ``LOGDAYS``.
607
-if [[ -n "$SCREEN_LOGDIR" ]]; then
608
-
609
-    # We make sure the directory is created.
610
-    if [[ -d "$SCREEN_LOGDIR" ]]; then
611
-        # We cleanup the old logs
612
-        find $SCREEN_LOGDIR -maxdepth 1 -name screen-\*.log -mtime +$LOGDAYS -exec rm {} \;
613
-    else
614
-        mkdir -p $SCREEN_LOGDIR
615
-    fi
616
-fi
617
-
618
-
619
-# Set Up Script Execution
620
-# -----------------------
621
-
622
-# Kill background processes on exit
623
-trap exit_trap EXIT
624
-function exit_trap {
625
-    local r=$?
626
-    jobs=$(jobs -p)
627
-    # Only do the kill when we're logging through a process substitution,
628
-    # which currently is only to verbose logfile
629
-    if [[ -n $jobs && -n "$LOGFILE" && "$VERBOSE" == "True" ]]; then
630
-        echo "exit_trap: cleaning up child processes"
631
-        kill 2>&1 $jobs
632
-    fi
633
-
634
-    # Kill the last spinner process
635
-    kill_spinner
636
-
637
-    if [[ $r -ne 0 ]]; then
638
-        echo "Error on exit"
639
-        if [[ -z $LOGDIR ]]; then
640
-            $TOP_DIR/tools/worlddump.py
641
-        else
642
-            $TOP_DIR/tools/worlddump.py -d $LOGDIR
643
-        fi
644
-    fi
645
-
646
-    exit $r
647
-}
648
-
649
-# Exit on any errors so that errors don't compound
650
-trap err_trap ERR
651
-function err_trap {
652
-    local r=$?
653
-    set +o xtrace
654
-    if [[ -n "$LOGFILE" ]]; then
655
-        echo "${0##*/} failed: full log in $LOGFILE"
656
-    else
657
-        echo "${0##*/} failed"
658
-    fi
659
-    exit $r
660
-}
661
-
662
-
663
-set -o errexit
664
-
665
-# Print the commands being run so that we can see the command that triggers
666
-# an error.  It is also useful for following along as the install occurs.
667
-set -o xtrace
668
-
669
-
670 497
 # Install Packages
671 498
 # ================
672 499