From e00d7e36bc0ff3eaad348e01e42d12bc5545adb4 Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Fri, 9 Sep 2016 15:59:56 +0200 Subject: [PATCH] MT#22241 Support hostname and pairname specific templates (PRO-/CARRIER-only) If we have multiple web0X proxies but need specific templates on just one of those pairs then we need to support *.customtt.tt2.$pairname + *.tt2.$pairname templates. While at it also support *.customtt.tt2.$nodename + foo.tt2.$nodename (where nodename is actual hostname and not sp1 or sp2 as already supported already). Test script for demonstration (especially for precedence): mkdir /etc/ngcp-config/templates/etc/mika cd /etc/ngcp-config/templates/etc/mika hostname=$(hostname) # e.g. web01b pairname=${hostname%[ab]} # e.g. web01 nodename=$(cat /etc/ngcp_nodename) # e.g. sp2 for file in foo.customtt.tt2.$hostname foo.customtt.tt2.$pairname foo.customtt.tt2.$nodename foo.customtt.tt2 foo.tt2.$hostname foo.tt2.$pairname foo.tt2.$nodename foo.tt2 ; do echo $file > $file ; done for file in foo.customtt.tt2.$hostname foo.customtt.tt2.$pairname foo.customtt.tt2.$nodename foo.customtt.tt2 foo.tt2.$hostname foo.tt2.$pairname foo.tt2.$nodename foo.tt2 ; do ngcpcfg build /etc/mika/ >/dev/null if [ "$(cat /etc/mika/foo)" = "$file" ] ; then echo "Content of file /etc/mika/foo matching $file" rm "$file" ; rm /etc/mika/foo else echo "Problem with precedence of file $file" return 1 fi done Change-Id: If7c222374bb56d7bf243200b37a414e39e050cd4 --- docs/ngcpcfg.txt | 50 ++++++++++++++++++++++++++++++++--------- functions/main | 55 +++++++++++++++++++++++++++++++++++++++------ helper/build_config | 10 +++++---- 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/docs/ngcpcfg.txt b/docs/ngcpcfg.txt index 0e6146ab..5f72e12e 100644 --- a/docs/ngcpcfg.txt +++ b/docs/ngcpcfg.txt @@ -99,34 +99,64 @@ hosts changes should be pushed to. Supported template files ~~~~~~~~~~~~~~~~~~~~~~~~ -Example for generating a configuration file named_/etc/foobar/baz_: +Example for generating a configuration file named_/etc/foobar/baz_ (from lower +precedence to higher precedence): * _/etc/ngcp-config/templates/etc/foobar/baz.tt2_: main and default template file, used by template-handler for generating /etc/foobar/baz. Configuration file is usually provided by a Debian package. -* _/etc/ngcp-config/templates/etc/foobar/baz.customtt.tt2_: system specific -template file, but configuration usually isn't provided by a Debian package and -can be modified independent from any Debian package mechanism. - * _/etc/ngcp-config/templates/etc/foobar/baz.tt2.$HA_NODE_: node specific template file. $HA_NODE is determined using the content of /etc/ngcp_ha_node (usually being _sp1_ for the first node and _sp2_ for the second node on the Sipwise Next Generation Platform). Wheras _*customtt.tt2_ files are used on all nodes in a High Availability setup the _*.tt2.$HA_NODE*_ file is specific for the single node only. A common usage case is master vs. slave configuration of a -service. Configuration file is usually provided by a Debian package. Note: -Feature is available in the High Availability setup only. +service. Configuration file is usually provided by a Debian package. +Note: Feature is available in the High Availability setup only. + +* _/etc/ngcp-config/templates/etc/foobar/baz.tt2.$PAIRNAME_: pair specific +template file. Given a PRO pair in a CARRIER environment where two nodes are +named _web01a_ and _web01b_ then $PAIRNAME corresponds to _web01_. This is +useful if there are multiple pairs available but only a specific pair of them +should include some specific configuration. +Note: Feature is available in the High Availability setup only. + +* _/etc/ngcp-config/templates/etc/foobar/baz.tt2.$HOSTNAME_: hostname specific +template file. While $HA_NODE is usually bound to _sp1_ for the first node and +_sp2_ via /etc/ngcp_ha_node it's possible to use host specific template file by +referring to its hostname ($HOSTNAME is determined via (hostname(1)). +Note: Feature is available in the High Availability setup only. + +* _/etc/ngcp-config/templates/etc/foobar/baz.customtt.tt2_: system specific +template file, but configuration usually isn't provided by a Debian package and +can be modified independent from any Debian package mechanism. * _/etc/ngcp-config/templates/etc/foobar/baz.customtt.tt2.$HA_NODE_: node specific template file. Regarding $HA_NODE the same as for _baz.tt2.$HA_NODE_ applies (see previous bullet), but the configuration file usually isn't provided by a Debian package but can be modified independent from any Debian package -mechanism. Note: Feature is available in the High Availability setup only. +mechanism. +Note: Feature is available in the High Availability setup only. + +* _/etc/ngcp-config/templates/etc/foobar/baz.customtt.tt2.$PAIRNAME_: +configuration file similar to +_/etc/ngcp-config/templates/etc/foobar/baz.tt2.$PAIRNAME_ but it's guaranteed +that the file won't be part of any Debian package mechanism. Note: Feature is +available in the High Availability setup only. + +* _/etc/ngcp-config/templates/etc/foobar/baz.customtt.tt2.$HOSTNAME_: +configuration file similar to +_/etc/ngcp-config/templates/etc/foobar/baz.tt2.$HOSTNAME_ but it's guaranteed +that the file won't be part of any Debian package mechanism. Note: Feature is +available in the High Availability setup only. +Note: Feature is available in the High Availability setup only. [IMPORTANT] -Configuration file priority: *.customtt.tt2.$HA_NODE takes precedence over -*.customtt.tt2, over *.tt2.$HA_NODE, over *.tt2. +Configuration file priority: *.customtt.tt2.$HOSTNAME takes precedence over +*.customtt.tt2.$PAIRNAME, over .customtt.tt2.$HA_NODE, over *.customtt.tt2, over +*.tt2.$HOSTNAME, over *.tt2.$PAIRNAME, over *.tt2.$HA_NODE, over *.tt2. + Support action related files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/functions/main b/functions/main index bab04dff..491593b3 100644 --- a/functions/main +++ b/functions/main @@ -203,6 +203,7 @@ fi if [ -r /usr/share/ngcp-ngcpcfg/functions/carrier_features ] ; then . /usr/share/ngcp-ngcpcfg/functions/carrier_features + set_host_and_pair_files # set $HOST_FILE + $PAIR_FILE for usage in generate_template_list fi ## }}} @@ -216,7 +217,7 @@ generate_template_list() { for dir in ${CONFIG_POOL} ; do [ -n "${dir}" ] || ( echo "${dir} doesn't exist" >&2 ; continue ) # iterate over all files - for file in $(find "$TEMPLATE_POOL_BASE/${dir}" -name \*.tt2 -o -name \*.tt2"${HA_FILE:-}") ; do + for file in $(find "$TEMPLATE_POOL_BASE/${dir}" -name \*.tt2 -o -name \*.tt2"${HA_FILE:-}" -o -name \*.tt2"${HOST_FILE:-}" -o -name \*.tt2"${PAIR_FILE:-}") ; do # *NO* arguments provided via cmdline if [ -z "${1:-}" ] ; then echo "$file" >> "${filelist_prepared}" @@ -232,18 +233,42 @@ generate_template_list() { done # remove all filenames where a preferred filename exists - # foo.customtt.tt2.spX > foo.customtt.tt2 > foo.tt2.spX > foo.tt2 + # foo.customtt.tt2.hostname > foo.customtt.tt2.pairname > foo.customtt.tt2.spX > foo.customtt.tt2 > foo.tt2.hostname > foo.tt2.pairname > foo.tt2.spX > foo.tt2 for line in $(cat ${filelist_prepared}); do - # reduce filenames from "foo.*" to "foo" + normalized_filename="$line" + if [ -n "${HA_FILE:-}" ] ; then - normalized_filename="${line%${HA_FILE}}" - else - normalized_filename="${line}" + normalized_filename="${normalized_filename%${HA_FILE}}" + fi + + if [ -n "${HOST_FILE:-}" ] ; then + normalized_filename="${normalized_filename%${HOST_FILE}}" fi + + if [ -n "${PAIR_FILE:-}" ] ; then + normalized_filename="${normalized_filename%${PAIR_FILE}}" + fi + normalized_filename="${normalized_filename%.customtt.tt2}" normalized_filename="${normalized_filename%.tt2}" + # foo.custom.tt2.hostname + if [ -n "${HOST_FILE:-}" ] ; then + if grep -q -- "^${normalized_filename}.customtt.tt2${HOST_FILE}$" "${filelist_prepared}" ; then + echo "${normalized_filename}.customtt.tt2${HOST_FILE}" >> "${filelist_final}" + continue + fi + fi + + # foo.custom.tt2.pairname + if [ -n "${PAIR_FILE:-}" ] ; then + if grep -q -- "^${normalized_filename}.customtt.tt2${PAIR_FILE}$" "${filelist_prepared}" ; then + echo "${normalized_filename}.customtt.tt2${PAIR_FILE}" >> "${filelist_final}" + continue + fi + fi + # foo.customtt.tt2.sp{1,2} if [ -n "${HA_FILE:-}" ] ; then if grep -q -- "^${normalized_filename}.customtt.tt2${HA_FILE:-}" "${filelist_prepared}" ; then @@ -258,6 +283,22 @@ generate_template_list() { continue fi + # foo.tt2.hostname + if [ -n "${HOST_FILE:-}" ] ; then + if grep -q -- "^${normalized_filename}.tt2${HOST_FILE}" "${filelist_prepared}" ; then + echo "${normalized_filename}.tt2${HOST_FILE}" >> "${filelist_final}" + continue + fi + fi + + # foo.tt2.pairname + if [ -n "${HOST_FILE:-}" ] ; then + if grep -q -- "^${normalized_filename}.tt2${PAIR_FILE}" "${filelist_prepared}" ; then + echo "${normalized_filename}.tt2${PAIR_FILE}" >> "${filelist_final}" + continue + fi + fi + # foo.tt2.sp{1,2} if [ -n "${HA_FILE:-}" ] ; then if grep -q -- "^${normalized_filename}.tt2${HA_FILE}" "${filelist_prepared}" ; then @@ -266,7 +307,7 @@ generate_template_list() { fi fi - # file should be used, so list it + # another file not matching any previous checks echo "$line" >> "${filelist_final}" done diff --git a/helper/build_config b/helper/build_config index a7de5a94..6cf79e58 100755 --- a/helper/build_config +++ b/helper/build_config @@ -23,10 +23,12 @@ HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}" input_file="$1" # like /etc/ngcp-config/templates/etc/mysql/my.cnf.tt2 # calculate output file {{{ - x=${input_file##${NGCPCTL_MAIN}/templates/} # drop leading /etc/ngcp-config - y=${x%%.tt2} # drop trailing suffix '.tt2' - [ -n "${HA_FILE:-}" ] && y=${y%%.tt2${HA_FILE}} # drop trailing suffix '.tt2.sp{1,2}' - y=${y%%.customtt} # drop trailing suffix '.customtt' + x=${input_file##${NGCPCTL_MAIN}/templates/} # drop leading /etc/ngcp-config + y=${x%%.tt2} # drop trailing suffix '.tt2' + [ -n "${HA_FILE:-}" ] && y=${y%%.tt2${HA_FILE}} # drop trailing suffix '.tt2.sp{1,2}' + [ -n "${PAIR_FILE:-}" ] && y=${y%%.tt2${PAIR_FILE}} # drop trailing suffix '.tt2.pairname' + [ -n "${HOST_FILE:-}" ] && y=${y%%.tt2${HOST_FILE}} # drop trailing suffix '.tt2.hostname' + y=${y%%.customtt} # drop trailing suffix '.customtt' # }}} export output_file="${y}" # like /etc/mysql/my.cnf, export variable # for usage within {pre,post}build scripts