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

# 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 {
    cd $TOP_DIR
    sudo git checkout stable/newton
    sudo cp $CONTRIB_DIR/devstack/local.conf $TOP_DIR/local.conf
    sudo cp $CONTRIB_DIR/functions-gbp $TOP_DIR/exercises/
    sudo cp $CONTRIB_DIR/devstack/exercises/*.sh $TOP_DIR/exercises/
    sudo cp $CONTRIB_DIR/devstack/lib/* $TOP_DIR/lib/
    sudo cp -r $CONTRIB_DIR/devstack/gbp-templates $TOP_DIR
    sudo sed -i 's/DEST=\/opt\/stack/DEST=\/opt\/stack\/new/g' $TOP_DIR/stackrc
    sudo sed -i 's/source $TOP_DIR\/lib\/neutron-legacy/source $TOP_DIR\/lib\/neutron-legacy\nsource $TOP_DIR\/lib\/gbp/g' $TOP_DIR/stack.sh
    sudo sed -i 's/# Extras Configuration/install_gbpclient\ninstall_gbpservice\ninit_gbpservice\n# Extras Configuration/g' $TOP_DIR/stack.sh
    sudo sed -i 's/create_neutron_initial_network//g' $TOP_DIR/stack.sh
    source $TOP_DIR/functions
    source $TOP_DIR/functions-common
}

function prepare_nfp_devstack {
    cd $TOP_DIR
    sudo cp $CONTRIB_DIR/devstack/local-nfp.conf $TOP_DIR/local.conf
    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 {
    cd $TOP_DIR
    sudo git checkout stable/newton
    sudo cp $CONTRIB_DIR/devstack/local-aim.conf $TOP_DIR/local.conf
    sudo cp $CONTRIB_DIR/devstack/exercises-aim/gbp_aim.sh $TOP_DIR/exercises/
    sudo sed -i 's/DEST=\/opt\/stack/DEST=\/opt\/stack\/new/g' $TOP_DIR/stackrc
    # 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
    # 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/newton 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/newton branch
    sudo git --git-dir=/opt/stack/new/neutron/.git --work-tree=/opt/stack/new/neutron checkout stable/newton
    sudo git --git-dir=/opt/stack/new/nova/.git --work-tree=/opt/stack/new/nova checkout stable/newton
    sudo git --git-dir=/opt/stack/new/keystone/.git --work-tree=/opt/stack/new/keystone checkout stable/newton
    sudo git --git-dir=/opt/stack/new/cinder/.git --work-tree=/opt/stack/new/cinder checkout stable/newton
    sudo git --git-dir=/opt/stack/new/requirements/.git --work-tree=/opt/stack/new/requirements checkout stable/newton
    export ENABLED_SERVICES="key,rabbit,mysql"

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

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

}

function run_gbp_rally {
    cd $NEW_BASE
    git clone http://github.com/group-policy/rally.git -b dev
    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 port-list
}
