#!/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.

#
# Script to make cloud selection simple
#
set -eu
set -o pipefail
SCRIPT_NAME=$(basename $0)
SCRIPT_HOME=$(cd $(dirname $0); pwd)

function show_options {
    echo "Usage: $SCRIPT_NAME <cloud>"
    echo
    echo "Options:"
    echo "    -h, --help                     -- print this help."
    echo "    --root <dir>                   -- Use <dir> as TRIPLEO_ROOT"
    echo
    echo "Echos the appropriate setup to interact with the requested cloud."
    echo "Choices for <cloud> are:"
    echo "        seed or s"
    echo "        undercloud or under or u"
    echo "        overcloud or over or o"
    echo
    echo "Run as follows to source the undercloud variables into the current shell:"
    echo "        source <( $SCRIPT_HOME/$SCRIPT_NAME undercloud )"
    echo
    exit $1
}

TEMP=`getopt -o h -l help,root: -n $SCRIPT_NAME -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating..." >&2
    exit 1
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
    case "$1" in
        -h|--help) show_options 0 >&2;;
        --root) TRIPLEO_ROOT=$2 ; shift 2;;
        --) shift ; break;;
        *) echo "Error: unsupported option $1." ; exit 1;;
    esac
done

if [[ -z "${TRIPLEO_ROOT:-}" ]] ; then
    echo "Error: You must have TRIPLEO_ROOT set in the environment, or specify it with --root" >&2
    show_options 1 >&2
fi

if (( $# != 1 )); then echo "Cloud to interact with is required" >&2; show_options 1 >&2; fi

set_common() {
    cat << EOF
source_config_file() {
    filename=\$1
    if [[ -e \$filename ]] ; then
        source \$filename
    elif [[ -e \${TRIPLEO_ROOT}/\$filename ]] ; then
        source \${TRIPLEO_ROOT}/\$filename
    else
        echo "Could not find \$filename - sourcing may not work" >&2
    fi
}
update_tripleo_no_proxy() {
    add=\$1
    echo \$no_proxy | grep -wq \$1 || export no_proxy=\$no_proxy,\$1
}
EOF
    # This file may not be there, depending on if the undercloud/overcloud are started yet
    echo "[ -f ${TRIPLEO_ROOT}/tripleorc ] && source ${TRIPLEO_ROOT}/tripleorc"
    # Need to reset TRIPLEO_ROOT after sourcing tripleorc
    echo "export TRIPLEO_ROOT=$TRIPLEO_ROOT"
    echo "source ${TRIPLEO_ROOT}/tripleo-incubator/scripts/devtest_variables.sh"
}

set_seed() {
    cat << EOF
source ${TRIPLEO_ROOT}/tripleo-incubator/seedrc
export SEED_IP=\$(os-apply-config -m \$TE_DATAFILE --type raw --key seed-ip)
export OS_AUTH_URL=http://\${SEED_IP}:5000/v2.0
update_tripleo_no_proxy \${SEED_IP}
export UNDERCLOUD_ID=\$(glance image-list | grep undercloud | grep qcow2 | awk '{print \$2}' | head -1)
EOF
    # Don't proxy to the seeds IP on the baremetal network
    echo "update_tripleo_no_proxy \$(OS_CONFIG_FILES=\$TE_DATAFILE os-apply-config \
        --key baremetal-network.seed.ip --type raw --key-default '192.0.2.1')"
}

set_undercloud() {
    cat << EOF
source_config_file tripleo-undercloud-passwords
source ${TRIPLEO_ROOT}/tripleo-incubator/undercloudrc
export UNDERCLOUD_IP=\$(os-apply-config -m \$TE_DATAFILE --type raw --key undercloud.endpointhost)
update_tripleo_no_proxy \$UNDERCLOUD_IP
EOF
}

set_overcloud() {
    cat << EOF
source_config_file tripleo-overcloud-passwords
source ${TRIPLEO_ROOT}/tripleo-incubator/overcloudrc-user
export OVERCLOUD_IP=\$(os-apply-config -m \$TE_DATAFILE --type raw --key overcloud.endpointhost)
update_tripleo_no_proxy \$OVERCLOUD_IP
EOF
}

# Get the argument to show what cloud to interact with
case "$1" in
    s|seed) cloud=seed;;
    u|under|undercloud) cloud=undercloud;;
    o|over|overcloud) cloud=overcloud;;
    *) echo "Error: unsupported cloud $1." ; exit 1 ;;
esac

# Call the appropriate functions
set_common
set_${cloud}
