#!/bin/bash
echo $0 started at $(date)

PARTICIPANT=${1}
. ./common-vars
. ./openstack_credentials.rc $PARTICIPANT

VM_NAME=`expr charmedosm-${PARTICIPANT}`
echo "Creating public port"
# This port gets created as the admin
openstack port create --security-group default --network osm-ext $VM_NAME
wait



NOT_READY=1
while [ $NOT_READY -eq 1 ] ; do
    openstack server list
    NOT_READY=$?
done


echo "Creating Keypair"
openstack keypair create --public-key ./hackfest_rsa.pub ${KEY_NAME} &
wait

echo "Launching OSM VM"
openstack server create --key-name ${KEY_NAME} --flavor ${FLAVOR} --image ${FOCAL} --nic port-id=$VM_NAME --user-data ./osm-cloud-init.yaml $VM_NAME &
wait

echo "Waiting for OSM VM to be ready"
IP=`openstack server list --name $VM_NAME --column Networks -f yaml | head -3 | tail -1 | grep -o '[0-9]\+[.][0-9]\+[.][0-9]\+[.][0-9]\+'`
while [ 1 ] ; do
    sleep 5
    ALIVE=$(ssh -T -o ConnectTimeout=1 -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} 'cloud-init status --wait | tail -1' 2> /dev/null)
    if [ "${ALIVE}" == "status: done" ] ; then break ; fi
done

echo $0 $@ complete at $(date)