#!/bin/bash

set -x

source {{ overcloudrc }}

image_name={{ image_name }}
openstack image show $image_name
if [ "$?" != "0" ]
then
    if [ ! -f cirros-0.4.0-x86_64-disk.img ]
    then
        curl -o cirros-0.4.0-x86_64-disk.img http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img
    fi

    openstack image create "cirros-ovn-migration-{{ resource_suffix }}"  --file cirros-0.4.0-x86_64-disk.img \
--disk-format qcow2 --container-format bare --public
    image_name="cirros-ovn-migration-{{ resource_suffix }}"
fi


openstack flavor create ovn-migration-{{ resource_suffix }} --ram 1024 --disk 1 --vcpus 1

openstack keypair create ovn-migration-{{ resource_suffix }} --private-key {{ ovn_migration_temp_dir }}/ovn_migration_ssh_key

openstack security group create ovn-migration-sg-{{ resource_suffix }}

openstack security group rule create --ingress --protocol icmp ovn-migration-sg-{{ resource_suffix }}

openstack security group rule create --ingress --protocol tcp --dst-port 22  ovn-migration-sg-{{ resource_suffix }}

openstack network create ovn-migration-net-{{ resource_suffix }}

neutron net-update ovn-migration-net-{{ resource_suffix }} --mtu 1442

openstack subnet create  --network ovn-migration-net-{{ resource_suffix }}  --subnet-range 172.168.168.0/24 ovn-migration-subnet-{{ resource_suffix }}

num_hypervisors=`openstack hypervisor stats show | grep count | awk '{print $4}'`

openstack server create --flavor ovn-migration-{{ resource_suffix }} --image $image_name \
--key-name ovn-migration-{{ resource_suffix }} \
--nic net-id=ovn-migration-net-{{ resource_suffix }} \
--security-group ovn-migration-sg-{{ resource_suffix }} \
--min $num_hypervisors --max $num_hypervisors \
ovn-migration-server-{{ resource_suffix }}


openstack router create ovn-migration-router-{{ resource_suffix }}

openstack router set --external-gateway {{ public_network_name }} ovn-migration-router-{{ resource_suffix }}

openstack router add subnet ovn-migration-router-{{ resource_suffix }} ovn-migration-subnet-{{ resource_suffix }}

for i in $(seq 1 $num_hypervisors)
do
    num_attempts=0
    while true
    do
        openstack server show ovn-migration-server-{{ resource_suffix }}-$i  -c status | grep ACTIVE
        if [ "$?" == "0" ]; then
            break
        fi
        sleep 5
        num_attempts=$((num_attempts+1))
        if [ $num_attempts -gt 24 ]
        then
            echo "VM is not up even after 2 minutes. Something is wrong"
            exit 1
        fi
    done

    vm_ip=`openstack server show ovn-migration-server-{{ resource_suffix }}-$i -c addresses | grep addresses | awk '{ split($4, ip, "="); print ip[2]}'`
    port_id=`openstack port list | grep $vm_ip | awk '{print $2}'`

    # Wait till the port is ACTIVE
    echo "Wait till the port is ACTIVE"
    port_status=`openstack port show $port_id -c status | grep status | awk '{print $4}'`

    num_attempts=0
    while [ "$port_status" != "ACTIVE" ]
    do
        num_attempts=$((num_attempts+1))
        sleep 5
        port_status=`openstack port show $port_id -c status | grep status | awk '{print $4}'`
        echo "Port status = $port_status"
        if [ $num_attempts -gt 24 ]
        then
            echo "Port is not up even after 2 minutes. Something is wrong"
            exit 1
        fi
    done

    echo "VM is up and the port is ACTIVE"

    server_ip=`openstack floating ip create --port $port_id \
{{ public_network_name }} -c floating_ip_address | grep floating_ip_address \
| awk '{print $4'}`

    echo $server_ip >> {{ ovn_migration_temp_dir }}/server_fips

    # Wait till the VM allows ssh connections

    vm_status="down"
    num_attempts=0
    while [ "$vm_status" != "up" ]
    do
        num_attempts=$((num_attempts+1))
        sleep 5
        openstack console log show ovn-migration-server-{{ resource_suffix }}-$i | grep "login:"
        if [ "$?" == "0" ]
        then
            vm_status="up"
        else
            if [ $num_attempts -gt 60 ]
            then
                echo "VM is not up with login prompt even after 5 minutes. Something is wrong."
                # Even though something seems wrong, lets try and ping.
                break
            fi
        fi
    done
done

chmod 0600 {{ ovn_migration_temp_dir }}/ovn_migration_ssh_key


for server_ip in `cat {{ ovn_migration_temp_dir }}/server_fips`
do
    num_attempts=0
    vm_reachable="false"
    while [ "$vm_reachable" != "true" ]
    do
        num_attempts=$((num_attempts+1))
        sleep 1
        ping -c 3 $server_ip
        if [ "$?" == "0" ]
        then
            vm_reachable="true"
        else
            if [ $num_attempts -gt 60 ]
            then
                echo "VM is not pingable. Something is wrong."
                exit 1
            fi
        fi
    done

    ssh -i {{ ovn_migration_temp_dir }}/ovn_migration_ssh_key -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null  cirros@$server_ip date
done

echo "Done with the resource creation : exiting"
exit 0
