-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy-bashrc
245 lines (192 loc) · 5.73 KB
/
haproxy-bashrc
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# .bashrc
# User specific aliases and functions
haproxyConfDir="/usr/local/etc/haproxy"
haproxyConf="${haproxyConfDir}/haproxy.cfg"
haproxyTmpConf="${haproxyConf}.tmp"
haproxyHostnameCommonString="EDIT-FOR-YOUR-HOSTNAME-COMMON-STRING"
lockfile="/var/run/lbmutex"
alias sob='source ~/.bashrc'
alias vib='vi ~/.bashrc'
lbedit() {
# set up the mutex
lblock || return 1
# first make sure it exists
[ -f "$haproxyConf" -a -r "$haproxyConf" ] || { echo "configuration file $haproxyConf missing or is not readable"; lbcleanup; return 1; }
# now let's make a writeable copy if it doesn't already exist
if [ ! -f "$haproxyTmpConf" ]; then
echo "creating new working copy.."; sleep 1
cp "$haproxyConf" "$haproxyTmpConf"
else
echo "working copy already exists.. resuming"
echo "if you want to discard the proposed changes, use lbrevert"
sleep 1
fi
if ! chmod u+w "$haproxyTmpConf"; then
echo ""
echo "chmod u+w $haproxyTmpConf failed -- bailing out"
lbcleanup
return 1
fi
# now we edit!
[ -n "$EDITOR" ] || EDITOR="vim"
$EDITOR "$haproxyTmpConf"
if [ "$?" = "148" ]; then
echo "It looks like you've suspended the editor. This script cant handle"
echo "re-entry, so your working copy is going to be saved, even if there"
echo "are no changes. Please launch 'lbedit' again to re-aquire the"
echo "editing mutex."
elif lbdiff >/dev/null; then
# if no edits were made, no reason to keep the temp file around
echo "no changes made; discarding working copy"
\rm "$haproxyTmpConf"
fi
lbcleanup
}
lbhelp () {
cat <<STUFF
lbhelp: print this help
lbedit: edit a copy of the running configuration
lbvalidate: tests the proposed configuration file for coherency
lbrevert: erase the proposed configuration file without committing
lbunlock: unlocks editing the file if a connection is lost
lbcommit: tests proposed config, overwrites running config with new,
restarts haproxy, then synchronizes the config directory
to the secondary machine
Note: If haproxy is not currently running, functionality will be limited,
as this normally identifies a standby machine. By design, editing is
inhibited on the standby.
STUFF
}
lbcommit() {
# set up the mutex
lblock || return 1
# first make sure the base config exists
[ -f "$haproxyConf" -a -r "$haproxyConf" ] || { echo "configuration file $haproxyConf missing or is not readable"; lbcleanup; return 1; }
# now we make sure there's a working copy
[ -f "$haproxyTmpConf" -a -r "$haproxyTmpConf" ] || { echo "proposed configuration file $haproxyTmpConf missing or is not readable"; echo ""; echo "try running lbedit first"; lbcleanup; return 1; }
if haproxyValidateTmpConf; then
if co -q -l "$haproxyConf"; then
echo "checkout succeeded"
\mv "$haproxyTmpConf" "$haproxyConf"
ci -u "$haproxyConf"
else
echo "checkout failed; someone probably checked it out manually"
lbcleanup
return 1
fi # checkout test
else
lbcleanup
return $?
fi # conf validation
lbreload
lbsync
lbcleanup
}
lbreload() {
service haproxy restart
}
lbunlock() {
if ! lbIsLocked; then
printf "Not locked"
return 0
fi
printf "WARNING: Do not run this unless you are SURE no one else is actively\n"
printf "editing the configuration file. Are you SURE? [N/y]: "
read ANSWER
# if [ ! -n "$ANSWER" ]; then
# echo "Not doing anything (probably a good choice)."
# return 0
# fi
for choice in "y" "ye" "yes" "yess" "Y" "Ye" "Yes" "Yess" "YE" "YES" "YESS" "YY" "YYY" "YYYY"; do
if [ "$ANSWER" = "$choice" ]; then
lbcleanup
return
fi
done
echo "Not doing anything (probably a good choice)."
}
lbcleanup () {
\rm "$lockfile"
}
lbsync () {
otherHost="$(fgrep ${haproxyHostnameCommonString} /etc/hosts | fgrep -v "$(uname -n)" | awk '{print $2}')"
[ -n "$otherHost" ] || { echo "Other host in configuration is not known"; return 1; }
echo "synchronizing configuration directory to $otherHost .. "
rsync -are 'ssh' "${haproxyConfDir}/" "root@${otherHost}:${haproxyConfDir}/"
ret=$?
[ $ret -eq 0 ] && echo "done"
return $ret
}
lblock () {
# check for a lock
if lbIsLocked; then
echo "Someone is editing the configuration, please try again later"
return 1
fi
# check if we're primary LB
if ! haproxyCheckRunning; then
return 1
fi
touch "$lockfile"
return $?
}
lbIsLocked() {
if [ -f "$lockfile" ]; then
return 0
fi
return 1 # only gets here if it's false
}
haproxyCheckRunning () {
if ! pidof haproxy >/dev/null; then
cat <<STUFF
haproxy isn't running; this machine is likely secondary.
editing and synchronization are inhibited.
if heartbeat is broken, or haproxy isn't starting, use
"service haproxy validate" and "service haproxy start"
to fix the configuration file and start haproxy.
The configuration file to edit is $haproxyConf
STUFF
return 1
fi
return 0
}
lbdiff() {
diff "$haproxyConf" "$haproxyTmpConf"
return $?
}
lbrevert() {
\rm "$haproxyTmpConf"
}
lbvalidate() {
if ! haproxyValidateTmpConf; then
haproxyValidateConf
fi
}
haproxyValidateConf () {
[ -f "$haproxyConf" ] || { echo "running configuration file missing"; return 1; }
echo -n "validating running configuration .. "
if ! haproxy -f "$haproxyConf" -c; then
echo
echo "try running lbedit or lbrevert"
echo
return 1
else
return 0
fi
}
haproxyValidateTmpConf () {
[ -f "$haproxyTmpConf" ] || { echo "proposed configuration file missing"; return 1; }
echo -n "validating proposed configuration .. "
if ! haproxy -f "$haproxyTmpConf" -c; then
echo
echo "try running lbedit or lbrevert"
echo
return 1
else
return 0
fi
}
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi