From a9414249af522324c68e4d8fe1656283162e5738 Mon Sep 17 00:00:00 2001
From: jiajun xu <jiajun.xu@intel.com>
Date: Thu, 6 Dec 2012 16:30:57 +0800
Subject: [PATCH] Add a service_check function

There is no function to check if the services invoked by devstack are
running well or not. We could use the function to check their status
and print them at the end of devstack running.

Change-Id: I845f6b5dddce5cffa7165ec58517f9ae5d8632a6
---
 functions | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 stack.sh  |  4 ++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/functions b/functions
index 0911557fa4..85ff42024c 100644
--- a/functions
+++ b/functions
@@ -684,6 +684,8 @@ function restart_service() {
 function screen_it {
     NL=`echo -ne '\015'`
     SCREEN_NAME=${SCREEN_NAME:-stack}
+    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
+
     if is_service_enabled $1; then
         # Append the service to the screen rc file
         screen_rc "$1" "$2"
@@ -699,7 +701,7 @@ function screen_it {
             screen -S $SCREEN_NAME -p $1 -X log on
             ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
         fi
-        screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
+        screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
     fi
 }
 
@@ -724,6 +726,47 @@ function screen_rc {
     fi
 }
 
+# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
+# This is used for service_check when all the screen_it are called finished
+# init_service_check
+function init_service_check() {
+    SCREEN_NAME=${SCREEN_NAME:-stack}
+    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
+
+    if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
+        mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
+    fi
+
+    rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
+}
+
+# Helper to get the status of each running service
+# service_check
+function service_check() {
+    local service
+    local failures
+    SCREEN_NAME=${SCREEN_NAME:-stack}
+    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
+
+
+    if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
+        echo "No service status directory found"
+        return
+    fi
+
+    # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
+    failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
+
+    for service in $failures; do
+        service=`basename $service`
+        service=${service::-8}
+        echo "Error: Service $service is not running"
+    done
+
+    if [ -n "$failures" ]; then
+        echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
+    fi
+}
 
 # ``pip install`` the dependencies of the package before ``setup.py develop``
 # so pip and not distutils processes the dependency chain
diff --git a/stack.sh b/stack.sh
index 942835635f..40708afd88 100755
--- a/stack.sh
+++ b/stack.sh
@@ -954,6 +954,8 @@ sleep 1
 # Set a reasonable status bar
 screen -r $SCREEN_NAME -X hardstatus alwayslastline "$SCREEN_HARDSTATUS"
 
+# Initialize the directory for service status check
+init_service_check
 
 # Keystone
 # --------
@@ -1726,6 +1728,8 @@ if [[ -x $TOP_DIR/local.sh ]]; then
     $TOP_DIR/local.sh
 fi
 
+# Check the status of running services
+service_check
 
 # Fin
 # ===