blob: 1d7b3b4ffb4710fc5fcc8474c6eb76d824a8b974 [file] [log] [blame]
Jakub Josef823e5422017-07-31 15:05:27 +02001
2/**
3 * Docker cleanup pipeline which can kill old containers (more than a day) and prune docker itself
4 *
5 * SALT_MASTER_URL
6 * SALT_MASTER_CREDENTIALS
7 * TEST_DATE_STRING - string representation of date which will be used for delete matching (ie. yesterday)
8 */
9common = new com.mirantis.mk.Common()
10salt = new com.mirantis.mk.Salt()
11jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
12
13node{
14 def saltMaster;
15 stage("Connect to MCP salt master"){
16 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
17 }
18 stage("Clean old containers"){
19 salt.cmdRun(saltMaster, 'I@jenkins:slave', """
20 docker ps --format='{{.ID}}' | xargs -n 1 -r docker inspect \\
21 -f '{{.ID}} {{.State.Running}} {{.State.StartedAt}}' \\
22 | awk '\$2 == "true" && \$3 <= "'\$(date -d '${TEST_DATE_STRING}' -Ins --utc \\
23 | sed 's/+0000/Z/')'" { print \$1 }' \\
24 | xargs -r docker rm -f
25 """, false)
26 }
27 stage("Run docker system prune"){
28 salt.cmdRun(saltMaster, 'I@jenkins:slave', "docker system prune -f")
29 }
30}