Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 2 | silent=false |
| 3 | cleaning=false |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 4 | all_computes=false |
| 5 | fill_mode=false |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 6 | zone=nova |
| 7 | use_fqdn=false |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 8 | |
| 9 | tmp_out=$(mktemp) |
| 10 | trap "rm -f ${tmp_out}" EXIT |
| 11 | |
| 12 | declare errors=() |
| 13 | |
| 14 | function show_help { |
| 15 | printf "Compute check/filling script\n\t-h, -?\tShow this help\n" |
| 16 | printf "\t-d\tCleaning of earlier created VMs\n" |
| 17 | printf "\t-q\tSilent mode\n" |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 18 | printf "\t-a\tEnumeratre all computes\n" |
| 19 | printf "\t-f\tFill mode\n" |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 20 | printf "\t-z <zone>\tAvailability zone to use on create\n" |
| 21 | printf "\t-n\tUse compute's FQDN when setting zone hint\n" |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 22 | printf "\nUsage: cmp_check.sh (-a | <compute_hostname>) (-f [<vm_count>|def:1])\n" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 23 | printf "\t<compute_hostname> is a host shortname\n" |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 24 | printf "\t<vm_count> is optional. Defaults to 1\n" |
| 25 | printf "Examples:\n" |
| 26 | printf "\tFill all computes with 3 VMs:\n\t\t'bash cmp_check.sh -fa 3'\n\n" |
| 27 | printf "\tFill specific compute with 5 VMs:\n\t\t'bash cmp_check.sh -f cmp001 5'\n\n" |
| 28 | printf "\tCheck all computes:\n\t\t'bash cmp_check.sh -a'\n\n" |
| 29 | printf "\tCheck specific compute:\n\t\t'bash cmp_check.sh cmp001'\n\n" |
| 30 | printf "\tClean all computes:\n\t\t'bash cmp_check.sh -da'\n\n" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | OPTIND=1 # Reset in case getopts has been used previously in the shell. |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 34 | while getopts "h?:qdafz:n" opt; do |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 35 | case "$opt" in |
| 36 | h|\?) |
| 37 | show_help |
| 38 | exit 0 |
| 39 | ;; |
| 40 | q) silent=true |
| 41 | ;; |
| 42 | d) cleaning=true |
| 43 | ;; |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 44 | a) all_computes=true |
| 45 | ;; |
| 46 | f) fill_mode=true |
| 47 | ;; |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 48 | z) zone=${OPTARG} |
| 49 | printf "# Using availability zone of '${zone}'\n" |
| 50 | ;; |
| 51 | n) use_fqdn=true |
| 52 | printf "# Using FQDN as a compute host name\n" |
| 53 | ;; |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 54 | esac |
| 55 | done |
| 56 | |
| 57 | shift $((OPTIND-1)) |
| 58 | [ "${1:-}" = "--" ] && shift |
| 59 | |
| 60 | # Check and create cmp_name var |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 61 | if [[ -z ${1+x} ]] && [[ ! ${all_computes} == "true" ]]; then |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 62 | show_help |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 63 | printf "\nERROR: No compute host specified\n" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 64 | exit 1 |
| 65 | fi |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 66 | if [[ ${all_computes} == "true" ]]; then |
| 67 | cmp_name=all |
| 68 | # if enumerate mode is set, vmcount source is ${1} |
| 69 | if [[ -z ${1+x} ]] || [[ ! ${fill_mode} == true ]]; then |
| 70 | vmcount=1 |
| 71 | else |
| 72 | vmcount=${1} |
| 73 | fi |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 74 | else |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 75 | cmp_name=${1} |
| 76 | # in single compute mode, vmcount source is ${2} |
| 77 | # in check mode count is always 1 |
| 78 | if [[ -z ${2+x} ]] || [[ ! ${fill_mode} == true ]]; then |
| 79 | vmcount=1 |
| 80 | else |
| 81 | vmcount=${2} |
| 82 | fi |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 83 | fi |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 84 | |
| 85 | function cmp_stats() { |
| 86 | cmpid=$(openstack hypervisor list --matching ${1} -f value -c ID) |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 87 | vars=( $(openstack hypervisor show ${cmpid} -f shell -c state -c running_vms -c vcpus -c vcpus_used -c memory_mb -c memory_mb_used) ) |
| 88 | [ ! 0 -eq $? ] && errors+=("${1}: $(cat ${vars[@]})") |
| 89 | if [ ! $state == '"up"' ]; then |
| 90 | echo "# Hypervisor fail, state is '${state}'" |
| 91 | errors |
| 92 | exit 1 |
| 93 | else |
| 94 | declare ${vars[@]} |
| 95 | printf "${1}: vms=%s vcpus=%s/%s ram=%s/%s\n" ${running_vms} ${vcpus_used} ${vcpus} ${memory_mb_used} ${memory_mb} |
| 96 | fi |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 97 | } |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 98 | |
| 99 | function waitfor () { |
| 100 | counter=0 |
| 101 | while [ ${counter} -lt 6 ]; do |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 102 | ids=( $(openstack server list --name ${1} --status ${2} -f value -c ID) ) |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 103 | if [ ${#ids[@]} -eq 0 ]; then |
| 104 | sleep 5 |
| 105 | counter=$((counter + 1)) |
| 106 | else |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 107 | [ ! "$silent" = true ] && printf "# '${1}' reached status ${2}\n" |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 108 | break |
| 109 | fi |
| 110 | done |
| 111 | } |
| 112 | |
| 113 | function getid() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 114 | openstack server list -c ID -c Name -f value | grep "${1}" | cut -d' ' -f1 |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 117 | function get_all_cmp() { |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 118 | if [ $use_fqdn == true ]; then |
| 119 | openstack hypervisor list -f value -c "Hypervisor Hostname" -c State | grep "up" | sort | cut -d' ' -f1 |
| 120 | else |
| 121 | openstack hypervisor list -f value -c "Hypervisor Hostname" -c State | grep "up" | sort | cut -d'.' -f1 |
| 122 | fi |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 125 | function vm_create() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 126 | [ ! "$silent" = true ] && set -x |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 127 | openstack server create --nic net-id=${fixed_net_left_id} --image ${cirros35_id} --flavor ${flavor_tiny_id} --key-name ${keypair_id} --security-group ${secgroup_all_id} --availability-zone ${zone}:${1} ${2} 2>${tmp_out} >/dev/nul |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 128 | [ ! 0 -eq $? ] && errors+=("${1}/${2}: $(cat ${tmp_out})") |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 129 | set +x |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 130 | [ ! "$silent" = true ] && cat ${tmp_out} |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | function vm_action() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 134 | openstack server ${1} ${2} 2>${tmp_out} >/dev/nul |
| 135 | if [ ! 0 -eq $? ]; then |
| 136 | errors+=("${cmp_name}: $(cat ${tmp_out})") |
| 137 | fi |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 140 | function errors { |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 141 | echo "==== Errors" |
| 142 | for i in "${!errors[@]}"; do |
| 143 | printf "#%s\n" "${errors[$i]}" |
| 144 | done |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | function join_by { local IFS="$1"; shift; echo "$*"; } |
| 148 | |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 149 | |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 150 | function clean_cmp() { |
| 151 | # #### Cleaning mode |
| 152 | if [ $cleaning = true ]; then |
| 153 | vmname=vm_${1} |
| 154 | vmid=( $(getid ${vmname}) ) |
| 155 | if [ ${#vmid[@]} -ne 0 ]; then |
| 156 | [ ! "$silent" = true ] && echo "# ${1}: cleaning ${#vmid[@]} VMs" |
| 157 | vm_action delete "$(join_by ' ' "${vmid[@]}")" |
| 158 | else |
| 159 | [ ! "$silent" = true ] && echo "# ${1}: ...no VMs found" |
| 160 | fi |
| 161 | fi |
| 162 | } |
| 163 | |
| 164 | function check_cmp_node() { |
| 165 | cmp_stats ${1} |
| 166 | vm_create ${1} ${2} |
| 167 | waitfor ${2} ACTIVE |
| 168 | vmid=$(getid ${2}) |
| 169 | |
| 170 | cmp_stats ${1} |
| 171 | |
| 172 | vm_action pause ${vmid} |
| 173 | waitfor ${2} PAUSED |
| 174 | vm_action unpause ${vmid} |
| 175 | waitfor ${2} ACTIVE |
| 176 | |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 177 | [ ! "$silent" = true ] && echo "# ... deleting created VMs" |
| 178 | clean_cmp ${1} |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 179 | cmp_stats ${1} |
| 180 | } |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 181 | |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 182 | if [ ! -f cvp.manifest ]; then |
| 183 | echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh" |
| 184 | exit 1 |
| 185 | else |
| 186 | source cvp.manifest |
| 187 | fi |
| 188 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 189 | [ ! "$silent" = true ] && echo "# Sourcing cvprc" |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 190 | source cvprc |
| 191 | |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 192 | # #### Checking for CMP existence |
| 193 | if [[ ! ${cmp_name} == "all" ]]; then |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 194 | echo "# Inspecting '${zone}:${cmp_name}'" |
| 195 | cmp_fqdn=$(openstack host list --zone ${zone} -f value -c 'Host Name' -c 'Zone' | grep ${cmp_name} | cut -d' ' -f1 2>${tmp_out}) |
| 196 | [ ! 0 -eq $? ] && errors+=("${cmp_name}\@${zone}: $(cat ${tmp_out})") |
| 197 | if [[ -z ${cmp_fqdn} ]]; then |
| 198 | echo "ERROR: ${cmp_name} not found in ${zone}" |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 199 | errors |
| 200 | exit 1 |
| 201 | fi |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 202 | printf "# Found ${cmp_fqdn} in '${zone}' using given name of ${cmp_name}\n" |
| 203 | vars=( $(openstack hypervisor show ${cmp_fqdn} -f shell -c id -c state -c hypervisor_hostname) ) |
| 204 | [ ! 0 -eq $? ] && errors+=("${cmp_name}: $(cat ${tmp_out})") |
| 205 | declare ${vars[@]} |
| 206 | # check that such node exists |
| 207 | if [ -z ${id+x} ]; then |
| 208 | # no id |
| 209 | echo "ERROR: ${cmp_name} not found among hypervisors" |
| 210 | errors |
| 211 | exit 1 |
| 212 | else |
| 213 | echo "# ${id}, ${hypervisor_hostname}, status '${state}'" |
| 214 | if [ ! ${state} == '"up"' ]; then |
| 215 | echo "ERROR: ${hypervisor_hostname} is '${state}'" |
| 216 | exit 1 |
| 217 | else |
| 218 | unset id |
| 219 | unset hypervisor_hostname |
| 220 | unset state |
| 221 | fi |
| 222 | fi |
| 223 | |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 224 | fi |
| 225 | |
| 226 | if [[ ${cmp_name} == all ]]; then |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 227 | echo "# Gathering compute count with state 'up'" |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 228 | cmp_nodes=( $(get_all_cmp) ) |
| 229 | else |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 230 | if [ $use_fqdn == true ]; then |
| 231 | cmp_nodes=( ${cmp_fqdn} ) |
| 232 | else |
| 233 | cmp_nodes=( ${cmp_name} ) |
| 234 | fi |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 235 | fi |
| 236 | |
| 237 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 238 | # #### Cleaning mode |
| 239 | if [ $cleaning = true ]; then |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 240 | # get all computes |
| 241 | if [[ ${cmp_name} == all ]]; then |
| 242 | echo "# Cleaning ${#cmp_nodes[@]} computes" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 243 | else |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 244 | echo "# Cleaning ${cmp_name}" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 245 | fi |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 246 | |
| 247 | # clean them |
| 248 | for node in ${cmp_nodes[@]}; do |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 249 | cname=$(echo ${node} | cut -d'.' -f1) |
| 250 | clean_cmp ${cname} |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 251 | done |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 252 | echo "# Done cleaning" |
| 253 | errors |
| 254 | exit 0 |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 255 | fi |
| 256 | |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 257 | # ### |
| 258 | if [[ ! ${fill_mode} = true ]]; then |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 259 | # ### CMP Checking mode |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 260 | if [[ ${cmp_name} == all ]]; then |
| 261 | echo "# Checking ${#cmp_nodes[@]} computes" |
| 262 | fi |
| 263 | # check node |
| 264 | for node in ${cmp_nodes[@]}; do |
| 265 | echo "# ${node}: checking" |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 266 | cname=$(echo ${node} | cut -d'.' -f1) |
| 267 | check_cmp_node ${node} vm_${cname} |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 268 | echo "# ${node}: done" |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 269 | done |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 270 | errors |
| 271 | exit 0 |
| 272 | else |
| 273 | # ### CMP fillling mode |
| 274 | if [[ ${cmp_name} == all ]]; then |
| 275 | echo "# Filling ${#cmp_nodes[@]} computes" |
| 276 | fi |
| 277 | |
| 278 | for node in ${cmp_nodes[@]}; do |
| 279 | echo "# ${node}: filling" |
| 280 | counter=1 |
| 281 | while [[ $counter -lt ${vmcount}+1 ]]; do |
Alex Savatieiev | 814307f | 2020-04-13 12:47:39 -0500 | [diff] [blame^] | 282 | cname=$(echo ${node} | cut -d'.' -f1) |
| 283 | vmname_c=vm_${cname}_$(printf "%02d" ${counter}) |
Alex Savatieiev | d0ae84f | 2019-10-23 13:36:37 -0500 | [diff] [blame] | 284 | [ ! "$silent" = true ] && echo "# ${node}: creating ${vmname_c}" |
| 285 | vm_create ${node} ${vmname_c} |
| 286 | cmp_stats ${node} |
| 287 | ((counter++)) |
| 288 | done |
| 289 | printf "# ${node}: done\n" |
| 290 | done |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 291 | fi |
| 292 | |
| 293 | errors |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 294 | |