Jakub Josef | 823e542 | 2017-07-31 15:05:27 +0200 | [diff] [blame] | 1 | |
| 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 | */ |
| 9 | common = new com.mirantis.mk.Common() |
| 10 | salt = new com.mirantis.mk.Salt() |
| 11 | jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 12 | |
| 13 | node{ |
| 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 | } |