#!/bin/bash
SERVERNAME=dialbox-02-med-guest
CONFIG_VOLUME=a5f61b10-a20d-4150-8b01-916553a844b7
echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image DialBox-4.0.2-x86_64 \
--user-data data-${SERVERNAME}.yaml \
--key-name "Sebastian Rojo" \
--flavor m1.dialbox.small \
--min 1 \
--max 1 \
--security-group 7d008d67-dcb6-481c-8f3a-f46f18ee6bef \
--security-group DialBox251 \
--availability-zone nuczone \
--nic net-id=65faf089-6a7e-4ce3-8f44-2c302d4ffc93,v4-fixed-ip=172.16.251.155 \
--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} ${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
