Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame^] | 3 | # Copyright 2017 the original author or authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | PROG=$(basename $0) |
| 18 | BASE_DIR=$(pwd) |
| 19 | |
| 20 | GREEN='\033[32;1m' |
| 21 | RED='\033[0;31m' |
| 22 | WHITE='\033[1;37m' |
| 23 | NC='\033[0m' # No Color |
| 24 | |
| 25 | SERVICES="" |
| 26 | STACKS="consul kafka fluentd voltha" |
| 27 | NETWORKS="voltha_net kafka_net" |
| 28 | |
| 29 | usage() { |
| 30 | echo >&2 "$PROG: [-d <dir>] [-l <log-dir>] [-h]" |
| 31 | echo >&2 " -z zero out the consul data" |
| 32 | echo >&2 " -h this message" |
| 33 | } |
| 34 | |
| 35 | VOLUME_CLEANUP=0 |
| 36 | |
| 37 | OPTIND=1 |
| 38 | while getopts d:l:c:zh OPT; do |
| 39 | case "$OPT" in |
| 40 | z) VOLUME_CLEANUP=1;; |
| 41 | c) export CONSUL_ROOT="$OPTARG";; |
| 42 | h) usage; |
| 43 | exit 1;; |
| 44 | esac |
| 45 | done |
| 46 | |
| 47 | for s in $SERVICES; do |
| 48 | echo -n "[service] $s ... " |
| 49 | if [ $(docker service ls | grep $s | wc -l) -ne 0 ]; then |
| 50 | OUT=$(docker service rm $s 2>&1) |
| 51 | if [ $? -eq 0 ]; then |
| 52 | echo -e "${GREEN}removed${NC}" |
| 53 | else |
| 54 | echo -e "${RED}ERROR: $OUT${NC}" |
| 55 | fi |
| 56 | else |
| 57 | echo -e "${WHITE}not running${NC}" |
| 58 | fi |
| 59 | done |
| 60 | |
| 61 | for s in $STACKS; do |
| 62 | echo -n "[stack] $s ... " |
| 63 | if [ $(docker stack ls | grep $s | wc -l) -ne 0 ]; then |
| 64 | OUT=$(docker stack rm $s 2>&1) |
| 65 | if [ $? -eq 0 ]; then |
| 66 | echo -e "${GREEN}removed${NC}" |
| 67 | else |
| 68 | echo -e "${RED}ERROR: $OUT${NC}" |
| 69 | fi |
| 70 | else |
| 71 | echo -e "${WHITE}not running${NC}" |
| 72 | fi |
| 73 | done |
| 74 | |
| 75 | for n in $NETWORKS; do |
| 76 | echo -n "[network] $n ... " |
| 77 | if [ $(docker network ls | grep $n | wc -l) -ne 0 ]; then |
| 78 | OUT=$(docker network rm $n 2>&1) |
| 79 | if [ $? -eq 0 ]; then |
| 80 | echo -e "${GREEN}removed${NC}" |
| 81 | else |
| 82 | echo -e "${RED}ERROR: $OUT${NC}" |
| 83 | fi |
| 84 | else |
| 85 | echo -e "${WHITE}not running${NC}" |
| 86 | fi |
| 87 | done |
| 88 | |
| 89 | # Attempt to count Ready Docker Swarm managers |
| 90 | SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l) |
| 91 | echo -n "[volume] consul ... " |
| 92 | if [ $VOLUME_CLEANUP -ne 0 ]; then |
| 93 | RUNNING=$(docker service ps volume_cleanup 2> /dev/null | wc -l) |
| 94 | if [ $RUNNING -ne 0 ]; then |
| 95 | docker service rm volume_cleanup > /dev/null |
| 96 | fi |
| 97 | docker service create --detach=true --restart-condition=none \ |
| 98 | --mode=global --name=volume_cleanup \ |
| 99 | --mount=type=bind,src=${CONSUL_ROOT:-/cord/incubator/voltha/consul}/data,dst=/consul/data \ |
| 100 | --mount=type=bind,src=${CONSUL_ROOT:-/cord/incubator/voltha/consul}/config,dst=/consul/config \ |
| 101 | alpine:latest \ |
| 102 | ash -c 'rm -rf /consul/data/* /consul/config/*' > /dev/null |
| 103 | |
| 104 | RETRY=10 |
| 105 | while [ $RETRY -ge 0 ]; do |
| 106 | COMPLETE=$(docker service ps --filter 'desired-state=Shutdown' --format '{{.DesiredState}}' volume_cleanup | wc -l) |
| 107 | ERRORS=$(docker service ps --format '{{.Error}}' volume_cleanup | grep -v "^$" | wc -l) |
| 108 | if [ $COMPLETE -eq $SWARM_MANAGER_COUNT ]; then |
| 109 | if [ $ERRORS -eq 0 ]; then |
| 110 | echo -e "${GREEN}data removed${NC}" |
| 111 | docker service rm volume_cleanup > /dev/null |
| 112 | break |
| 113 | else |
| 114 | echo -e "${RED}ERROR: $(docker service ps --format '{{.Error}}' volume_cleanup | awk '{printf("%s ", $0)}')${NC}" |
| 115 | exit 1 |
| 116 | fi |
| 117 | |
| 118 | fi |
| 119 | sleep 5 |
| 120 | RETRY=$(expr $RETRY - 1) |
| 121 | done |
| 122 | else |
| 123 | echo -e "${WHITE}skipped${NC}" |
| 124 | fi |