#!/bin/bash
SERVERNAME=vdi.sapian.com.co
echo "Creating VM ${SERVERNAME}"

nova boot ${SERVERNAME} \
--image ubuntu-16.04-amd64 \
--user-data ${SERVERNAME}.yaml \
--key-name "Sebastian Rojo" \
--flavor m1.ubuntu.noswap.tiny \
--min-count 1 \
--max-count 1 \
--security-groups default \
--nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957 \


for RUN in {1..50}; do
    if [ "$(nova show ${SERVERNAME} | awk '/status/ {print $4}')" == "ACTIVE" ]
    then
      echo "${SERVERNAME} Active. Attahing openvpn Certs and Config volume..."
      nova volume-attach ${SERVERNAME} a21b2b77-8e34-44a3-bc3e-5a9392ca9463 /dev/vdb
      #nova add-floating-ip ${SERVERNAME} 172.16.253.107
      nova show ${SERVERNAME}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
