#!/bin/bash
SERVERNAME=ipa-01-med

echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image fedora-31-x86_64 \
--user-data ${SERVERNAME}.cloud-init.yaml \
--key-name "sebastian_rojo_at_sapian" \
--flavor m1.small.8d \
--min 1 \
--max 1 \
--nic net-id=349d35b9-d817-4c62-b04a-4517248a719b \
--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 server add floating ip ${SERVERNAME} 172.16.253.108
      #openstack volume show ${CONFIG_VOLUME}
      openstack server show ${SERVERNAME}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
