blob: 07bc9ca031cb3e825ddd7215b3e396810b184d09 [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
Alexdb7786b2022-02-21 17:58:29 -0600127 openstack server create --nic net-id=${fixed_net_left_id} --image ${cirros51_id} --flavor ${flavor_tiny_id} --key-name ${keypair_id} --security-group ${secgroup_all_id} --availability-zone ${zone}:${1} ${2} 2>${tmp_out} >/dev/null
Alex83075ac2022-02-16 13:39:50 -0600128 #openstack server create --nic net-id=${fixed_net_left_id} --image ${ubuntu16_id} --flavor ${flavor_high_id} --key-name ${keypair_id} --security-group ${secgroup_all_id} --availability-zone ${zone}:${1} ${2} 2>${tmp_out} >/dev/null
129 #openstack server create --nic net-id=${fixed_net_right_id} --image ${ubuntu16_id} --flavor ${flavor_high_id} --key-name ${keypair_id} --security-group ${secgroup_all_id} --availability-zone ${zone}:${1} ${2} 2>${tmp_out} >/dev/null
Alexdb7786b2022-02-21 17:58:29 -0600130 #openstack server create --nic net-id=${fixed_net_left_id} --image ${ubuntu20_id} --flavor ${flavor_high_id} --key-name ${keypair_id} --security-group ${secgroup_all_id} --availability-zone ${zone}:${1} ${2} 2>${tmp_out} >/dev/null
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500131 [ ! 0 -eq $? ] && errors+=("${1}/${2}: $(cat ${tmp_out})")
Alex Savatieievbadc4762019-09-30 13:46:37 -0500132 set +x
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500133 [ ! "$silent" = true ] && cat ${tmp_out}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500134}
135
136function vm_action() {
Alex6892c8c2021-01-28 16:07:29 -0600137 openstack server ${1} ${2} 2>${tmp_out} >/dev/null
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500138 if [ ! 0 -eq $? ]; then
139 errors+=("${cmp_name}: $(cat ${tmp_out})")
140 fi
Alex Savatieievbadc4762019-09-30 13:46:37 -0500141}
142
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500143function errors {
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500144 echo "==== Errors"
145 for i in "${!errors[@]}"; do
146 printf "#%s\n" "${errors[$i]}"
147 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500148}
149
150function join_by { local IFS="$1"; shift; echo "$*"; }
151
Alex Savatieievbadc4762019-09-30 13:46:37 -0500152
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500153function clean_cmp() {
154 # #### Cleaning mode
155 if [ $cleaning = true ]; then
156 vmname=vm_${1}
157 vmid=( $(getid ${vmname}) )
158 if [ ${#vmid[@]} -ne 0 ]; then
159 [ ! "$silent" = true ] && echo "# ${1}: cleaning ${#vmid[@]} VMs"
160 vm_action delete "$(join_by ' ' "${vmid[@]}")"
161 else
162 [ ! "$silent" = true ] && echo "# ${1}: ...no VMs found"
163 fi
164 fi
165}
166
167function check_cmp_node() {
168 cmp_stats ${1}
169 vm_create ${1} ${2}
170 waitfor ${2} ACTIVE
171 vmid=$(getid ${2})
172
173 cmp_stats ${1}
174
175 vm_action pause ${vmid}
176 waitfor ${2} PAUSED
177 vm_action unpause ${vmid}
178 waitfor ${2} ACTIVE
179
Alex Savatieiev814307f2020-04-13 12:47:39 -0500180 [ ! "$silent" = true ] && echo "# ... deleting created VMs"
181 clean_cmp ${1}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500182 cmp_stats ${1}
183}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500184
Alex Savatieievbadc4762019-09-30 13:46:37 -0500185if [ ! -f cvp.manifest ]; then
186 echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh"
187 exit 1
188else
189 source cvp.manifest
190fi
191
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500192[ ! "$silent" = true ] && echo "# Sourcing cvprc"
Alex Savatieievbadc4762019-09-30 13:46:37 -0500193source cvprc
194
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500195# #### Checking for CMP existence
196if [[ ! ${cmp_name} == "all" ]]; then
Alex Savatieiev814307f2020-04-13 12:47:39 -0500197 echo "# Inspecting '${zone}:${cmp_name}'"
198 cmp_fqdn=$(openstack host list --zone ${zone} -f value -c 'Host Name' -c 'Zone' | grep ${cmp_name} | cut -d' ' -f1 2>${tmp_out})
199 [ ! 0 -eq $? ] && errors+=("${cmp_name}\@${zone}: $(cat ${tmp_out})")
200 if [[ -z ${cmp_fqdn} ]]; then
201 echo "ERROR: ${cmp_name} not found in ${zone}"
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500202 errors
203 exit 1
204 fi
Alex Savatieiev814307f2020-04-13 12:47:39 -0500205 printf "# Found ${cmp_fqdn} in '${zone}' using given name of ${cmp_name}\n"
206 vars=( $(openstack hypervisor show ${cmp_fqdn} -f shell -c id -c state -c hypervisor_hostname) )
207 [ ! 0 -eq $? ] && errors+=("${cmp_name}: $(cat ${tmp_out})")
208 declare ${vars[@]}
209 # check that such node exists
210 if [ -z ${id+x} ]; then
211 # no id
212 echo "ERROR: ${cmp_name} not found among hypervisors"
213 errors
214 exit 1
215 else
216 echo "# ${id}, ${hypervisor_hostname}, status '${state}'"
217 if [ ! ${state} == '"up"' ]; then
218 echo "ERROR: ${hypervisor_hostname} is '${state}'"
219 exit 1
220 else
221 unset id
222 unset hypervisor_hostname
223 unset state
224 fi
225 fi
226
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500227fi
228
229if [[ ${cmp_name} == all ]]; then
Alex Savatieiev814307f2020-04-13 12:47:39 -0500230 echo "# Gathering compute count with state 'up'"
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500231 cmp_nodes=( $(get_all_cmp) )
232else
Alex Savatieiev814307f2020-04-13 12:47:39 -0500233 if [ $use_fqdn == true ]; then
234 cmp_nodes=( ${cmp_fqdn} )
235 else
236 cmp_nodes=( ${cmp_name} )
237 fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500238fi
239
240
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500241# #### Cleaning mode
242if [ $cleaning = true ]; then
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500243 # get all computes
244 if [[ ${cmp_name} == all ]]; then
245 echo "# Cleaning ${#cmp_nodes[@]} computes"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500246 else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500247 echo "# Cleaning ${cmp_name}"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500248 fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500249
250 # clean them
251 for node in ${cmp_nodes[@]}; do
Alex Savatieiev814307f2020-04-13 12:47:39 -0500252 cname=$(echo ${node} | cut -d'.' -f1)
253 clean_cmp ${cname}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500254 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500255 echo "# Done cleaning"
256 errors
257 exit 0
Alex Savatieievbadc4762019-09-30 13:46:37 -0500258fi
259
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500260# ###
261if [[ ! ${fill_mode} = true ]]; then
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500262 # ### CMP Checking mode
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500263 if [[ ${cmp_name} == all ]]; then
264 echo "# Checking ${#cmp_nodes[@]} computes"
265 fi
266 # check node
267 for node in ${cmp_nodes[@]}; do
268 echo "# ${node}: checking"
Alex Savatieiev814307f2020-04-13 12:47:39 -0500269 cname=$(echo ${node} | cut -d'.' -f1)
270 check_cmp_node ${node} vm_${cname}
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500271 echo "# ${node}: done"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500272 done
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500273 errors
274 exit 0
275else
276 # ### CMP fillling mode
277 if [[ ${cmp_name} == all ]]; then
278 echo "# Filling ${#cmp_nodes[@]} computes"
279 fi
280
281 for node in ${cmp_nodes[@]}; do
282 echo "# ${node}: filling"
283 counter=1
284 while [[ $counter -lt ${vmcount}+1 ]]; do
Alex Savatieiev814307f2020-04-13 12:47:39 -0500285 cname=$(echo ${node} | cut -d'.' -f1)
286 vmname_c=vm_${cname}_$(printf "%02d" ${counter})
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500287 [ ! "$silent" = true ] && echo "# ${node}: creating ${vmname_c}"
288 vm_create ${node} ${vmname_c}
289 cmp_stats ${node}
290 ((counter++))
291 done
292 printf "# ${node}: done\n"
293 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500294fi
295
296errors
Alex Savatieievbadc4762019-09-30 13:46:37 -0500297