#!/bin/bash
SERVERNAME=wp-01-med-573146306811
MEDIA_VOLUME=fb85475f-7ea1-4822-960d-646a12fcdfd3
echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image auto-sync/ubuntu-focal-20.04-amd64-server-20200921.1-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.189 \
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} ${MEDIA_VOLUME}
      #openstack server add floating ip ${SERVERNAME} 172.16.253.108
      openstack volume show ${MEDIA_VOLUME}
      openstack server show ${SERVERNAME}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
