#!/bin/bash
SERVERNAME=ccdllo-01-med-host
INSTALL_VOLUME=b0d6c514-39c4-4023-ab54-9feafef34db2
DOWNLOAD_VOLUME=96f8766c-6916-4f56-88ac-209fedc83c05

echo "Creating VM ${SERVERNAME}"

openstack server create \
--image ubuntu-16.04.4-amd64 \
--user-data data-${SERVERNAME}.yaml \
--key-name "Sebastian Rojo" \
--flavor m1.ubuntu.noswap.tiny  \
--min 1 \
--max 1 \
--security-group DefaultSapian \
--security-group nfs \
--availability-zone nuczone \
--nic net-id=7a9ae8e9-8425-4084-94cc-fb70c069e957,v4-fixed-ip=172.16.253.117 \
--wait \
${SERVERNAME}

for RUN in {1..50}; do
    if [ "$(nova show ${SERVERNAME} | awk '/status/ {print $4}')" == "ACTIVE" ]
    then
      #cinder reset-state --state available b0d6c514-39c4-4023-ab54-9feafef34db2
      #cinder reset-state --attach-status "detached" b0d6c514-39c4-4023-ab54-9feafef34db2
      #cinder reset-state --state available f76e3d73-6faf-4e51-ad87-2a4a7794a062
      #cinder reset-state --attach-status "detached" f76e3d73-6faf-4e51-ad87-2a4a7794a062
      echo "${SERVERNAME} Active. Attahing INSTALL_VOLUME ${INSTALL_VOLUME}..."
      openstack server add volume --device /dev/vdb ${SERVERNAME} ${INSTALL_VOLUME}
      sleep 5
      echo "${SERVERNAME} Active. Attahing DOWNLOAD_VOLUME ${DOWNLOAD_VOLUME}..."
      openstack server add volume --device /dev/vdc ${SERVERNAME} ${DOWNLOAD_VOLUME}
      sleep 5
      openstack server show ${SERVERNAME}
      exit 0
    else
      echo "${SERVERNAME} is not active yet"
      sleep 2
    fi
done
