#!/bin/bash

set -eux

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

export INSTACK_ROOT=${INSTACK_ROOT:-""}
if [ -n "$INSTACK_ROOT" ]; then
    export PATH=$PATH:$INSTACK_ROOT/instack-undercloud/scripts
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

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

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

    NETWORK_JSON=$(mktemp)
    NETWORK_JSON_DATA='{"physical":{}}'
    NETWORK_JSON_DATA=$(jq '.physical = .physical + {
        "gateway": "'$NETWORK_GATEWAY'",
        "metadata_server": "'$UNDERCLOUD_IP'",
        "cidr": "'$NETWORK_CIDR'",
        "allocation_start": "'$DHCP_START'",
        "allocation_end": "'$DHCP_END'",
        "name": "'$PHYSICAL_NETWORK'",
    }' <<< $NETWORK_JSON_DATA)
    if [ -n "${UNDERCLOUD_NAMESERVER:-}" ]; then
        NETWORK_JSON_DATA=$(jq '.physical = .physical + {
            "nameserver": "'$UNDERCLOUD_NAMESERVER'",
        }' <<< $NETWORK_JSON_DATA)
    fi
    jq . > $NETWORK_JSON <<< $NETWORK_JSON_DATA
    setup-neutron -n $NETWORK_JSON
    rm $NETWORK_JSON
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

if [ "$(hiera enable_mistral)" = "true" ]; then
    # 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
fi

# IP forwarding is needed to allow the overcloud nodes access to the outside
# internet in cases where they are on an isolated network.
sysctl -w net.ipv4.ip_forward=1
# Make it persistent
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/ip-forward.conf
