-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdopaas-ctl.sh
188 lines (177 loc) · 6.73 KB
/
dopaas-ctl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <Wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */
# @see: http://www.huati365.com/answer/j6BxQYLqYVeWe4k
[ -z "$currDir" ] && export currDir=$(echo "$(cd "`dirname "$0"`"/; pwd)")
. ${currDir}/deploy-common.sh
[ -n "$(command -v clear)" ] && clear # e.g centos8+ not clear
log ""
log "「 Welcome to DoPaaS Ctl (Host) 」"
log " ___ ___ ___ "
log " | . \ ___ | . \ ___ ___ / __>"
log " | | |/ . \| _/<_> |<_> |\__ \\"
log " |___/\___/|_| <___|<___|<___/"
log ""
log " Wiki: https://github.com/wl4g/dopaas/blob/master/README.md"
log " Wiki(CN): https://gitee.com/wl4g/dopaas/blob/master/README_CN.md"
log " Authors: <Wanglsir@gmail.com, 983708408@qq.com>"
log " Version: 2.0.0"
log " Time: $(date '+%Y-%m-%d %H:%M:%S')"
log " Installation logs writing: $logFile"
log " -----------------------------------------------------------------------"
log ""
function doCommandApps() {
local cmd=$1 # Requires
local targetAppName=$2 # Optional
[[ -z "$cmd" ]] && logErr "Do ctl command is requires!" && exit -1
local deployBuildModulesSize=0
if [ "$runtimeMode" == "standalone" ]; then
local deployBuildModules=("${deployStandaloneBuildModules[@]}") # Copy build targets array
elif [ "$runtimeMode" == "cluster" ]; then # The 'cluster' mode is deploy to the remote hosts
local deployBuildModules=("${deployClusterBuildModules[@]}") # Copy build targets array
else
logErr "Invalid config runtime mode: $runtimeMode"; exit -1
fi
deployBuildModules[${#deployBuildModules[@]}]="$deployEurekaBuildModule"
deployBuildModulesSize=${#deployBuildModules[@]}
if [ $deployBuildModulesSize -gt 0 ]; then
# Check target appName is invalid. (if necessary)
if [ -n "$targetAppName" ]; then
local found="false"
for ((i=0;i<${#deployBuildModules[@]};i++)) do
local buildModule=${deployBuildModules[i]}
local appName=$(echo "$buildModule"|awk -F ',' '{print $1}')
if [ "$targetAppName" == "$appName" ]; then
found="true"
break
fi
done
[ "$found" == "false" ] && logErr "Unknown appName '$targetAppName' !" && exit -1
fi
# Execution commands to remote.
for ((i=0;i<${#deployBuildModules[@]};i++)) do
local buildModule=${deployBuildModules[i]}
local appName=$(echo "$buildModule"|awk -F ',' '{print $1}')
local appPort=$(echo "$buildModule"|awk -F ',' '{print $2}')
# Matching target appName. (if necessary)
if [[ -n "$targetAppName" && "$targetAppName" != "$appName" ]]; then
continue
fi
# Exec remote commands to nodes of appName.
{
local count=-1
for node in `cat $deployClusterNodesConfigPath`; do
((count+=1))
if [[ $count == 0 || -n "$(echo $node|grep -E '^#')" ]]; then
continue # Skip head or annotation rows.
fi
# Extract node info & trim
local host=$(echo $node|awk -F ',' '{print $1}'|sed -e 's/^\s*//' -e 's/\s*$//')
local user=$(echo $node|awk -F ',' '{print $2}'|sed -e 's/^\s*//' -e 's/\s*$//')
local passwd=$(echo $node|awk -F ',' '{print $3}'|sed -e 's/^\s*//' -e 's/\s*$//')
if [[ "$host" == "" || "$user" == "" ]]; then
logErr "[$appName:$appPort/cluster] Invalid cluster node info, host/user is required! host: $host, user: $user, password: $passwd"; exit -1
fi
log "[$appName:$appPort/$host] Manage ($cmd) on $host ..."
doRemoteCmd "$user" "$passwd" "$host" "[ -n \"$(command -v systemctl)\" ] && systemctl ${cmd} ${appName} || /etc/init.d/${appName}.service ${cmd}" "false" "true" &
done
wait
} &
done
fi
}
function doCommandZookeeper() {
local cmd=$1
[[ -z "$cmd" ]] && logErr "Do ctl command is requires!" && exit -1
local count=-1
for node in `cat $deployClusterNodesConfigPath`; do
((count+=1))
if [[ $count == 0 || -n "$(echo $node|grep -E '^#')" ]]; then
continue # Skip head or annotation rows.
fi
# Extract node info & trim
local host=$(echo $node|awk -F ',' '{print $1}'|sed -e 's/^\s*//' -e 's/\s*$//')
local user=$(echo $node|awk -F ',' '{print $2}'|sed -e 's/^\s*//' -e 's/\s*$//')
local passwd=$(echo $node|awk -F ',' '{print $3}'|sed -e 's/^\s*//' -e 's/\s*$//')
if [[ "$host" == "" || "$user" == "" ]]; then
logErr "[zookeeper/$host] Invalid cluster node info, host/user is required! host: $host, user: $user, password: $passwd"; exit -1
fi
log "[zookeeper/$host] Manage ($cmd) on $host ..."
doRemoteCmd "$user" "$passwd" "$host" "sudo ${zkHome}/bin/zkServer.sh ${cmd}" "false" "true" &
done
wait
}
function waitForComplete() {
local arg1=$1
local arg2=$2
wait
log "--------------------------------------------------------------------"
log "Execution $arg1 $arg2 to all nodes finished !"
exit 0
}
function usage() {
echo "
Usage: {start-all|stop-all|restart-all|status-all|<appName> <start|stop|restart|status>}
start-all Start all remote nodes apps.
stop-all Stop all remote nodes apps.
restart-all Restart all remote nodes apps.
status-all Query status all remote nodes apps.
<appName> <start|stop|restart|status> for example: {cmdb-facade restart}
Restart only the cmdb-facade application of all remote nodes,
The optionals are: zookeeper/eureka-server/iam-web/iam-facade/iam-data/cmdb-facade/cmdb-manager/... etc.
"
}
# ----- Main call. -----
arg1=$1
arg2=$2
case $arg1 in
help|-help|--help|-h)
usage
;;
status-all)
doCommandApps "status"
doCommandZookeeper "status"
waitForComplete "status"
;;
start-all)
doCommandApps "start"
doCommandZookeeper "start"
waitForComplete "start"
;;
stop-all)
doCommandApps "stop"
doCommandZookeeper "stop"
waitForComplete "stop"
;;
restart-all)
doCommandApps "restart"
doCommandZookeeper "restart"
waitForComplete "restart"
;;
*)
if [[ -z "$arg1" || -z "$arg2" ]]; then
usage; exit -1
fi
if [ "$arg1" == "zookeeper" ]; then
doCommandZookeeper "$arg2"
else
doCommandApps "$arg2" "$arg1"
fi
waitForComplete "$arg1 $arg2"
;;
*)
exit -1
esac