#include tcwg/default.yaml.inc

- job:
    name: tcwg-update-#{CACHE}
    project-type: freestyle
    defaults: global
    properties:
      - default-properties
    parameters:
        - bool:
            name: generate_abe
#if CACHE_snapshots_ref
            default: true
#elif CACHE_bmk_ref
            default: false
#endif
            description: 'Generate ABE snapshots'
        - bool:
            name: generate_bmk
#if CACHE_snapshots_ref
            default: false
#elif CACHE_bmk_ref
            default: true
#endif
            description: 'Generate benchmark source cache'
        - bool:
            name: generate_misc
#if CACHE_snapshots_ref
            default: true
#elif CACHE_bmk_ref
            default: false
#endif
            description: 'Generate misc file'
        - bool:
            name: update_abe_git
            default: true
            description: 'Update Git repos'
        - bool:
            name: verbose
            default: true
            description: 'Enable verbose output'
        - string:
            name: scripts_branch
            default: refs/remotes/origin/master
            description: 'Scripts revision to use'
        - string:
            name: abe_branch
            default: tested
            description: 'Abe revision to use'
        - string:
            name: distro
            default: default
            description: 'Docker image tag to use'
    disabled: false
    concurrent: false
#if CACHE_snapshots_ref
    node: tcwg-x86_64-dev-01
#elif CACHE_bmk_ref
    node: tcwg-bmk-bkp-01
#endif
    display-name: 'TCWG CCC Update #{CACHE} cache on TCWG machines'
    triggers:
        - timed: '@weekly'
    scm:
      - jenkins-scripts
    wrappers:
        - timeout:
            timeout: 600
        - timestamps
        - ssh-agent-credentials:
            users:
#if CACHE_snapshots_ref
                # tcwg-buildslave user id
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
#elif CACHE_bmk_ref
                - 'tcwg-benchmark'
#endif
        - build-name:
            name: '#${BUILD_NUMBER}'
    builders:
        - shell: |
            #!/bin/bash
            set -ex

            new_cache=$(mktemp -d)
            trap "rm -rf $new_cache" EXIT
#if CACHE_snapshots_ref
            cache_dir=$HOME/snapshots-ref
#elif CACHE_bmk_ref
            cache_dir=/home/shared/git
            sudo mkdir -p $cache_dir
            sudo chown -R $USER:tcwg-infra $cache_dir
#endif
            # Use previous cache to speed up re-generation.
            rsync -a --del $cache_dir/ $new_cache/
            ./jenkins-scripts/tcwg-generate-source-cache.sh \
              --cache_dir "$new_cache" \
              --generate_abe "$generate_abe" \
              --generate_bmk "$generate_bmk" \
              --generate_misc "$generate_misc" \
              --update_abe_git "$update_abe_git" \
              --verbose "$verbose" \
              --abe_branch "$abe_branch"
            rsync -a --del $new_cache/ $cache_dir/
        - trigger-builds:
            - project: tcwg-buildslave-command
              predefined-parameters: |
#if CACHE_snapshots_ref
                command=set -ex; cache_dir=/home/tcwg-buildslave/snapshots-ref; sudo mkdir -p $cache_dir; sudo chown -R $USER:tcwg-infra $cache_dir; rsync -az --del dev-01.tcwglab:$cache_dir/ $cache_dir/
#elif CACHE_bmk_ref
                command=set -ex; cache_dir=/home/shared/git; sudo mkdir -p $cache_dir; sudo chown -R $USER:tcwg-infra $cache_dir; rsync -az --del bkp-01.tcwglab:$cache_dir/ $cache_dir/
#endif
                displayname=#{CACHE}
              parameter-factories:
                - factory: allnodesforlabel
                  name: nodes
#if CACHE_snapshots_ref
                  node-label: tcwg-build || tcwg-build_bmk || tcwg-dev
#elif CACHE_bmk_ref
                  node-label: tcwg-bmk-hw
#endif
                  ignore-offline-nodes: false
              block: false
    publishers:
        - email-ext:
            recipients: tcwg-validation@linaro.org
            aborted: true

