#!/bin/bash

set -eux

### --start_docs
## Validate the overcloud deployment
## =================================

## .. note: The following set of scripts are generated from a jinja template
##    which may be viewed here: https://github.com/openstack/tripleo-quickstart-extras/blob/master/roles/validate-simple/templates/overcloud-validate.sh.j2

## * Create cleanup env function for heat
## ::

function cleanup {
    openstack stack delete --yes {{ validate_stack_name }}
    if [[ $({{ working_dir }}/wait_for -w 300 -d 30 -s "Stack not found" -- "openstack stack show {{ validate_stack_name }}") ]]; then
        echo "openstack stack delete"
    else
        if [[ $(openstack stack list | grep {{ validate_stack_name }} | grep -i failed) ]]; then
            openstack stack delete --yes {{ validate_stack_name }}
        fi
    fi
    openstack image list | awk '/pingtest/ {print $2}' | while read IMAGEID; do openstack image delete $IMAGEID; done
    neutron net-delete {{ public_net_name }}
}
{% if not skip_pingtest_cleanup %}
# trap on exit #nodocs
trap cleanup EXIT #nodocs
{% endif %}

{% if generate_pingtest_subunit|bool %}
# Install dependencies for generate_subunit #nodocs
sudo yum install -y python-os-testr

## * Start measuring time of test
## ::

TEST_START=$(date +%s)
{% endif %}

## * Source tenantrc file
## ::

source {{ working_dir }}/{{ tenantrc }}

## * Check if an environment is already deployed and, in case, cleanup it
## ::

[ "x$(openstack stack list | grep {{ validate_stack_name }} || true)" != "x" ] && cleanup
# get validate image
if [[ ! -d {{ validate_image_dir }} ]]; then
    mkdir {{ validate_image_dir }};
fi

IMAGE_PATH={{ validate_image_dir }}/{{ image_disk }}
INITRAMFS_PATH={{ validate_image_dir }}/{{ image_initramfs }}
KERNEL_PATH={{ validate_image_dir }}/{{ image_kernel }}

if [[ -e $IMAGE_PATH && -e $INITRAMFS_PATH && -e $KERNEL_PATH ]]; then
    echo "Using existing images in {{ validate_image_dir }}"
else
    pushd {{ validate_image_dir }}
    if [[ -e {{ validate_image_path }} ]]; then
        tar -zxvf {{ validate_image_path }}
    else
        curl -L -s -S -o {{ validate_image_dir }}/{{ validate_image_file }} {{ validate_image_url }}/{{ validate_image_file }}
        tar -zxvf {{ validate_image_file }}
        rm -f "{{ validate_image_file }}"
    fi
    popd
fi

## * Upload cirros image into glance
## ::

ramdisk_id=$(openstack image create pingtest_initramfs --public --container-format ari --disk-format ari --file $INITRAMFS_PATH | awk '/ id / {print $4}')
kernel_id=$(openstack image create pingtest_kernel --public --container-format aki --disk-format aki --file $KERNEL_PATH | awk '/ id / {print $4}')
openstack image create {{ validate_image_name }} --public --container-format ami --disk-format ami \
    --property kernel_id=$kernel_id --property ramdisk_id=$ramdisk_id --file $IMAGE_PATH

## * Create external network

## .. note: Liberty compatibility:
##    (heat can't create an external network in Liberty)
## ::

if ! neutron net-list | grep '{{ public_net_name }}'; then

neutron net-create {{ public_net_name }} --router:external=True \
    --provider:network_type {{ public_net_type }} \
    --provider:physical_network {{ public_physical_network }} \
{% if public_segmentation_id != '' %}
    --provider:segmentation_id {{ public_segmentation_id }}
{% endif %}


neutron subnet-create --name ext-subnet \
    --allocation-pool \
    start={{ public_net_pool_start }},end={{ public_net_pool_end }} \
    --disable-dhcp \
    --gateway {{ public_net_gateway }} \
    {{ public_net_name }} {{ floating_ip_cidr }}

fi

## * Create test ping stack
## ::

cat << EOF > {{ working_dir }}/validate-template-override.yaml
{{ validate_template_environment|to_nice_yaml }}
EOF

openstack stack create {{ validate_stack_name }} \
    --template {{ validate_template_path }}/{{ validate_template }} \
    --environment {{ working_dir }}/validate-template-override.yaml

## * Wait for the stack creation to complete
## * Give time to the instance to boot

### --stop_docs
/bin/bash {{ working_dir }}/wait_for -w 600 --delay 30 \
    --success-match {{ validate_success_status }} \
    --fail-match {{ validate_failed_status }} -- openstack stack show {{ validate_stack_name }}
sleep 30

### --start_docs
## * Get the floating ip
## ::

### --stop_docs
set +e
### --start_docs

vm1_ip=`openstack stack output show {{ validate_stack_name }} server1_public_ip --format json | \
        jq -r '.["output_value"]'`

sleep 30
ping -c 3 $vm1_ip

## * Print debug output if ping fails
## ::

if [ "$?" == "0" ]; then
    echo "Overcloud pingtest, SUCCESS"
{% if generate_pingtest_subunit|bool %}
    generate-subunit $TEST_START $(( $(date +%s) - TEST_START)) success pingtest > {{ working_dir }}/pingtest.subunit
{% endif %}
    exit 0
else
    nova console-log Server1
    nova show Server1
    nova service-list
    neutron agent-list
    openstack stack show {{ validate_stack_name }}
    echo "Overcloud pingtest, FAIL"
{% if generate_pingtest_subunit|bool %}
    generate-subunit $TEST_START $(( $(date +%s) - TEST_START)) fail pingtest > {{ working_dir }}/pingtest.subunit
{% endif %}
    exit 1
fi

### --stop_docs
