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
diff --git a/stack.sh b/stack.sh
index 420fc25..a79f794 100755
--- a/stack.sh
+++ b/stack.sh
@@ -390,8 +390,31 @@
# Horizon currently truncates usernames and passwords at 20 characters
read_password ADMIN_PASSWORD "ENTER A PASSWORD TO USE FOR HORIZON AND KEYSTONE (20 CHARS OR LESS)."
-LOGFILE=${LOGFILE:-"$PWD/stack.sh.$$.log"}
-(
+# Log files
+# ---------
+
+# Set up logging for stack.sh
+# Set LOGFILE to turn on logging
+# We append '.xxxxxxxx' to the given name to maintain history
+# where xxxxxxxx is a representation of the date the file was created
+if [[ -n "$LOGFILE" ]]; then
+ # First clean up old log files. Use the user-specified LOGFILE
+ # as the template to search for, appending '.*' to match the date
+ # we added on earlier runs.
+ LOGDAYS=${LOGDAYS:-7}
+ LOGDIR=$(dirname "$LOGFILE")
+ LOGNAME=$(basename "$LOGFILE")
+ find $LOGDIR -maxdepth 1 -name $LOGNAME.\* -mtime +$LOGDAYS -exec rm {} \;
+
+ TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
+ LOGFILE=$LOGFILE.$(date "+$TIMESTAMP_FORMAT")
+ # Redirect stdout/stderr to tee to write the log file
+ exec 1> >( tee "${LOGFILE}" ) 2>&1
+ echo "stack.sh log $LOGFILE"
+ # Specified logfile name always links to the most recent log
+ ln -sf $LOGFILE $LOGDIR/$LOGNAME
+fi
+
# So that errors don't compound we exit on any errors so you see only the
# first error that occurred.
trap failed ERR
@@ -1403,13 +1426,8 @@
# Fin
# ===
+set +o xtrace
-) 2>&1 | tee "${LOGFILE}"
-
-# Check that the left side of the above pipe succeeded
-for ret in "${PIPESTATUS[@]}"; do [ $ret -eq 0 ] || exit $ret; done
-
-(
# Using the cloud
# ===============
@@ -1436,5 +1454,3 @@
# Indicate how long this took to run (bash maintained variable 'SECONDS')
echo "stack.sh completed in $SECONDS seconds."
-
-) | tee -a "$LOGFILE"