Last active
December 25, 2015 13:09
-
-
Save josqu4red/6981955 to your computer and use it in GitHub Desktop.
Haproxy multi instance initscripts, FreeBSD & Debian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: haproxy | |
# Required-Start: $local_fs $network $remote_fs | |
# Required-Stop: $local_fs $remote_fs | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: fast and reliable load balancing reverse proxy | |
# Description: This file should be used to start and stop haproxy. | |
### END INIT INFO | |
# Author: Arnaud Cornet <[email protected]> | |
PATH=/sbin:/usr/sbin:/bin:/usr/bin | |
PIDFILE=/var/run/haproxy.pid | |
CONFIG=/etc/haproxy/haproxy.conf | |
HAPROXY=/usr/sbin/haproxy | |
EXTRAOPTS= | |
ENABLED=0 | |
test -x $HAPROXY || exit 0 | |
if [ -e /etc/default/haproxy ]; then | |
. /etc/default/haproxy | |
fi | |
test "$ENABLED" != "0" || exit 0 | |
[ -f /etc/default/rcS ] && . /etc/default/rcS | |
. /lib/lsb/init-functions | |
if [ -n "$2" ]; then | |
profile="$2" | |
if [ "x${PROFILES}" != "x" ]; then | |
PIDFILE="/var/run/haproxy_${profile}.pid" | |
CONFIG="/etc/haproxy/haproxy_${profile}.conf" | |
if [ ! -f "$CONFIG" ]; then | |
echo "You must define a configuration file (${CONFIG})" | |
exit 1 | |
fi | |
else | |
echo "$0: extra argument ignored" | |
fi | |
else | |
if [ "x${PROFILES}" != "x" -a "x$1" != "x" ]; then | |
for profile in ${PROFILES}; do | |
echo "===> haproxy profile: ${profile}" | |
/etc/init.d/haproxy $1 ${profile} | |
done | |
exit 0 | |
fi | |
fi | |
haproxy_start() | |
{ | |
start-stop-daemon --start --pidfile "$PIDFILE" \ | |
--exec $HAPROXY -- -q -f "$CONFIG" -p "$PIDFILE" \ | |
$EXTRAOPTS || return 2 | |
return 0 | |
} | |
haproxy_stop() | |
{ | |
if [ ! -f $PIDFILE ] ; then | |
# This is a success according to LSB | |
return 0 | |
fi | |
for pid in $(cat $PIDFILE) ; do | |
/bin/kill $pid || return 4 | |
done | |
rm -f $PIDFILE | |
return 0 | |
} | |
haproxy_reload() | |
{ | |
$HAPROXY -f "$CONFIG" -p $PIDFILE -sf $(cat $PIDFILE) \ | |
|| return 2 | |
return 0 | |
} | |
haproxy_status() | |
{ | |
if [ ! -f $PIDFILE ] ; then | |
# program not running | |
return 3 | |
fi | |
for pid in $(cat $PIDFILE) ; do | |
if ! ps --no-headers p "$pid" | grep haproxy > /dev/null ; then | |
# program running, bogus pidfile | |
return 1 | |
fi | |
done | |
return 0 | |
} | |
haproxy_checkconfig() | |
{ | |
$HAPROXY -q -c -f $CONFIG | |
[ $? -eq 0 ] || exit 1 | |
} | |
case "$1" in | |
start) | |
log_daemon_msg "Starting haproxy" "haproxy" | |
haproxy_checkconfig | |
haproxy_start | |
ret=$? | |
case "$ret" in | |
0) | |
log_end_msg 0 | |
;; | |
1) | |
log_end_msg 1 | |
echo "pid file '$PIDFILE' found, haproxy not started." | |
;; | |
2) | |
log_end_msg 1 | |
;; | |
esac | |
exit $ret | |
;; | |
stop) | |
log_daemon_msg "Stopping haproxy" "haproxy" | |
haproxy_stop | |
ret=$? | |
case "$ret" in | |
0|1) | |
log_end_msg 0 | |
;; | |
2) | |
log_end_msg 1 | |
;; | |
esac | |
exit $ret | |
;; | |
reload|force-reload) | |
log_daemon_msg "Reloading haproxy" "haproxy" | |
haproxy_checkconfig | |
haproxy_reload | |
case "$?" in | |
0|1) | |
log_end_msg 0 | |
;; | |
2) | |
log_end_msg 1 | |
;; | |
esac | |
;; | |
restart) | |
log_daemon_msg "Restarting haproxy" "haproxy" | |
haproxy_checkconfig | |
haproxy_stop | |
haproxy_start | |
case "$?" in | |
0) | |
log_end_msg 0 | |
;; | |
1) | |
log_end_msg 1 | |
;; | |
2) | |
log_end_msg 1 | |
;; | |
esac | |
;; | |
status) | |
haproxy_status | |
ret=$? | |
case "$ret" in | |
0) | |
echo "haproxy is running." | |
;; | |
1) | |
echo "haproxy dead, but $PIDFILE exists." | |
;; | |
*) | |
echo "haproxy not running." | |
;; | |
esac | |
exit $ret | |
;; | |
*) | |
echo "Usage: /etc/init.d/haproxy {start|stop|reload|restart|status}" | |
exit 2 | |
;; | |
esac | |
: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# $FreeBSD: net/haproxy/files/haproxy.in 305869 2012-10-14 11:46:52Z crees $ | |
# | |
# PROVIDE: haproxy | |
# REQUIRE: DAEMON LOGIN | |
# KEYWORD: shutdown | |
####### | |
# | |
# Add the following lines to /etc/rc.conf to enable haproxy: | |
# | |
# haproxy_enable (bool): default: "NO" | |
# Set to "YES" to enable haproxy | |
# haproxy_profiles (str): Set to "" by default. | |
# Define your profiles here. | |
# haproxy_pidfile (str): default: /var/run/${name}.pid | |
# Set to the full path of the pid file | |
# haproxy_config (str): default: /usr/local/etc/${name}.conf | |
# Set to the full path of the config file | |
# haproxy_flags (str): default: Autogenerated using pidfile and config options | |
# Set to override with your own options | |
# | |
####### | |
# | |
# rc.d Script Runtime Options: | |
# | |
# start - starts application normally | |
# stop - (softstop) stops all proxies and exits once all sessions are closed | |
# forcestop - (immediate) stops all proxies and kills active sessions | |
# reload - hot-reconfig using "-sf" option (active sessions kept) | |
# forcereload - hot-reconfig using "-st" option (active sessions killed) | |
# restart - equiv to "stop" then "start" | |
# configtest - checks configuration file defined in haproxy_config | |
# | |
####### | |
. /etc/rc.subr | |
name="haproxy" | |
rcvar=haproxy_enable | |
command="/usr/local/sbin/haproxy" | |
_pidprefix="/var/run" | |
# Load Configs/Set Defaults | |
load_rc_config $name | |
: ${haproxy_enable:="NO"} | |
: ${haproxy_profiles:=""} | |
pidfile=${haproxy_pidfile:-"/var/run/haproxy.pid"} | |
: ${haproxy_config:="/usr/local/etc/${name}.conf"} | |
: ${haproxy_flags="-q -f ${haproxy_config} -p ${pidfile}"} | |
# Update the globals | |
required_files=$haproxy_config | |
# Commands: start, stop, restart, reload, configtest | |
extra_commands="reload configtest" | |
configtest_cmd="$command -c -f $haproxy_config" | |
start_precmd="$command -q -c -f $haproxy_config" | |
reload_cmd="haproxy_reload" | |
# For stopping, SIGUSR1 = softstop, SIGTERM = faststop | |
sig_stop=${rc_force:-USR1} | |
if [ -n "$2" ]; then | |
profile="$2" | |
if [ "x${haproxy_profiles}" != "x" ]; then | |
pidfile="${_pidprefix}/haproxy_${profile}.pid" | |
eval haproxy_config="\${haproxy_${profile}_config:-/usr/local/etc/haproxy_${profile}.conf}" | |
if [ "x${haproxy_config}" = "x" ]; then | |
echo "You must define a configuration file (haproxy_${profile}_config)" | |
exit 1 | |
fi | |
required_files="${haproxy_config}" | |
eval haproxy_enable="\${haproxy_${profile}_enable:-${haproxy_enable}}" | |
eval haproxy_flags="\${haproxy_${profile}_flags:-${haproxy_flags}}" | |
haproxy_flags="-q -f ${haproxy_config} -p ${pidfile}" | |
else | |
echo "$0: extra argument ignored" | |
fi | |
else | |
if [ "x${haproxy_profiles}" != "x" -a "x$1" != "x" ]; then | |
for profile in ${haproxy_profiles}; do | |
echo "===> haproxy profile: ${profile}" | |
/usr/local/etc/rc.d/haproxy $1 ${profile} | |
retcode="$?" | |
if [ "0${retcode}" -ne 0 ]; then | |
failed="${profile} (${retcode}) ${failed:-}" | |
else | |
success="${profile} ${success:-}" | |
fi | |
done | |
exit 0 | |
fi | |
fi | |
haproxy_reload() | |
{ | |
# Check configuration file quietly first | |
${command} -q -c -f ${haproxy_config} | |
if [ $? -ne 0 ]; then | |
err 1 "Error found in ${haproxy_config} - not reloading current process!" | |
fi | |
rc_pid=$(check_pidfile ${pidfile} ${command}) | |
if [ $rc_pid ]; then | |
if [ $rc_force ]; then | |
${command} ${haproxy_flags} -st ${rc_pid} | |
else | |
${command} ${haproxy_flags} -sf ${rc_pid} | |
fi | |
else | |
_run_rc_notrunning | |
return 1 | |
fi | |
} | |
run_rc_command "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment