mirror of
https://github.com/haproxy/haproxy.git
synced 2026-05-19 08:32:34 -04:00
socat was used with the ${MASTER_SOCKET} variable directly, letting it
auto-detect the network protocol. However, when given a plain filename
that does not point to a UNIX socket, socat would create a file at that
path instead of reporting an error.
To fix this, the address type is now determined explicitly: if
MASTER_SOCKET points to an existing UNIX socket file (checked with -S),
UNIX-CONNECT: is used; if it matches a <host>:<port> pattern, TCP: is
used; otherwise an error is reported. The socat_addr variable is also
properly scoped as local to the reload() function.
Could be backported in 3.3.
126 lines
2.4 KiB
Bash
Executable file
126 lines
2.4 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
export VERBOSE=1
|
|
export TIMEOUT=90
|
|
export MASTER_SOCKET="${MASTER_SOCKET:-/var/run/haproxy-master.sock}"
|
|
export RET=
|
|
|
|
alert() {
|
|
if [ "$VERBOSE" -ge "1" ]; then
|
|
echo "[ALERT] $*" >&2
|
|
fi
|
|
}
|
|
|
|
|
|
reload() {
|
|
if [ -S "$MASTER_SOCKET" ]; then
|
|
socat_addr="UNIX-CONNECT:${MASTER_SOCKET}"
|
|
else
|
|
case "$MASTER_SOCKET" in
|
|
*:[0-9]*)
|
|
socat_addr="TCP:${MASTER_SOCKET}"
|
|
;;
|
|
*)
|
|
alert "Invalid master socket address '${MASTER_SOCKET}': expected a UNIX socket file or <host>:<port>"
|
|
return 1
|
|
;;
|
|
esac
|
|
fi
|
|
while read -r line; do
|
|
|
|
if [ "$line" = "Success=0" ]; then
|
|
RET=1
|
|
elif [ "$line" = "Success=1" ]; then
|
|
RET=0
|
|
elif [ "$line" = "Another reload is still in progress." ]; then
|
|
alert "$line"
|
|
elif [ "$line" = "--" ]; then
|
|
continue;
|
|
else
|
|
if [ "$RET" = 1 ] && [ "$VERBOSE" = "2" ]; then
|
|
echo "$line" >&2
|
|
elif [ "$VERBOSE" = "3" ]; then
|
|
echo "$line" >&2
|
|
fi
|
|
fi
|
|
|
|
done < <(echo "reload" | socat -t"${TIMEOUT}" "$socat_addr" -)
|
|
|
|
if [ -z "$RET" ]; then
|
|
alert "Couldn't finish the reload before the timeout (${TIMEOUT})."
|
|
return 1
|
|
fi
|
|
|
|
return "$RET"
|
|
}
|
|
|
|
usage() {
|
|
echo "Usage:"
|
|
echo " $0 [options]*"
|
|
echo ""
|
|
echo " Trigger a reload from the master socket"
|
|
echo " Require socat"
|
|
echo " EXPERIMENTAL script!"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " -S, --master-socket <addr> Unix socket path or <host>:<port> (default: ${MASTER_SOCKET})"
|
|
echo " -d, --debug Debug mode, set -x"
|
|
echo " -t, --timeout Timeout (socat -t) (default: ${TIMEOUT})"
|
|
echo " -s, --silent Silent mode (no output)"
|
|
echo " -v, --verbose Verbose output (output from haproxy on failure)"
|
|
echo " -vv Even more verbose output (output from haproxy on success and failure)"
|
|
echo " -h, --help This help"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 -S ${MASTER_SOCKET} -d ${TIMEOUT}"
|
|
}
|
|
|
|
|
|
main() {
|
|
while [ -n "$1" ]; do
|
|
case "$1" in
|
|
-S|--master-socket)
|
|
MASTER_SOCKET="$2"
|
|
shift 2
|
|
;;
|
|
-t|--timeout)
|
|
TIMEOUT="$2"
|
|
shift 2
|
|
;;
|
|
-s|--silent)
|
|
VERBOSE=0
|
|
shift
|
|
;;
|
|
-v|--verbose)
|
|
VERBOSE=2
|
|
shift
|
|
;;
|
|
-vv|--verbose)
|
|
VERBOSE=3
|
|
shift
|
|
;;
|
|
-d|--debug)
|
|
DEBUG=1
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage "$@"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "[ALERT] ($$) : Unknown option '$1'" >&2
|
|
usage "$@"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ -n "$DEBUG" ]; then
|
|
set -x
|
|
fi
|
|
}
|
|
|
|
main "$@"
|
|
reload
|