blob: 231d67c983cf68940ad86dbb8aad0e7549053d5f [file] [log] [blame]
Alex Savatieievbadc4762019-09-30 13:46:37 -05001#!/bin/bash
Alex Savatieieveaf0a992019-10-02 17:51:54 -05002silent=false
3cleaning=false
Alex Savatieievd0ae84f2019-10-23 13:36:37 -05004all_computes=false
5fill_mode=false
Alex Savatieiev814307f2020-04-13 12:47:39 -05006zone=nova
7use_fqdn=false
Alex Savatieieveaf0a992019-10-02 17:51:54 -05008
9tmp_out=$(mktemp)
10trap "rm -f ${tmp_out}" EXIT
11
12declare errors=()
13
14function 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 Savatieievd0ae84f2019-10-23 13:36:37 -050018 printf "\t-a\tEnumeratre all computes\n"
19 printf "\t-f\tFill mode\n"
Alex Savatieiev814307f2020-04-13 12:47:39 -050020 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 Savatieievd0ae84f2019-10-23 13:36:37 -050022 printf "\nUsage: cmp_check.sh (-a | <compute_hostname>) (-f [<vm_count>|def:1])\n"
Alex Savatieieveaf0a992019-10-02 17:51:54 -050023 printf "\t<compute_hostname> is a host shortname\n"
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050024 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 Savatieieveaf0a992019-10-02 17:51:54 -050031}
32
33OPTIND=1 # Reset in case getopts has been used previously in the shell.
Alex Savatieiev814307f2020-04-13 12:47:39 -050034while getopts "h?:qdafz:n" opt; do
Alex Savatieieveaf0a992019-10-02 17:51:54 -050035 case "$opt" in
36 h|\?)
37 show_help
38 exit 0
39 ;;
40 q) silent=true
41 ;;
42 d) cleaning=true
43 ;;
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050044 a) all_computes=true
45 ;;
46 f) fill_mode=true
47 ;;
Alex Savatieiev814307f2020-04-13 12:47:39 -050048 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 Savatieieveaf0a992019-10-02 17:51:54 -050054 esac
55done
56
57shift $((OPTIND-1))
58[ "${1:-}" = "--" ] && shift
59
60# Check and create cmp_name var
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050061if [[ -z ${1+x} ]] && [[ ! ${all_computes} == "true" ]]; then
Alex Savatieieveaf0a992019-10-02 17:51:54 -050062 show_help
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050063 printf "\nERROR: No compute host specified\n"
Alex Savatieieveaf0a992019-10-02 17:51:54 -050064 exit 1
65fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050066if [[ ${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 Savatieieveaf0a992019-10-02 17:51:54 -050074else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050075 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 Savatieieveaf0a992019-10-02 17:51:54 -050083fi
Alex Savatieieveaf0a992019-10-02 17:51:54 -050084
85function cmp_stats() {
86 cmpid=$(openstack hypervisor list --matching ${1} -f value -c ID)
Alex Savatieiev814307f2020-04-13 12:47:39 -050087 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 Savatieieveaf0a992019-10-02 17:51:54 -050097}
Alex Savatieievbadc4762019-09-30 13:46:37 -050098
99function waitfor () {
100 counter=0
101 while [ ${counter} -lt 6 ]; do
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500102 ids=( $(openstack server list --name ${1} --status ${2} -f value -c ID) )
Alex Savatieievbadc4762019-09-30 13:46:37 -0500103 if [ ${#ids[@]} -eq 0 ]; then
104 sleep 5
105 counter=$((counter + 1))
106 else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500107 [ ! "$silent" = true ] && printf "# '${1}' reached status ${2}\n"
Alex Savatieievbadc4762019-09-30 13:46:37 -0500108 break
109 fi
110 done
111}
112
113function getid() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500114 openstack server list -c ID -c Name -f value | grep "${1}" | cut -d' ' -f1
Alex Savatieievbadc4762019-09-30 13:46:37 -0500115}
116
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500117function get_all_cmp() {
Alex Savatieiev814307f2020-04-13 12:47:39 -0500118 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 Savatieievd0ae84f2019-10-23 13:36:37 -0500123}
124
Alex Savatieievbadc4762019-09-30 13:46:37 -0500125function vm_create() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500126 [ ! "$silent" = true ] && set -x
Alex Savatieiev814307f2020-04-13 12:47:39 -0500127 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 Savatieieveaf0a992019-10-02 17:51:54 -0500128 [ ! 0 -eq $? ] && errors+=("${1}/${2}: $(cat ${tmp_out})")
Alex Savatieievbadc4762019-09-30 13:46:37 -0500129 set +x
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500130 [ ! "$silent" = true ] && cat ${tmp_out}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500131}
132
133function vm_action() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500134 openstack server ${1} ${2} 2>${tmp_out} >/dev/nul
135 if [ ! 0 -eq $? ]; then
136 errors+=("${cmp_name}: $(cat ${tmp_out})")
137 fi
Alex Savatieievbadc4762019-09-30 13:46:37 -0500138}
139
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500140function errors {
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500141 echo "==== Errors"
142 for i in "${!errors[@]}"; do
143 printf "#%s\n" "${errors[$i]}"
144 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500145}
146
147function join_by { local IFS="$1"; shift; echo "$*"; }
148
Alex Savatieievbadc4762019-09-30 13:46:37 -0500149
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500150function 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
164function 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 Savatieiev814307f2020-04-13 12:47:39 -0500177 [ ! "$silent" = true ] && echo "# ... deleting created VMs"
178 clean_cmp ${1}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500179 cmp_stats ${1}
180}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500181
Alex Savatieievbadc4762019-09-30 13:46:37 -0500182if [ ! -f cvp.manifest ]; then
183 echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh"
184 exit 1
185else
186 source cvp.manifest
187fi
188
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500189[ ! "$silent" = true ] && echo "# Sourcing cvprc"
Alex Savatieievbadc4762019-09-30 13:46:37 -0500190source cvprc
191
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500192# #### Checking for CMP existence
193if [[ ! ${cmp_name} == "all" ]]; then
Alex Savatieiev814307f2020-04-13 12:47:39 -0500194 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 Savatieievd0ae84f2019-10-23 13:36:37 -0500199 errors
200 exit 1
201 fi
Alex Savatieiev814307f2020-04-13 12:47:39 -0500202 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 Savatieievd0ae84f2019-10-23 13:36:37 -0500224fi
225
226if [[ ${cmp_name} == all ]]; then
Alex Savatieiev814307f2020-04-13 12:47:39 -0500227 echo "# Gathering compute count with state 'up'"
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500228 cmp_nodes=( $(get_all_cmp) )
229else
Alex Savatieiev814307f2020-04-13 12:47:39 -0500230 if [ $use_fqdn == true ]; then
231 cmp_nodes=( ${cmp_fqdn} )
232 else
233 cmp_nodes=( ${cmp_name} )
234 fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500235fi
236
237
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500238# #### Cleaning mode
239if [ $cleaning = true ]; then
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500240 # get all computes
241 if [[ ${cmp_name} == all ]]; then
242 echo "# Cleaning ${#cmp_nodes[@]} computes"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500243 else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500244 echo "# Cleaning ${cmp_name}"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500245 fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500246
247 # clean them
248 for node in ${cmp_nodes[@]}; do
Alex Savatieiev814307f2020-04-13 12:47:39 -0500249 cname=$(echo ${node} | cut -d'.' -f1)
250 clean_cmp ${cname}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500251 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500252 echo "# Done cleaning"
253 errors
254 exit 0
Alex Savatieievbadc4762019-09-30 13:46:37 -0500255fi
256
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500257# ###
258if [[ ! ${fill_mode} = true ]]; then
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500259 # ### CMP Checking mode
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500260 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 Savatieiev814307f2020-04-13 12:47:39 -0500266 cname=$(echo ${node} | cut -d'.' -f1)
267 check_cmp_node ${node} vm_${cname}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500268 echo "# ${node}: done"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500269 done
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500270 errors
271 exit 0
272else
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 Savatieiev814307f2020-04-13 12:47:39 -0500282 cname=$(echo ${node} | cut -d'.' -f1)
283 vmname_c=vm_${cname}_$(printf "%02d" ${counter})
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500284 [ ! "$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 Savatieieveaf0a992019-10-02 17:51:54 -0500291fi
292
293errors
Alex Savatieievbadc4762019-09-30 13:46:37 -0500294