-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha10cli_v3
executable file
·275 lines (250 loc) · 6.38 KB
/
a10cli_v3
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
# If there exist a10cli.conf variable override files use it...
[ -f a10cli.ini ] && . a10cli.ini
[ -f scripts/a10cli.ini ] && . scripts/a10cli.ini
[ -f /opt/.a10cli.ini ] && . /opt/.a10cli.ini
[ -f /.a10cli.ini ] && . /.a10cli.ini
MAP_INTERFACES=(ens3 eth0)
USERNAME=changeme
PASSWORD=changeme
FORMAT=json
VERSION=v3
CURL_TIMEOUT=3
CURR_RETRY=120
CURR_TIMEOUT=1
# Environment setting
PROD_LB=172.30.1.251
PROD_IPCLASS=172.30
STG_LB=172.16.1.251
STG_IPCLASS=172.16
DEV_LB=10.0.1.251
DEV_IPCLASS=10.0
# -----------------------------------
showVersion(){
echo "A10 command line v3.0 version."
echo "Mapping A10 API v3 only support real server control."
}
helpMsg(){
echo ""
echo -e "Usage: $0 [option]"
echo ""
echo -e "[option]"
echo -e " stop,start,restart select one active."
echo -e " overconn confirm the connection until 0"
echo ""
exit 1
}
MapInterface() {
SOURCE_INTERFACES=($(ls /sys/class/net/))
for (( i = 0; i < ${#MAP_INTERFACES[@]}; i++ )); do
for (( j = 0; j < ${#SOURCE_INTERFACES[@]}; j++ )); do
if [[ ${MAP_INTERFACES[i]} == ${SOURCE_INTERFACES[j]} ]]; then
INTERFACE=$SOURCE_INTERFACES
fi
done
done
}
GetENV(){
if [ ! -z $ENV ]; then
if [ $ENV == 'prod' ]; then
LB=$PROD_LB
IPCLASS=$PROD_IPCLASS
elif [ $ENV == 'staging' ]; then
LB=$STG_LB
IPCLASS=$STG_IPCLASS
elif [ $ENV == 'dev' ]; then
LB=$DEV_LB
IPCLASS=$DEV_IPCLASS
else
echo "[fail] Can not get env type."
exit 1
fi
else
echo "[fail] Can not get the environment."
exit 1
fi
}
GetIP() {
MapInterface
for (( i = 0; i < ${#INTERFACE[@]}; i++ )); do
SERVICE_IP=$(ifconfig ${INTERFACE[i]} | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}')
if [ ! -z $SERVICE_IP ]; then
break
fi
done
}
GetSession(){
SESSION=$(curl -s -k --connect-timeout $CURL_TIMEOUT "https://${LB}/axapi/${VERSION}/auth" -H "Content-Type:application/json" -d "{ \"credentials\": { \"username\": \"${USERNAME}\", \"password\": \"${PASSWORD}\" } }" | grep \"signature\" | awk -F'"' '{print $4}')
if [ -z $SESSION ]; then
echo [Error]: Did not get a A10 session.
exit 1
fi
}
CloseSession(){
# use session.close
Command "logoff"
if echo $RETURN_DATA | grep OK &>/dev/null ; then
echo session is close.
fi
}
Command(){
CMD=$1
shift
POST=$@
if [ -z $SESSION ]; then
GetSession
fi
CMDURI="https://${LB}/axapi/${VERSION}/${CMD}"
if [ -z "$POST" ]; then
RETURN_DATA=$(curl -s -k --connect-timeout $CURL_TIMEOUT $CMDURI -H "Content-Type:application/json" -H "Authorization: A10 ${SESSION}")
else
RETURN_DATA=$(curl -s -k --connect-timeout $CURL_TIMEOUT $CMDURI -H "Content-Type:application/json" -H "Authorization: A10 ${SESSION}" -d "$POST")
fi
}
GetSLBServer(){
local IP
IP=$1
# use slb.server.search api
Command "slb/server?host=${IP}"
SLB_SERVER=$(echo $RETURN_DATA | awk -F'name' '{print $2}' | awk -F'"' '{print $3}')
if [ -z $SLB_SERVER ]; then
echo "No such Server: ${IP}"
return 2
fi
unset RETURN_DATA
}
GetSLBServer_Status(){
Command "slb/server?name=${SLB_SERVER}"
SLB_SERVER_STATUS=$(echo $RETURN_DATA | awk -F'action' '{print $2}' | awk -F'"' '{print $3}')
unset RETURN_DATA
}
UpdateServerStatus(){
local STATUS
STATUS=$1
# use slb.server.update
Command "slb/server/${SLB_SERVER}" "{ \"server\": { \"action\": \"$STATUS\" } }"
CURR_STATUS=$(echo $RETURN_DATA | awk -F'action' '{print $2}' | awk -F'"' '{print $3}')
if [ $CURR_STATUS == 'enable' ]; then
echo "[200] ${SLB_SERVER} slb.server.action is enable"
elif [ $CURR_STATUS == 'disable' ]; then
echo "[200] ${SLB_SERVER} slb.server.action is disable"
else
echo "[fail] ${SLB_SERVER} slb.server.action is failed."
fi
}
CheckConnectZero(){
# use slb.server.fetchStatistics
for (( i = 0; i < $CURR_RETRY; i++ )); do
Command "slb/server/${SLB_SERVER}/stats"
CURCONNS=$(echo $RETURN_DATA | awk -F'curr_conn":' '{print $2}' | awk -F, '{print $1}')
if [ "$CURCONNS" == 0 ]; then
echo "The current connection is clean"
break
else
echo "The current connection is $CURCONNS, retry $i times."
sleep $CURR_TIMEOUT
if [ $i == $(($CURR_RETRY -1)) ]; then
echo "$SLB_SERVER connect check fail, found $CURCONNS connect."
exit 1
fi
fi
done
unset RETURN_DATA
}
CallWork(){
local IP
IP=$1
GetSLBServer $IP
if [ $? -eq 0 ]; then
GetSLBServer_Status
if [ $SLB_SERVER_STATUS == $UPDATE_STATUS ]; then
echo "The state already exists, skip update"
else
UpdateServerStatus $UPDATE_STATUS
fi
GetSLBServer_Status
echo "Current status: $SLB_SERVER is $SLB_SERVER_STATUS"
fi
}
Run(){
if [ -z $SERVICE_IP ]; then
GetIP
fi
for ipopt in ${SERVICE_IP[@]}; do
CallWork $ipopt
done
}
GetENV
if [[ $# -gt 0 ]]; then
for opt in $@
do
case $opt in
overconn)
shift
if [ -z $SERVICE_IP ]; then
GetIP
fi
for ipopt in ${SERVICE_IP[@]}; do
GetSLBServer $ipopt
if [ $? -eq 0 ]; then
CheckConnectZero
fi
done
;;
start)
shift
UPDATE_STATUS=enable
Run
;;
stop)
shift
UPDATE_STATUS=disable
Run
;;
restart)
shift
UPDATE_STATUS=disable
Run
case "$SLB_SERVER_STATUS" in
0|1)
UPDATE_STATUS=enable
SERVICE_IP=()
Run
;;
*)
echo "SLB server status unknow."
;;
esac
;;
status)
shift
if [ -z $SERVICE_IP ]; then
GetIP
fi
for ipopt in ${SERVICE_IP[@]}; do
GetSLBServer $ipopt
if [ $? -eq 0 ]; then
GetSLBServer_Status
echo "Current status: $SLB_SERVER is $SLB_SERVER_STATUS"
if [ $SLB_SERVER_STATUS == 'enable' ]; then
echo "[200] ${SLB_SERVER} slb.server.action is enable"
elif [ $SLB_SERVER_STATUS == 'disable' ]; then
echo "[200] ${SLB_SERVER} slb.server.action is disable"
fi
fi
done
;;
-v|-V|--version|-version|version)
showVersion
;;
-h|--help|-help|*)
helpMsg
;;
esac
done
else
helpMsg
fi
if [ -n $SESSION ]; then
CloseSession
fi