###################################################
# Non-ci jobs for Bottlenecks project
# They will only be enabled on request by projects!
###################################################
- project:
    name: bottlenecks-project-jobs

    project: 'bottlenecks'

    jobs:
        - 'bottlenecks-verify-{stream}'
        - 'bottlenecks-merge-{stream}'
        - 'bottlenecks-{suite}-upload-artifacts-{stream}'

    stream:
        - master:
            branch: '{stream}'
            #This is used for common project file storage
            gs-pathname: ''
            #This is used for different test suite dependent packages storage
            gs-packagepath: '/{suite}'
            disabled: false
        - danube:
            branch: 'stable/{stream}'
            gs-pathname: '/{stream}'
            gs-packagepath: '/{stream}/{suite}'
            disabled: false

    suite:
        - 'rubbos'
        - 'vstf'
        - 'posca'

################################
# job templates
################################

- job-template:
    name: 'bottlenecks-verify-{stream}'

    disabled: '{obj:disabled}'

    parameters:
        - project-parameter:
            project: '{project}'
            branch: '{branch}'
        - 'opnfv-build-ubuntu-defaults'

    scm:
        - git-scm-gerrit

    triggers:
        - gerrit:
            server-name: 'gerrit.opnfv.org'
            trigger-on:
                - patchset-created-event:
                    exclude-drafts: 'false'
                    exclude-trivial-rebase: 'false'
                    exclude-no-code-change: 'false'
                - draft-published-event
                - comment-added-contains-event:
                    comment-contains-value: 'recheck'
                - comment-added-contains-event:
                    comment-contains-value: 'reverify'
            projects:
              - project-compare-type: 'ANT'
                project-pattern: '{project}'
                branches:
                  - branch-compare-type: 'ANT'
                    branch-pattern: '**/{branch}'
    builders:
        - bottlenecks-hello
        #- bottlenecks-unit-tests

- job-template:
    name: 'bottlenecks-merge-{stream}'

    disabled: '{obj:disabled}'

    parameters:
        - project-parameter:
            project: '{project}'
            branch: '{branch}'
        - 'opnfv-build-ubuntu-defaults'

    scm:
        - git-scm

    triggers:
        - gerrit:
            server-name: 'gerrit.opnfv.org'
            trigger-on:
                - change-merged-event
                - comment-added-contains-event:
                    comment-contains-value: 'remerge'
            projects:
              - project-compare-type: 'ANT'
                project-pattern: '{project}'
                branches:
                    - branch-compare-type: 'ANT'
                      branch-pattern: '**/{branch}'

    builders:
        - bottlenecks-hello
        #- bottlenecks-unit-tests

- job-template:
    name: 'bottlenecks-{suite}-upload-artifacts-{stream}'


    disabled: '{obj:disabled}'

    concurrent: true

    properties:
        - logrotate-default
        - throttle:
            enabled: true
            max-total: 1
            max-per-node: 1
            option: 'project'

    parameters:
        - project-parameter:
            project: '{project}'
            branch: '{branch}'
        - 'opnfv-build-ubuntu-defaults'
        - bottlenecks-parameter:
            gs-packagepath: '{gs-packagepath}'

    scm:
        - git-scm

    builders:
        - 'bottlenecks-builder-upload-artifact'
        - 'bottlenecks-workspace-cleanup'

####################
# parameter macros
####################
- parameter:
    name: bottlenecks-parameter
    parameters:
        - string:
           name: CACHE_DIR
           default: $WORKSPACE/cache{gs-packagepath}
           description: "the cache to store packages downloaded from public IP"
        - string:
           name: SUITE_URL
           default: gs://artifacts.opnfv.org/bottlenecks{gs-packagepath}
           description: "LF artifacts url for storage of bottlenecks packages"
        - string:
           name: PACKAGE_URL
           default: http://205.177.226.237:9999/bottlenecks{gs-packagepath}/
           description: "the url where we store the packages used for bottlenecks rubbos"

####################################
#builders for bottlenecks project
####################################
- builder:
    name: bottlenecks-builder-upload-artifact
    builders:
        - shell: |
            #!/bin/bash
            set -o errexit

            echo "Bottlenecks: upload to artifacts from the public IP"

            [[ -d $CACHE_DIR ]] || mkdir -p $CACHE_DIR

            for file in $(curl -s $PACKAGE_URL |
                               grep href |
                               sed 's/.*href="//' |
                               sed 's/".*//' |
                               grep '^[a-zA-Z].*'); do
                 curl --connect-timeout 10 -o $CACHE_DIR/$file $PACKAGE_URL$file -v
                 echo "bottlenecks: copy file $CACHE_DIR/$file to $SUITE_URL"
                 gsutil cp $CACHE_DIR/$file $SUITE_URL
            done

- builder:
    name: bottlenecks-workspace-cleanup
    builders:
        - shell: |
            #!/bin/bash
            set -o errexit

            echo "Bottlenecks: cleanup cache used for storage downloaded packages"

            /bin/rm -rf $CACHE_DIR

- builder:
    name: bottlenecks-unit-tests
    builders:
        - shell: |
            #!/bin/bash
            set -o errexit
            set -o pipefail

            echo "Running unit tests..."
            cd $WORKSPACE
            virtualenv $WORKSPACE/bottlenecks_venv
            source $WORKSPACE/bottlenecks_venv/bin/activate

            # install python packages
            easy_install -U setuptools
            easy_install -U pip
            pip install -r requirements.txt

            # unit tests
            /bin/bash $WORKSPACE/tests.sh

            deactivate

- builder:
    name: bottlenecks-hello
    builders:
        - shell: |
            #!/bin/bash
            set -o errexit

            echo "hello"