Skip to content

Commit 9d49514

Browse files
committed
Optimize application startup script #399
1 parent e633ca6 commit 9d49514

10 files changed

+960
-0
lines changed

sbin/common.sh

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#!/bin/sh
2+
#
3+
# Copyright 2019 WeBank
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
#Actively load user env
19+
source ~/.bash_profile
20+
21+
export local_host="`hostname --fqdn`"
22+
23+
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
24+
25+
function isLocal(){
26+
if [ "$1" == "127.0.0.1" ];then
27+
return 0
28+
elif [ "$1" == "" ]; then
29+
return 0
30+
elif [ "$1" == "localhost" ]; then
31+
return 0
32+
elif [ "$1" == $local_host ]; then
33+
return 0
34+
elif [ "$1" == $ipaddr ]; then
35+
return 0
36+
fi
37+
return 1
38+
}
39+
40+
function executeCMD(){
41+
isLocal $1
42+
flag=$?
43+
if [ $flag == "0" ];then
44+
echo "Is local execution:$2"
45+
eval $2
46+
else
47+
echo "Is remote execution:$2"
48+
ssh -p $SSH_PORT $1 $2
49+
fi
50+
51+
}
52+
function copyFile(){
53+
isLocal $1
54+
flag=$?
55+
src=$2
56+
dest=$3
57+
if [ $flag == "0" ];then
58+
echo "Is local cp "
59+
cp -r "$src" "$dest"
60+
else
61+
echo "Is remote cp "
62+
scp -r -P $SSH_PORT "$src" $1:"$dest"
63+
fi
64+
65+
}
66+
67+
function isSuccess(){
68+
if [ $? -ne 0 ]; then
69+
echo "Failed to " + $1
70+
exit 1
71+
else
72+
echo "Succeed to" + $1
73+
fi
74+
}
75+
76+
77+
function start()
78+
{
79+
echo "Start to check whether the $SERVER_NAME is running"
80+
if [[ -f "${SERVER_PID}" ]]; then
81+
pid=$(cat ${SERVER_PID})
82+
if kill -0 ${pid} >/dev/null 2>&1; then
83+
echo "$SERVER_NAME is already running."
84+
exit 1
85+
fi
86+
fi
87+
export SERVER_START_BIN=$DSS_HOME/sbin/ext/$SERVER_NAME
88+
if [[ ! -f "${SERVER_START_BIN}" ]]; then
89+
echo "The $SERVER_NAME is wrong or the corresponding startup script does not exist: "
90+
echo "$SERVER_START_BIN"
91+
exit 1
92+
else
93+
echo "Start to start server, startup script: $SERVER_START_BIN"
94+
sh $SERVER_START_BIN
95+
fi
96+
}
97+
98+
function wait_for_server_to_die() {
99+
local pid
100+
local count
101+
pid=$1
102+
timeout=$2
103+
count=0
104+
timeoutTime=$(date "+%s")
105+
let "timeoutTime+=$timeout"
106+
currentTime=$(date "+%s")
107+
forceKill=1
108+
109+
while [[ $currentTime -lt $timeoutTime ]]; do
110+
$(kill ${pid} > /dev/null 2> /dev/null)
111+
if kill -0 ${pid} > /dev/null 2>&1; then
112+
sleep 3
113+
else
114+
forceKill=0
115+
break
116+
fi
117+
currentTime=$(date "+%s")
118+
done
119+
120+
if [[ forceKill -ne 0 ]]; then
121+
$(kill -9 ${pid} > /dev/null 2> /dev/null)
122+
fi
123+
}
124+
125+
126+
function stop()
127+
{
128+
if [[ ! -f "${SERVER_PID}" ]]; then
129+
echo "server $SERVER_NAME is not running"
130+
else
131+
pid=$(cat ${SERVER_PID})
132+
if [[ -z "${pid}" ]]; then
133+
echo "server $SERVER_NAME is not running"
134+
else
135+
wait_for_server_to_die $pid 40
136+
$(rm -f ${SERVER_PID})
137+
echo "server $SERVER_NAME is stopped."
138+
fi
139+
fi
140+
}
141+
142+
function restart()
143+
{
144+
stop
145+
sleep 2
146+
start
147+
}
148+
149+
status()
150+
{
151+
if [[ ! -f "${SERVER_PID}" ]]; then
152+
echo "server $SERVER_NAME is stopped"
153+
else
154+
pid=$(cat ${SERVER_PID})
155+
if [[ -z "${pid}" ]]; then
156+
echo "server $SERVER_NAME is not running"
157+
else
158+
echo "server $SERVER_NAME is running."
159+
fi
160+
fi
161+
}
162+
163+
function setServerName(){
164+
if [[ $PROJECT_NAME == *"project"* ]]; then
165+
SERVER_NAME=dss-framework-project-server
166+
elif [[ $PROJECT_NAME == *"orchestrator"* ]]; then
167+
SERVER_NAME=dss-framework-orchestrator-server
168+
elif [[ $PROJECT_NAME == *"apiservice"* ]]; then
169+
SERVER_NAME=dss-apiservice-server
170+
elif [[ $PROJECT_NAME == *"datapipe"* ]]; then
171+
SERVER_NAME=dss-datapipe-server
172+
elif [[ $PROJECT_NAME == *"workflow"* ]]; then
173+
SERVER_NAME=dss-workflow-server
174+
elif [[ $PROJECT_NAME == *"execution"* ]]; then
175+
SERVER_NAME=dss-flow-execution-server
176+
else
177+
echo "please input: sh dss-daemon.sh [start,restart,stop] [server name]; for example : sh dss-daemon.sh restart project "
178+
echo "server name : project、orchestrator、apiservice、datapipe、workflow、execution"
179+
exit 1
180+
fi
181+
}
182+

sbin/dss-daemon.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
#
3+
# description: Starts and stops Server
4+
#
5+
# @name: dss-demo
6+
# @author: peacewong
7+
# @created: 01.16.2021
8+
#
9+
# Modified for dss 1.0.0
10+
11+
12+
cd `dirname $0`
13+
cd ..
14+
INSTALL_HOME=`pwd`
15+
16+
function print_usage(){
17+
echo "Usage: dss-daemon [start | stop | restart | status] [serverName]"
18+
echo " serverName The service name of the operation"
19+
echo "Most commands print help when invoked w/o parameters."
20+
}
21+
22+
if [ $# != 2 ]; then
23+
print_usage
24+
exit 2
25+
fi
26+
27+
# set DSS_HOME
28+
if [ "$DSS_HOME" = "" ]; then
29+
export DSS_HOME=$INSTALL_HOME
30+
fi
31+
32+
# set DSS_CONF_DIR
33+
if [ "$DSS_CONF_DIR" = "" ]; then
34+
export DSS_CONF_DIR=$DSS_HOME/conf
35+
fi
36+
37+
# get pid directory
38+
if [ "$DSS_PID_DIR" = "" ]; then
39+
export DSS_PID_DIR="$DSS_HOME/pid"
40+
fi
41+
if [ ! -w "$DSS_PID_DIR" ] ; then
42+
mkdir -p "$DSS_PID_DIR"
43+
fi
44+
source $DSS_HOME/sbin/common.sh
45+
source $DSS_HOME/conf/config.sh
46+
typeset -l PROJECT_NAME
47+
PROJECT_NAME=$2
48+
49+
## get project full name
50+
setServerName
51+
52+
COMMAND=$1
53+
export SERVER_PID=$DSS_PID_DIR/$SERVER_NAME.pid
54+
case $COMMAND in
55+
start|stop|restart|status)
56+
$COMMAND $SERVER_NAME
57+
;;
58+
*)
59+
print_usage
60+
exit 2
61+
;;
62+
esac

sbin/dss-start-all.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2019 WeBank
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Start all dss applications
19+
info="We will start all dss applications, it will take some time, please wait"
20+
echo ${info}
21+
22+
#Actively load user env
23+
24+
25+
cd `dirname $0`
26+
cd ..
27+
INSTALL_HOME=`pwd`
28+
29+
# set DSS_HOME
30+
if [ "$DSS_HOME" = "" ]; then
31+
export DSS_HOME=$INSTALL_HOME
32+
fi
33+
34+
# set DSS_CONF_DIR
35+
if [ "$DSS_CONF_DIR" = "" ]; then
36+
export DSS_CONF_DIR=$DSS_HOME/conf
37+
fi
38+
39+
local_host="`hostname --fqdn`"
40+
source $DSS_HOME/sbin/common.sh
41+
source $DSS_HOME/conf/config.sh
42+
43+
function startApp(){
44+
echo "<-------------------------------->"
45+
echo "Begin to start $SERVER_NAME"
46+
SERVER_START_CMD="sh $DSS_INSTALL_HOME/sbin/dss-daemon.sh restart $SERVER_NAME"
47+
if test -z "$SERVER_IP"
48+
then
49+
SERVER_IP=$local_host
50+
fi
51+
executeCMD $SERVER_IP "$SERVER_START_CMD"
52+
echo "End to start $SERVER_NAME"
53+
echo "<-------------------------------->"
54+
sleep 1
55+
}
56+
57+
function checkServer() {
58+
echo "<-------------------------------->"
59+
echo "Begin to check $SERVER_NAME"
60+
SERVER_CHECK_CMD="sh $DSS_HOME/sbin/dss-daemon.sh status $SERVER_NAME"
61+
if test -z "$SERVER_IP"
62+
then
63+
SERVER_IP=$local_host
64+
fi
65+
66+
executeCMD $SERVER_IP "$SERVER_CHECK_CMD"
67+
68+
if [ $? -ne 0 ]; then
69+
ALL_SERVER_NAME=$SERVER_NAME
70+
LOG_PATH=$DSS_HOME/logs/$ALL_SERVER_NAME.log
71+
echo "ERROR: your $ALL_SERVER_NAME microservice is not start successful !!! ERROR logs as follows :"
72+
echo "Please check detail log, log path :$LOG_PATH"
73+
echo '<---------------------------------------------------->'
74+
executeCMD $ALL_SERVER_NAME "tail -n 50 $LOG_PATH"
75+
echo '<---------------------------------------------------->'
76+
echo "Please check detail log, log path :$LOG_PATH"
77+
exit 1
78+
fi
79+
echo "<-------------------------------->"
80+
sleep 3
81+
}
82+
83+
84+
function startDssProject(){
85+
SERVER_NAME=dss-framework-project-server
86+
SERVER_IP=$DSS_FRAMEWORK_PROJECT_SERVER_INSTALL_IP
87+
startApp
88+
89+
SERVER_NAME=dss-framework-orchestrator-server
90+
SERVER_IP=$DSS_FRAMEWORK_ORCHESTRATOR_SERVER_INSTALL_IP
91+
startApp
92+
93+
SERVER_NAME=dss-apiservice-server
94+
SERVER_IP=$DSS_APISERVICE_SERVER_INSTALL_IP
95+
startApp
96+
97+
SERVER_NAME=dss-datapipe-server
98+
SERVER_IP=$DSS_DATAPIPE_SERVER_INSTALL_IP
99+
startApp
100+
101+
SERVER_NAME=dss-workflow-server
102+
SERVER_IP=$DSS_WORKFLOW_SERVER_INSTALL_IP
103+
startApp
104+
105+
SERVER_NAME=dss-flow-execution-server
106+
SERVER_IP=$DSS_FLOW_EXECUTION_SERVER_INSTALL_IP
107+
startApp
108+
}
109+
110+
function checkDssService(){
111+
SERVER_NAME=dss-framework-project-server
112+
SERVER_IP=$DSS_FRAMEWORK_PROJECT_SERVER_INSTALL_IP
113+
checkServer
114+
115+
SERVER_NAME=dss-framework-orchestrator-server
116+
SERVER_IP=$DSS_FRAMEWORK_ORCHESTRATOR_SERVER_INSTALL_IP
117+
checkServer
118+
119+
SERVER_NAME=dss-apiservice-server
120+
SERVER_IP=$DSS_APISERVICE_SERVER_INSTALL_IP
121+
checkServer
122+
123+
SERVER_NAME=dss-datapipe-server
124+
SERVER_IP=$DSS_DATAPIPE_SERVER_INSTALL_IP
125+
checkServer
126+
127+
SERVER_NAME=dss-workflow-server
128+
SERVER_IP=$DSS_WORKFLOW_SERVER_INSTALL_IP
129+
checkServer
130+
131+
SERVER_NAME=dss-flow-execution-server
132+
SERVER_IP=$DSS_FLOW_EXECUTION_SERVER_INSTALL_IP
133+
checkServer
134+
}
135+
136+
137+
138+
startDssProject
139+
checkDssService

0 commit comments

Comments
 (0)