#!/usr/bin/env bash # # Install HAProxy # Script works on Ubuntu 12.04 and 14.04 only set -e set -u set -o pipefail # These settings are for Ubuntu 12.04 only, where we compile from source export HAPROXY_VERSION=1.5.3 export HAPROXY_CPU=generic # Figure out which version of Ubuntu we have export UBUNTU_VERSION=`cat /etc/issue | awk '{print $2}' | awk -F '.' '{print $1$2}'` # on Ubuntu 14.04 LTS installs from backports function install1404 { export DEBIAN_FRONTEND=noninteractive aptitude update aptitude -y -q -t trusty-backports install haproxy exit 0 } # on Ubuntu 12.04 LTS installs from source function install1204 { # Download the compilers and prerequisite -dev packages export DEBIAN_FRONTEND=noninteractive aptitude update aptitude -q -y install build-essential libssl-dev libpcre3-dev zlib1g-dev virt-what # If we are running on bare metal and not in a virtual environment, the compile with # CPU-native features. export IS_VIRTUALIZED=`virt-what` if [ "${IS_VIRTUALIZED}" = "" ]; then export HAPROXY_CPU=native fi # Download the source code cd /usr/src curl http://www.haproxy.org/download/1.5/src/haproxy-${HAPROXY_VERSION}.tar.gz | tar zx cd haproxy-${HAPROXY_VERSION} # Compile and install make TARGET=linux2628 CPU=${HAPROXY_CPU} USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 make install PREFIX=/usr # Test for haproxy user and create it if needed. Chroot it and prevent it from # getting shell access id -u haproxy &>/dev/null || useradd -d /var/lib/haproxy -s /bin/false haproxy # Set up the default haproxy config files mkdir -p /etc/haproxy/errors cp examples/errorfiles/* /etc/haproxy/errors cat > /etc/haproxy/haproxy.cfg < /etc/default/haproxy < /etc/init.d/haproxy < PATH=/sbin:/usr/sbin:/bin:/usr/bin PIDFILE=/var/run/haproxy.pid CONFIG=/etc/haproxy/haproxy.cfg HAPROXY=/usr/sbin/haproxy RUNDIR=/run/haproxy EXTRAOPTS= test -x \$HAPROXY || exit 0 if [ -e /etc/default/haproxy ]; then . /etc/default/haproxy fi test -f "\$CONFIG" || exit 0 [ -f /etc/default/rcS ] && . /etc/default/rcS . /lib/lsb/init-functions check_haproxy_config() { \$HAPROXY -c -f "\$CONFIG" >/dev/null if [ \$? -eq 1 ]; then log_end_msg 1 exit 1 fi } haproxy_start() { [ -d "\$RUNDIR" ] || mkdir "\$RUNDIR" chown haproxy:haproxy "\$RUNDIR" chmod 2775 "\$RUNDIR" check_haproxy_config start-stop-daemon --quiet --oknodo --start --pidfile "\$PIDFILE" \\ --exec \$HAPROXY -- -f "\$CONFIG" -D -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() { check_haproxy_config \$HAPROXY -f "\$CONFIG" -p \$PIDFILE -D \$EXTRAOPTS -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 } case "\$1" in start) log_daemon_msg "Starting haproxy" "haproxy" 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_reload ret=\$? case "\$ret" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac exit \$ret ;; restart) log_daemon_msg "Restarting haproxy" "haproxy" haproxy_stop haproxy_start ret=\$? case "\$ret" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; 2) log_end_msg 1 ;; esac exit \$ret ;; 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 : EOF chmod +x /etc/init.d/haproxy # Make a chroot for haproxy, add syslog config to make log socket in said chroot mkdir -p /var/lib/haproxy/dev cat > /etc/rsyslog.d/haproxy.conf < /etc/logrotate.d/haproxy </dev/null 2>&1 || true endscript } EOF # Start on reboot update-rc.d haproxy defaults service haproxy start # Clean up source cd ~ rm -rf /usr/src/haproxy-${HAPROXY_VERSION} exit 0 } # Actually execute the installations if [ "${UBUNTU_VERSION}" = "1404" ]; then install1404 fi if [ "${UBUNTU_VERSION}" = "1204" ]; then install1204 fi echo This script supports Ubuntu 12.04 or 14.04 only. exit 1