aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/sh/vespa-configserver-remove-state
blob: d67b9115d8c661e6effffd967903e97eecc5f23f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

# BEGIN environment bootstrap section
# Do not edit between here and END as this section should stay identical in all scripts

findpath () {
    myname=${0}
    mypath=${myname%/*}
    myname=${myname##*/}
    empty_if_start_slash=${mypath%%/*}
    if [ "${empty_if_start_slash}" ]; then
        mypath=$(pwd)/${mypath}
    fi
    if [ "$mypath" ] && [ -d "$mypath" ]; then
        return
    fi
    mypath=$(pwd)
    if [ -f "${mypath}/${myname}" ]; then
        return
    fi
    echo "FATAL: Could not figure out the path where $myname lives from $0"
    exit 1
}

COMMON_ENV=libexec/vespa/common-env.sh

source_common_env () {
    if [ "$VESPA_HOME" ] && [ -d "$VESPA_HOME" ]; then
        export VESPA_HOME
        common_env=$VESPA_HOME/$COMMON_ENV
        if [ -f "$common_env" ]; then
            . $common_env
            return
        fi
    fi
    return 1
}

findroot () {
    source_common_env && return
    if [ "$VESPA_HOME" ]; then
        echo "FATAL: bad VESPA_HOME value '$VESPA_HOME'"
        exit 1
    fi
    if [ "$ROOT" ] && [ -d "$ROOT" ]; then
        VESPA_HOME="$ROOT"
        source_common_env && return
    fi
    findpath
    while [ "$mypath" ]; do
        VESPA_HOME=${mypath}
        source_common_env && return
        mypath=${mypath%/*}
    done
    echo "FATAL: missing VESPA_HOME environment variable"
    echo "Could not locate $COMMON_ENV anywhere"
    exit 1
}

findhost () {
    if [ "${VESPA_HOSTNAME}" = "" ]; then
        VESPA_HOSTNAME=$(vespa-detect-hostname || hostname -f || hostname || echo "localhost") || exit 1
    fi
    validate="${VESPA_HOME}/bin/vespa-validate-hostname"
    if [ -f "$validate" ]; then
        "$validate" "${VESPA_HOSTNAME}" || exit 1
    fi
    export VESPA_HOSTNAME
}

findroot
findhost

ROOT=${VESPA_HOME%/}
export ROOT

# END environment bootstrap section

cd ${VESPA_HOME} || { echo "Cannot cd to ${VESPA_HOME}" 1>&2; exit 1; }

usage() {
    (
        echo "This script will remove config server state on this node."
        echo "It will refuse to execute if config server is running."
        echo "The following options are recognized:"
        echo ""

        echo "-h|-help)              print this help text"
        echo "-nosudo                do not use sudo when running command (default)"
        echo "-sudo                  use sudo when running command"
        echo "-force                 do not ask for confirmation before removal"
    ) >&2
}

sudo="sudo"
ask=true
remove_zookeeper_dir=true
remove_tenants_dir=true
confirmed=true
zookeeper_dir=var/zookeeper
tenants_dir=var/db/vespa/config_server/serverdb/tenants

if [ -w $zookeeper_dir ]; then
    sudo=""
fi

while [ $# -gt 0 ]; do
    case $1 in
        -h|-help)        usage; exit 0;;
        -nosudo)         shift; sudo="" ;;
        -sudo)           shift; sudo="sudo" ;;
        -force)          shift; ask=false ;;
        *)               echo "Unrecognized option '$1'" >&2; usage; exit 1;;
    esac
done
# Will first check if config server is running on this node
P_CONFIGSERVER=var/run/configserver.pid
if [ -f $P_CONFIGSERVER ] && $sudo kill -0 `cat $P_CONFIGSERVER` 2>/dev/null; then
  echo "[ERROR] Will not remove state while config server is running" 1>&2
  echo "[ERROR] Do 'vespa-stop-configserver' and 'ps xgauww' to check for running config server process" 1>&2
  exit 1
fi

removedata() {
    echo "[info] removing data: $sudo rm -rf $*"
    $sudo rm -rf $*
    echo "[info] removed."
}

confirm() {
    confirmed=false
    echo -n 'Really remove state for config server in '${VESPA_HOME}/$1'? Type "yes" if you are sure ==> ' 1>&2
    answer=no
    read answer
    if [ "$answer" = "yes" ]; then
	confirmed=true
    else
        confirmed=false
        echo "[info] skipping removal ('$answer' != 'yes')"
    fi
}

garbage_collect_dirs() {
    find $zookeeper_dir $tenants_dir -type d -depth 2>/dev/null | while read dir; do
	[ "$dir" = "$zookeeper_dir" ] && continue
	$sudo rmdir "$dir" 2>/dev/null
    done
}

confirm_and_clean_dir() {
    if $ask; then
	kb=$(du -sk $1 | awk '{print $1}')
	if [ $kb -gt 100 ]; then
	    confirm $1
	fi
    fi
    if $confirmed; then
	removedata $1/*
    fi
}

garbage_collect_dirs

if $remove_zookeeper_dir && [ -d $zookeeper_dir ]; then
    confirm_and_clean_dir $zookeeper_dir
fi

if $remove_tenants_dir && [ -d $tenants_dir ]; then
    confirm_and_clean_dir $tenants_dir
fi

garbage_collect_dirs

exit 0