Skip to content

Commit cfbdeba

Browse files
authored
refactor: cloud-sysvmadm script (#9200)
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent a385ff1 commit cfbdeba

File tree

1 file changed

+143
-139
lines changed

1 file changed

+143
-139
lines changed

setup/bindir/cloud-sysvmadm.in

Lines changed: 143 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
# under the License.
1919

2020

21-
. /etc/rc.d/init.d/functions
22-
2321
#set -x
2422

2523
usage() {
2624
usage_content="
27-
The tool for stopping/starting running system vms and domain routers
25+
The tool for stopping/starting running system vms and domain routers. It requires integration API port to be enabled.
2826
2927
Usage: %s: [-d] [-u] [-p] [-m] [-s] [-r] [-a] [-n] [-t] [-l] [-z] [-v] [-i] [-j]
3028
@@ -63,6 +61,7 @@ vmids=""
6361
vmidsclause=""
6462
withids=""
6563
vmtable="vm_instance"
64+
integrationport=0
6665

6766

6867

@@ -104,6 +103,12 @@ do
104103
done
105104

106105

106+
integrationport=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "SELECT value FROM configuration WHERE name='integration.api.port'"`)
107+
if [ $integrationport -le 0 ]; then
108+
echo "Integration port is not enabled!"
109+
exit
110+
fi
111+
107112
prepare_ids_clause() {
108113
if [[ ! -z "$vmidsclause" ]]; then
109114
return
@@ -117,168 +122,167 @@ prepare_ids_clause() {
117122

118123
stop_start_system() {
119124
prepare_ids_clause
120-
secondary=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from $vmtable where state=\"Running\" and type=\"SecondaryStorageVm\"$zone$vmidsclause"`)
121-
console=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from $vmtable where state=\"Running\" and type=\"ConsoleProxy\"$zone$vmidsclause"`)
122-
length_secondary=(${#secondary[@]})
123-
length_console=(${#console[@]})
124-
125-
126-
echo -e "\nStopping and starting $length_secondary secondary storage vm(s)$inzone$withids..."
127-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_secondary secondary storage vm(s)$inzone$withids..." >>$LOGFILE
128-
129-
for d in "${secondary[@]}"; do
130-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Stopping secondary storage vm with id $d" >>$LOGFILE
131-
jobresult=$(send_request stopSystemVm $d)
132-
if [ "$jobresult" != "1" ]; then
133-
echo -e "ERROR: Failed to stop secondary storage vm with id $d \n"
134-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to stop secondary storage vm with id $d" >>$LOGFILE
135-
else
136-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Starting secondary storage vm with id $d" >>$LOGFILE
137-
jobresult=$(send_request startSystemVm $d SSVM)
138-
if [ "$jobresult" != "1" ]; then
139-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start secondary storage vm with id $d" >>$LOGFILE
140-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start secondary storage vm with id $d" >>$LOGFILE
141-
fi
142-
fi
143-
done
125+
secondary=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select uuid from $vmtable where state=\"Running\" and type=\"SecondaryStorageVm\"$zone$vmidsclause"`)
126+
console=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select uuid from $vmtable where state=\"Running\" and type=\"ConsoleProxy\"$zone$vmidsclause"`)
127+
length_secondary=(${#secondary[@]})
128+
length_console=(${#console[@]})
129+
130+
131+
echo -e "\nStopping and starting $length_secondary secondary storage vm(s)$inzone$withids..."
132+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_secondary secondary storage vm(s)$inzone$withids..." >>$LOGFILE
133+
134+
for d in "${secondary[@]}"; do
135+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Stopping secondary storage vm with id $d" >>$LOGFILE
136+
jobresult=$(send_request stopSystemVm $d)
137+
if [ "$jobresult" != "1" ]; then
138+
echo -e "ERROR: Failed to stop secondary storage vm with id $d \n"
139+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to stop secondary storage vm with id $d" >>$LOGFILE
140+
else
141+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Starting secondary storage vm with id $d" >>$LOGFILE
142+
jobresult=$(send_request startSystemVm $d SSVM)
143+
if [ "$jobresult" != "1" ]; then
144+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start secondary storage vm with id $d" >>$LOGFILE
145+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start secondary storage vm with id $d" >>$LOGFILE
146+
fi
147+
fi
148+
done
144149

145-
if [ "$length_secondary" == "0" ];then
146-
echo -e "No running secondary storage vms found \n"
147-
else
148-
echo -e "Done stopping and starting secondary storage vm(s)$inzone$withids"
149-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Done stopping and starting secondary storage vm(s)$inzone$withids." >>$LOGFILE
150-
fi
150+
if [ "$length_secondary" == "0" ];then
151+
echo -e "No running secondary storage vms found \n"
152+
else
153+
echo -e "Done stopping and starting secondary storage vm(s)$inzone$withids"
154+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Done stopping and starting secondary storage vm(s)$inzone$withids." >>$LOGFILE
155+
fi
151156

152-
echo -e "\nStopping and starting $length_console console proxy vm(s)$inzone$withids..."
153-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_console console proxy vm(s)$inzone$withids..." >>$LOGFILE
157+
echo -e "\nStopping and starting $length_console console proxy vm(s)$inzone$withids..."
158+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_console console proxy vm(s)$inzone$withids..." >>$LOGFILE
159+
160+
for d in "${console[@]}"; do
161+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Stopping console proxy with id $d" >>$LOGFILE
162+
jobresult=$(send_request stopSystemVm $d)
163+
if [ "$jobresult" != "1" ]; then
164+
echo -e "ERROR: Failed to stop console proxy vm with id $d \n"
165+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to stop console proxy vm with id $d" >>$LOGFILE
166+
else
167+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Starting console proxy vm with id $d" >>$LOGFILE
168+
jobresult=$(send_request startSystemVm $d consoleProxy)
169+
if [ "$jobresult" != "1" ]; then
170+
echo -e "ERROR: Failed to start console proxy vm with id $d \n"
171+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start console proxy vm with id $d" >>$LOGFILE
172+
fi
173+
fi
174+
done
154175

155-
for d in "${console[@]}"; do
156-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Stopping console proxy with id $d" >>$LOGFILE
157-
jobresult=$(send_request stopSystemVm $d)
158-
if [ "$jobresult" != "1" ]; then
159-
echo -e "ERROR: Failed to stop console proxy vm with id $d \n"
160-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to stop console proxy vm with id $d" >>$LOGFILE
176+
if [ "$length_console" == "0" ];then
177+
echo -e "No running console proxy vms found \n"
161178
else
162-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Starting console proxy vm with id $d" >>$LOGFILE
163-
jobresult=$(send_request startSystemVm $d consoleProxy)
164-
if [ "$jobresult" != "1" ]; then
165-
echo -e "ERROR: Failed to start console proxy vm with id $d \n"
166-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to start console proxy vm with id $d" >>$LOGFILE
167-
fi
179+
echo "Done stopping and starting console proxy vm(s) $inzone$withids."
180+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] Done stopping and starting console proxy vm(s) $inzone$withids." >>$LOGFILE
168181
fi
169-
done
170-
171-
if [ "$length_console" == "0" ];then
172-
echo -e "No running console proxy vms found \n"
173-
else
174-
echo "Done stopping and starting console proxy vm(s) $inzone$withids."
175-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] Done stopping and starting console proxy vm(s) $inzone$withids." >>$LOGFILE
176-
fi
177182
}
178183

179184
stop_start_router() {
180185
prepare_ids_clause
181-
router=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from vm_instance where state=\"Running\" and type=\"DomainRouter\"$zone$vmidsclause"`)
182-
length_router=(${#router[@]})
186+
router=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select uuid from vm_instance where state=\"Running\" and type=\"DomainRouter\"$zone$vmidsclause"`)
187+
length_router=(${#router[@]})
183188

184-
echo -e "\nStopping and starting $length_router running routing vm(s)$inzone$withids... "
185-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_router running routing vm(s)$inzone$withids... " >>$LOGFILE
189+
echo -e "\nStopping and starting $length_router running routing vm(s)$inzone$withids... "
190+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Stopping and starting $length_router running routing vm(s)$inzone$withids... " >>$LOGFILE
186191

187-
#Spawn reboot router in parallel - run commands in <n> chunks - number of threads is configurable
192+
#Spawn reboot router in parallel - run commands in <n> chunks - number of threads is configurable
188193

189-
pids=()
190-
for d in "${router[@]}"; do
194+
pids=()
195+
for d in "${router[@]}"; do
191196

192-
reboot_router $d &
197+
reboot_router $d &
193198

194-
pids=( "${pids[@]}" $! )
199+
pids=( "${pids[@]}" $! )
195200

196-
length_pids=(${#pids[@]})
197-
unfinishedPids=(${#pids[@]})
201+
length_pids=(${#pids[@]})
202+
unfinishedPids=(${#pids[@]})
198203

199-
if [ $maxthreads -gt $length_router ]; then
200-
maxthreads=$length_router
201-
fi
204+
if [ $maxthreads -gt $length_router ]; then
205+
maxthreads=$length_router
206+
fi
202207

203-
if [ $length_pids -ge $maxthreads ]; then
204-
while [ $unfinishedPids -gt 0 ]; do
205-
sleep 10
206-
count=0
207-
for (( i = 0 ; i < $length_pids; i++ )); do
208-
if ! ps ax | grep -v grep | grep ${pids[$i]} > /dev/null; then
209-
count=`expr $count + 1`
210-
fi
211-
done
208+
if [ $length_pids -ge $maxthreads ]; then
209+
while [ $unfinishedPids -gt 0 ]; do
210+
sleep 10
211+
count=0
212+
for (( i = 0 ; i < $length_pids; i++ )); do
213+
if ! ps ax | grep -v grep | grep ${pids[$i]} > /dev/null; then
214+
count=`expr $count + 1`
215+
fi
216+
done
212217

213-
if [ $count -eq $unfinishedPids ]; then
214-
unfinishedPids=0
215-
fi
218+
if [ $count -eq $unfinishedPids ]; then
219+
unfinishedPids=0
220+
fi
216221

217-
done
222+
done
218223

219-
#remove all elements from pids
220-
if [ $unfinishedPids -eq 0 ]; then
221-
pids=()
222-
length_pids=(${#pids[@]})
223-
fi
224+
#remove all elements from pids
225+
if [ $unfinishedPids -eq 0 ]; then
226+
pids=()
227+
length_pids=(${#pids[@]})
228+
fi
224229

225-
fi
230+
fi
226231

227-
done
232+
done
228233

229-
if [ "$length_router" == "0" ];then
230-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] No running router vms found \n" >>$LOGFILE
231-
else
232-
while [ $unfinishedPids -gt 0 ]; do
233-
sleep 10
234-
done
234+
if [ "$length_router" == "0" ];then
235+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] No running router vms found \n" >>$LOGFILE
236+
else
237+
while [ $unfinishedPids -gt 0 ]; do
238+
sleep 10
239+
done
235240

236-
echo -e "Done restarting router(s)$inzone$withids. \n"
237-
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Done restarting router(s)$inzone$withids. \n" >>$LOGFILE
241+
echo -e "Done restarting router(s)$inzone$withids. \n"
242+
echo -e "[$(date "+%Y.%m.%d-%H.%M.%S")] Done restarting router(s)$inzone$withids. \n" >>$LOGFILE
238243

239-
fi
244+
fi
240245
}
241246

242247
stop_start_all() {
243-
stop_start_system
244-
stop_start_router
248+
stop_start_system
249+
stop_start_router
245250
}
246251

247252
send_request(){
248-
jobid=`curl -sS "http://$ms:8096/?command=$1&id=$2&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
249-
if [ "$jobid" == "" ]; then
250-
echo 2
251-
return
252-
fi
253-
jobresult=$(query_async_job_result $jobid)
253+
jobid=`curl -sS "http://$ms:$integrationport/?command=$1&id=$2&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
254+
if [ "$jobid" == "" ]; then
255+
echo 2
256+
return
257+
fi
258+
jobresult=$(query_async_job_result $jobid)
254259
if [ "$jobresult" != "1" ]; then
255260
echo -e "ERROR: Failed to $1 id=$2; jobId is $jobid \n"
256261
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to $1 id=$2; jobId is $jobid" >>$LOGFILE
257262
fi
258-
echo $jobresult
263+
echo $jobresult
259264
}
260265

261266

262267
reboot_router(){
263-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Restarting router with id $1" >>$LOGFILE
264-
jobid=`curl -sS "http://$ms:8096/?command=rebootRouter&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
265-
if [ "$jobid" == "" ]; then
266-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart domainRouter with id $1; unable to submit the job" >>$LOGFILE
267-
echo 2
268-
return
269-
fi
270-
271-
jobresult=$(query_async_job_result $jobid)
272-
273-
if [ "$jobresult" != "1" ]; then
274-
echo -e "ERROR: Failed to restart domainRouter with id $1 \n"
275-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart domainRouter with id $1; jobId $jobid" >>$LOGFILE
276-
exit 0
277-
else
278-
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Successfully restarted domainRouter with id $1; jobId $jobid" >>$LOGFILE
279-
exit 0
280-
fi
268+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Restarting router with id $1" >>$LOGFILE
269+
jobid=`curl -sS "http://$ms:$integrationport/?command=rebootRouter&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
270+
if [ "$jobid" == "" ]; then
271+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart domainRouter with id $1; unable to submit the job" >>$LOGFILE
272+
echo 2
273+
return
274+
fi
275+
276+
jobresult=$(query_async_job_result $jobid)
281277

278+
if [ "$jobresult" != "1" ]; then
279+
echo -e "ERROR: Failed to restart domainRouter with id $1 \n"
280+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart domainRouter with id $1; jobId $jobid" >>$LOGFILE
281+
exit 0
282+
else
283+
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Successfully restarted domainRouter with id $1; jobId $jobid" >>$LOGFILE
284+
exit 0
285+
fi
282286
}
283287

284288
restart_networks(){
@@ -346,7 +350,7 @@ restart_networks(){
346350
}
347351

348352
restart_network(){
349-
jobid=`curl -sS "http://$ms:8096/?command=restartNetwork&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
353+
jobid=`curl -sS "http://$ms:$integrationport/?command=restartNetwork&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
350354
if [ "$jobid" == "" ]; then
351355
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart network with id $1; unable to submit the job" >>$LOGFILE
352356
echo 2
@@ -367,7 +371,7 @@ restart_network(){
367371
restart_vpc(){
368372
echo -e "INFO: Restarting vpc with id $1"
369373
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] INFO: Restarting vpc with id $1" >>$LOGFILE
370-
jobid=`curl -sS "http://$ms:8096/?command=restartVPC&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
374+
jobid=`curl -sS "http://$ms:$integrationport/?command=restartVPC&id=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}`
371375
if [ "$jobid" == "" ]; then
372376
echo "[$(date "+%Y.%m.%d-%H.%M.%S")] ERROR: Failed to restart vpc with id $1; unable to submit the job" >>$LOGFILE
373377
echo 2
@@ -387,7 +391,7 @@ restart_vpc(){
387391

388392

389393
restart_vpcs(){
390-
vpcs=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from vpc WHERE removed is null$zone"`)
394+
vpcs=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select uuid from vpc WHERE removed is null$zone"`)
391395
length_vpcs=(${#vpcs[@]})
392396

393397
echo -e "\nRestarting $length_vpcs vpcs... "
@@ -450,21 +454,21 @@ restart_vpcs(){
450454
}
451455

452456
query_async_job_result() {
453-
while [ 1 ]
454-
do
455-
jobstatus=`curl -sS "http://$ms:8096/?command=queryAsyncJobResult&jobId=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F, {'print $4'} | awk -F: {'print $2'}`
456-
if [ "$jobstatus" != "0" ]; then
457-
echo $jobstatus
458-
break
459-
fi
460-
sleep 5
461-
done
457+
while [ 1 ]
458+
do
459+
jobstatus=`curl -sS "http://$ms:$integrationport/?command=queryAsyncJobResult&jobId=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F, {'print $7'} | awk -F: {'print $2'}`
460+
if [ "$jobstatus" != "0" ]; then
461+
echo $jobstatus
462+
break
463+
fi
464+
sleep 5
465+
done
462466
}
463467

464468
if [ "$system$router$all$help$redundant$vpc" == "" ]
465469
then
466-
usage
467-
exit
470+
usage
471+
exit
468472
fi
469473

470474
if [ "$help" == "1" ]

0 commit comments

Comments
 (0)