Skip to content

Instantly share code, notes, and snippets.

@DRN88
Last active June 9, 2022 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRN88/12bd5772d097a7974e6eb1c12e62f5f1 to your computer and use it in GitHub Desktop.
Save DRN88/12bd5772d097a7974e6eb1c12e62f5f1 to your computer and use it in GitHub Desktop.
HAProxy main config generator from conf.d configs
#!/bin/bash
#
# 1. Read and validate config files in conf.d folder: NN_configname.conf
# 2. Merge them to '.haproxy-candidate.cfg'
# 3. Validate the candidate config with haproxy
# 4. If the candidate config is valid overwrite haproxy.cfg
#
MAIN_CONFIG="/etc/haproxy/haproxy.cfg"
TEMP_CONFIG="/etc/haproxy/.haproxy-candidate.cfg"
CONFD_DIR="/etc/haproxy/conf.d"
mkdir -p ${CONFD_DIR} && chmod 0750 ${CONFD_DIR}
CONFD_CONFIG_LIST="`ls -1 ${CONFD_DIR} | grep '^[0-9]\{2\}_.*\.conf$'`"
cat << EOT > ${TEMP_CONFIG}
###
### This file is autogenerated by haproxy-merge-configs.sh
### Changes manually added to this config file will be overwritten
### To add settings use conf.d folder and NN_configname.conf
###
### Last modification: `date`
###
EOT
# Generating candidate config
for CONFIG in `echo ${CONFD_CONFIG_LIST}`; do
echo "### BEGIN: ${CONFIG}" >> ${TEMP_CONFIG}
cat ${CONFD_DIR}/${CONFIG} >> ${TEMP_CONFIG}
echo "### END: ${CONFIG}" >> ${TEMP_CONFIG}
echo -e "\n\n\n" >> ${TEMP_CONFIG}
done
# Validating candidate config
HAPROXY_VALIDATE_STDOUT="`haproxy -c -V -f ${TEMP_CONFIG}`"
if [ $? -ne 0 ]; then
echo ${HAPROXY_VALIDATE_STDOUT}
exit 1
else
mv ${TEMP_CONFIG} ${MAIN_CONFIG}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment