#!/bin/bash
#
# This is a helper to get tripleo-cd admins to prepare a ci-overcloud for CI repeatably.
set -ux
set -o pipefail

source $1

set -e

# These are only here for local install tests and should be noop if deploy-ci-overcloud was used to create overcloud
$TRIPLEO_ROOT/tripleo-incubator/scripts/assert-admin-users $TRIPLEO_ROOT/tripleo-incubator/tripleo-cloud/tripleo-cd-admins
$TRIPLEO_ROOT/tripleo-incubator/scripts/assert-users $TRIPLEO_ROOT/tripleo-incubator/tripleo-cloud/tripleo-cd-admins
$TRIPLEO_ROOT/tripleo-incubator/scripts/assert-users $TRIPLEO_ROOT/tripleo-incubator/tripleo-cloud/tripleo-cd-users

# Get the ID of the nodepool tenant
NODEPOOL_TENANT_ID=$(openstack project list | awk '$4=="openstack-nodepool" {print $2}')

# Each instance uses 3 ports
neutron quota-update --tenant-id $NODEPOOL_TENANT_ID --port 300
nova quota-update --cores -1 --ram $TE_NP_RAM_QUOTA --instances 100 --floating-ips 100 --fixed-ips -1 $NODEPOOL_TENANT_ID

# Create the test network and subnet if they don't exist
if ! neutron net-show tripleo-bm-test ; then
    neutron net-create tripleo-bm-test --tenant-id $NODEPOOL_TENANT_ID --provider:network_type flat --provider:physical_network $TE_PROVIDER_NET_NAME
fi
TESTNETID=$(neutron net-show tripleo-bm-test | awk '$2=="id" {print $4}')
if [ $(neutron net-show tripleo-bm-test | awk '$2=="subnets" {print $4}') == "|" ] ; then
    neutron subnet-create --ip_version 4 --tenant-id $NODEPOOL_TENANT_ID \
        --allocation-pool start=172.16.0.1,end=172.16.3.234 --no-gateway \
        $TESTNETID 172.16.0.0/22 --dns-nameservers list=true 8.8.8.8
fi

# update subnet of default-net with some dns
DEFAULTSUBNETID=$(neutron net-show default-net | awk '$2=="subnets" {print $4}')
neutron subnet-update $DEFAULTSUBNETID --dns-nameservers list=true 8.8.8.8

# we may already have this so keystone may error
# XXX: we can query this membership.
openstack role add --user tripleo-ci --project openstack-nodepool Member || true

# Create the flavor nodepool uses
nova flavor-show h1.ci || nova flavor-create h1.ci auto 8192 20 1

# Create a flavor for mirrors with more disk space then is normal
nova flavor-show d1.medium || nova flavor-create d1.medium auto 4096 200 2

USE_CACHE=${USE_CACHE:-0}
# The the images that nodepool uses to creat templates
F20_IMAGE_FILE=$TRIPLEO_ROOT/fedora-20.x86_64.qcow2
if [ ! -e $F20_IMAGE_FILE -o "$USE_CACHE" != "1" ] ; then
    FEDORA_IMAGE=$(wget -q http://dl.fedoraproject.org/pub/fedora/linux/updates/20/Images/x86_64/ -O - | grep -o -E 'href="([^"#]+qcow2)"' | cut -d'"' -f2)
    curl http://dl.fedoraproject.org/pub/fedora/linux/updates/20/Images/x86_64/$FEDORA_IMAGE > $F20_IMAGE_FILE
fi
PRECISE_IMAGE_FILE=$TRIPLEO_ROOT/precise-server-cloudimg-amd64-disk1.img
PRECISE_IMAGE_URL=http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64-disk1.img
if [ ! -e $PRECISE_IMAGE_FILE -o "$USE_CACHE" != "1" ] ; then
    curl $PRECISE_IMAGE_URL -o $PRECISE_IMAGE_FILE
fi
if ! glance image-show "Fedora 20 64-bit" ; then
    glance image-create --name "Fedora 20 64-bit" --disk-format qcow2 --container-format bare --is-public 1 --file $F20_IMAGE_FILE --progress
fi
if ! glance image-show "Ubuntu Precise 12.04 LTS Server 64-bit" ; then
    glance image-create --name "Ubuntu Precise 12.04 LTS Server 64-bit" --disk-format qcow2 --container-format bare --is-public 1 --file $PRECISE_IMAGE_FILE --progress
fi

NETLIST=$(neutron net-list)
DEFAULT_NET=$(awk '/default-net/ { print $2 }' <<< "$NETLIST")
EXT_NET=$(awk '/ext-net/ { print $2 }' <<< "$NETLIST")

# Create and boot test env gear broker
BROKER_IMG=$TRIPLEO_ROOT/te-broker.qcow2
if [ ! -e $BROKER_IMG -o "$USE_CACHE" != "1" ] ; then
    $TRIPLEO_ROOT/diskimage-builder/bin/disk-image-create -a amd64 -o $BROKER_IMG \
        $TE_DISTRO vm geard stackuser dhcp-all-interfaces
fi
# XXX(lifeless) make a load-image patch for virt use
if glance image-show te-broker &> /dev/null; then
    glance image-delete te-broker
fi
glance image-create --name "te-broker" --disk-format qcow2 --container-format bare --is-public 1 --file $BROKER_IMG --progress

NP_CREDS="--os-username=openstack-nodepool --os-password=$TE_OVERCLOUDNPPASSWD --os-tenant-name=openstack-nodepool"
if ! nova $NP_CREDS keypair-list | grep -q " default "; then
    nova $NP_CREDS keypair-add default > ~/.ssh/tripleo-ci
    chmod 0600 ~/.ssh/tripleo-ci
fi
if ! nova $NP_CREDS show te-broker ; then
    nova $NP_CREDS boot --image "te-broker" --flavor $TE_BROKER_FLAVOR \
        --nic net-id=$DEFAULT_NET \
        --nic net-id=$TESTNETID,v4-fixed-ip=172.16.3.254 --key-name default \
        te-broker
fi

# Create and boot squid broker
SQUID_IMG=$TRIPLEO_ROOT/squid.qcow2
if [ ! -e $SQUID_IMG -o "$USE_CACHE" != "1" ] ; then
    $TRIPLEO_ROOT/diskimage-builder/bin/disk-image-create -a amd64 -o $SQUID_IMG \
        $TE_DISTRO vm squid stackuser dhcp-all-interfaces
fi
# XXX(lifeless) make a load-image patch for virt use
if glance image-show squid &> /dev/null; then
    glance image-delete squid
fi
glance image-create --name squid --disk-format qcow2 --container-format bare --is-public 1 --file $SQUID_IMG --progress

if ! nova $NP_CREDS show squid ; then
    nova $NP_CREDS boot --image "squid" --flavor $TE_BROKER_FLAVOR \
        --nic net-id=$DEFAULT_NET \
        --nic net-id=$TESTNETID,v4-fixed-ip=172.16.3.253 --key-name default \
        squid
fi

# Create and boot bandersnatch pypi mirror
BANDERSNATCH_IMG=$TRIPLEO_ROOT/bandersnatch.qcow2
if [ ! -e $BANDERSNATCH_IMG -o "$USE_CACHE" != "1" ] ; then
    $TRIPLEO_ROOT/diskimage-builder/bin/disk-image-create -a amd64 -o $BANDERSNATCH_IMG \
        $TE_DISTRO vm bandersnatch stackuser dhcp-all-interfaces
fi
# XXX(lifeless) make a load-image patch for virt use
if glance image-show bandersnatch &> /dev/null; then
    glance image-delete bandersnatch
fi
glance image-create --name bandersnatch --disk-format qcow2 --container-format bare --is-public 1 --file $BANDERSNATCH_IMG --progress

if ! nova $NP_CREDS show bandersnatch ; then
    nova $NP_CREDS boot --image "bandersnatch" --flavor d1.medium \
        --nic net-id=$DEFAULT_NET \
        --nic net-id=$TESTNETID,v4-fixed-ip=172.16.3.252 --key-name default \
        bandersnatch
fi

# Assign a floting IP to the broker
TE_ID=$(nova $NP_CREDS show te-broker | awk '$2=="id" { print $4 }')
FLOATING_IP=$(nova $NP_CREDS floating-ip-list | awk '$4=="'$TE_ID'" {print $2}')
if [ -z "$FLOATING_IP" ]; then
    FLOATING_IP=$(nova $NP_CREDS floating-ip-create | awk '$8=="ext-net" {print $2}')
    nova $NP_CREDS add-floating-ip te-broker $FLOATING_IP
fi

# Ensure the default tripleo security group has ICMP and SSH inbound access

if ! neutron $NP_CREDS security-group-rule-list | grep -q 'default.*icmp' ; then
    neutron $NP_CREDS security-group-rule-create default --protocol icmp \
        --direction ingress --port-range-min 8
fi
if ! neutron $NP_CREDS security-group-rule-list | grep -q 'default.*tcp' ; then
    neutron $NP_CREDS security-group-rule-create default --protocol tcp \
        --direction ingress --port-range-min 22 --port-range-max 22
fi
