#!/bin/bash
SERVERNAME=mariadb-01-med
MYSQL_VOLUME=34a25853-5824-4bc2-91cc-dbb80910d929

echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image "auto-sync/ubuntu-bionic-18.04-amd64-server-20200218-disk1.img" \
--user-data ${SERVERNAME}.cloud-init.yaml \
--key-name "sebastian_rojo_at_sapian" \
--flavor m1.medium.md \
--min 1 \
--max 1 \
--nic net-id=349d35b9-d817-4c62-b04a-4517248a719b \
--wait \
${SERVERNAME}
#--nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957,v4-fixed-ip=172.16.253.144 \


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 ${MYSQL_VOLUME}..."
      openstack server add volume --device /dev/vdb ${SERVERNAME} ${MYSQL_VOLUME}
      #openstack server add floating ip ${SERVERNAME} 172.16.253.108
      openstack volume show ${MYSQL_VOLUME}
      openstack server show ${SERVERNAME}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
