- job:
    name: tcwg-cleanup-stale-containers
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 100
        - throttle:
            max-per-node: 1
            option: project
    parameters:
        - label:
            name: slave_list
            default: tcwg
            all-nodes: true
            matching-label: 'allCases'
            description: 'Machines to run on'
        - string:
            name: cleanup_running_hours
            default: '48'
            description: 'Delete containers that are more then "hours" old.  Use "0" to disable time check.  Use negative values for dry-run.'
        - string:
            name: cleanup_stopped_hours
            default: '240'
            description: 'Delete containers that are more then "hours" old.  Use "0" to disable the cleanup.  Use negative values for dry-run.'
        - string:
            name: cleanup_ssh_agent_hours
            default: '48'
            description: 'Kill ssh-agent processes that are more then "hours" old.  Use "0" to disable time check.'
        - bool:
            name: cleanup_volumes
            default: 'true'
            description: 'Whether to cleanup dangling volumes'
        - bool:
            name: cleanup_images
            default: 'true'
            description: 'Whether to cleanup images'
        - bool:
            name: verbose
            default: 'false'
            description: 'Whether to be verbose'
        - string:
            name: scripts_branch
            default: refs/heads/master
            description: 'Scripts revision to use'
    disabled: false
    concurrent: true
    display-name: 'TCWG CCC Cleanup stale containers'
    workspace: workspace/tcwg-cleanup-stale-containers_$EXECUTOR_NUMBER/$NODE_NAME
    triggers:
        - timed: '@daily'
    scm:
        - git:
            url: https://gitlab.com/LinaroLtd/tcwg/jenkins-scripts.git
            refspec: +refs/heads/*:refs/remotes/origin/* +refs/changes/*:refs/changes/*
            branches:
                - ${scripts_branch}
            skip-tag: true
            shallow-clone: true
            wipe-workspace: true
    wrappers:
        - timeout:
            timeout: 600
        - timestamps
        - ssh-agent-credentials:
            users:
              - 'tcwg-benchmark'
        - build-name:
            name: '#${BUILD_NUMBER}-${NODE_NAME}'
    builders:
        - shell: |
            #!/bin/bash
            set -ex
            cat << EOF > mail-body.txt
            Cleaning stale containers on $NODE_NAME.
            Please check console output at $BUILD_URL to view the results
            EOF
            case " $NODE_LABELS " in
              *" tcwg-secondary-node "*)
                # Only cleanup ssh-agent processes for secondary nodes.
                ./tcwg-cleanup-stale-containers.sh --cleanup-running-hours 0 --cleanup-stopped-hours 0 --cleanup-ssh-agent-hours $cleanup_ssh_agent_hours --cleanup-volumes false --cleanup-images false --verbose $verbose || true
                exit 0
                ;;
            esac

            $WORKSPACE/tcwg-cleanup-stale-containers.sh --cleanup-running-hours $cleanup_running_hours --cleanup-stopped-hours $cleanup_stopped_hours --cleanup-ssh-agent-hours $cleanup_ssh_agent_hours --cleanup-volumes $cleanup_volumes --cleanup-images $cleanup_images --verbose $verbose
    publishers:
        - email-ext:
            recipients: tcwg-validation@linaro.org
            aborted: true
            content-type: text
            body: |
              ${FILE,path="mail-body.txt"}
