Re: #include?

From: Manuel Soto <manuelsspace-listas#yahoo.com>
Date: Thu, 17 Jul 2008 13:26:26 -0430


From: Krzysztof Oledzki <ole#ans.pl>
Date: Fri, 30 May 2008 00:13:21 +0200 (CEST)

On Thu, 29 May 2008, Alberto Giménez wrote:

> On Thu, May 29, 2008 at 10:37 PM, Dan Zubey <dzubey#odysseyware.com> wrote:
>> Willy, would it be possible to add the directive 'include' to have the
>> pre-processor include another config file from the local filesystem?
>> i.e.,
>>
>> option include /etc/haproxy/backend.conf
>>
>>
>> It would help immensely to make things more readable for my coworkers :)
>>
>> I know I could write a pre-preprocessor to make a config file, just
>> wondering if we could have it in haproxy.
>
> Hi Dan, if you *really* need the include thing, you can use cpp and
> #include "files". That should work and you will not need to invent
> your own wheel ;)
>
> Another approach could be write a wrapper for haproxy initscript (or
> executable, directly) that "cat"s the config parts or so, but I think
> "cpp" approach is simpler and would fit your needs.

Hello,

I have the same requirement. Several teams has and will have their services defined in the haproxy, what I do implement is a schema where configurations are included to produce a central file. This is working on Solaris 10/SPARC

Every configuration is defined by a file located in a folder named haproxy.d, names has /etc/init.d like syntax dda+

Script annexed.

Hopping this help.
Manuel Soto

=====[ BEGIN ]=======================================================
#!/bin/ksh -eu

true <<EOF
Copyright (C) 2008 Manuel Soto

    This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by     the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.

    This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the     GNU General Public License for more details.

    You should have received a copy of the GNU General Public License     along with this program. If not, see <http://www.gnu.org/licenses EOF # Variables y sus valores por omision

# Si el prefix no es el $HOME cambie aqui su ubicacion, por ejemplo /usr/local/haproxy

: ${PREFIX:=$HOME}
: ${HAPROXY:=${PREFIX}/haproxy}
: ${CONFIG:=${PREFIX}/haproxy.cfg}
: ${PIDFILE:=${PREFIX}/haproxy.pid}

#: ${HAPROXY_OPT:= -db -V}
: ${HAPROXY_OPT:=-D}
: ${M4:=/usr/ccs/bin/m4}
: ${LOGGER:=logger}
: ${FACILITY:=local0}

: ${SYSDATE:=$(date +'%Y%m%d%H%M%S')}

: ${THIS_SCRIPT:=$(basename $0)}
: ${NAME_THIS_SCRIPT:=${THIS_SCRIPT%.*}} log(){

    local LEVEL=${1:-notice} ; shift
    local MENSAJE="$*"
    logger -p"${FACILITY}.${LEVEL}" "${SYSDATE} ${NAME_THIS_SCRIPT}: ${MENSAJE}"     echo " ${MENSAJE}" >&2
}

compila(){

    # I know. this function may be smaller but, this is easy to maintain

    if [ -w "${CONFIG}" ]
    then

        log notice "Writable configuration file replaced!!"     fi

    if test -z "$(find ${PREFIX}/haproxy.d -name '*.inc' -newer ${CONFIG})" || test ! -f "${CONFIG}"

    then

        log notice "Configurarion updated. Keep unchanged"
        return 0

    fi

    trap exit "rm ${CONFIG}.m4 ${CONFIG}.tmp"

    echo "#--------------[ WARNING ]----------------
# Do not edit this file
#
# This is a file generated from
# ${THIS_SCRIPT}
# and

# " $(ls haproxy.d/*.inc) "
#--------------------------" >${CONFIG}.m4

    for INCLUDE in ${PREFIX}/haproxy.d/*.inc     do

        [ -f "${INCLUDE}" ] && echo "include(${INCLUDE})" >>${CONFIG}.m4     done

    if ${M4} -s "${CONFIG}.m4" > ${CONFIG}.tmp     then

        if [ -f "${CONFIG}" ]
        then
            if mv "${CONFIG}" "${CONFIG}-${SYSDATE}"
            then
            :
            else
                log err "Unable to save backup file. Compilation aborted"
                return 3
            fi

        fi
        if mv "${CONFIG}.tmp" "${CONFIG}" &&
            chmod a-wx "${CONFIG}"
        then
            :
        else
            log err "Unable to generate configuration file"
            return 2
        fi
    else
        log err "Error compiling configuration file: ${CONFIG}.m4"
        return 1

    fi
}

check(){

    ${HAPROXY} -f ${CONFIG} -p ${PIDFILE} -c }

case $1 in
restart)

    compila &&
    check &&
    exec ${HAPROXY} -f ${CONFIG} -p ${PIDFILE} ${HAPROXY_OPT} -sf $(cat ${PIDFILE})

    ;;
start)

    compila &&
    check &&
    exec ${HAPROXY} -f ${CONFIG} -p ${PIDFILE} ${HAPROXY_OPT}     ;;
stop)

    kill $(cat ${PIDFILE})
    ;;
check)

    compila &&
    check || exit 1
    ;;
*)

    echo Uso:
    echo "$0 {start|restart|stop|check}"     ;;
esac
exit 0

=====[ END ]=======================================================
Received on 2008/07/17 19:56

This archive was generated by hypermail 2.2.0 : 2008/07/17 20:00 CEST