#!/bin/bash
SERVERNAME=ss-01-med
echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image auto-sync/ubuntu-focal-20.04-amd64-server-20200504-disk1.img \
--user-data ${SERVERNAME}.cloud-init.yaml \
--key-name "sebastian_rojo_at_sapian" \
--flavor m1.small.8d \
--min 1 \
--max 1 \
--nic net-id=df8108c8-e2a7-4fa4-8345-104655ddd563 \
--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
