You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by kr...@apache.org on 2019/03/13 18:42:31 UTC

[knox] branch master updated: KNOX-1804 - Moving copy-pasted bash functions to knox-function.sh (#71)

This is an automated email from the ASF dual-hosted git repository.

krisden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 7d97dbe  KNOX-1804 - Moving copy-pasted bash functions to knox-function.sh (#71)
7d97dbe is described below

commit 7d97dbeacf0f992670242d56303716a35d7f652d
Author: Sandor Molnar <sm...@apache.org>
AuthorDate: Wed Mar 13 19:42:27 2019 +0100

    KNOX-1804 - Moving copy-pasted bash functions to knox-function.sh (#71)
---
 gateway-release-common/home/bin/knox-functions.sh | 175 ++++++++++++++++++++++
 gateway-release/home/bin/gateway.sh               | 168 +--------------------
 gateway-release/home/bin/ldap.sh                  | 171 +--------------------
 3 files changed, 185 insertions(+), 329 deletions(-)

diff --git a/gateway-release-common/home/bin/knox-functions.sh b/gateway-release-common/home/bin/knox-functions.sh
index 7a8207c..ea38492 100644
--- a/gateway-release-common/home/bin/knox-functions.sh
+++ b/gateway-release-common/home/bin/knox-functions.sh
@@ -17,6 +17,27 @@
 #  limitations under the License.
 #
 
+############################
+##### common variables #####
+############################
+
+# The app's home dir
+APP_HOME_DIR=`dirname $APP_BIN_DIR`
+
+# The app's PID
+APP_PID=0
+
+# The start wait time
+APP_START_WAIT_TIME=2
+
+# The kill wait time limit
+APP_KILL_WAIT_TIME=10
+
+
+############################
+##### common functions #####
+############################
+
 JAVA_VERSION_PATTERNS=( "1.6.0_31/bin/java$" "1.6.0_.*/bin/java$" "1.6.0.*/bin/java$" "1.6\..*/bin/java$" "/bin/java$" )
 
 function findJava() {
@@ -104,3 +125,157 @@ function printEnv() {
     fi
 }
 
+function appIsRunning {
+   if [ $1 -eq 0 ]; then return 0; fi
+
+   ps -p $1 > /dev/null
+
+   if [ $? -eq 1 ]; then
+     return 0
+   else
+     return 1
+   fi
+}
+
+# Returns 0 if the app is running and sets the $PID variable
+# TODO: this may be a false indication: it may happen the process started but it'll return with a <>0 exit code due to validation errors; this should be fixed ASAP
+function getPID {
+   if [ ! -d $APP_PID_DIR ]; then
+      printf "Can't find PID dir.\n"
+      exit 1
+   fi
+   if [ ! -f $APP_PID_FILE ]; then
+     APP_PID=0
+     return 1
+   fi
+
+   APP_PID="$(<$APP_PID_FILE)"
+
+   ps -p $APP_PID > /dev/null
+   # if the exit code was 1 then it isn't running
+   if [ "$?" -eq "1" ];
+   then
+     return 1
+   fi
+
+   return 0
+}
+
+function appStart {
+   if [ "$APP_RUNNING_IN_FOREGROUND" == true ]; then
+      $JAVA $APP_JAVA_OPTS -jar $APP_JAR $@
+   else
+      getPID
+      if [ $? -eq 0 ]; then
+         printf "$APP_LABEL is already running with PID $APP_PID.\n"
+         exit 0
+      fi
+
+      printf "Starting $APP_LABEL "
+
+      rm -f $APP_PID_FILE
+
+      nohup $JAVA $APP_JAVA_OPTS -jar $APP_JAR $@ >>$APP_OUT_FILE 2>>$APP_ERR_FILE & printf $!>$APP_PID_FILE || exit 1
+
+      ##give a second to the JVM to start and run validation
+      sleep 1
+
+      getPID
+      for ((i=0; i<APP_START_WAIT_TIME*10; i++)); do
+         appIsRunning $APP_PID
+         if [ $? -eq 0 ]; then break; fi
+         sleep 0.1
+      done
+      appIsRunning $APP_PID
+      if [ $? -ne 1 ]; then
+         printf "failed.\n"
+         rm -f $APP_PID_FILE
+         exit 1
+      fi
+      printf "succeeded with PID $APP_PID.\n"
+      return 0
+   fi
+}
+
+function appStop {
+   getPID
+   appIsRunning $APP_PID
+   if [ "$?" -eq "0" ]; then
+     printf "$APP_LABEL is not running.\n"
+     rm -f $APP_PID_FILE
+     return 0
+   fi
+
+   printf "Stopping $APP_LABEL with PID $APP_PID "
+   appKill $APP_PID >>$APP_OUT_FILE 2>>$APP_ERR_FILE
+
+   if [ "$?" -ne "0" ]; then
+     printf "failed. \n"
+     exit 1
+   else
+     rm -f $APP_PID_FILE
+     printf "succeeded.\n"
+     return 0
+   fi
+}
+
+function appStatus {
+   printf "$APP_LABEL "
+   getPID
+   if [ "$?" -eq "1" ]; then
+     printf "is not running. No PID file found.\n"
+     return 0
+   fi
+
+   appIsRunning $APP_PID
+   if [ "$?" -eq "1" ]; then
+     printf "is running with PID $APP_PID.\n"
+     exit 1
+   else
+     printf "is not running.\n"
+     return 0
+   fi
+}
+
+# Removing the app's PID/ERR/OUT files if app is not running
+function appClean {
+   getPID
+   appIsRunning $APP_PID
+   if [ "$?" -eq "0" ]; then
+     deleteLogFiles
+     return 0
+   else
+     printf "Can't clean files.  $APP_LABEL is running with PID $APP_PID.\n"
+     exit 1
+   fi
+}
+
+function appKill {
+   local localPID=$1
+   kill $localPID || return 1
+   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
+      appIsRunning $localPID
+      if [ "$?" -eq "0" ]; then return 0; fi
+      sleep 0.1
+   done
+
+   kill -s KILL $localPID || return 1
+   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
+      appIsRunning $localPID
+      if [ "$?" -eq "0" ]; then return 0; fi
+      sleep 0.1
+   done
+
+   return 1
+}
+
+function deleteLogFiles {
+     rm -f $APP_PID_FILE
+     printf "Removed the $APP_LABEL PID file: $APP_PID_FILE.\n"
+
+     rm -f $APP_OUT_FILE
+     printf "Removed the $APP_LABEL OUT file: $APP_OUT_FILE.\n"
+
+     rm -f $APP_ERR_FILE
+     printf "Removed the $APP_LABEL ERR file: $APP_ERR_FILE.\n"
+}
diff --git a/gateway-release/home/bin/gateway.sh b/gateway-release/home/bin/gateway.sh
index bbefe46..d059274 100755
--- a/gateway-release/home/bin/gateway.sh
+++ b/gateway-release/home/bin/gateway.sh
@@ -35,9 +35,6 @@ APP_BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 # The app's jar name
 APP_JAR="$APP_BIN_DIR/gateway.jar"
 
-# The app's home dir
-APP_HOME_DIR=`dirname $APP_BIN_DIR`
-
 # The app's conf dir
 DEFAULT_APP_CONF_DIR="$APP_HOME_DIR/conf"
 APP_CONF_DIR=${KNOX_GATEWAY_CONF_DIR:-$DEFAULT_APP_CONF_DIR}
@@ -59,9 +56,6 @@ APP_MEM_OPTS="$KNOX_GATEWAY_MEM_OPTS"
 # The app's debugging options
 APP_DBG_OPTS="$KNOX_GATEWAY_DBG_OPTS"
 
-# The app's PID
-APP_PID=0
-
 #dynamic library path
 DEFAULT_JAVA_LIB_PATH="-Djava.library.path=$APP_HOME_DIR/ext/native"
 APP_JAVA_LIB_PATH=${KNOX_GATEWAY_JAVA_LIB_PATH:-$DEFAULT_JAVA_LIB_PATH}
@@ -75,11 +69,10 @@ APP_PID_FILE="$APP_PID_DIR/$APP_NAME.pid"
 APP_OUT_FILE="$APP_LOG_DIR/$APP_NAME.out"
 APP_ERR_FILE="$APP_LOG_DIR/$APP_NAME.err"
 
-# The start wait time
-APP_START_WAIT_TIME=2
+DEFAULT_APP_RUNNING_IN_FOREGROUND="$GATEWAY_SERVER_RUN_IN_FOREGROUND"
+APP_RUNNING_IN_FOREGROUND=${KNOX_GATEWAY_RUNNING_IN_FOREGROUND:-DEFAULT_APP_RUNNING_IN_FOREGROUND}
 
-# The kill wait time limit
-APP_KILL_WAIT_TIME=10
+APP_JAVA_OPTS="$APP_JAVA_LIB_PATH $APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS"
 
 function main {
    checkJava
@@ -92,6 +85,7 @@ function main {
          if [ "$2" = "--printEnv" ]; then
            printEnv
          fi
+         checkEnv
          appStart
          ;;
       stop)   
@@ -118,149 +112,6 @@ function setupEnv {
    return 0
 }
 
-function appStart {
-   checkEnv
-
-   if [ "$GATEWAY_SERVER_RUN_IN_FOREGROUND" == true ]; then
-      $JAVA $APP_JAVA_LIB_PATH $APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS -jar $APP_JAR
-   else
-      getPID
-      if [ "$?" -eq "0" ]; then
-         printf "$APP_LABEL is already running with PID $APP_PID.\n"
-         exit 0
-      fi
-  
-      printf "Starting $APP_LABEL "
-   
-      rm -f $APP_PID_FILE
-
-      nohup $JAVA $APP_JAVA_LIB_PATH $APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS -jar $APP_JAR >>$APP_OUT_FILE 2>>$APP_ERR_FILE & printf $!>$APP_PID_FILE || exit 1
-
-      getPID
-      for ((i=0; i<APP_START_WAIT_TIME*10; i++)); do
-         appIsRunning $APP_PID
-         if [ "$?" -eq "0" ]; then break; fi
-         sleep 0.1
-      done
-      appIsRunning $APP_PID
-      if [ "$?" -ne "1" ]; then
-         printf "failed.\n"
-         rm -f $APP_PID_FILE
-         exit 1
-      fi
-      printf "succeeded with PID $APP_PID.\n"
-      return 0
-   fi
-}
-
-function appStop {
-   getPID
-   appIsRunning $APP_PID
-   if [ "$?" -eq "0" ]; then
-     printf "$APP_LABEL is not running.\n"
-     rm -f $APP_PID_FILE
-     return 0
-   fi
-  
-   printf "Stopping $APP_LABEL with PID $APP_PID "
-   appKill $APP_PID >>$APP_OUT_FILE 2>>$APP_ERR_FILE
-
-   if [ "$?" -ne "0" ]; then
-     printf "failed. \n"
-     exit 1
-   else
-     rm -f $APP_PID_FILE
-     printf "succeeded.\n"
-     return 0
-   fi
-}
-
-function appStatus {
-   printf "$APP_LABEL "
-   getPID
-   if [ "$?" -eq "1" ]; then
-     printf "is not running. No PID file found.\n"
-     return 0
-   fi
-
-   appIsRunning $APP_PID
-   if [ "$?" -eq "1" ]; then
-     printf "is running with PID $APP_PID.\n"
-     exit 1
-   else
-     printf "is not running.\n"
-     return 0
-   fi
-}
-
-# Removed the app PID file if app is not run
-function appClean {
-   getPID
-   appIsRunning $APP_PID
-   if [ "$?" -eq "0" ]; then
-     deleteLogFiles
-     return 0
-   else
-     printf "Can't clean files.  $APP_LABEL is running with PID $APP_PID.\n"
-     exit 1
-   fi
-}
-
-function appKill {
-   local localPID=$1
-   kill $localPID || return 1
-   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
-      appIsRunning $localPID
-      if [ "$?" -eq "0" ]; then return 0; fi
-      sleep 0.1
-   done
-
-   kill -s KILL $localPID || return 1
-   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
-      appIsRunning $localPID
-      if [ "$?" -eq "0" ]; then return 0; fi
-      sleep 0.1
-   done
-
-   return 1
-}
-
-# Returns 0 if the app is running and sets the $PID variable.
-function getPID {
-   if [ ! -d $APP_PID_DIR ]; then
-      printf "Can't find PID dir.\n"
-      exit 1
-   fi
-   if [ ! -f $APP_PID_FILE ]; then
-     APP_PID=0
-     return 1
-   fi
-   
-   APP_PID="$(<$APP_PID_FILE)"
-
-   ps -p $APP_PID > /dev/null
-   # if the exit code was 1 then it isn't running
-   # and it is safe to start
-   if [ "$?" -eq "1" ];
-   then
-     return 1
-   fi
-
-   return 0
-}
-
-function appIsRunning {
-   if [ "$1" -eq "0" ]; then return 0; fi
-
-   ps -p $1 > /dev/null
-
-   if [ "$?" -eq "1" ]; then
-     return 0
-   else
-     return 1
-   fi
-}
-
 function checkReadDir {
     if [ ! -e "$1" ]; then
         printf "Directory $1 does not exist.\n"
@@ -299,17 +150,6 @@ function checkEnv {
     checkWriteDir $APP_PID_DIR
 }
 
-function deleteLogFiles {
-     rm -f $APP_PID_FILE
-     printf "Removed the $APP_LABEL PID file: $APP_PID_FILE.\n"
-     
-     rm -f $APP_OUT_FILE
-     printf "Removed the $APP_LABEL OUT file: $APP_OUT_FILE.\n"
-     
-     rm -f $APP_ERR_FILE
-     printf "Removed the $APP_LABEL ERR file: $APP_ERR_FILE.\n"
-}
-
 function printHelp {
    $JAVA -jar $APP_JAR -help
    return 0
diff --git a/gateway-release/home/bin/ldap.sh b/gateway-release/home/bin/ldap.sh
index bca41b7..aac5c04 100755
--- a/gateway-release/home/bin/ldap.sh
+++ b/gateway-release/home/bin/ldap.sh
@@ -35,9 +35,6 @@ APP_JAR="$APP_BIN_DIR/ldap.jar"
 # Source common functions
 . $APP_BIN_DIR/knox-functions.sh
 
-# The app's home dir
-APP_HOME_DIR=`dirname $APP_BIN_DIR`
-
 # The app's conf dir
 DEFAULT_APP_CONF_DIR="$APP_HOME_DIR/conf"
 APP_CONF_DIR=${KNOX_LDAP_CONF_DIR:-$DEFAULT_APP_CONF_DIR}
@@ -55,9 +52,6 @@ APP_MEM_OPTS="$KNOX_LDAP_MEM_OPTS"
 # The app's debugging options
 APP_DBG_OPTS="$KNOX_LDAP_DBG_OPTS"
 
-# The app's PID
-APP_PID=0
-
 # The name of the PID file
 DEFAULT_APP_PID_DIR="$APP_HOME_DIR/pids"
 APP_PID_DIR=${KNOX_LDAP_PID_DIR:-$DEFAULT_APP_PID_DIR}
@@ -67,11 +61,11 @@ APP_PID_FILE="$APP_PID_DIR/$APP_NAME.pid"
 APP_OUT_FILE="$APP_LOG_DIR/$APP_NAME.out"
 APP_ERR_FILE="$APP_LOG_DIR/$APP_NAME.err"
 
-# The start wait time
-APP_START_WAIT_TIME=2
+DEFAULT_APP_RUNNING_IN_FOREGROUND="$LDAP_SERVER_RUN_IN_FOREGROUND"
+APP_RUNNING_IN_FOREGROUND=${KNOX_LDAP_RUNNING_IN_FOREGROUND:-DEFAULT_APP_RUNNING_IN_FOREGROUND}
 
-# The kill wait time limit
-APP_KILL_WAIT_TIME=10
+# JAVA options used by the JVM
+APP_JAVA_OPTS="$APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS"
 
 function main {
    checkJava
@@ -81,7 +75,8 @@ function main {
          if [ "$2" = "--printEnv" ]; then
            printEnv
          fi
-         appStart
+         createLogFiles
+         appStart $APP_CONF_DIR
          ;;
       stop)   
          appStop
@@ -98,149 +93,6 @@ function main {
    esac
 }
 
-function appStart {
-   createLogFiles
-
-   if [ "$LDAP_SERVER_RUN_IN_FOREGROUND" == true ]; then
-      $JAVA $APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS -jar $APP_JAR $APP_CONF_DIR
-   else
-      getPID
-      if [ $? -eq 0 ]; then
-         printf "$APP_LABEL is already running with PID $APP_PID.\n"
-         exit 0
-      fi
-  
-      printf "Starting $APP_LABEL "
-   
-      rm -f $APP_PID_FILE
-
-      nohup $JAVA $APP_MEM_OPTS $APP_DBG_OPTS $APP_LOG_OPTS -jar $APP_JAR $APP_CONF_DIR >>$APP_OUT_FILE 2>>$APP_ERR_FILE & printf $!>$APP_PID_FILE || exit 1
-
-      getPID
-      for ((i=0; i<APP_START_WAIT_TIME*10; i++)); do
-         appIsRunning $APP_PID
-         if [ $? -eq 0 ]; then break; fi
-         sleep 0.1
-      done
-      appIsRunning $APP_PID
-      if [ $? -ne 1 ]; then
-         printf "failed.\n"
-         rm -f $APP_PID_FILE
-         exit 1
-      fi
-      printf "succeeded with PID $APP_PID.\n"
-      return 0
-   fi
-}
-
-function appStop {
-   getPID
-   appIsRunning $APP_PID
-   if [ $? -eq 0 ]; then
-     printf "$APP_LABEL is not running.\n"
-     rm -f $APP_PID_FILE
-     return 0
-   fi
-  
-   printf "Stopping $APP_LABEL with PID $APP_PID "
-   appKill $APP_PID >>$APP_OUT_FILE 2>>$APP_ERR_FILE
-
-   if [ $? -ne 0 ]; then 
-     printf "failed. \n"
-     exit 1
-   else
-     rm -f $APP_PID_FILE
-     printf "succeeded.\n"
-     return 0
-   fi
-}
-
-function appStatus {
-   printf "$APP_LABEL "
-   getPID
-   if [ $? -eq 1 ]; then
-     printf "is not running. No PID file found.\n"
-     return 0
-   fi
-
-   appIsRunning $APP_PID
-   if [ $? -eq 1 ]; then
-     printf "is running with PID $APP_PID.\n"
-     exit 1
-   else
-     printf "is not running.\n"
-     return 0
-   fi
-}
-
-# Removed the PID file if app is not run
-function appClean {
-   getPID
-   appIsRunning $APP_PID
-   if [ $? -eq 0 ]; then 
-     deleteLogFiles
-     return 0
-   else
-     printf "Can't clean files.  $APP_LABEL is running with PID $APP_PID.\n"
-     exit 1
-   fi
-}
-
-function appKill {
-   local localPID=$1
-   kill $localPID || return 1
-   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
-      appIsRunning $localPID
-      if [ $? -eq 0 ]; then return 0; fi
-      sleep 0.1
-   done
-
-   kill -s KILL $localPID || return 1
-   for ((i=0; i<APP_KILL_WAIT_TIME*10; i++)); do
-      appIsRunning $localPID
-      if [ $? -eq 0 ]; then return 0; fi
-      sleep 0.1
-   done
-
-   return 1
-}
-
-# Returns 0 if the app is running and sets the $APP_PID variable.
-function getPID {
-   if [ ! -d $APP_PID_DIR ]; then
-      printf "Can't find pid dir.\n"
-      exit 1
-   fi
-   if [ ! -f $APP_PID_FILE ]; then
-     APP_PID=0
-     return 1
-   fi
-   
-   APP_PID="$(<$APP_PID_FILE)"
-
-   ps -p $APP_PID > /dev/null
-   # if the exit code was 1 then it isn't running
-   # and it is safe to start
-   if [ "$?" -eq "1" ];
-   then
-     return 1
-   fi
-      
-   return 0
-}
-
-function appIsRunning {
-   if [ $1 -eq 0 ]; then return 0; fi
-
-   ps -p $1 > /dev/null
-
-   if [ $? -eq 1 ]; then
-     return 0
-   else
-     return 1
-   fi
-}
-
 function createLogFiles {
    if [ ! -d "$APP_LOG_DIR" ]; then
       printf "Can't find log dir.\n"
@@ -250,17 +102,6 @@ function createLogFiles {
    if [ ! -f "$APP_ERR_FILE" ]; then touch $APP_ERR_FILE; fi   
 }
 
-function deleteLogFiles {
-     rm -f $APP_PID_FILE
-     printf "Removed the $APP_LABEL PID file: $APP_PID_FILE.\n"
-     
-     rm -f $APP_OUT_FILE
-     printf "Removed the $APP_LABEL OUT file: $APP_OUT_FILE.\n"
-     
-     rm -f $APP_ERR_FILE
-     printf "Removed the $APP_LABEL ERR file: $APP_ERR_FILE.\n"
-}
-
 function printHelp {
    printf "Usage: $0 {start|stop|status|clean}\n"
    return 0