Skip to content

Instantly share code, notes, and snippets.

@zzf01
Forked from jlazic/haconfig.sh
Last active June 28, 2020 16:51
Show Gist options
  • Select an option

  • Save zzf01/2f41951e807b0497208ccfea9b4c4fd6 to your computer and use it in GitHub Desktop.

Select an option

Save zzf01/2f41951e807b0497208ccfea9b4c4fd6 to your computer and use it in GitHub Desktop.
Split monolithic HAProxy configuration
#!/bin/bash
#Requirements: etckeeper, colordiff
#Script based on https://gist.github.com/jlazic/e65f5bda141ffaed5640 by Josip Lazić
#Article: https://lazic.info/josip/post/splitting-haproxy-config/
#This script concatenates multiple files of haproxy configuration into
#one file, and than checks if monolithic config contains errors. If everything is
#OK with new config script will write new config to $CURRENTCFG and reload haproxy
#Also, script will commit changes to etckeeper, if you don't use etckeeper you
#should start using it.
#Script assumes following directory structure:
#/etc/haproxy/conf.d/
#├── 00-global.cfg
#├── 15-lazic.cfg
#├── 16-togs.cfg
#├── 17-svartberg.cfg
#├── 18-home1.cfg.disabled
#└── 99-globalend.cfg
#Every site has it's own file, so you can disable site by changing
#it's file extension, or appending .disabled, like I do.
CURRENTCFG=/etc/haproxy/haproxy.cfg
NEWCFG=/tmp/haproxy.cfg.tmp
CONFIGDIR=/etc/haproxy/conf.d
HAPROXY=$(which haproxy)
COLORDIFF=$(which colordiff)
ETCKEEPER=$(which etckeeper)
NOACTION=0
test "$1" = "auto" && NOACTION=1
test ! -f "$HAPROXY" && echo "haproxy executable not found" && exit 1
test ! -f "$COLORDIFF" && echo "colordiff executable not found" && exit 1
test ! -f "$ETCKEEPER" && echo "etckeeper executable not found" && exit 1
test ! -f "$CURRENTCFG" && echo "current configuration not found" && exit 1
test ! -d "$CONFIGDIR" && echo "config directory not found" && exit 1
echo -e "Compiling templates and redirects from $CONFIGDIR\n"
for f in `ls -1 $CONFIGDIR/*.template`; do
TEMPLATE=`basename -s .template $f`
cp $f $CONFIGDIR/${TEMPLATE}.cfg
REDIRECT=`sed -ne "s/___\(.*\)___/\1/p" $f | tr -d '[:space:]'`
if test -n "$REDIRECT" && test -f "$CONFIGDIR/$REDIRECT"; then
sed -e '/___.*___/ {' -e "r $CONFIGDIR/$REDIRECT" -e 'd' -e '}' -i $CONFIGDIR/${TEMPLATE}.cfg
fi
done
echo -e "Compiling *.cfg files from $CONFIGDIR\n"
test $NOACTION -eq 0 && ls $CONFIGDIR/*.cfg
cat $CONFIGDIR/*.cfg > $NEWCFG
echo "Differences between current and new config"
echo "==================================================================================================="
if [ $NOACTION -eq 1 ]; then
$COLORDIFF -s $CURRENTCFG $NEWCFG
else
$COLORDIFF -s -U 3 $CURRENTCFG $NEWCFG
fi
CDOUT=$?
echo "==================================================================================================="
if [ $CDOUT -eq 0 ]; then
echo "You should make some changes first :)"
exit 1 #Exit if old and new configuration are the same
fi
echo -n "Checking if new config is valid... "
$HAPROXY -c -f $NEWCFG
if [ $? -eq 0 ]; then
if [ $NOACTION -eq 1 ]; then
cat /etc/haproxy/conf.d/*.cfg > $CURRENTCFG
$ETCKEEPER commit -m "Updating haproxy configuration" &>/dev/null
echo "Restarting haproxy..."
service haproxy restart
else
echo "Check if there are some warnings in new configuration."
read -p "Should I copy new configuration to $CURRENTCFG and reload haproxy? [y/N]" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
cat /etc/haproxy/conf.d/*.cfg > $CURRENTCFG
$ETCKEEPER commit -m "Updating haproxy configuration"
echo "Restarting haproxy..."
service haproxy restart
fi
fi
echo -e "\nAll done. Bye.\n\n"
else
echo "There are errors in new configuration, please fix them and try again."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment