#!/bin/bash

KOLLA_GLOBALS_FILE="/usr/share/kolla/ansible/group_vars/__GLOBAL__"

AUTO_VARS_FILE=".docker-ostk.$$"

RELEASE_TAG="2.1.1"

function cleanup {
    rm -f ${AUTO_VARS_FILE}
}
trap cleanup EXIT

function usage {
    cat << -EOF
Overview:

    This command provides a simple wrapper to execute most common OpenStack CLI
    clients using Docker.

    It makes use of the 'oracle/ol-openstack-utils' image, which should be
    available in the registry defined in ${KOLLA_GLOBALS_FILE}.  The image used
    can be overridden by setting OPENSTACK_UTILS_IMAGE.

Environment variables:

    For convenience, Keystone related variables (e.g. OS_USERNAME, OS_PASSWORD,
    etc.) will be passed in automatically if set in your environment. Extra
    environment data can be defined as key/value pairs, one per line, in a
    file, and setting ENV_FILE to point to this. (e.g. ENV_FILE=myvars.txt)

Local data:

    The current working directory is bind-mounted into the container, to allow
    it access to local files, e.g. Glance images, Nova key pairs, etc.  Note,
    this means local files must be beneath the current working directory.

Usage:

    $ $(basename $0) <client> <args>

    Where:

    <client> is an OpenStack CLI command, e.g. nova
    <args> are arguments to the CLI command, e.g. list

Examples:

    # List Nova instances
    $ $(basename $0) nova list

    # Add an image to Glance
    $ $(basename $0) glance image-create --name "cirros-0.3.4-x86_64" --file \\
        cirros-0.3.4-x86_64-disk.img --disk-format qcow2 \\
        --container-format bare --visibility public --progress
-EOF
    exit 1
}

function get_registry {
    if [[ -f "${KOLLA_GLOBALS_FILE}" ]]; then
        REGISTRY="$(awk '/^docker_registry/ {
                        gsub(/["]/, "", $0); print $2
                    }' \
        ${KOLLA_GLOBALS_FILE})"
    fi

    if [[ "${REGISTRY}" != "" ]]; then
        REGISTRY="${REGISTRY}/"
    fi
}

function get_release_tag {
    if [[ -f "${KOLLA_GLOBALS_FILE}" ]]; then
        GLOBALS_RELEASE_TAG="$(awk '/^openstack_release/ {
                                 gsub(/["]/, "", $0); print $2
                               }' \
                               ${KOLLA_GLOBALS_FILE})"
    fi

    if [[ "${GLOBALS_RELEASE_TAG}" != "" ]]; then
        RELEASE_TAG="${GLOBALS_RELEASE_TAG}"
    fi
}

function configure_args {
    if [[ -f "${ENV_FILE}" ]]; then
        ARGS="${ARGS} --env-file ${ENV_FILE}"
    fi

    if [[ "${OS_AUTH_URL}" ]]; then
        echo "OS_AUTH_URL=${OS_AUTH_URL}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_USERNAME}" ]]; then
        echo "OS_USERNAME=${OS_USERNAME}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_PASSWORD}" ]]; then
        echo "OS_PASSWORD=${OS_PASSWORD}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_TENANT_NAME}" ]]; then
        echo "OS_TENANT_NAME=${OS_TENANT_NAME}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_PROJECT_NAME}" ]]; then
        echo "OS_PROJECT_NAME=${OS_PROJECT_NAME}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_PROJECT_DOMAIN_ID}" ]]; then
        echo "OS_PROJECT_DOMAIN_ID=${OS_PROJECT_DOMAIN_ID}" >> ${AUTO_VARS_FILE}
    fi

    if [[ "${OS_USER_DOMAIN_ID}" ]]; then
        echo "OS_USER_DOMAIN_ID=${OS_USER_DOMAIN_ID}" >> ${AUTO_VARS_FILE}
    fi

    echo "OS_VOLUME_API_VERSION=${OS_VOLUME_API_VERSION:=2}" >> ${AUTO_VARS_FILE}

    # NOTE(pbourke): AUTO_VARS_FILE takes precedence over ENV_FILE
    chmod 0400 ${AUTO_VARS_FILE}
    ARGS="${ARGS} --env-file ${AUTO_VARS_FILE}"
}

if [[ $# == 0 || $1 == "-h" || $1 == "--help" ]]; then
    usage
fi

get_registry
get_release_tag

: ${OPENSTACK_UTILS_IMAGE:=${REGISTRY}"oracle/ol-openstack-utils:${RELEASE_TAG}"}

CMD="docker"
ARGS="run --rm -it -v $(pwd):/data"

configure_args

${CMD} ${ARGS} ${OPENSTACK_UTILS_IMAGE} "${@}"
