blob: 677efdf0d5f6b32cccdc30d459cfa6f965c4bef2 [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()
chnyda625f4b42017-10-11 14:10:31 +020012def python = new com.mirantis.mk.Python()
13
14def pepperEnv = "pepperEnv"
Jakub Josef823e5422017-07-31 15:05:27 +020015
16node{
17 def saltMaster;
chnyda625f4b42017-10-11 14:10:31 +020018 stage('Setup virtualenv for Pepper') {
Dmitrii Kabanovf31c8962017-10-12 21:00:30 -070019 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Jakub Josef823e5422017-07-31 15:05:27 +020020 }
21 stage("Clean old containers"){
chnyda625f4b42017-10-11 14:10:31 +020022 salt.cmdRun(pepperEnv, 'I@jenkins:slave', """
Jakub Josef823e5422017-07-31 15:05:27 +020023 docker ps --format='{{.ID}}' | xargs -n 1 -r docker inspect \\
24 -f '{{.ID}} {{.State.Running}} {{.State.StartedAt}}' \\
25 | awk '\$2 == "true" && \$3 <= "'\$(date -d '${TEST_DATE_STRING}' -Ins --utc \\
26 | sed 's/+0000/Z/')'" { print \$1 }' \\
27 | xargs -r docker rm -f
28 """, false)
29 }
30 stage("Run docker system prune"){
chnyda625f4b42017-10-11 14:10:31 +020031 salt.cmdRun(pepperEnv, 'I@jenkins:slave', "docker system prune -f")
Jakub Josef823e5422017-07-31 15:05:27 +020032 }
33}