From 33cb43034e13ceb9b55d26ac95e28eeaf47a9cec Mon Sep 17 00:00:00 2001
From: Dean Troyer <dtroyer@gmail.com>
Date: Mon, 10 Dec 2012 16:47:36 -0600
Subject: [PATCH] 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
---
 openrc   |  5 +++++
 stack.sh | 15 ++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/openrc b/openrc
index 2553b4aa1c..3ef44fd151 100644
--- a/openrc
+++ b/openrc
@@ -26,6 +26,11 @@ source $RC_DIR/functions
 # Load local configuration
 source $RC_DIR/stackrc
 
+# Load the last env variables if available
+if [[ -r $TOP_DIR/.stackenv ]]; then
+    source $TOP_DIR/.stackenv
+fi
+
 # Get some necessary configuration
 source $RC_DIR/lib/tls
 
diff --git a/stack.sh b/stack.sh
index f2fd68cc67..5002f8b902 100755
--- a/stack.sh
+++ b/stack.sh
@@ -90,6 +90,11 @@ DEST=${DEST:-/opt/stack}
 # 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 @@ function echo_nolog() {
 # 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 @@ if is_service_enabled tempest; then
     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
 # ================