#!/bin/bash
SERVERNAME=rc4-02-med-dialbox-cloud
#CONFIG_VOLUME=1e96c0cb-e074-4bea-a2a7-62e8c198f433

echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image "debian-9-openstack-amd64" \
--user-data data-${SERVERNAME}.yaml \
--key-name "Sebastian Rojo" \
--flavor m1.medium.md \
--min 1 \
--max 1 \
--security-group 7d008d67-dcb6-481c-8f3a-f46f18ee6bef \
--security-group ngcp-rc4 \
--availability-zone novacompute1:novacompute3.sapian.com.co  \
--nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957 \
--nic net-id=e0b1f231-fa1c-4e90-b648-fc27c3f3e8a0,v4-fixed-ip=181.129.77.254 \
--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
