blob: ebb4aea269fc7893bc4c39893c69e07b75825c89 [file] [log] [blame]
Sergio Slobodriancab0a392017-07-13 08:42:10 -04001#!/bin/bash
2
David K. Bainbridge12463052018-01-19 09:26:09 -08003# 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
17PROG=$(basename $0)
18BASE_DIR=$(pwd)
19
20GREEN='\033[32;1m'
21RED='\033[0;31m'
22WHITE='\033[1;37m'
23NC='\033[0m' # No Color
24
25SERVICES=""
26STACKS="consul kafka fluentd voltha"
27NETWORKS="voltha_net kafka_net"
28
29usage() {
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
35VOLUME_CLEANUP=0
36
37OPTIND=1
38while 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
45done
46
47for 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
59done
60
61for 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
73done
74
75for 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
87done
88
89# Attempt to count Ready Docker Swarm managers
90SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l)
91echo -n "[volume] consul ... "
92if [ $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
122else
123 echo -e "${WHITE}skipped${NC}"
124fi