#!/bin/bash
SERVERNAME=dialbox6-01-med
CONFIG_VOLUME=70c9da49-524d-4f6c-a805-6de457b54e62

echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image auto-sync/ubuntu-bionic-18.04-amd64-server-20200317-disk1.img \
--user-data ${SERVERNAME}.cloud-init.yaml \
--key-name "sebastian_rojo_at_sapian" \
--flavor m1.small.8d \
--min 1 \
--max 1 \
--nic port-id=2b8d3d19-4d23-4c01-aa28-6235b1dccd6a \
--wait \
${SERVERNAME}

for RUN in {1..50}; do
    if [ "$(openstack server show ${SERVERNAME} | awk '/status/ {print $4}')" == "ACTIVE" ]
    then
      openstack server show ${SERVERNAME}
      echo "${SERVERNAME} Active. Attahing CONFIG_VOLUME ${CONFIG_VOLUME}..."
      openstack server add volume --device /dev/vdb ${SERVERNAME} ${CONFIG_VOLUME}
      openstack volume show ${CONFIG_VOLUME}
      openstack server show ${SERVERNAME}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
