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