-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfmctl
More file actions
168 lines (146 loc) · 5.42 KB
/
bfmctl
File metadata and controls
168 lines (146 loc) · 5.42 KB
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VERSION="v3.1.8"
usage() {
cat <<'EOF'
Usage: bfmctl [command] [options]
Commands (one at a time):
-version Show bfmctl version
-status Show cluster status
-pause Pause cluster check
-resume Resume cluster check
-mailPause Pause mail notifications
-mailResume Resume mail notifications
-watchMode [A|M] Set watch strategy A:availability, M:manual
-switchOver <ip:port> Switchover to selected slave
-reinit <ip:port> Re-initialize selected slave with pg_rewind/basebackup
-encrypt <clearString> Encrypt a clear string for BFM password function
-sync <appName> Change replication to sync on selected slave (use application name)
-async <appName> Change replication to async on selected slave (use application name)
Options:
-u <username> Username for basic auth
-h | --help Show this help
Notes:
* Password is read interactively; the -p option is NOT allowed.
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
prop_file=""
if [[ -f "$SCRIPT_DIR/application.properties" ]]; then
prop_file="$SCRIPT_DIR/application.properties"
elif [[ -f "./application.properties" ]]; then
prop_file="./application.properties"
fi
get_prop() {
local key="$1"
if [[ -n "${prop_file}" ]]; then
awk -F'=' -v k="$key" '
$1 ~ ("^"k"[ \t]*$") || $1 ~ ("^"k"[ \t]*") {
val=$0; sub(/^[^=]*=/,"",val); gsub(/[ \t\r]/,"",val); print val; exit
}' "${prop_file}"
fi
}
command=""
arg=""
user=""
password=""
for a in "$@"; do
if [[ "$a" == "-p" || "$a" == "--password" ]]; then
echo "Error: -p/--password is not allowed. Use -u and enter password interactively." >&2
exit 1
fi
done
while [[ $# -gt 0 ]]; do
case "$1" in
-u)
[[ $# -ge 2 ]] || { echo "Error: -u requires a username." >&2; exit 1; }
user="$2"; shift 2
;;
-version|-status|-pause|-resume|-mailPause|-mailResume)
command="${1#-}"; arg=""; shift
;;
-watchMode)
[[ $# -ge 2 ]] || { echo "Error: -watchMode requires A or M." >&2; exit 1; }
command="strategy"; arg="$2"; shift 2
;;
-switchOver)
[[ $# -ge 2 ]] || { echo "Error: -switchOver requires <ip:port>." >&2; exit 1; }
command="switchOver"; arg="$2"; shift 2
;;
-reinit)
[[ $# -ge 2 ]] || { echo "Error: -reinit requires <ip:port>." >&2; exit 1; }
command="reinit"; arg="$2"; shift 2
;;
-encrypt)
[[ $# -ge 2 ]] || { echo "Error: -encrypt requires <clearString>." >&2; exit 1; }
command="encrypt"; arg="$2"; shift 2
;;
-sync)
[[ $# -ge 2 ]] || { echo "Error: -sync requires <appName>." >&2; exit 1; }
command="sync"; arg="$2"; shift 2
;;
-async)
[[ $# -ge 2 ]] || { echo "Error: -async requires <appName>." >&2; exit 1; }
command="async"; arg="$2"; shift 2
;;
-h|--help)
usage; exit 0
;;
*)
echo "Error: unknown argument: $1" >&2
usage
exit 1
;;
esac
done
if [[ "$command" == "version" ]]; then
echo -e "${VERSION}"
exit 0
fi
if [[ -z "${user}" ]]; then
echo "user is NOT set. Use -u <username>."
exit 1
fi
read -s -p "Enter password: " password
echo
if [[ -z "${password}" ]]; then
echo "password is NOT set."
exit 1
fi
bfmPair="$(get_prop 'watcher.cluster-pair')"
bfmSSL="$(get_prop 'bfm.use-tls')"
bfmPort="$(get_prop 'watcher.cluster-port')"
bfmPair="${bfmPair:-127.0.0.1:9994}"
bfmSSL="${bfmSSL:-false}"
bfmPort="${bfmPort:-9994}"
if [[ "${bfmSSL}" == "true" ]]; then
proto="https"
else
proto="http"
fi
pair_active="$(curl -k -fsS "${proto}://${bfmPair}/bfm/get-active-bfm" --user "${user}:${password}" || true)"
if [[ "${pair_active:-}" == "T" ]]; then
active_bfm="${bfmPair}"
else
active_bfm="127.0.0.1:${bfmPort}"
fi
echo -e "\nActive BFM : ${active_bfm}"
case "${command}" in
status) curl -k -X GET "${proto}://${active_bfm}/bfm/cluster-status" --user "${user}:${password}" ;;
pause) curl -k -X GET "${proto}://${active_bfm}/bfm/check-pause" --user "${user}:${password}" ;;
resume) curl -k -X GET "${proto}://${active_bfm}/bfm/check-resume" --user "${user}:${password}" ;;
mailPause) curl -k -X GET "${proto}://${active_bfm}/bfm/mail-pause" --user "${user}:${password}" ;;
mailResume) curl -k -X GET "${proto}://${active_bfm}/bfm/mail-resume" --user "${user}:${password}" ;;
strategy) curl -k -X POST "${proto}://${active_bfm}/bfm/watch-strategy/${arg}" --user "${user}:${password}" ;;
switchOver) curl -k -X POST "${proto}://${active_bfm}/bfm/switchover/${arg}" --user "${user}:${password}" ;;
reinit) curl -k -X POST "${proto}://${active_bfm}/bfm/reinit/${arg}" --user "${user}:${password}" ;;
encrypt) curl -k -X POST "${proto}://${active_bfm}/bfm/encrypt/${arg}" --user "${user}:${password}" ;;
sync) curl -k -X POST "${proto}://${active_bfm}/bfm/setsync/${arg}" --user "${user}:${password}" ;;
async) curl -k -X POST "${proto}://${active_bfm}/bfm/setasync/${arg}" --user "${user}:${password}" ;;
"") echo "command not found..."; usage; exit 1 ;;
*) echo "Unknown command '${command}'"; usage; exit 1 ;;
esac