#!/bin/bash
SERVERNAME=phabricator-01-med-guest
echo "Creating VM ${SERVERNAME}"
PHABRICATOR_VOLUME=ba890df0-ecf4-46c1-b96e-d5c68046e0f0
GOGS_VOLUME=51c79d19-22d3-4d98-836d-cd34aeee755b

openstack server create  \
  --image ubuntu-18.04.2.1-amd64 \
  --user-data ${SERVERNAME}.data.yaml \
  --key-name "Sebastian Rojo" \
  --flavor m1.medium.md \
  --min 1 \
  --max 1 \
  --security-group gogs-ssh \
  --security-group phabricatro-ssh \
  --security-group DefaultSapian \
  --security-group WebApp \
  --availability-zone nuczone  \
  --nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957,v4-fixed-ip=172.16.253.163 \
  --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 PHABRICATOR_VOLUME ${PHABRICATOR_VOLUME}..."
      openstack server add volume --device /dev/vdb ${SERVERNAME} ${PHABRICATOR_VOLUME}
      echo "${SERVERNAME} Active. Attahing GOGS_VOLUME ${GOGS_VOLUME}..."
      openstack server add volume --device /dev/vdc ${SERVERNAME} ${GOGS_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
