#!/usr/bin/env bash

TOP_DIR="$BASE/new/devstack"
NEW_BASE="$BASE/new"
GBP_DIR="$NEW_BASE/group-based-policy"
SCRIPTS_DIR="/usr/local/jenkins/slave_scripts"
LOGS_DIR="$NEW_BASE/logs"
ARCHIVE_LOGS_DIR="$BASE/logs"

function prepare_gbp_devstack {
    cd $TOP_DIR
    sudo git checkout stable/kilo
    sudo cp $CONTRIB_DIR/devstack/local.conf $TOP_DIR/local.conf
    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/exit 1/echo/g' $TOP_DIR/exercise.sh
    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\ninit_gbpservice\n# Extras Configuration/g' $TOP_DIR/stack.sh
    sudo sed -i 's/echo_summary "Creating initial neutron network elements"//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
    pip_install -e $GBP_DIR
}

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 $TOP_DIR/openrc admin admin
    rally deployment create --fromenv --name=gbp
    rally -v 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 generate_testr_results {
    # Give job user rights to access tox logs
    sudo -H chmod o+rw .
    sudo -H chmod o+rw -R .testrepository
    if [ -f ".testrepository/0" ] ; then
        .tox/dsvm-functional/bin/subunit-1to2 < .testrepository/0 > ./testrepository.subunit
        .tox/dsvm-functional/bin/python $SCRIPTS_DIR/subunit2html.py ./testrepository.subunit testr_results.html
        gzip -9 ./testrepository.subunit
        gzip -9 ./testr_results.html
        sudo mv ./*.gz $ARCHIVE_LOGS_DIR/
    fi
}

function check_residual_resources {
    source $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

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