#!/usr/bin/env bash

TOP_DIR="$BASE/new/devstack"
NEW_BASE="$BASE/new"
SCRIPTS_DIR="/usr/os-testr-env/bin"
LOGS_DIR="$NEW_BASE/logs"
ARCHIVE_LOGS_DIR="$BASE/logs"
LOCAL_CONF=$TOP_DIR/local.conf

# The following config is legacy and is used for GBP non-devstack plugin
# install
GBP_DIR="$NEW_BASE/group-based-policy"
GBP_FUNC_DIR=$GBP_DIR/gbpservice/tests/contrib/gbpfunctests

XTRACE=$(set +o | grep xtrace)

function prepare_gbp_devstack_pre {
    cd $TOP_DIR
    sudo git checkout stable/queens
    sudo sed -i 's/DEST=\/opt\/stack/DEST=\/opt\/stack\/new/g' $TOP_DIR/stackrc
    sudo sed -i 's/source $TOP_DIR\/lib\/neutron/source $TOP_DIR\/lib\/neutron\nsource $TOP_DIR\/lib\/neutron-legacy/g' $TOP_DIR/stack.sh
}

function prepare_gbp_devstack_post {
    # The following should updated when master moves to a new release
    # We need to do the following since the infra job clones these repos and
    # checks out the master branch (as this is the master branch) and later
    # does not switch to the stable/queens branch when installing devstack
    # since the repo is already present.
    # This can be worked around by changing the job description in
    # project-config to set BRANCH_OVERRIDE to use the stable/queens branch
    sudo git --git-dir=/opt/stack/new/neutron/.git --work-tree=/opt/stack/new/neutron checkout stable/queens
    sudo git --git-dir=/opt/stack/new/nova/.git --work-tree=/opt/stack/new/nova checkout stable/queens
    sudo git --git-dir=/opt/stack/new/keystone/.git --work-tree=/opt/stack/new/keystone checkout stable/queens
    sudo git --git-dir=/opt/stack/new/cinder/.git --work-tree=/opt/stack/new/cinder checkout stable/queens
    sudo git --git-dir=/opt/stack/new/requirements/.git --work-tree=/opt/stack/new/requirements checkout stable/queens

    source $TOP_DIR/functions
    source $TOP_DIR/functions-common
}

function append_to_localconf {
    # REVISIT: Instead of using the subnet directly here, ideally we
    # should be doing something similar to what is being done in here:
    # https://github.com/openstack/neutron/commit/96a821c48b7de0017d00c43a3f9da91585873354
    sudo sed -i "$ a IPV4_ADDRS_SAFE_TO_USE=10.1.0.0/20" $TOP_DIR/local.conf
}

function prepare_gbp_devstack {
    prepare_gbp_devstack_pre
    sudo mv $GBP_DIR/devstack/local-gbp.conf $TOP_DIR/local.conf
    append_to_localconf
    sudo cp -r $CONTRIB_DIR/devstack/gbp-templates $TOP_DIR
    sudo cp $CONTRIB_DIR/devstack/exercises/*.sh $TOP_DIR/exercises/
    prepare_gbp_devstack_post
}

function prepare_nfp_devstack {
    cd $TOP_DIR
    sudo cp $CONTRIB_DIR/devstack/local-nfp.conf $TOP_DIR/local.conf
    append_to_localconf
    sudo rm -rf $TOP_DIR/exercises/*.sh
    sudo cp $CONTRIB_DIR/devstack/exercises-nfp/*.sh $TOP_DIR/exercises/
    #sudo cp $CONTRIB_DIR/devstack/exercises-nfp/lb.sh $TOP_DIR/exercises/
    sudo cp -r $CONTRIB_DIR/devstack/nfp-templates $TOP_DIR
    sudo cp $CONTRIB_DIR/devstack/nfp $TOP_DIR/lib/
    sed -i 's/source $TOP_DIR\/lib\/dstat/source $TOP_DIR\/lib\/dstat\nsource $TOP_DIR\/lib\/nfp/g' stack.sh
    sed -i 's/# Restore\/close logging file descriptors/nfp_setup $TOP_DIR\n# Restore\/close logging file descriptors/g' stack.sh
    source $TOP_DIR/functions
    source $TOP_DIR/functions-common
}

function prepare_gbp_aim_devstack {
    prepare_gbp_devstack_pre
    sudo cp $CONTRIB_DIR/devstack/local-aim.conf $TOP_DIR/local.conf
    append_to_localconf
    sudo cp $CONTRIB_DIR/devstack/exercises-aim/gbp_aim.sh $TOP_DIR/exercises/
    sudo cp $CONTRIB_DIR/devstack/exercises-aim/neutron_aim.sh $TOP_DIR/exercises/
    # Use the aim version of the shared PRS test
    sudo mv $GBP_FUNC_DIR/testcases/tc_gbp_prs_pr_shared_func.py.aim $GBP_FUNC_DIR/testcases/tc_gbp_prs_pr_shared_func.py
    sudo mv $GBP_FUNC_DIR/testcases/tc_gbp_prs_func.py.aim $GBP_FUNC_DIR/testcases/tc_gbp_prs_func.py
    export ENABLED_SERVICES="key,rabbit,mysql"
    prepare_gbp_devstack_post
}

function source_creds {
    local xtrace=$(set +o | grep xtrace)
    set +o xtrace
    source $1 $2 $3
    $xtrace

}

function run_gbp_rally {
    # REVISIT: Temporarily disabling this job until its updated to run with Ocata
    exit 1
    cd $NEW_BASE
    git clone http://github.com/group-policy/rally.git -b dev-ocata
    cd rally
    sed -i 's/\"times\"\: 1/\"times\"\: 10/g' samples/tasks/scenarios/gbp/gbp.json
    sed -i 's/\"concurrency\"\: 1/\"concurrency\"\: 10,/g' samples/tasks/scenarios/gbp/gbp.json
    ./install_rally.sh -f
    . /home/jenkins/rally/bin/activate
    source_creds $TOP_DIR/openrc admin admin
    rally deployment create --fromenv --name=gbp
    rally task start samples/tasks/scenarios/gbp/gbp.json
    rally task results > $LOGS_DIR/rally-task-results.log.20
    deactivate
}

# Prepare the log files for Jenkins to upload
function prepare_logs {
    cd $LOGS_DIR
    for f in $(find . -name "*.log.2*"); do
        sudo mv $f ${f/.log.*/.txt}
    done
    sudo gzip -9fk `find . -maxdepth 1 \! -type l -name "*.txt" | xargs ls -d`
    mv *.gz $ARCHIVE_LOGS_DIR/
}

function check_residual_resources {
    source_creds $NEW_BASE/devstack/openrc $1 $2
    gbp l3policy-list
    gbp l2policy-list
    gbp group-list
    gbp policy-target-list
    gbp policy-rule-set-list
    gbp policy-rule-list
    gbp policy-classifier-list
    gbp policy-action-list
    gbp servicechain-instance-list
    gbp servicechain-node-list
    gbp servicechain-spec-list
    gbp service-profile-list
    gbp network-service-policy-list
    gbp nat-pool-list
    gbp external-policy-list
    gbp external-segment-list
    gbp apg-list

    neutron router-list
    neutron net-list
    neutron subnet-list
    neutron subnetpool-list
    neutron port-list
}
