#!/bin/bash
#
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -eux
set -o pipefail

function show_options() {
    echo "Usage: $SCRIPT_NAME REGION_CODE CONFIG_BASE"
    echo
    echo "Deploy the CI overcloud for a region."
    echo
    echo "Options:"
    echo "    REGION_CODE: short name for the region. Required."
    echo "    CONFIG_BASE: directory containing the config data for the region."
    echo "                 defaults to \$PWD/configs"
    echo
    echo "    These two values will be combined to find the configs. For"
    echo "    instance, given '$SCRIPT_NAME hp2 myconfig', networking data"
    echo "    will be searched for in myconfig/hp2_networks.json"
}

if [ -z "$1" ]; then
    echo "REGION_CODE is required"
    echo
    show_options
    exit 1
fi

REGION_CODE="${1}"
CONFIG_DIR="${2:-$PWD/configs}"
FILE_PREFIX=$CONFIG_DIR/$REGION_CODE

CI_ENV_FILE=${FILE_PREFIX}_ci_env.json
NETWORKS_FILE=${FILE_PREFIX}_networks.json
OC_ENV_FILE=${FILE_PREFIX}_oc_env.json

if [[ ! -e $CI_ENV_FILE || ! -e $NETWORKS_FILE || ! -e $OC_ENV_FILE ]]; then
    echo "Missing config file. Couldn't find one of:"
    echo "${CI_ENV_FILE}, ${NETWORKS_FILE}, or ${OC_ENV_FILE}".
    echo "Please ensure all three are present then re-run this script"
    exit 2
fi

cd /

# We want to map the DC network into the VMs:
export OVERCLOUD_HYPERVISOR_PUBLIC_INTERFACE=$(jq -r .OvercloudHypervisorPublicInterface $OC_ENV_FILE)

# API endpoint for host file injection
export OVERCLOUD_NAME=$(jq -r .OvercloudName $OC_ENV_FILE)

# How many machines
export OVERCLOUD_CONTROLSCALE=$(jq -r .OvercloudControlscale $OC_ENV_FILE)
export OVERCLOUD_COMPUTESCALE=$(jq -r .OvercloudComputescale $OC_ENV_FILE)
# Experience shows a 3:2 memory ratio between testenvs and CI slaves is about right

# And a unique name for the thing we're deploying.
export STACKNAME=ci-overcloud

#export OVERCLOUD_DIB_EXTRA_ARGS="pypi"
export NODE_ARCH=$(jq -r .OvercloudNodeArch $OC_ENV_FILE)
# We need to inject a static mapping to the API endpoint.
export DIB_COMMON_ELEMENTS="stackuser hosts use-ephemeral"
#export DIB_COMMON_ELEMENTS="pypi-openstack pip-cache stackuser hosts"
#source /opt/stack/tripleo-incubator/scripts/refresh-env /opt/stack
#source /opt/stack/tripleo-incubator/scripts/devtest_variables.sh
#source /root/stackrc

export OVERCLOUD_PUBLIC_VLAN_START=$(jq -r .overcloud.public_vlan.start $NETWORKS_FILE)
export OVERCLOUD_PUBLIC_VLAN_FINISH=$(jq -r .overcloud.public_vlan.finish $NETWORKS_FILE)
export OVERCLOUD_PUBLIC_VLAN_CIDR=$(jq -r .overcloud.public_vlan.cidr $NETWORKS_FILE)
export OVERCLOUD_PUBLIC_VIRTUAL_INTERFACE=$(jq -r .NeutronPublicInterface $CI_ENV_FILE)
export OVERCLOUD_NTP_SERVER=$(jq -r .NtpServer $CI_ENV_FILE)
export OVERCLOUD_NAME=$(jq -r .OvercloudName $OC_ENV_FILE)
set +e
OVERCLOUD_LIBVIRT_TYPE="kvm" \
    devtest_overcloud.sh "${OVERCLOUD_PUBLIC_VIRTUAL_INTERFACE}" "" "" "" \
    "${OVERCLOUD_PUBLIC_VLAN_START}" "${OVERCLOUD_PUBLIC_VLAN_FINISH}" \
    "${OVERCLOUD_PUBLIC_VLAN_CIDR}" \
    "${TRIPLEO_ROOT}/tripleo-incubator/tripleo-cloud/tripleo-cd-admins" \
    "${TRIPLEO_ROOT}/tripleo-incubator/tripleo-cloud/tripleo-cd-users" \
    "${STACKNAME}" "/home/shared/ssl/${OVERCLOUD_NAME}" "${OVERCLOUD_NAME}"
RESULT=$?
set -e

if [ $RESULT == 0 ] ; then
    set +e
    export TE_DATAFILE=$TRIPLEO_ROOT/testenv.json
    source $TRIPLEO_ROOT/tripleo-incubator/overcloudrc
    $TRIPLEO_ROOT/tripleo-image-elements/elements/tripleo-cd/bin/prepare-ci-overcloud \
        $TRIPLEO_ROOT/tripleo-image-elements/elements/tripleo-cd/configs/te_${REGION_CODE}rc
    RESULT=$?
    set -e
fi

MSG=$(echo "************** $STACKNAME $REGION_CODE complete status=$RESULT ************")
echo "$MSG"
set -x
send-irc tripleo $STACKNAME "$MSG"
