aboutsummaryrefslogtreecommitdiff
path: root/tcwg-cleanup-stale-workspaces.yaml
blob: 53f83813ad572dd959733056bb73ba3f7ca65d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
- job:
    name: tcwg-cleanup-stale-workspaces
    project-type: matrix
    defaults: global
    logrotate:
        daysToKeep: 30
        numToKeep: 30
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            linaro:
                - job-build
                - job-cancel
        - throttle:
            max-per-node: 1
    parameters:
        - string:
            name: days
            default: '10'
            description: 'Delete workspace more then days old.  To effectively disable time check use 0.'
        - bool:
            name: dry_run
            default: 'false'
            description: 'Whether to do a dry-run'
        - string:
            name: workspace_top
            default: '/home/tcwg-buildslave/workspace'
            description: 'Top-level workspace directory.  Can be used to delete workspaces for specific jobs.  Do not override unless you understand what you are doing.'
        - string:
            name: maxdepth
            default: '1'
            description: 'Depth of search for outdated directories.  Do not override unless you understand what you are doing.'
    disabled: false
    node: tcwg-x86_64-cam
    concurrent: false
    display-name: 'TCWG Cleanup stale workspaces'
    triggers:
        - timed: '@daily'
    axes:
        - axis:
            type: slave
            name: label
            values:
                - tcwg-aarch64-07
                - tcwg-aarch64-build-01
                - tcwg-aarch64-build-02
                - tcwg-aarch64-test-01
                - tcwg-aarch64-test-02
                - tcwg-x86_64-build-01
                - tcwg-x86_64-build-02
                - tcwg-x86_64-build-03
                - tcwg-x86_64-build-04
                - tcwg-x86_64-build-05
                - tcwg-x86_64-build-06
                - tcwg-x86_64-dev-01
                - tcwg-x86_64-ex40build-01
                - tcwg-x86_64-ex40build-02
                - tcwg-x86_64-ex40build-04
                - tcwg-x86_64-ex40build-06
                - tcwg-x86_64-ex40build-07
                - tcwg-x86_64-ex40build-08
                - tcwg-x86_64-ex40build-09
    execution-strategy:
        sequential: false
    wrappers:
        - timeout:
            timeout: 600
        - timestamps
        - ssh-agent-credentials:
            # tcwg-buildslave user id
            users:
                - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
        - build-name:
            name: '#${BUILD_NUMBER}'
    builders:
        - shell: |
            #!/bin/bash

            set -e
            set -x

            if [ "$days" -lt "0" ]; then
              echo "ERROR: Refusing to delete workspaces that are $days days old"
              exit 1
            fi

            echo "Date / size report before:"
            ls -ld $workspace_top/* || true
            du -hs $workspace_top/* || true

            # Semantics of find's mtime "+N" stands for N+1 days old or older.
            days_1=$(($days-1))

            # Handle dirs with whitespaces by setting $IFS to newline.
            SAVEIFS=$IFS
            IFS=$'\n'
            dirs=($(find $workspace_top/ -maxdepth $maxdepth -type d -mtime +$days_1))
            IFS=$SAVEIFS

            rm_dirs=()
            for dir in "${dirs[@]}"; do
              if [ x"$(find "$dir" -mtime -$days | wc -l)" = x"0" ]; then
                rm_dirs=("${rm_dirs[@]}" "$dir")
              fi
            done

            if [ ${#rm_dirs[@]} != 0 ]; then
              echo "Removing directories:"
              ls -ld "${rm_dirs[@]}"
              du -hs "${rm_dirs[@]}"
              if ! $dry_run; then
                rm -rf "${rm_dirs[@]}"
              else
                echo "DRY_RUN: NOT REMOVING DIRECTORIES"
              fi

              echo "Date / size report after:"
              ls -ld $workspace_top/* || true
              du -hs $workspace_top/* || true
            fi