#!/bin/bash

set -eux

source /root/tripleo-undercloud-passwords
source /root/stackrc

INSTACK_ROOT=${INSTACK_ROOT:-""}
export INSTACK_ROOT
if [ -n "$INSTACK_ROOT" ]; then
    PATH=$PATH:$INSTACK_ROOT/instack-undercloud/scripts
    export PATH
fi

if [ ! -f /root/.ssh/authorized_keys ]; then
    sudo mkdir -p /root/.ssh
    sudo chmod 7000 /root/.ssh/
    sudo touch /root/.ssh/authorized_keys
    sudo chmod 600 /root/.ssh/authorized_keys
fi

if [ ! -f /root/.ssh/id_rsa ]; then
    ssh-keygen -b 1024 -N '' -f /root/.ssh/id_rsa
fi

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

UNDERCLOUD_IP=$(os-apply-config --key local-ip --type netaddress)
export UNDERCLOUD_IP

DHCP_START=$(os-apply-config --key neutron.dhcp_start --type netaddress)
DHCP_END=$(os-apply-config --key neutron.dhcp_end --type netaddress)
NETWORK_CIDR=$(os-apply-config --key neutron.network_cidr --type raw)
NETWORK_GATEWAY=$(os-apply-config --key neutron.network_gateway --type netaddress)
METADATA_SERVER=$UNDERCLOUD_IP
PHYSICAL_NETWORK=ctlplane

net_create=1
ctlplane_id=$(neutron net-list -f csv -c id -c name --quote none | tail -n +2 | grep ctlplane | cut -d, -f 1)
subnet_ids=$(neutron subnet-list -f csv -c id --quote none | tail -n +2)
subnet_id=

for subnet_id in $subnet_ids; do
    network_id=$(neutron subnet-show -f value -c network_id $subnet_id)
    if [ "$network_id" = "$ctlplane_id" ]; then
        break
    fi
done



if [ -n "$subnet_id" ]; then
    cidr=$(neutron subnet-show $subnet_id -f value -c cidr)
    # If the cidr's are equal, we can get by with just a network update
    if [ "$cidr" = "$NETWORK_CIDR" ]; then
        net_create=0
        neutron subnet-update $subnet_id \
            --allocation-pool start=$DHCP_START,end=$DHCP_END \
            --gateway $NETWORK_GATEWAY \
            --host-route destination=169.254.169.254/32,nexthop=$METADATA_SERVER
    else
        echo "New cidr $NETWORK_CIDR does not equal old cidr $cidr"
        echo "Will attempt to delete and recreate subnet $subnet_id"
    fi
fi

if [ "$net_create" -eq "1" ]; then
    # Delete the subnet and network to make sure it doesn't already exist
    if neutron subnet-list | grep start; then
        neutron subnet-delete $(neutron subnet-list | grep start | awk '{print $2}')
    fi
    if neutron net-show ctlplane; then
        neutron net-delete ctlplane
    fi

    neutron net-create ctlplane \
        --provider:network_type flat \
        --provider:physical_network ctlplane

    neutron subnet-create --name ctlplane-subnet \
        --allocation-pool start=$DHCP_START,end=$DHCP_END \
        --gateway $NETWORK_GATEWAY \
        --host-route destination=169.254.169.254/32,nexthop=$METADATA_SERVER \
        ctlplane $NETWORK_CIDR
fi

# Disable nova quotas
openstack quota set --cores -1 --instances -1 --ram -1 $(openstack project show admin | awk '$2=="id" {print $4}')

# instack-prepare-for-overcloud
rm -rf $HOME/.novaclient

# load workflows
for workbook in $(mistral workbook-list | grep tripleo | cut -f 2 -d ' '); do
    mistral workbook-delete $workbook
done
for workflow in $(mistral workflow-list | grep tripleo | cut -f 2 -d ' '); do
    mistral workflow-delete $workflow
done
for workbook in $(ls /usr/share/openstack-tripleo-common/workbooks/*); do
    mistral workbook-create $workbook
done

