blob: 0b6c492f34292c6c8857c48ebdb8a1ae4b600f8c [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 Savatieieveaf0a992019-10-02 17:51:54 -05006
7tmp_out=$(mktemp)
8trap "rm -f ${tmp_out}" EXIT
9
10declare errors=()
11
12function 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 Savatieievd0ae84f2019-10-23 13:36:37 -050016 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 Savatieieveaf0a992019-10-02 17:51:54 -050019 printf "\t<compute_hostname> is a host shortname\n"
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050020 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 Savatieieveaf0a992019-10-02 17:51:54 -050027}
28
29OPTIND=1 # Reset in case getopts has been used previously in the shell.
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050030while getopts "h?:qdaf" opt; do
Alex Savatieieveaf0a992019-10-02 17:51:54 -050031 case "$opt" in
32 h|\?)
33 show_help
34 exit 0
35 ;;
36 q) silent=true
37 ;;
38 d) cleaning=true
39 ;;
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050040 a) all_computes=true
41 ;;
42 f) fill_mode=true
43 ;;
Alex Savatieieveaf0a992019-10-02 17:51:54 -050044 esac
45done
46
47shift $((OPTIND-1))
48[ "${1:-}" = "--" ] && shift
49
50# Check and create cmp_name var
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050051if [[ -z ${1+x} ]] && [[ ! ${all_computes} == "true" ]]; then
Alex Savatieieveaf0a992019-10-02 17:51:54 -050052 show_help
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050053 printf "\nERROR: No compute host specified\n"
Alex Savatieieveaf0a992019-10-02 17:51:54 -050054 exit 1
55fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050056if [[ ${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 Savatieieveaf0a992019-10-02 17:51:54 -050064else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050065 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 Savatieieveaf0a992019-10-02 17:51:54 -050073fi
Alex Savatieieveaf0a992019-10-02 17:51:54 -050074
75function 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 Savatieievbadc4762019-09-30 13:46:37 -050081
82function waitfor () {
83 counter=0
84 while [ ${counter} -lt 6 ]; do
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050085 ids=( $(openstack server list --name ${1} --status ${2} -f value -c ID) )
Alex Savatieievbadc4762019-09-30 13:46:37 -050086 if [ ${#ids[@]} -eq 0 ]; then
87 sleep 5
88 counter=$((counter + 1))
89 else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -050090 [ ! "$silent" = true ] && printf "# '${1}' reached status ${2}\n"
Alex Savatieievbadc4762019-09-30 13:46:37 -050091 break
92 fi
93 done
94}
95
96function getid() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -050097 openstack server list -c ID -c Name -f value | grep "${1}" | cut -d' ' -f1
Alex Savatieievbadc4762019-09-30 13:46:37 -050098}
99
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500100function get_all_cmp() {
101 openstack hypervisor list -f value -c "Hypervisor Hostname" | cut -d'.' -f1
102}
103
Alex Savatieievbadc4762019-09-30 13:46:37 -0500104function vm_create() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500105 [ ! "$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 Savatieievbadc4762019-09-30 13:46:37 -0500108 set +x
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500109 [ ! "$silent" = true ] && cat ${tmp_out}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500110}
111
112function vm_action() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500113 openstack server ${1} ${2} 2>${tmp_out} >/dev/nul
114 if [ ! 0 -eq $? ]; then
115 errors+=("${cmp_name}: $(cat ${tmp_out})")
116 fi
Alex Savatieievbadc4762019-09-30 13:46:37 -0500117}
118
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500119function errors {
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500120 echo "==== Errors"
121 for i in "${!errors[@]}"; do
122 printf "#%s\n" "${errors[$i]}"
123 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500124}
125
126function join_by { local IFS="$1"; shift; echo "$*"; }
127
Alex Savatieievbadc4762019-09-30 13:46:37 -0500128
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500129function 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
143function 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 Savatieievbadc4762019-09-30 13:46:37 -0500161
Alex Savatieievbadc4762019-09-30 13:46:37 -0500162if [ ! -f cvp.manifest ]; then
163 echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh"
164 exit 1
165else
166 source cvp.manifest
167fi
168
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500169[ ! "$silent" = true ] && echo "# Sourcing cvprc"
Alex Savatieievbadc4762019-09-30 13:46:37 -0500170source cvprc
171
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500172# #### Checking for CMP existence
173if [[ ! ${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
183fi
184
185if [[ ${cmp_name} == all ]]; then
186 echo "# Gathering compute count"
187 cmp_nodes=( $(get_all_cmp) )
188else
189 cmp_nodes=( ${cmp_name} )
190fi
191
192
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500193# #### Cleaning mode
194if [ $cleaning = true ]; then
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500195 # get all computes
196 if [[ ${cmp_name} == all ]]; then
197 echo "# Cleaning ${#cmp_nodes[@]} computes"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500198 else
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500199 echo "# Cleaning ${cmp_name}"
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500200 fi
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500201
202 # clean them
203 for node in ${cmp_nodes[@]}; do
204 clean_cmp ${node}
205 done
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500206 echo "# Done cleaning"
207 errors
208 exit 0
Alex Savatieievbadc4762019-09-30 13:46:37 -0500209fi
210
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500211# ###
212if [[ ! ${fill_mode} = true ]]; then
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500213 # ### CMP Checking mode
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500214 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 Savatieieveaf0a992019-10-02 17:51:54 -0500222 done
Alex Savatieievd0ae84f2019-10-23 13:36:37 -0500223 errors
224 exit 0
225else
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 Savatieieveaf0a992019-10-02 17:51:54 -0500243fi
244
245errors
Alex Savatieievbadc4762019-09-30 13:46:37 -0500246