#!/bin/bash
SERVERNAME=repo-01-med-docker
CENTOSREPOID=929dee1f-2d3f-4c6b-88d1-1f34759a24f2
EPELREPOID=9b5911d0-7a7d-40d9-bb82-201466bed447

echo "Creating VM ${SERVERNAME}"

openstack server create  \
--image "ubuntu-18.04.0.2-amd64" \
--user-data data-${SERVERNAME}.yaml \
--key-name "Sebastian Rojo" \
--flavor m1.ubuntu.tiny.sd \
--min 1 \
--max 1 \
--security-group DefaultSapian \
--security-group dockerwebapps \
--availability-zone nuczone \
--nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957 \
--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...CENTOSREPOID"
      openstack server add volume ${SERVERNAME} ${CENTOSREPOID}
      echo "${SERVERNAME} Active. Attahing config...EPELREPOID"
      openstack server add volume ${SERVERNAME} ${EPELREPOID}
      exit 0
    else
      echo "VM ${SERVERNAME} is not active yet"
      sleep 2
    fi
done
