Browse code

Improve log file handling:

* Elimiate subshells to produce logfiles (fixes bug 885091)
* Clean up log files older than 7 days (default)
* Append date/time to specified log file name
* Default LOGFILE='', now must set to get logging

This changes the default behaviour of stack.sh to not write a log file
unless LOGFILE is set.

Change-Id: I5d3fb65e12ccdb52fca5a41ee8f5777c046cd375

Dean Troyer authored on 2011/12/28 02:45:55
Showing 1 changed files
... ...
@@ -390,8 +390,31 @@ read_password SERVICE_TOKEN "ENTER A SERVICE_TOKEN TO USE FOR THE SERVICE ADMIN
390 390
 # Horizon currently truncates usernames and passwords at 20 characters
391 391
 read_password ADMIN_PASSWORD "ENTER A PASSWORD TO USE FOR HORIZON AND KEYSTONE (20 CHARS OR LESS)."
392 392
 
393
-LOGFILE=${LOGFILE:-"$PWD/stack.sh.$$.log"}
394
-(
393
+# Log files
394
+# ---------
395
+
396
+# Set up logging for stack.sh
397
+# Set LOGFILE to turn on logging
398
+# We append '.xxxxxxxx' to the given name to maintain history
399
+# where xxxxxxxx is a representation of the date the file was created
400
+if [[ -n "$LOGFILE" ]]; then
401
+    # First clean up old log files.  Use the user-specified LOGFILE
402
+    # as the template to search for, appending '.*' to match the date
403
+    # we added on earlier runs.
404
+    LOGDAYS=${LOGDAYS:-7}
405
+    LOGDIR=$(dirname "$LOGFILE")
406
+    LOGNAME=$(basename "$LOGFILE")
407
+    find $LOGDIR -maxdepth 1 -name $LOGNAME.\* -mtime +$LOGDAYS -exec rm {} \;
408
+
409
+    TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
410
+    LOGFILE=$LOGFILE.$(date "+$TIMESTAMP_FORMAT")
411
+    # Redirect stdout/stderr to tee to write the log file
412
+    exec 1> >( tee "${LOGFILE}" ) 2>&1
413
+    echo "stack.sh log $LOGFILE"
414
+    # Specified logfile name always links to the most recent log
415
+    ln -sf $LOGFILE $LOGDIR/$LOGNAME
416
+fi
417
+
395 418
 # So that errors don't compound we exit on any errors so you see only the
396 419
 # first error that occurred.
397 420
 trap failed ERR
... ...
@@ -1403,13 +1426,8 @@ fi
1403 1403
 # Fin
1404 1404
 # ===
1405 1405
 
1406
+set +o xtrace
1406 1407
 
1407
-) 2>&1 | tee "${LOGFILE}"
1408
-
1409
-# Check that the left side of the above pipe succeeded
1410
-for ret in "${PIPESTATUS[@]}"; do [ $ret -eq 0 ] || exit $ret; done
1411
-
1412
-(
1413 1408
 # Using the cloud
1414 1409
 # ===============
1415 1410
 
... ...
@@ -1436,5 +1454,3 @@ echo "This is your host ip: $HOST_IP"
1436 1436
 
1437 1437
 # Indicate how long this took to run (bash maintained variable 'SECONDS')
1438 1438
 echo "stack.sh completed in $SECONDS seconds."
1439
-
1440
-) | tee -a "$LOGFILE"