|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +. /etc/rc.d/init.d/functions |
| 4 | + |
| 5 | +#set -x |
| 6 | + |
| 7 | +usage() { |
| 8 | + printf "\nThe tool stopping/starting running system vms and domain routers \n\nUsage: %s: [-d] [-u] [-p] [-s] [-r] [-a] \n\n -d - cloud DB server ip address, defaulted to localhost if not specified \n -u - user name to access cloud DB, defaulted to "root" if not specified \n -p - cloud DB user password, defaulted to no password if not specified \n\n -s - stop then start all running SSVMs and Console Proxies \n -r - stop then start all running Virtual Routers\n -a - stop then start all running SSVMs, Console Proxies, and Virtual Routers\n\n" $(basename $0) >&2 |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +system= |
| 13 | +router= |
| 14 | +all= |
| 15 | +db=localhost |
| 16 | +user=root |
| 17 | +password= |
| 18 | + |
| 19 | + |
| 20 | +while getopts 'sard:u:p:' OPTION |
| 21 | +do |
| 22 | + case $OPTION in |
| 23 | + s) system=1 |
| 24 | + ;; |
| 25 | + r) router=1 |
| 26 | + ;; |
| 27 | + a) all=1 |
| 28 | + ;; |
| 29 | + d) db="$OPTARG" |
| 30 | + ;; |
| 31 | + u) user="$OPTARG" |
| 32 | + ;; |
| 33 | + p) password="$OPTARG" |
| 34 | + ;; |
| 35 | + ?) usage |
| 36 | + esac |
| 37 | +done |
| 38 | + |
| 39 | + |
| 40 | +stop_start_system() { |
| 41 | +secondary=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from vm_instance where state=\"Running\" and type=\"SecondaryStorageVm\""`) |
| 42 | +console=(`mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e "select id from vm_instance where state=\"Running\" and type=\"ConsoleProxy\""`) |
| 43 | +length_secondary=(${#secondary[@]}) |
| 44 | +length_console=(${#console[@]}) |
| 45 | + |
| 46 | + |
| 47 | +echo -e "\nStopping and starting secondary storage vms..." |
| 48 | +for d in "${secondary[@]}"; do |
| 49 | + jobresult=$(send_request stopSystemVm $d) |
| 50 | + if [ "$jobresult" != "1" ]; then |
| 51 | + echo "ERROR: Failed to stop secondary storage vm with id $d" |
| 52 | + else |
| 53 | + jobresult=$(send_request startSystemVm $d) |
| 54 | + if [ "$jobresult" != "1" ]; then |
| 55 | + echo "ERROR: Failed to start secondary storage vm with id $d" |
| 56 | + fi |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +if [ "$length_secondary" == "0" ];then |
| 61 | + echo -e "No running secondary storage vms found \n" |
| 62 | +else |
| 63 | + echo -e "Done \n" |
| 64 | +fi |
| 65 | + |
| 66 | +echo "Stopping and starting console proxy vms..." |
| 67 | +for d in "${console[@]}"; do |
| 68 | + jobresult=$(send_request stopSystemVm $d) |
| 69 | + if [ "$jobresult" != "1" ]; then |
| 70 | + echo "ERROR: Failed to stop console proxy vm with id $d" |
| 71 | + else |
| 72 | + jobresult=$(send_request startSystemVm $d) |
| 73 | + if [ "$jobresult" != "1" ]; then |
| 74 | + echo "ERROR: Failed to start console proxy vm with id $d" |
| 75 | + fi |
| 76 | + fi |
| 77 | +done |
| 78 | + |
| 79 | +if [ "$length_console" == "0" ];then |
| 80 | + echo -e "No running console proxy vms found \n" |
| 81 | +else |
| 82 | + echo -e "Done \n" |
| 83 | +fi |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +stop_start_router() { |
| 88 | +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\""`) |
| 89 | +length_router=(${#router[@]}) |
| 90 | + |
| 91 | +echo -e "\nStopping and starting running routing vms..." |
| 92 | +for d in "${router[@]}"; do |
| 93 | + jobresult=$(send_request stopRouter $d) |
| 94 | + if [ "$jobresult" != "1" ]; then |
| 95 | + echo "ERROR: Failed to stop domain router with id $d" |
| 96 | + else |
| 97 | + jobresult=$(send_request startRouter $d) |
| 98 | + if [ "$jobresult" != "1" ]; then |
| 99 | + echo "ERROR: Failed to start domain router with id $d" |
| 100 | + fi |
| 101 | + fi |
| 102 | +done |
| 103 | + |
| 104 | +if [ "$length_router" == "0" ];then |
| 105 | + echo -e "No running router vms found \n" |
| 106 | +else |
| 107 | + echo -e "Done \n" |
| 108 | +fi |
| 109 | +} |
| 110 | + |
| 111 | +stop_start_all() { |
| 112 | +stop_start_system |
| 113 | +stop_start_router |
| 114 | +} |
| 115 | + |
| 116 | +send_request(){ |
| 117 | +jobid=`curl -sS "http://localhost:8096/?command=$1&id=$2&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F: {'print $3'}` |
| 118 | +if [ "$jobid" == "" ]; then |
| 119 | + echo 2 |
| 120 | + return |
| 121 | +fi |
| 122 | +jobresult=$(query_async_job_result $jobid) |
| 123 | +echo $jobresult |
| 124 | +} |
| 125 | + |
| 126 | +query_async_job_result() { |
| 127 | +while [ 1 ] |
| 128 | +do |
| 129 | + jobstatus=`curl -sS "http://localhost:8096/?command=queryAsyncJobResult&jobId=$1&response=json" | sed 's/\"//g' | sed 's/ //g' | sed 's/{//g' | sed 's/}//g' | awk -F, {'print $2'} | awk -F: {'print $2'}` |
| 130 | + if [ "$jobstatus" != "0" ]; then |
| 131 | + echo $jobstatus |
| 132 | + break |
| 133 | + fi |
| 134 | +sleep 5 |
| 135 | +done |
| 136 | +} |
| 137 | + |
| 138 | +if [ "$system$router$all" == "" ] |
| 139 | +then |
| 140 | + usage |
| 141 | + exit |
| 142 | +fi |
| 143 | + |
| 144 | + |
| 145 | +if [ "$all" == "1" ] |
| 146 | +then |
| 147 | + stop_start_all |
| 148 | + exit |
| 149 | +fi |
| 150 | + |
| 151 | +if [ "$system" == "1" ] |
| 152 | +then |
| 153 | + stop_start_system |
| 154 | +fi |
| 155 | + |
| 156 | +if [ "$router" == "1" ] |
| 157 | +then |
| 158 | + stop_start_router |
| 159 | +fi |
0 commit comments