Skip to content

Instantly share code, notes, and snippets.

@pulketo
Forked from jlazic/haconfig.sh
Last active September 14, 2021 19:56
Show Gist options
  • Save pulketo/600fbacd4f35b81a070ad1b029335c26 to your computer and use it in GitHub Desktop.
Save pulketo/600fbacd4f35b81a070ad1b029335c26 to your computer and use it in GitHub Desktop.
Split monolithic HAProxy configuration
#!/bin/bash
#Requirements: nothing
#This script renames haproxy individual site configuration files to disable them for h2gencfg to be aware of them
#Script assumes templates on: /etc/haproxy/templates
#config files on /etc/haproxy/conf.d/
#├── 20-http-domain.tld.cfg
#├── 40-https-domain.tld.cfg
#└── 50-backends-domain.tld.cfg
TIMEDATE=$(date +"%Y%m%d-%H%M%S")
syntaxis () {
echo "example: "
echo -ne "\t$0 <www.domain.tld>\n"
exit 1
}
NEWSITE=${1}
YES=${5}
if [ -z "${NEWSITE}" ];then
echo "No site url supplied"
syntaxis
exit 1
fi
if [ "${YES}" = "-y" ];then
REPLY="y"
else
REPLY="n"
fi
COLORDIFF=$(which colordiff 2>/dev/null)||COLORDIFF=$(which diff 2>/dev/null)||COLORDIFF=$(true)
CONFIGDIR=/etc/haproxy/conf.d
TEMPLATEDIR=/etc/haproxy/templates
BEFOREFILE=/tmp/h2esite.before.${TIMEDATE}
AFTERFILE=/tmp/h2esite.after.${TIMEDATE}
ls -lrt ${CONFIGDIR} >${BEFOREFILE}
test ! -d "${CONFIGDIR}" && echo "error:config directory not found" && exit 1
test ! -d "${TEMPLATEDIR}" && echo "error:template directory not found" && exit 1
for f in ${TEMPLATEDIR}/*; do
d=${f/domain.tld/$NEWSITE}
d=${d/.disabled/}
enabled=${d/templates/conf.d}
disabling="${enabled}.disabling"
stat $enabled 1>/dev/null 2>/dev/null&&(mv ${enabled} ${disabling}&&echo "renaming ${enabled}->${disabling}")
done
C=`find ${CONFIGDIR} -maxdepth 1 -name "*-${NEWSITE}.cfg.disabling" | wc -l`
T=`find ${TEMPLATEDIR} -maxdepth 1 -type f | wc -l`
#echo "C:${C}"
#echo "T:${T}"
if [ ${C} -eq "0" ];then
echo "Nothing to disabled, maybe is already disabled."
exit 1
fi
if ! [ ${C} -eq ${T} ];then
echo "Number of renamed files different than template, terminating"
exit 1
fi
echo "Renaming test OK... go ahead"
ls ${CONFIGDIR}/*-${NEWSITE}.disabling
rename cfg.disabling cfg.disabled ${CONFIGDIR}/*-${NEWSITE}.cfg.disabling
ls -lrt ${CONFIGDIR} >${AFTERFILE}
$COLORDIFF ${BEFOREFILE} ${AFTERFILE}
echo "==================================================================================================="
echo "Now you could regenerate haproxy.cfg"
exit 0
#!/bin/bash
#Requirements: nothing
#This script renames haproxy individual site configuration files to enable them for h2gencfg to be aware of them
#Script assumes templates on: /etc/haproxy/templates
#/etc/haproxy/conf.d/
#├── 20-http-domain.tld.cfg
#├── 40-https-domain.tld.cfg
#└── 50-backends-domain.tld.cfg
TIMEDATE=$(date +"%Y%m%d-%H%M%S")
syntaxis () {
echo "example: "
echo -ne "\t$0 <www.domain.tld> [-y]\n"
exit 1
}
NEWSITE=${1}
YES=${5}
if [ -z "${NEWSITE}" ];then
echo "No site url supplied"
syntaxis
exit 1
fi
if [ "${YES}" = "-y" ];then
REPLY="y"
else
REPLY="n"
fi
COLORDIFF=$(which colordiff 2>/dev/null)||COLORDIFF=$(which diff 2>/dev/null)||COLORDIFF=$(true)
CONFIGDIR=/etc/haproxy/conf.d
TEMPLATEDIR=/etc/haproxy/templates
BEFOREFILE=/tmp/h2esite.before.${TIMEDATE}
AFTERFILE=/tmp/h2esite.after.${TIMEDATE}
ls -lrt ${CONFIGDIR} >${BEFOREFILE}
test ! -d "${CONFIGDIR}" && echo "error:config directory not found" && exit 1
test ! -d "${TEMPLATEDIR}" && echo "error:template directory not found" && exit 1
for f in ${TEMPLATEDIR}/*; do
d=${f/domain.tld/$NEWSITE}
disabled=${d/templates/conf.d}
enabling=${disabled/.disabled/.enabling}
stat $disabled 1>/dev/null 2>/dev/null&&(mv ${disabled} ${enabling}&&echo "renaming ${disabled}->${enabling}")
done
C=`find ${CONFIGDIR} -maxdepth 1 -name "*-${NEWSITE}.cfg.enabling" | wc -l`
T=`find ${TEMPLATEDIR} -maxdepth 1 -type f | wc -l`
#echo "C:${C}"
#echo "T:${T}"
if [ ${C} -eq "0" ];then
echo "Nothing to enable, maybe is already enabled."
exit 1
fi
if ! [ ${C} -eq ${T} ];then
echo "Number of renamed files different than template, terminating"
exit 1
fi
echo "Renaming test OK... go ahead"
ls ${CONFIGDIR}/*-${NEWSITE}.enabling
rename cfg.enabling cfg ${CONFIGDIR}/*-${NEWSITE}.cfg.enabling
ls -lrt ${CONFIGDIR} >${AFTERFILE}
$COLORDIFF ${BEFOREFILE} ${AFTERFILE}
echo "==================================================================================================="
echo "Now you could regenerate haproxy.cfg w"
exit 0
#!/bin/bash
#Requirements: nothing
#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-main-default.cfg
#├── 10-http-default.cfg
#├── 20-http-your-site1.tld.cfg
#├── 20-http-your-site2.tld.cfg
#├── 30-https-default.cfg
#├── 40-https-your-site1.tld.cfg
#├── 40-https-your-site2.tld.cfg
#├── 50-backends-your-site1.tld.cfg
#├── 50-backends-your-site2.tld.cfg
#└── 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.
TIMEDATE=$(date +"%Y%m%d-%H%M%S")
CURRENTCFG=/etc/haproxy/haproxy.cfg
LATESTBACKUP="$(ls -lrt ${CURRENTCFG}.*.backup 2>/dev/null|tail -n1)"
NEWCFG=/tmp/haproxy.cfg.tmp
CONFIGDIR=/etc/haproxy/conf.d
HAPROXY=$(which haproxy)
COLORDIFF=$(which colordiff 2>/dev/null)||COLORDIFF=$(which diff 2>/dev/null)||COLORDIFF=$(true)
#noETCKEEPER, you should go backwards, but...
#is current config OK...and is current config different than last backup... then backup
$HAPROXY -c "${CURRENTCFG}" 1>/dev/null 2>/dev/null && (diff ${CURRENTCFG} ${LATESTBACKUP} 2>/dev/null || cp -p "${CURRENTCFG}" "${CURRENTCFG}.${TIMEDATE}.backup" )
NOACTION=0
test "$1" = "auto" && NOACTION=1
test ! -f "${HAPROXY}" && echo "error: haproxy executable not found" && exit 1
test ! -f "${COLORDIFF}" && echo "error: diff executable not found" && exit 1
test ! -f "${CURRENTCFG}" && echo "warning: current configuration not found"
test ! -d "${CONFIGDIR}" && echo "error:customers 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 "==================================================================================================="
${COLORDIFF} -s -U 3 ${CURRENTCFG} ${NEWCFG}
#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 :)"
read -p "Maybe you just want to reload current config? [y/N]" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]];then
echo -ne "\nRestarting haproxy...\n"
haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) -f ${CURRENTCFG} && exit 0
fi
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}
echo "Restarting haproxy..."
haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) -f ${CONFIGDIR}
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}
echo -ne "\nRestarting haproxy...\n"
haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) -f ${CURRENTCFG}
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
#!/bin/bash
#Requirements: nothing
#This script generates configuration files for a certain domain based on templates
#Script assumes templates on: /etc/haproxy/templates
#/etc/haproxy/conf.d/
#├── 20-http-domain.tld.cfg
#├── 40-https-domain.tld.cfg
#└── 50-backends-domain.tld.cfg
syntaxis () {
echo "example: "
echo -ne "\t$0 <www.domain.tld> <servername> <ip> [port] [-y]\n"
exit 1
}
NEWSITE=${1}
SERVERNAME=${2}
SERVERIP=${3}
SERVERPORT=${4}
YES=${5}
if [ -z "${NEWSITE}" ];then
echo "No site url supplied"
syntaxis
exit 1
fi
if [ -z "${SERVERNAME}" ];then
echo "No servername supplied"
syntaxis
exit 1
fi
if [ -z "${SERVERIP}" ];then
echo "No IP"
syntaxis
exit 1
fi
if [ -z "${SERVERPORT}" ];then
SERVERPORT="80"
fi
if [ "${YES}" = "-y" ];then
REPLY="y"
else
REPLY="n"
fi
TIMEDATE=$(date +"%Y%m%d-%H%M%S")
#CURRENTCFG=/etc/haproxy/haproxy.cfg
#LATESTBACKUP="$(ls -lrt ${CURRENTCFG}.*.backup 2>/dev/null|tail -n1)"
#NEWCFG=/tmp/haproxy.cfg.tmp
CONFIGDIR=/etc/haproxy/conf.d
TEMPLATEDIR=/etc/haproxy/templates
TMPDIR="/tmp/haproxy.newsite.${TIMEDATE}"
HAPROXY=$(which haproxy)
COLORDIFF=$(which colordiff 2>/dev/null)||COLORDIFF=$(which diff 2>/dev/null)||COLORDIFF=$(true)
NOACTION=0
test "$1" = "auto" && NOACTION=1
test ! -d "${CONFIGDIR}" && echo "error:config directory not found" && exit 1
test ! -d "${TEMPLATEDIR}" && echo "error:template directory not found" && exit 1
echo -e "Generating new site\n"
echo -e "Compiling template files to ${TMPDIR}\n"
cp -Rp ${TEMPLATEDIR} ${TMPDIR}
echo "Renaming according url"
rename "domain.tld" "${NEWSITE}" ${TMPDIR}/* && echo "renamed ok..." || ( echo "rename error"; exit 1 )
echo "Replacing domain.tld with url"
sed -i "s/DOMAIN.TLD/${NEWSITE}/g" ${TMPDIR}/* && echo "DOMAIN.TLD replacing went ok..." || ( echo "DOMAIN.TLD replacing error"; exit 1 )
sed -i "s/SERVERNAME/${SERVERNAME}/g" ${TMPDIR}/* && echo "SERVERNAME replacing went ok..." || ( echo "SERVERNAME replacing error"; exit 1 )
sed -i "s/SERVERIP/${SERVERIP}/g" ${TMPDIR}/* && echo "SERVERIP replacing went ok..." || ( echo "SERVERIP replacing error"; exit 1 )
sed -i "s/SERVERPORT/${SERVERPORT}/g" ${TMPDIR}/* && echo "SERVERPORT replacing went ok..." || ( echo "SERVERPORT replacing error"; exit 1 )
echo "==================================================================================================="
find ${TMPDIR} -type f -exec grep -RHn -e '' "{}" \;
echo "==================================================================================================="
if ! [[ ${REPLY} =~ ^[Yy]$ ]];then
read -p "Shall we save new created domain to ${CONFIGDIR} ? [y/N]" -n 1 -r
fi
if [[ ${REPLY} =~ ^[Yy]$ ]];then
echo -ne "\nCopying from ${TMPDIR} to ${CONFIGDIR}\n"
mv ${TMPDIR}/* ${CONFIGDIR} || exit 1
fi
echo -ne "\n===================================================================================================\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment