Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
David K. Bainbridge | bba65ff | 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]" |
David K. Bainbridge | 737b74f | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 31 | 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. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 34 | echo >&2 " -h this message" |
| 35 | } |
| 36 | |
| 37 | VOLUME_CLEANUP=0 |
| 38 | |
| 39 | OPTIND=1 |
| 40 | while getopts d:l:c:zh OPT; do |
| 41 | case "$OPT" in |
| 42 | z) VOLUME_CLEANUP=1;; |
| 43 | c) export CONSUL_ROOT="$OPTARG";; |
David K. Bainbridge | 737b74f | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 44 | l) export VOLTHA_LOGS="$OPTARG";; |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 45 | h) usage; |
| 46 | exit 1;; |
| 47 | esac |
| 48 | done |
| 49 | |
| 50 | for 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 |
| 62 | done |
| 63 | |
| 64 | for 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 |
| 76 | done |
| 77 | |
| 78 | for 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 |
| 90 | done |
| 91 | |
| 92 | # Attempt to count Ready Docker Swarm managers |
| 93 | SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l) |
David K. Bainbridge | 737b74f | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 94 | echo -n "[cleanup] consul and fluentd ... " |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 95 | if [ $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. Bainbridge | 737b74f | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 104 | --mount=type=bind,src=${VOLTHA_LOGS:-/var/log/voltha/logging_volume},dst=/fluentd/log \ |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 105 | alpine:latest \ |
David K. Bainbridge | 737b74f | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 106 | ash -c 'rm -rf /consul/data/* /consul/config/* /fluentd/log/*' > /dev/null |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 107 | |
| 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 |
| 126 | else |
| 127 | echo -e "${WHITE}skipped${NC}" |
| 128 | fi |