Browse code

Merge "Add run_process() to start services without screen"

Jenkins authored on 2013/03/21 03:10:36
Showing 3 changed files
... ...
@@ -858,26 +858,69 @@ function restart_service() {
858 858
 }
859 859
 
860 860
 
861
+# _run_process() is designed to be backgrounded by run_process() to simulate a
862
+# fork.  It includes the dirty work of closing extra filehandles and preparing log
863
+# files to produce the same logs as screen_it().  The log filename is derived
864
+# from the service name and global-and-now-misnamed SCREEN_LOGDIR
865
+# _run_process service "command-line"
866
+function _run_process() {
867
+    local service=$1
868
+    local command="$2"
869
+
870
+    # Undo logging redirections and close the extra descriptors
871
+    exec 1>&3
872
+    exec 2>&3
873
+    exec 3>&-
874
+    exec 6>&-
875
+
876
+    if [[ -n ${SCREEN_LOGDIR} ]]; then
877
+        exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
878
+        ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
879
+
880
+        # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
881
+        export PYTHONUNBUFFERED=1
882
+    fi
883
+
884
+    exec /bin/bash -c "$command"
885
+    die "$service exec failure: $command"
886
+}
887
+
888
+
889
+# run_process() launches a child process that closes all file descriptors and
890
+# then exec's the passed in command.  This is meant to duplicate the semantics
891
+# of screen_it() without screen.  PIDs are written to
892
+# $SERVICE_DIR/$SCREEN_NAME/$service.pid
893
+# run_process service "command-line"
894
+function run_process() {
895
+    local service=$1
896
+    local command="$2"
897
+
898
+    # Spawn the child process
899
+    _run_process "$service" "$command" &
900
+    echo $!
901
+}
902
+
903
+
861 904
 # Helper to launch a service in a named screen
862 905
 # screen_it service "command-line"
863 906
 function screen_it {
864 907
     SCREEN_NAME=${SCREEN_NAME:-stack}
865 908
     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
866
-    SCREEN_DEV=`trueorfalse True $SCREEN_DEV`
909
+    USE_SCREEN=$(trueorfalse True $USE_SCREEN)
867 910
 
868 911
     if is_service_enabled $1; then
869 912
         # Append the service to the screen rc file
870 913
         screen_rc "$1" "$2"
871 914
 
872
-        screen -S $SCREEN_NAME -X screen -t $1
915
+        if [[ "$USE_SCREEN" = "True" ]]; then
916
+            screen -S $SCREEN_NAME -X screen -t $1
873 917
 
874
-        if [[ -n ${SCREEN_LOGDIR} ]]; then
875
-            screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
876
-            screen -S $SCREEN_NAME -p $1 -X log on
877
-            ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
878
-        fi
918
+            if [[ -n ${SCREEN_LOGDIR} ]]; then
919
+                screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
920
+                screen -S $SCREEN_NAME -p $1 -X log on
921
+                ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
922
+            fi
879 923
 
880
-        if [[ "$SCREEN_DEV" = "True" ]]; then
881 924
             # sleep to allow bash to be ready to be send the command - we are
882 925
             # creating a new window in screen and then sends characters, so if
883 926
             # bash isn't running by the time we send the command, nothing happens
... ...
@@ -886,7 +929,8 @@ function screen_it {
886 886
             NL=`echo -ne '\015'`
887 887
             screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
888 888
         else
889
-            screen -S $SCREEN_NAME -p $1 -X exec /bin/bash -c "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\""
889
+            # Spawn directly without screen
890
+            run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
890 891
         fi
891 892
     fi
892 893
 }
... ...
@@ -800,8 +800,17 @@ fi
800 800
 # Configure screen
801 801
 # ----------------
802 802
 
803
-if [ -z "$SCREEN_HARDSTATUS" ]; then
804
-    SCREEN_HARDSTATUS='%{= .} %-Lw%{= .}%> %n%f %t*%{= .}%+Lw%< %-=%{g}(%{d}%H/%l%{g})'
803
+USE_SCREEN=$(trueorfalse True $USE_SCREEN)
804
+if [[ "$USE_SCREEN" == "True" ]]; then
805
+    # Create a new named screen to run processes in
806
+    screen -d -m -S $SCREEN_NAME -t shell -s /bin/bash
807
+    sleep 1
808
+
809
+    # Set a reasonable status bar
810
+    if [ -z "$SCREEN_HARDSTATUS" ]; then
811
+        SCREEN_HARDSTATUS='%{= .} %-Lw%{= .}%> %n%f %t*%{= .}%+Lw%< %-=%{g}(%{d}%H/%l%{g})'
812
+    fi
813
+    screen -r $SCREEN_NAME -X hardstatus alwayslastline "$SCREEN_HARDSTATUS"
805 814
 fi
806 815
 
807 816
 # Clear screen rc file
... ...
@@ -810,12 +819,6 @@ if [[ -e $SCREENRC ]]; then
810 810
     echo -n > $SCREENRC
811 811
 fi
812 812
 
813
-# Create a new named screen to run processes in
814
-screen -d -m -S $SCREEN_NAME -t shell -s /bin/bash
815
-sleep 1
816
-
817
-# Set a reasonable status bar
818
-screen -r $SCREEN_NAME -X hardstatus alwayslastline "$SCREEN_HARDSTATUS"
819 813
 
820 814
 # Initialize the directory for service status check
821 815
 init_service_check
... ...
@@ -30,8 +30,8 @@ NOVA_ENABLED_APIS=ec2,osapi_compute,metadata
30 30
 # stuffing text into the screen windows so that a developer can use
31 31
 # ctrl-c, up-arrow, enter to restart the service. Starting services
32 32
 # this way is slightly unreliable, and a bit slower, so this can
33
-# be disabled for automated testing by setting this value to false.
34
-SCREEN_DEV=True
33
+# be disabled for automated testing by setting this value to False.
34
+USE_SCREEN=True
35 35
 
36 36
 # Repositories
37 37
 # ------------
... ...
@@ -198,3 +198,6 @@ VOLUME_BACKING_FILE_SIZE=${VOLUME_BACKING_FILE_SIZE:-5130M}
198 198
 
199 199
 PRIVATE_NETWORK_NAME=${PRIVATE_NETWORK_NAME:-"private"}
200 200
 PUBLIC_NETWORK_NAME=${PUBLIC_NETWORK_NAME:-"nova"}
201
+
202
+# Compatibility until it's eradicated from CI
203
+USE_SCREEN=${SCREEN_DEV:-$USE_SCREEN}