Created
March 9, 2015 20:02
-
-
Save jlazic/e65f5bda141ffaed5640 to your computer and use it in GitHub Desktop.
Split monolithic HAProxy configuration
This file contains 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/bash | |
#Requirements: etckeeper, diffcolor | |
#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 | |
echo "Compiling *.cfg files from $CONFIGDIR" | |
ls $CONFIGDIR/*.cfg | |
cat $CONFIGDIR/*.cfg > $NEWCFG | |
echo "Differences between current and new config" | |
diff -s -U 3 $CURRENTCFG $NEWCFG | colordiff | |
if [ $? -ne 0 ]; then | |
echo "You should make some changes first :)" | |
exit 1 #Exit if old and new configuration are the same | |
fi | |
echo -e "Checking if new config is valid..." | |
haproxy -c -f $NEWCFG | |
if [ $? -eq 0 ]; then | |
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 | |
echo " " | |
echo "Working..." | |
cat /etc/haproxy/conf.d/*.cfg > $CURRENTCFG | |
etckeeper commit -m "Updating haproxy configuration" | |
echo "Reloading haproxy..." | |
service haproxy reload | |
fi | |
else | |
echo "There are errors in new configuration, please fix them and try again." | |
exit 1 | |
fi |
Thank you so much!
Thanks for the script! Line 29 has a mistake since "$?" refers to the exit code of colordiff rather than the diff itself. The fix is to change it to:
colordiff -s -U 3 $CURRENTCFG $NEWCFG
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot, works great,