Updates on CMP script

 - Wording
 - Silent mode
 - Stats info function
 - Errors collection and summary
 - Checking mode when vm count is not set
 - Filling mode when VM count is set, Create N VMs and exit
 - Cleaning mode

Change-Id: If0eebfdf8806c688be3340a35c61bc60e8023029
Related-PROD: PROD-33607
diff --git a/scripts/cmp_check.sh b/scripts/cmp_check.sh
index 5426f65..32b308e 100644
--- a/scripts/cmp_check.sh
+++ b/scripts/cmp_check.sh
@@ -1,6 +1,63 @@
 #!/bin/bash
+silent=false
+cleaning=false
+
+tmp_out=$(mktemp)
+trap "rm -f ${tmp_out}" EXIT
+
+declare errors=()
+
+function show_help {
+    printf "Compute check/filling script\n\t-h, -?\tShow this help\n"
+    printf "\t-d\tCleaning of earlier created VMs\n"
+    printf "\t-q\tSilent mode\n"
+    printf "\nUsage: cmp_check.sh <compute_hostname> [<vm_count>|def:1]\n"
+    printf "\t<compute_hostname> is a host shortname\n"
+    printf "\t<vm_count> is optional.\n"
+    printf "\t\tIf not set, script will check CMP: create, do actions and delete a VM\n"
+    printf "\t\tIf set, script will create a <vm_count> of VMs and exit\n\n"
+}
+
+OPTIND=1 # Reset in case getopts has been used previously in the shell.
+while getopts "h?:qd" opt; do
+    case "$opt" in
+    h|\?)
+        show_help
+        exit 0
+        ;;
+    q)  silent=true
+        ;;
+    d)  cleaning=true
+        ;;
+    esac
+done
+
+shift $((OPTIND-1))
+[ "${1:-}" = "--" ] && shift
+
+# Check and create cmp_name var
+if [[ -z ${1+x} ]]; then
+   show_help
+   printf "\nERROR: No compute host specified"
+   exit 1
+fi
 cmp_name=${1}
-vmname=vm_${1}_01
+
+# Check and create vmname var
+if [[ -z ${2+x} ]]; then
+   vmcount=1
+else
+   vmcount=${2}
+fi
+vmname=vm_${1}
+
+
+function cmp_stats() {
+   cmpid=$(openstack hypervisor list --matching ${1} -f value -c ID)
+   vars=( $(openstack hypervisor show ${cmpid} -f shell -c running_vms -c vcpus -c vcpus_used -c memory_mb -c memory_mb_used) )
+   declare ${vars[@]}
+   printf "${1}: vms=%s vcpus=%s/%s ram=%s/%s\n" ${running_vms} ${vcpus_used} ${vcpus} ${memory_mb_used} ${memory_mb}
+}
 
 function waitfor () {
    counter=0
@@ -10,26 +67,40 @@
          sleep 5
          counter=$((counter + 1))
       else
-         printf "# '${vmname}' reached status ${1}\n"
+         [ ! "$silent" = true ] && printf "# '${vmname}' reached status ${1}\n"
          break
       fi
    done
 }
 
 function getid() {
-   openstack server list --name ${1} -f value -c ID
+   openstack server list -c ID -c Name -f value | grep "${1}" | cut -d' ' -f1
 }
 
 function vm_create() {
-   set -x
-   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>&1 >/dev/nul
+   [ ! "$silent" = true ] && set -x
+   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
+   [ ! 0 -eq $? ] && errors+=("${1}/${2}: $(cat ${tmp_out})")
    set +x
+   [ ! "$silent" = true ] && cat ${tmp_out}
 }
 
 function vm_action() {
-   openstack server ${1} ${2}
+   openstack server ${1} ${2} 2>${tmp_out} >/dev/nul
+   if [ ! 0 -eq $? ]; then
+      errors+=("${cmp_name}: $(cat ${tmp_out})")
+   fi
 }
 
+function errors {
+        echo "==== Errors"
+        for i in "${!errors[@]}"; do
+                printf "#%s\n" "${errors[$i]}"
+        done
+}
+
+function join_by { local IFS="$1"; shift; echo "$*"; }
+
 # temp file for commands
 cmds=$(mktemp)
 #trap "rm -f ${cmds}" EXIT
@@ -37,7 +108,6 @@
 
 # trap "source adminrc" EXIT
 
-echo "### CMP check for booting VMs"
 if [ ! -f cvp.manifest ]; then
    echo "ERROR: No cvp.manifest file detected. Consider running prepare.sh"
    exit 1
@@ -50,31 +120,59 @@
    exit 1
 fi
 
-echo "# Sourcing cvprc"
+[ ! "$silent" = true ] && echo "# Sourcing cvprc"
 source cvprc
 
-echo "# Checking for previously created VMs"
-vmid=( $(getid ${vmname}) )
-if [ ${#vmid[@]} -ne 0 ]; then
-   echo "# Found previously created VMs. Cleaning."
-   vm_action delete ${vmid[@]}
-else
-   echo "# ...no VMs found"
+# #### Cleaning mode
+if [ $cleaning = true ]; then
+   echo "# Cleaning mode (${cmp_name})"
+   vmid=( $(getid ${vmname}) )
+   if [ ${#vmid[@]} -ne 0 ]; then
+      [ ! "$silent" = true ] && echo "# Found ${#vmid[@]} previously created VMs. Cleaning."
+      vm_action delete "$(join_by ' ' "${vmid[@]}")"
+   else
+      [ ! "$silent" = true ] && echo "# ...no VMs found"
+   fi
+   echo "# Done cleaning"
+   errors
+   exit 0
 fi
 
-printf "### Checking '${cmp_name}': Create, Pause, Unpause, Delete a VM\n"
-echo "# ... creating VM on ${cmp_name} using name of ${vmname}"
-vm_create ${cmp_name} ${vmname}
-waitfor ACTIVE
-vmid=$(openstack server list --name ${vmname} -f value -c ID)
+if [ ${vmcount} = 1 ]; then
+   echo "# Checking mode (${cmp_name})"
+   # ### CMP Checking mode
+   # if there are only 1 to boot, check actions with it too
+   cmp_stats ${cmp_name}
+   vm_create ${cmp_name} ${vmname}
+   waitfor ACTIVE
+   vmid=$(getid ${vmname})
 
-vm_action pause ${vmid}
-waitfor PAUSED
-vm_action unpause ${vmid}
-waitfor ACTIVE
+   cmp_stats ${cmp_name}
 
-echo "# ... deleting create VM (${vmid})"
-vm_action delete ${vmid}
+   vm_action pause ${vmid}
+   waitfor PAUSED
+   vm_action unpause ${vmid}
+   waitfor ACTIVE
 
-printf "\n# Done checking ${cmp_name}\n"
+   [ ! "$silent" = true ] && echo "# ... deleting created VM (${vmid})"
+   vm_action delete ${vmid}
+
+   cmp_stats ${cmp_name}
+   printf "# Done checking ${cmp_name}\n"
+else
+   echo "# Filling mode (${cmp_name})"
+   # ### CMP fillling mode
+   # if vmcount>1, just create them and exit
+   counter=1
+   while [[ $counter -lt ${vmcount}+1 ]]; do
+      vmname_c=${vmname}_$(printf "%02d" ${counter})
+      [ ! "$silent" = true ] && echo "# ... creating VM on ${cmp_name} using name of ${vmname_c}"
+      vm_create ${cmp_name} ${vmname_c}
+      cmp_stats ${cmp_name}
+      ((counter++))
+   done
+   printf "# Done filling ${cmp_name}\n"
+fi
+
+errors