diff options
author | jacobkeeler <jacob.keeler@livioradio.com> | 2020-06-08 18:10:03 -0400 |
---|---|---|
committer | jacobkeeler <jacob.keeler@livioradio.com> | 2020-06-08 18:11:28 -0400 |
commit | c4547ea1779fec8c97ea30765a52133e53fb4cf1 (patch) | |
tree | f07b3e84d39cbf8c9ad944342d50c004805c5fb2 | |
parent | 3108dc057f2f9a1b53be63970500ed97ed02178a (diff) | |
download | sdl_core-c4547ea1779fec8c97ea30765a52133e53fb4cf1.tar.gz |
Add daemon.sh to unify scripts for external and regularfeature/daemon_script
-rw-r--r-- | src/appMain/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/appMain/core.sh | 71 | ||||
-rw-r--r-- | src/appMain/core_external_proprietary.sh | 135 | ||||
-rw-r--r-- | src/appMain/daemon.sh | 143 |
4 files changed, 155 insertions, 196 deletions
diff --git a/src/appMain/CMakeLists.txt b/src/appMain/CMakeLists.txt index b2d4bf75bf..c2b7255ecc 100644 --- a/src/appMain/CMakeLists.txt +++ b/src/appMain/CMakeLists.txt @@ -255,7 +255,7 @@ else() GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) endif() -install(FILES start.sh DESTINATION bin +install(FILES start.sh daemon.sh DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) diff --git a/src/appMain/core.sh b/src/appMain/core.sh index 423ef206f2..6604368993 100644 --- a/src/appMain/core.sh +++ b/src/appMain/core.sh @@ -1,69 +1,8 @@ #!/bin/bash -cd $(dirname $0) -DIR=$(pwd) +DIR=$(dirname $0) -CORE_PID_FILE=${DIR}/core.pid -CORE_APPLICATION_NAME=smartDeviceLinkCore - -function core_start() { - if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then - echo "Core is already running" - return 1 - elif [ -n "$(pgrep -f $CORE_APPLICATION_NAME)" ]; then - echo "Core is already running outside of this script" - echo "All instances of Core can be stopped using the \"kill\" command" - return 2 - else - echo "Starting SmartDeviceLink Core" - LD_LIBRARY_PATH=$DIR ${DIR}/${CORE_APPLICATION_NAME} & - CORE_PID=$! - echo $CORE_PID > $CORE_PID_FILE - return 0 - fi -} - -function core_stop() { - RESULT=1 - if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then - echo "Stopping SmartDeviceLink Core" - CORE_PID=$(cat $CORE_PID_FILE) - kill $CORE_PID - - # If Core doesn't close normally within 10 seconds, force-close it - timeout 10s tail --pid=$CORE_PID -f /dev/null - if [ -n "$(ps -p $CORE_PID -o pid=)" ]; then - echo "Core did not shut down properly, force-killing" - kill -9 $CORE_PID - fi - RESULT=0 - fi - - if [ -f "$CORE_PID_FILE" ]; then - rm $CORE_PID_FILE - fi - return $RESULT -} - -if [ "$1" == "stop" ]; then - core_stop - if [ "$?" -ne 0 ]; then - echo "Core is not running (or was started outside of this script)" - fi -elif [ "$1" == "restart" ]; then - core_stop - if [ "$?" -eq 0 ]; then - core_start - else - echo "Core is not running (or was started outside of this script)" - fi -elif [ "$1" == "start" ]; then - core_start -elif [ "$1" == "kill" ]; then - core_stop - pkill -9 -f $CORE_APPLICATION_NAME - if [ "$?" -eq 0 ]; then - echo "Killed all lingering instances of SDL Core" - fi -else - echo "usage: core.sh [start/restart/stop/kill]" +EXTERNAL_POLICIES=false +if [ "$2" = "true" ]; then + EXTERNAL_POLICIES=true fi +$DIR/daemon.sh $1 $EXTERNAL_POLICIES diff --git a/src/appMain/core_external_proprietary.sh b/src/appMain/core_external_proprietary.sh index 6ca96c28e0..dc43bfb42c 100644 --- a/src/appMain/core_external_proprietary.sh +++ b/src/appMain/core_external_proprietary.sh @@ -1,132 +1,9 @@ #!/bin/bash -cd $(dirname $0) -DIR=$(pwd) +DIR=$(dirname $0) -CORE_PID_FILE=${DIR}/core.pid -CORE_APPLICATION_NAME=smartDeviceLinkCore -PM_PID_FILE=${DIR}/policy_manager.pid -PM_APPLICATION_NAME=sample_policy_manager.py - -function core_start() { - if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then - echo "Core is already running" - return 1 - elif [ -n "$(pgrep -f $CORE_APPLICATION_NAME)" ]; then - echo "Core is already running outside of this script" - echo "All instances of Core can be stopped using the \"kill\" command" - return 2 - else - echo "Starting SmartDeviceLink Core" - LD_LIBRARY_PATH=$DIR ${DIR}/${CORE_APPLICATION_NAME} & - CORE_PID=$! - echo $CORE_PID > $CORE_PID_FILE - return 0 - fi -} - -function core_stop() { - RESULT=1 - if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then - echo "Stopping SmartDeviceLink Core" - CORE_PID=$(cat $CORE_PID_FILE) - kill $CORE_PID - - # If Core doesn't close normally within 10 seconds, force-close it - timeout 10s tail --pid=$CORE_PID -f /dev/null - if [ -n "$(ps -p $CORE_PID -o pid=)" ]; then - echo "Core did not shut down properly, force-killing" - kill -9 $CORE_PID - fi - RESULT=0 - fi - - if [ -f "$CORE_PID_FILE" ]; then - rm $CORE_PID_FILE - fi - return $RESULT -} - -function pm_install_dependencies() { - pip3 list | grep -F tornado > /dev/null - if [ $? -eq 1 ]; then - echo "Installing tornado python package" - sudo -H pip3 install tornado - fi -} - -function pm_start() { - if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then - echo "Policy Server is already running" - return 1 - elif [ -n "$(pgrep -f $PM_APPLICATION_NAME)" ]; then - echo "Policy Server is already running outside of this script" - echo "All instances of Core can be stopped using the \"kill\" command" - return 2 - else - pm_install_dependencies - echo "Starting Policy Manager" - python3 ${DIR}/${PM_APPLICATION_NAME} --pack_port 8088 --unpack_port 8089 --add_http_header --encryption & - PM_PID=$! - echo $PM_PID > $PM_PID_FILE - return 0 - fi -} - -function pm_stop() { - RESULT=1 - if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then - echo "Stopping Policy Manager" - kill -INT $(cat $PM_PID_FILE) - kill -9 $(cat $PM_PID_FILE) - RESULT=0 - fi - - # Clear PID regardless of whether the process was running or not - if [ -f "$PM_PID_FILE" ]; then - rm $PM_PID_FILE - fi - return $RESULT -} - -if [ "$1" == "stop" ]; then - core_stop - if [ "$?" -ne 0 ]; then - echo "Core is not running (or was started outside of this script)" - fi - - pm_stop - if [ "$?" -ne 0 ]; then - echo "Policy Server is not running (or was started outside of this script)" - fi -elif [ "$1" == "restart" ]; then - core_stop - if [ "$?" -eq 0 ]; then - core_start - else - echo "Core is not running (or was started outside of this script)" - fi - - pm_stop - if [ "$?" -eq 0 ]; then - pm_start - else - echo "Policy Server is not running (or was started outside of this script)" - fi -elif [ "$1" == "start" ]; then - core_start - pm_start -elif [ "$1" == "kill" ]; then - core_stop - pkill -9 -f $CORE_APPLICATION_NAME - if [ "$?" -eq 0 ]; then - echo "Killed all lingering instances of SDL Core" - fi - - pm_stop - pkill -9 -f $PM_APPLICATION_NAME - if [ "$?" -eq 0 ]; then - echo "Killed all lingering instances of the Policy Server" - fi -else - echo "usage: core.sh [start/restart/stop/kill]" +EXTERNAL_POLICIES=true +# Allow for manual override to disable sample policy server +if [ "$2" = "false" ]; then + EXTERNAL_POLICIES=false fi +$DIR/daemon.sh $1 $EXTERNAL_POLICIES diff --git a/src/appMain/daemon.sh b/src/appMain/daemon.sh new file mode 100644 index 0000000000..ed53134dee --- /dev/null +++ b/src/appMain/daemon.sh @@ -0,0 +1,143 @@ +#!/bin/bash +cd $(dirname $0) +COMMAND=$1 +EXTERNAL_POLICIES=$2 +DIR=$(pwd) + +CORE_PID_FILE=${DIR}/core.pid +CORE_APPLICATION_NAME=smartDeviceLinkCore +PM_PID_FILE=${DIR}/policy_manager.pid +PM_APPLICATION_NAME=sample_policy_manager.py + +function core_start() { + if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then + echo "Core is already running" + return 1 + elif [ -n "$(pgrep -f $CORE_APPLICATION_NAME)" ]; then + echo "Core is already running outside of this script" + echo "All instances of Core can be stopped using the \"kill\" command" + return 2 + else + echo "Starting SmartDeviceLink Core" + LD_LIBRARY_PATH=$DIR ${DIR}/${CORE_APPLICATION_NAME} & + CORE_PID=$! + echo $CORE_PID > $CORE_PID_FILE + return 0 + fi +} + +function core_stop() { + RESULT=1 + if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then + echo "Stopping SmartDeviceLink Core" + CORE_PID=$(cat $CORE_PID_FILE) + kill $CORE_PID + + # If Core doesn't close normally within 10 seconds, force-close it + timeout 10s tail --pid=$CORE_PID -f /dev/null + if [ -n "$(ps -p $CORE_PID -o pid=)" ]; then + echo "Core did not shut down properly, force-killing" + kill -9 $CORE_PID + fi + RESULT=0 + fi + + if [ -f "$CORE_PID_FILE" ]; then + rm $CORE_PID_FILE + fi + return $RESULT +} + +function pm_install_dependencies() { + pip3 list | grep -F tornado > /dev/null + if [ $? -eq 1 ]; then + echo "Installing tornado python package" + sudo -H pip3 install tornado + fi +} + +function pm_start() { + if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then + echo "Policy Server is already running" + return 1 + elif [ -n "$(pgrep -f $PM_APPLICATION_NAME)" ]; then + echo "Policy Server is already running outside of this script" + echo "All instances of Core can be stopped using the \"kill\" command" + return 2 + else + pm_install_dependencies + echo "Starting Policy Manager" + python3 ${DIR}/${PM_APPLICATION_NAME} --pack_port 8088 --unpack_port 8089 --add_http_header --encryption & + PM_PID=$! + echo $PM_PID > $PM_PID_FILE + return 0 + fi +} + +function pm_stop() { + RESULT=1 + if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then + echo "Stopping Policy Manager" + kill -INT $(cat $PM_PID_FILE) + kill -9 $(cat $PM_PID_FILE) + RESULT=0 + fi + + # Clear PID regardless of whether the process was running or not + if [ -f "$PM_PID_FILE" ]; then + rm $PM_PID_FILE + fi + return $RESULT +} + +if [ "$COMMAND" == "stop" ]; then + core_stop + if [ "$?" -ne 0 ]; then + echo "Core is not running (or was started outside of this script)" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + if [ "$?" -ne 0 ]; then + echo "Policy Server is not running (or was started outside of this script)" + fi + fi +elif [ "$COMMAND" == "restart" ]; then + core_stop + if [ "$?" -eq 0 ]; then + core_start + else + echo "Core is not running (or was started outside of this script)" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + if [ "$?" -eq 0 ]; then + pm_start + else + echo "Policy Server is not running (or was started outside of this script)" + fi + fi +elif [ "$COMMAND" == "start" ]; then + core_start + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_start + fi +elif [ "$COMMAND" == "kill" ]; then + core_stop + pkill -9 -f $CORE_APPLICATION_NAME + if [ "$?" -eq 0 ]; then + echo "Killed all lingering instances of SDL Core" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + pkill -9 -f $PM_APPLICATION_NAME + if [ "$?" -eq 0 ]; then + echo "Killed all lingering instances of the Policy Server" + fi + fi +else + echo $1 $COMMAND + echo "usage: core.sh [start/restart/stop/kill] [use_sample_policy_manager?]" +fi |