blob: 32b308e8a6053a6755d12211bf6df278dffa1fb4 [file] [log] [blame]
Alex Savatieievbadc4762019-09-30 13:46:37 -05001#!/bin/bash
Alex Savatieieveaf0a992019-10-02 17:51:54 -05002silent=false
3cleaning=false
4
5tmp_out=$(mktemp)
6trap "rm -f ${tmp_out}" EXIT
7
8declare errors=()
9
10function show_help {
11 printf "Compute check/filling script\n\t-h, -?\tShow this help\n"
12 printf "\t-d\tCleaning of earlier created VMs\n"
13 printf "\t-q\tSilent mode\n"
14 printf "\nUsage: cmp_check.sh <compute_hostname> [<vm_count>|def:1]\n"
15 printf "\t<compute_hostname> is a host shortname\n"
16 printf "\t<vm_count> is optional.\n"
17 printf "\t\tIf not set, script will check CMP: create, do actions and delete a VM\n"
18 printf "\t\tIf set, script will create a <vm_count> of VMs and exit\n\n"
19}
20
21OPTIND=1 # Reset in case getopts has been used previously in the shell.
22while getopts "h?:qd" opt; do
23 case "$opt" in
24 h|\?)
25 show_help
26 exit 0
27 ;;
28 q) silent=true
29 ;;
30 d) cleaning=true
31 ;;
32 esac
33done
34
35shift $((OPTIND-1))
36[ "${1:-}" = "--" ] && shift
37
38# Check and create cmp_name var
39if [[ -z ${1+x} ]]; then
40 show_help
41 printf "\nERROR: No compute host specified"
42 exit 1
43fi
Alex Savatieievbadc4762019-09-30 13:46:37 -050044cmp_name=${1}
Alex Savatieieveaf0a992019-10-02 17:51:54 -050045
46# Check and create vmname var
47if [[ -z ${2+x} ]]; then
48 vmcount=1
49else
50 vmcount=${2}
51fi
52vmname=vm_${1}
53
54
55function cmp_stats() {
56 cmpid=$(openstack hypervisor list --matching ${1} -f value -c ID)
57 vars=( $(openstack hypervisor show ${cmpid} -f shell -c running_vms -c vcpus -c vcpus_used -c memory_mb -c memory_mb_used) )
58 declare ${vars[@]}
59 printf "${1}: vms=%s vcpus=%s/%s ram=%s/%s\n" ${running_vms} ${vcpus_used} ${vcpus} ${memory_mb_used} ${memory_mb}
60}
Alex Savatieievbadc4762019-09-30 13:46:37 -050061
62function waitfor () {
63 counter=0
64 while [ ${counter} -lt 6 ]; do
65 ids=( $(openstack server list --name ${vmname} --status ${1} -f value -c ID) )
66 if [ ${#ids[@]} -eq 0 ]; then
67 sleep 5
68 counter=$((counter + 1))
69 else
Alex Savatieieveaf0a992019-10-02 17:51:54 -050070 [ ! "$silent" = true ] && printf "# '${vmname}' reached status ${1}\n"
Alex Savatieievbadc4762019-09-30 13:46:37 -050071 break
72 fi
73 done
74}
75
76function getid() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -050077 openstack server list -c ID -c Name -f value | grep "${1}" | cut -d' ' -f1
Alex Savatieievbadc4762019-09-30 13:46:37 -050078}
79
80function vm_create() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -050081 [ ! "$silent" = true ] && set -x
82 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
83 [ ! 0 -eq $? ] && errors+=("${1}/${2}: $(cat ${tmp_out})")
Alex Savatieievbadc4762019-09-30 13:46:37 -050084 set +x
Alex Savatieieveaf0a992019-10-02 17:51:54 -050085 [ ! "$silent" = true ] && cat ${tmp_out}
Alex Savatieievbadc4762019-09-30 13:46:37 -050086}
87
88function vm_action() {
Alex Savatieieveaf0a992019-10-02 17:51:54 -050089 openstack server ${1} ${2} 2>${tmp_out} >/dev/nul
90 if [ ! 0 -eq $? ]; then
91 errors+=("${cmp_name}: $(cat ${tmp_out})")
92 fi
Alex Savatieievbadc4762019-09-30 13:46:37 -050093}
94
Alex Savatieieveaf0a992019-10-02 17:51:54 -050095function errors {
96 echo "==== Errors"
97 for i in "${!errors[@]}"; do
98 printf "#%s\n" "${errors[$i]}"
99 done
100}
101
102function join_by { local IFS="$1"; shift; echo "$*"; }
103
Alex Savatieievbadc4762019-09-30 13:46:37 -0500104# temp file for commands
105cmds=$(mktemp)
106#trap "rm -f ${cmds}" EXIT
107#echo "# Using tempfile: '${cmds}'"
108
109# trap "source adminrc" EXIT
110
Alex Savatieievbadc4762019-09-30 13:46:37 -0500111if [ ! -f cvp.manifest ]; then
112 echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh"
113 exit 1
114else
115 source cvp.manifest
116fi
117
118if [ -z ${cmp_name} ]; then
119 echo "CMP node name not specified"
120 exit 1
121fi
122
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500123[ ! "$silent" = true ] && echo "# Sourcing cvprc"
Alex Savatieievbadc4762019-09-30 13:46:37 -0500124source cvprc
125
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500126# #### Cleaning mode
127if [ $cleaning = true ]; then
128 echo "# Cleaning mode (${cmp_name})"
129 vmid=( $(getid ${vmname}) )
130 if [ ${#vmid[@]} -ne 0 ]; then
131 [ ! "$silent" = true ] && echo "# Found ${#vmid[@]} previously created VMs. Cleaning."
132 vm_action delete "$(join_by ' ' "${vmid[@]}")"
133 else
134 [ ! "$silent" = true ] && echo "# ...no VMs found"
135 fi
136 echo "# Done cleaning"
137 errors
138 exit 0
Alex Savatieievbadc4762019-09-30 13:46:37 -0500139fi
140
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500141if [ ${vmcount} = 1 ]; then
142 echo "# Checking mode (${cmp_name})"
143 # ### CMP Checking mode
144 # if there are only 1 to boot, check actions with it too
145 cmp_stats ${cmp_name}
146 vm_create ${cmp_name} ${vmname}
147 waitfor ACTIVE
148 vmid=$(getid ${vmname})
Alex Savatieievbadc4762019-09-30 13:46:37 -0500149
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500150 cmp_stats ${cmp_name}
Alex Savatieievbadc4762019-09-30 13:46:37 -0500151
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500152 vm_action pause ${vmid}
153 waitfor PAUSED
154 vm_action unpause ${vmid}
155 waitfor ACTIVE
Alex Savatieievbadc4762019-09-30 13:46:37 -0500156
Alex Savatieieveaf0a992019-10-02 17:51:54 -0500157 [ ! "$silent" = true ] && echo "# ... deleting created VM (${vmid})"
158 vm_action delete ${vmid}
159
160 cmp_stats ${cmp_name}
161 printf "# Done checking ${cmp_name}\n"
162else
163 echo "# Filling mode (${cmp_name})"
164 # ### CMP fillling mode
165 # if vmcount>1, just create them and exit
166 counter=1
167 while [[ $counter -lt ${vmcount}+1 ]]; do
168 vmname_c=${vmname}_$(printf "%02d" ${counter})
169 [ ! "$silent" = true ] && echo "# ... creating VM on ${cmp_name} using name of ${vmname_c}"
170 vm_create ${cmp_name} ${vmname_c}
171 cmp_stats ${cmp_name}
172 ((counter++))
173 done
174 printf "# Done filling ${cmp_name}\n"
175fi
176
177errors
Alex Savatieievbadc4762019-09-30 13:46:37 -0500178