# lib/tempest
# Install and configure Tempest

# Dependencies:
# ``functions`` file
# ``lib/nova`` service is runing
# <list other global vars that are assumed to be defined>
# - ``DEST``
# - ``ADMIN_PASSWORD``
# - ``DEFAULT_IMAGE_NAME``
# - ``S3_SERVICE_PORT``
# - ``SERVICE_HOST``
# - ``BASE_SQL_CONN`` ``lib/database`` declares
# Optional Dependencies:
# IDENTITY_USE_SSL, IDENTITY_HOST, IDENTITY_PORT, IDENTITY_PATH
# ALT_* (similar vars exists in keystone_data.sh)
# ``OS_USERNAME``
# ``IMAGE_PORT``, ``IMAGE_HOST``
# ``LIVE_MIGRATION_AVAILABLE``
# ``USE_BLOCK_MIGRATION_FOR_LIVE_MIGRATION``
# ``DEFAULT_INSTANCE_TYPE``
# ``DEFAULT_INSTANCE_USER``
# ``stack.sh`` calls the entry points in this order:
#
# install_tempest
# configure_tempest

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace


# Defaults
# --------

# Set up default directories
TEMPEST_DIR=$DEST/tempest
TEMPEST_CONF_DIR=$TEMPEST_DIR/etc
TEMPEST_CONF=$TEMPEST_CONF_DIR/tempest.conf

NOVA_SOURCE_DIR=$DEST/nova

BUILD_INTERVAL=3
BUILD_TIMEOUT=400


# Entry Points
# ------------

# configure_tempest() - Set config files, create data dirs, etc
function configure_tempest() {
    local image_lines
    local images
    local num_images
    local image_uuid
    local image_uuid_alt
    local errexit
    local password
    local line
    local flavors
    local flavors_ref
    local flavor_lines
    local public_network_id
    local tenant_networks_reachable

    # TODO(afazekas):
    # sudo python setup.py deploy

    # This function exits on an error so that errors don't compound and you see
    # only the first error that occured.
    errexit=$(set +o | grep errexit)
    set -o errexit

    # Save IFS
    ifs=$IFS

    # Glance should already contain images to be used in tempest
    # testing. Here we simply look for images stored in Glance
    # and set the appropriate variables for use in the tempest config
    # We ignore ramdisk and kernel images, look for the default image
    # ``DEFAULT_IMAGE_NAME``. If not found, we set the ``image_uuid`` to the
    # first image returned and set ``image_uuid_alt`` to the second,
    # if there is more than one returned...
    # ... Also ensure we only take active images, so we don't get snapshots in process
    declare -a images

    while read -r IMAGE_NAME IMAGE_UUID; do
        if [ "$IMAGE_NAME" = "$DEFAULT_IMAGE_NAME" ]; then
            image_uuid="$IMAGE_UUID"
            image_uuid_alt="$IMAGE_UUID"
        fi
        images+=($IMAGE_UUID)
    done < <(glance image-list --status=active | awk -F'|' '!/^(+--)|ID|aki|ari/ { print $3,$2 }')

    case "${#images[*]}" in
        0)
            echo "Found no valid images to use!"
            exit 1
            ;;
        1)
            if [ -z "$image_uuid" ]; then
                image_uuid=${images[0]}
                image_uuid_alt=${images[0]}
            fi
            ;;
        *)
            if [ -z "$image_uuid" ]; then
                image_uuid=${images[0]}
                image_uuid_alt=${images[1]}
            fi
            ;;
    esac

    # Create tempest.conf from tempest.conf.sample
    # copy every time, because the image UUIDS are going to change
    cp $TEMPEST_CONF.sample $TEMPEST_CONF

    IDENTITY_USE_SSL=${IDENTITY_USE_SSL:-False}
    IDENTITY_HOST=${IDENTITY_HOST:-127.0.0.1}
    IDENTITY_PORT=${IDENTITY_PORT:-5000}
    # TODO(jaypipes): This is dumb and needs to be removed
    # from the Tempest configuration file entirely...
    IDENTITY_PATH=${IDENTITY_PATH:-tokens}

    password=${ADMIN_PASSWORD:-secrete}

    # See files/keystone_data.sh where alt_demo user
    # and tenant are set up...
    ALT_USERNAME=${ALT_USERNAME:-alt_demo}
    ALT_TENANT_NAME=${ALT_TENANT_NAME:-alt_demo}

    # Check Nova for existing flavors and, if set, look for the
    # ``DEFAULT_INSTANCE_TYPE`` and use that. Otherwise, just use the first flavor.
    flavor_lines=`nova flavor-list`
    IFS=$'\r\n'
    flavors=""
    for line in $flavor_lines; do
        if [ -z $DEFAULT_INSTANCE_TYPE ]; then
            flavors="$flavors `echo $line | grep -v "^\(|\s*ID\|+--\)" | cut -d' ' -f2`"
        else
            flavors="$flavors `echo $line | grep -v "^\(|\s*ID\|+--\)" | grep "$DEFAULT_INSTANCE_TYPE" | cut -d' ' -f2`"
    fi
    done

    IFS=" "
    flavors=($flavors)
    num_flavors=${#flavors[*]}
    echo "Found $num_flavors flavors"
    if [[ $num_flavors -eq 0 ]]; then
        echo "Found no valid flavors to use!"
        exit 1
    fi
    flavor_ref=${flavors[0]}
    flavor_ref_alt=$flavor_ref
    if [[ $num_flavors -gt 1 ]]; then
        flavor_ref_alt=${flavors[1]}
    fi

    if [ "$Q_USE_NAMESPACE" != "False" ]; then
        tenant_networks_reachable=false
    else
        tenant_networks_reachable=true
    fi

    if is_service_enabled q-l3; then
        public_network_id=$(quantum net-list | grep $PUBLIC_NETWORK_NAME | \
            awk '{print $2}')
    fi

    # Timeouts
    iniset $TEMPEST_CONF compute build_timeout $BUILD_TIMEOUT
    iniset $TEMPEST_CONF volume build_timeout $BUILD_TIMEOUT
    iniset $TEMPEST_CONF boto build_timeout $BUILD_TIMEOUT
    iniset $TEMPEST_CONF compute build_interval $BUILD_INTERVAL
    iniset $TEMPEST_CONF volume build_interval $BUILD_INTERVAL
    iniset $TEMPEST_CONF boto build_interval $BUILD_INTERVAL
    iniset $TEMPEST_CONF boto http_socket_timeout 5

    iniset $TEMPEST_CONF identity use_ssl $IDENTITY_USE_SSL
    iniset $TEMPEST_CONF identity host $IDENTITY_HOST
    iniset $TEMPEST_CONF identity port $IDENTITY_PORT
    iniset $TEMPEST_CONF identity path $IDENTITY_PATH

    iniset $TEMPEST_CONF compute password "$password"
    iniset $TEMPEST_CONF compute alt_username $ALT_USERNAME
    iniset $TEMPEST_CONF compute alt_password "$password"
    iniset $TEMPEST_CONF compute alt_tenant_name $ALT_TENANT_NAME
    iniset $TEMPEST_CONF compute resize_available False
    iniset $TEMPEST_CONF compute change_password_available False
    iniset $TEMPEST_CONF compute compute_log_level ERROR
    # Note(nati) current tempest don't create network for each tenant
    # so reuse same tenant for now
    if is_service_enabled quantum; then
        TEMPEST_ALLOW_TENANT_ISOLATION=${TEMPEST_ALLOW_TENANT_ISOLATION:-False}
    fi
    iniset $TEMPEST_CONF compute allow_tenant_isolation ${TEMPEST_ALLOW_TENANT_ISOLATION:-True}
    #Skip until #1074039 is fixed
    iniset $TEMPEST_CONF compute run_ssh False
    iniset $TEMPEST_CONF compute ssh_user ${DEFAULT_INSTANCE_USER:-$OS_USERNAME}
    iniset $TEMPEST_CONF compute network_for_ssh $PRIVATE_NETWORK_NAME
    iniset $TEMPEST_CONF compute ip_version_for_ssh 4
    iniset $TEMPEST_CONF compute ssh_timeout 4
    iniset $TEMPEST_CONF compute image_ref $image_uuid
    iniset $TEMPEST_CONF compute image_ref_alt $image_uuid_alt
    iniset $TEMPEST_CONF compute flavor_ref $flavor_ref
    iniset $TEMPEST_CONF compute flavor_ref_alt $flavor_ref_alt
    iniset $TEMPEST_CONF compute source_dir $NOVA_SOURCE_DIR
    iniset $TEMPEST_CONF compute live_migration_available ${LIVE_MIGRATION_AVAILABLE:-False}
    iniset $TEMPEST_CONF compute use_block_migration_for_live_migration ${USE_BLOCK_MIGRATION_FOR_LIVE_MIGRATION:-False}
    # Inherited behavior, might be wrong
    iniset $TEMPEST_CONF compute bin_dir $NOVA_BIN_DIR
    # TODO(jaypipes): Create the key file here... right now, no whitebox
    # tests actually use a key.
    iniset $TEMPEST_CONF compute path_to_private_key $TEMPEST_DIR/id_rsa
    iniset $TEMPEST_CONF compute db_uri $BASE_SQL_CONN/nova

    # image
    iniset $TEMPEST_CONF image host ${IMAGE_HOST:-127.0.0.1}
    iniset $TEMPEST_CONF image port ${IMAGE_PORT:-9292}
    iniset $TEMPEST_CONF image password "$password"

    # identity-admin
    iniset $TEMPEST_CONF "identity-admin" password "$password"

    # compute admin
    iniset $TEMPEST_CONF "compute-admin" password "$password"

    # network admin
    iniset $TEMPEST_CONF "network-admin" password "$password"

    # network
    iniset $TEMPEST_CONF network api_version 2.0
    iniset $TEMPEST_CONF network password "$password"
    iniset $TEMPEST_CONF network tenant_networks_reachable "$tenant_networks_reachable"
    iniset $TEMPEST_CONF network public_network_id "$public_network_id"

    #boto
    iniset $TEMPEST_CONF boto ec2_url "http://$SERVICE_HOST:8773/services/Cloud"
    iniset $TEMPEST_CONF boto s3_url "http://$SERVICE_HOST:${S3_SERVICE_PORT:-3333}"

    echo "Created tempest configuration file:"
    cat $TEMPEST_CONF

    # Restore IFS
    IFS=$ifs
    #Restore errexit
    $errexit
}

# install_tempest() - Collect source and prepare
function install_tempest() {
    git_clone $TEMPEST_REPO $TEMPEST_DIR $TEMPEST_BRANCH

    # Tempest doesn't satisfy its dependencies on its own, so
    # install them here instead.
    pip_install -r $TEMPEST_DIR/tools/pip-requires
}

# Restore xtrace
$XTRACE