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 |
| 4 | |
| 5 | tmp_out=$(mktemp) |
| 6 | trap "rm -f ${tmp_out}" EXIT |
| 7 | |
| 8 | declare errors=() |
| 9 | |
| 10 | function 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 | |
| 21 | OPTIND=1 # Reset in case getopts has been used previously in the shell. |
| 22 | while 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 |
| 33 | done |
| 34 | |
| 35 | shift $((OPTIND-1)) |
| 36 | [ "${1:-}" = "--" ] && shift |
| 37 | |
| 38 | # Check and create cmp_name var |
| 39 | if [[ -z ${1+x} ]]; then |
| 40 | show_help |
| 41 | printf "\nERROR: No compute host specified" |
| 42 | exit 1 |
| 43 | fi |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 44 | cmp_name=${1} |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 45 | |
| 46 | # Check and create vmname var |
| 47 | if [[ -z ${2+x} ]]; then |
| 48 | vmcount=1 |
| 49 | else |
| 50 | vmcount=${2} |
| 51 | fi |
| 52 | vmname=vm_${1} |
| 53 | |
| 54 | |
| 55 | function 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 Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 61 | |
| 62 | function 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 Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 70 | [ ! "$silent" = true ] && printf "# '${vmname}' reached status ${1}\n" |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 71 | break |
| 72 | fi |
| 73 | done |
| 74 | } |
| 75 | |
| 76 | function getid() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 77 | 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] | 78 | } |
| 79 | |
| 80 | function vm_create() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 81 | [ ! "$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 Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 84 | set +x |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 85 | [ ! "$silent" = true ] && cat ${tmp_out} |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | function vm_action() { |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 89 | openstack server ${1} ${2} 2>${tmp_out} >/dev/nul |
| 90 | if [ ! 0 -eq $? ]; then |
| 91 | errors+=("${cmp_name}: $(cat ${tmp_out})") |
| 92 | fi |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 95 | function errors { |
| 96 | echo "==== Errors" |
| 97 | for i in "${!errors[@]}"; do |
| 98 | printf "#%s\n" "${errors[$i]}" |
| 99 | done |
| 100 | } |
| 101 | |
| 102 | function join_by { local IFS="$1"; shift; echo "$*"; } |
| 103 | |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 104 | # temp file for commands |
| 105 | cmds=$(mktemp) |
| 106 | #trap "rm -f ${cmds}" EXIT |
| 107 | #echo "# Using tempfile: '${cmds}'" |
| 108 | |
| 109 | # trap "source adminrc" EXIT |
| 110 | |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 111 | if [ ! -f cvp.manifest ]; then |
| 112 | echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh" |
| 113 | exit 1 |
| 114 | else |
| 115 | source cvp.manifest |
| 116 | fi |
| 117 | |
| 118 | if [ -z ${cmp_name} ]; then |
| 119 | echo "CMP node name not specified" |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 123 | [ ! "$silent" = true ] && echo "# Sourcing cvprc" |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 124 | source cvprc |
| 125 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 126 | # #### Cleaning mode |
| 127 | if [ $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 Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 139 | fi |
| 140 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 141 | if [ ${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 Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 149 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 150 | cmp_stats ${cmp_name} |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 151 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 152 | vm_action pause ${vmid} |
| 153 | waitfor PAUSED |
| 154 | vm_action unpause ${vmid} |
| 155 | waitfor ACTIVE |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 156 | |
Alex Savatieiev | eaf0a99 | 2019-10-02 17:51:54 -0500 | [diff] [blame] | 157 | [ ! "$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" |
| 162 | else |
| 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" |
| 175 | fi |
| 176 | |
| 177 | errors |
Alex Savatieiev | badc476 | 2019-09-30 13:46:37 -0500 | [diff] [blame] | 178 | |