Write selected env variables to .stackenv

There are some environment variables that are derived in stack.sh
and cubersome to re-create later, so save them at the end of stack.sh
for use by other supporting scripts, such as openrc.

Change-Id: I1bbf717b970f8ceac0ff7da74aeaf19474997e07
diff --git a/stack.sh b/stack.sh
index f2fd68c..5002f8b 100755
--- a/stack.sh
+++ b/stack.sh
@@ -90,6 +90,11 @@
 # Sanity Check
 # ============
 
+# Clean up last environment var cache
+if [[ -r $TOP_DIR/.stackenv ]]; then
+    rm $TOP_DIR/.stackenv
+fi
+
 # Import database configuration
 source $TOP_DIR/lib/database
 
@@ -537,9 +542,9 @@
 # Set ``LOGFILE`` to turn on logging
 # Append '.xxxxxxxx' to the given name to maintain history
 # where 'xxxxxxxx' is a representation of the date the file was created
+TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
 if [[ -n "$LOGFILE" || -n "$SCREEN_LOGDIR" ]]; then
     LOGDAYS=${LOGDAYS:-7}
-    TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
     CURRENT_LOG_TIME=$(date "+$TIMESTAMP_FORMAT")
 fi
 
@@ -1705,6 +1710,14 @@
     echo '**************************************************'
 fi
 
+# Save some values we generated for later use
+CURRENT_RUN_TIME=$(date "+$TIMESTAMP_FORMAT")
+echo "# $CURRENT_RUN_TIME" >$TOP_DIR/.stackenv
+for i in BASE_SQL_CONN ENABLED_SERVICES HOST_IP LOGFILE \
+  SERVICE_HOST SERVICE_PROTOCOL TLS_IP; do
+    echo $i=${!i} >>$TOP_DIR/.stackenv
+done
+
 
 # Run local script
 # ================