blob: df8595a2278c1ab8044fd2fc9547e724ab0f4fda [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]"
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -080031 echo >&2 " -z zero out the consul fluentd data"
32 echo >&2 " -l <log-dir> directory from which fluentd logs will be removed"
33 echo >&2 " -c <consul-dir> directory from which consul data is removed"
David K. Bainbridge12463052018-01-19 09:26:09 -080034 echo >&2 " -h this message"
35}
36
37VOLUME_CLEANUP=0
38
39OPTIND=1
40while getopts d:l:c:zh OPT; do
41 case "$OPT" in
42 z) VOLUME_CLEANUP=1;;
43 c) export CONSUL_ROOT="$OPTARG";;
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -080044 l) export VOLTHA_LOGS="$OPTARG";;
David K. Bainbridge12463052018-01-19 09:26:09 -080045 h) usage;
46 exit 1;;
47 esac
48done
49
50for s in $SERVICES; do
51 echo -n "[service] $s ... "
52 if [ $(docker service ls | grep $s | wc -l) -ne 0 ]; then
53 OUT=$(docker service rm $s 2>&1)
54 if [ $? -eq 0 ]; then
55 echo -e "${GREEN}removed${NC}"
56 else
57 echo -e "${RED}ERROR: $OUT${NC}"
58 fi
59 else
60 echo -e "${WHITE}not running${NC}"
61 fi
62done
63
64for s in $STACKS; do
65 echo -n "[stack] $s ... "
66 if [ $(docker stack ls | grep $s | wc -l) -ne 0 ]; then
67 OUT=$(docker stack rm $s 2>&1)
68 if [ $? -eq 0 ]; then
69 echo -e "${GREEN}removed${NC}"
70 else
71 echo -e "${RED}ERROR: $OUT${NC}"
72 fi
73 else
74 echo -e "${WHITE}not running${NC}"
75 fi
76done
77
78for n in $NETWORKS; do
79 echo -n "[network] $n ... "
80 if [ $(docker network ls | grep $n | wc -l) -ne 0 ]; then
81 OUT=$(docker network rm $n 2>&1)
82 if [ $? -eq 0 ]; then
83 echo -e "${GREEN}removed${NC}"
84 else
85 echo -e "${RED}ERROR: $OUT${NC}"
86 fi
87 else
88 echo -e "${WHITE}not running${NC}"
89 fi
90done
91
92# Attempt to count Ready Docker Swarm managers
93SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l)
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -080094echo -n "[cleanup] consul and fluentd ... "
David K. Bainbridge12463052018-01-19 09:26:09 -080095if [ $VOLUME_CLEANUP -ne 0 ]; then
96 RUNNING=$(docker service ps volume_cleanup 2> /dev/null | wc -l)
97 if [ $RUNNING -ne 0 ]; then
98 docker service rm volume_cleanup > /dev/null
99 fi
100 docker service create --detach=true --restart-condition=none \
101 --mode=global --name=volume_cleanup \
102 --mount=type=bind,src=${CONSUL_ROOT:-/cord/incubator/voltha/consul}/data,dst=/consul/data \
103 --mount=type=bind,src=${CONSUL_ROOT:-/cord/incubator/voltha/consul}/config,dst=/consul/config \
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -0800104 --mount=type=bind,src=${VOLTHA_LOGS:-/var/log/voltha/logging_volume},dst=/fluentd/log \
David K. Bainbridge12463052018-01-19 09:26:09 -0800105 alpine:latest \
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -0800106 ash -c 'rm -rf /consul/data/* /consul/config/* /fluentd/log/*' > /dev/null
David K. Bainbridge12463052018-01-19 09:26:09 -0800107
108 RETRY=10
109 while [ $RETRY -ge 0 ]; do
110 COMPLETE=$(docker service ps --filter 'desired-state=Shutdown' --format '{{.DesiredState}}' volume_cleanup | wc -l)
111 ERRORS=$(docker service ps --format '{{.Error}}' volume_cleanup | grep -v "^$" | wc -l)
112 if [ $COMPLETE -eq $SWARM_MANAGER_COUNT ]; then
113 if [ $ERRORS -eq 0 ]; then
114 echo -e "${GREEN}data removed${NC}"
115 docker service rm volume_cleanup > /dev/null
116 break
117 else
118 echo -e "${RED}ERROR: $(docker service ps --format '{{.Error}}' volume_cleanup | awk '{printf("%s ", $0)}')${NC}"
119 exit 1
120 fi
121
122 fi
123 sleep 5
124 RETRY=$(expr $RETRY - 1)
125 done
126else
127 echo -e "${WHITE}skipped${NC}"
128fi