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

openstack server create  \
--image auto-sync/ubuntu-bionic-18.04-amd64-server-20200407-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=349d35b9-d817-4c62-b04a-4517248a719b \
--nic net-id=6027791a-f238-40af-aa7d-79b44b41e84d,v4-fixed-ip=172.22.226.114 \
--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
