Bash – скрипт удаления образов с Docker Swarm нод

Автор: | 21/04/2017
 

Имеется Docker Swarm:

$ docker node ls
ID                           HOSTNAME             STATUS  AVAILABILITY  MANAGER STATUS
h8p0sed4q6n6yyheq3wbxum2r    fgvwm46c3b5uk000001  Ready   Active        
oaphqh0i6f8kc013fy5goi37u    fgvwm46c3b5uk000002  Ready   Active        
qgj0un2g0jsqhhr0izd9fyn0d    fgvwm46c3b5uk000000  Ready   Active        
so01pmq9sdl9s34uq1m419m7z *  fgvwm46c3b5uk000000  Ready   Drain         Leader

Задача – перед деплоем “зачистить” старые образы.

Скрипт получает список всех нод фермы (только workers, т.к. менеджеры в статусе Drain), подключается к каждой, проверят занятое место и кол-во образов, после чего удаляет все, кроме используемых контейнерами в настоящий момент.

Собственно – сам скрипт:

#!/usr/bin/env bash

ENV=$1

[[ -z $ENV ]] && { echo -e "ERROR: please secify environemt as first argument. Vaild values are: dev|qa|staging|production. Exit."; exit 1; }

USER=jmadmin
RSA=/home/jmadmin/.ssh/jm-website-sw-$ENV

get_nodes () {
        docker node ls | grep Active | cut -d" " -f1 | grep -v ID
}

get_node_df () {

        local node_ip=$1

        ssh -t -t -o StrictHostKeyChecking=no $USER@$node_ip -i $RSA "bash -c '
                docker system df
        '"
}

get_node_ip () {

        local node=$1
        docker inspect $node | grep Addr | cut -d":" -f 2 | sed -e 's/\"//g'
}

cleaup_node () {

        local node_ip=$1

        ssh -t -t -o StrictHostKeyChecking=no $USER@$node_ip -i $RSA "bash -c '
                time docker images -q -a | xargs --no-run-if-empty docker rmi
        '"
}

for node in $(get_nodes); do
        get_node_df $(get_node_ip $node)
        cleaup_node $(get_node_ip $node)
done

И его выполнение (образы уже почищены):

[simterm]

$ ./docker_cleanup.sh dev | tee clean.log
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              2                   2                   1.769GB             0B (0%)
Containers          2                   2                   2B                  0B (0%)
Local Volumes       0                   0                   0B                  0B
Connection to 192.168.0.5 closed.
Error response from daemon: conflict: unable to delete 0a2de3be37a3 (cannot be forced) - image is being used by running container e727463d8c55
Error response from daemon: conflict: unable to delete 0414859ea855 (cannot be forced) - image is being used by running container a0e26ee428a3

real    0m0.056s
user    0m0.036s
sys     0m0.004s
Connection to 192.168.0.5 closed.
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              2                   2                   1.589GB             662.8MB (41%)
Containers          2                   2                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Connection to 192.168.0.6 closed.
Error response from daemon: conflict: unable to delete 1e64e6b2ff11 (cannot be forced) - image is being used by running container 33de0fd0cf90
Error response from daemon: conflict: unable to delete d31885df196a (cannot be forced) - image is being used by running container ce6fb77fddfe

real    0m0.049s
user    0m0.024s
sys     0m0.008s
Connection to 192.168.0.6 closed.
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              3                   3                   2.454GB             662.8MB (27%)                                                                                                                                                 
Containers          3                   3                   52.68MB             0B (0%)
Local Volumes       0                   0                   0B                  0B
Connection to 192.168.0.4 closed.
Error response from daemon: conflict: unable to delete d31885df196a (cannot be forced) - image is being used by running container dafcdfe9ff38
Error response from daemon: conflict: unable to delete 0414859ea855 (cannot be forced) - image is being used by running container efa86b80f0fa
Error response from daemon: conflict: unable to delete 32d02c3fa8f9 (cannot be forced) - image is being used by running container 4f078aff3fa8

real    0m0.052s
user    0m0.028s
sys     0m0.008s
Connection to 192.168.0.4 closed.

[/simterm]