#!/bin/bash
# Maintainer: Martin Weise <martin.weise@tuwien.ac.at>
# Short:      Creates a owner or compute vm
# Synopsis:   ./bin/vm-create (owner|compute) username pubkey
function usage () {
  echo "USAGE: $0 username pubkey"
}

if [ $# -ne 2 ]; then
  usage
  exit 1
fi
echo -e "VM CREATE [Start] **************************************************************"
./resources/cssrv02/pubkey-check $2
if [ $? -ne 0 ]; then
  echo -e "\e[91mfailed: no public key file was provided\e[39m\n"
  exit 1
fi
echo -e "\e[92mok: [localhost]\e[39m\n"

echo -e "VM CREATE [Create] *************************************************************"
ansible-playbook "./create-owner-vm.yml" -e "username=$1" -e "pubkey=$2"

status=$?
if [ $status -eq 0 ]; then
  echo -e "\e[92mok: [localhost]\e[39m\n"
  cat /tmp/credentials.txt
  exit 0
else
  echo -e "VM CREATE [Done] ***************************************************************"
  echo -e "\e[91mfailed: playbook returned non-zero exit code\e[39m"
  exit 1
fi
