Move configuration functions into inc/*
* config/INI functions from functions-common to to inc/ini-config
* local.conf meta-config functions from lib/config to inc/meta-config
Change-Id: I00fab724075a693529273878875cfd292d00b18a
diff --git a/inc/ini-config b/inc/ini-config
new file mode 100644
index 0000000..0d6d169
--- /dev/null
+++ b/inc/ini-config
@@ -0,0 +1,223 @@
+#!/bin/bash
+#
+# **inc/ini-config** - Configuration/INI functions
+#
+# Support for manipulating INI-style configuration files
+#
+# These functions have no external dependencies and no side-effects
+
+# Save trace setting
+INC_CONF_TRACE=$(set +o | grep xtrace)
+set +o xtrace
+
+
+# Config Functions
+# ================
+
+# Append a new option in an ini file without replacing the old value
+# iniadd config-file section option value1 value2 value3 ...
+function iniadd {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ shift 3
+
+ local values="$(iniget_multiline $file $section $option) $@"
+ iniset_multiline $file $section $option $values
+ $xtrace
+}
+
+# Comment an option in an INI file
+# inicomment config-file section option
+function inicomment {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
+ $xtrace
+}
+
+# Get an option from an INI file
+# iniget config-file section option
+function iniget {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ local line
+
+ line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
+ echo ${line#*=}
+ $xtrace
+}
+
+# Get a multiple line option from an INI file
+# iniget_multiline config-file section option
+function iniget_multiline {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ local values
+
+ values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
+ echo ${values}
+ $xtrace
+}
+
+# Determinate is the given option present in the INI file
+# ini_has_option config-file section option
+function ini_has_option {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ local line
+
+ line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
+ $xtrace
+ [ -n "$line" ]
+}
+
+# Add another config line for a multi-line option.
+# It's normally called after iniset of the same option and assumes
+# that the section already exists.
+#
+# Note that iniset_multiline requires all the 'lines' to be supplied
+# in the argument list. Doing that will cause incorrect configuration
+# if spaces are used in the config values.
+#
+# iniadd_literal config-file section option value
+function iniadd_literal {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ local value=$4
+
+ [[ -z $section || -z $option ]] && return
+
+ # Add it
+ sed -i -e "/^\[$section\]/ a\\
+$option = $value
+" "$file"
+
+ $xtrace
+}
+
+# Remove an option from an INI file
+# inidelete config-file section option
+function inidelete {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+
+ [[ -z $section || -z $option ]] && return
+
+ # Remove old values
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
+
+ $xtrace
+}
+
+# Set an option in an INI file
+# iniset config-file section option value
+function iniset {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ local value=$4
+
+ [[ -z $section || -z $option ]] && return
+
+ if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
+ # Add section at the end
+ echo -e "\n[$section]" >>"$file"
+ fi
+ if ! ini_has_option "$file" "$section" "$option"; then
+ # Add it
+ sed -i -e "/^\[$section\]/ a\\
+$option = $value
+" "$file"
+ else
+ local sep=$(echo -ne "\x01")
+ # Replace it
+ sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
+ fi
+ $xtrace
+}
+
+# Set a multiple line option in an INI file
+# iniset_multiline config-file section option value1 value2 valu3 ...
+function iniset_multiline {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+
+ shift 3
+ local values
+ for v in $@; do
+ # The later sed command inserts each new value in the line next to
+ # the section identifier, which causes the values to be inserted in
+ # the reverse order. Do a reverse here to keep the original order.
+ values="$v ${values}"
+ done
+ if ! grep -q "^\[$section\]" "$file"; then
+ # Add section at the end
+ echo -e "\n[$section]" >>"$file"
+ else
+ # Remove old values
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
+ fi
+ # Add new ones
+ for v in $values; do
+ sed -i -e "/^\[$section\]/ a\\
+$option = $v
+" "$file"
+ done
+ $xtrace
+}
+
+# Uncomment an option in an INI file
+# iniuncomment config-file section option
+function iniuncomment {
+ local xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local file=$1
+ local section=$2
+ local option=$3
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
+ $xtrace
+}
+
+function isset {
+ nounset=$(set +o | grep nounset)
+ set +o nounset
+ [[ -n "${!1+x}" ]]
+ result=$?
+ $nounset
+ return $result
+}
+
+
+# Restore xtrace
+$INC_CONF_TRACE
+
+# Local variables:
+# mode: shell-script
+# End:
diff --git a/inc/meta-config b/inc/meta-config
new file mode 100644
index 0000000..c8789bf
--- /dev/null
+++ b/inc/meta-config
@@ -0,0 +1,185 @@
+#!/bin/bash
+#
+# **lib/meta-config** - Configuration file manipulation functions
+#
+# Support for DevStack's local.conf meta-config sections
+#
+# These functions have no external dependencies and the following side-effects:
+#
+# CONFIG_AWK_CMD is defined, default is ``awk``
+
+# Meta-config files contain multiple INI-style configuration files
+# using a specific new section header to delimit them:
+#
+# [[group-name|file-name]]
+#
+# group-name refers to the group of configuration file changes to be processed
+# at a particular time. These are called phases in ``stack.sh`` but
+# group here as these functions are not DevStack-specific.
+#
+# file-name is the destination of the config file
+
+# Save trace setting
+INC_META_XTRACE=$(set +o | grep xtrace)
+set +o xtrace
+
+
+# Allow the awk command to be overridden on legacy platforms
+CONFIG_AWK_CMD=${CONFIG_AWK_CMD:-awk}
+
+# Get the section for the specific group and config file
+# get_meta_section infile group configfile
+function get_meta_section {
+ local file=$1
+ local matchgroup=$2
+ local configfile=$3
+
+ [[ -r $file ]] || return 0
+ [[ -z $configfile ]] && return 0
+
+ $CONFIG_AWK_CMD -v matchgroup=$matchgroup -v configfile=$configfile '
+ BEGIN { group = "" }
+ /^\[\[.+\|.*\]\]/ {
+ if (group == "") {
+ gsub("[][]", "", $1);
+ split($1, a, "|");
+ if (a[1] == matchgroup && a[2] == configfile) {
+ group=a[1]
+ }
+ } else {
+ group=""
+ }
+ next
+ }
+ {
+ if (group != "")
+ print $0
+ }
+ ' $file
+}
+
+
+# Get a list of config files for a specific group
+# get_meta_section_files infile group
+function get_meta_section_files {
+ local file=$1
+ local matchgroup=$2
+
+ [[ -r $file ]] || return 0
+
+ $CONFIG_AWK_CMD -v matchgroup=$matchgroup '
+ /^\[\[.+\|.*\]\]/ {
+ gsub("[][]", "", $1);
+ split($1, a, "|");
+ if (a[1] == matchgroup)
+ print a[2]
+ }
+ ' $file
+}
+
+
+# Merge the contents of a meta-config file into its destination config file
+# If configfile does not exist it will be created.
+# merge_config_file infile group configfile
+function merge_config_file {
+ local file=$1
+ local matchgroup=$2
+ local configfile=$3
+
+ get_meta_section $file $matchgroup $configfile | \
+ $CONFIG_AWK_CMD -v configfile=$configfile '
+ BEGIN {
+ section = ""
+ last_section = ""
+ section_count = 0
+ }
+ /^\[.+\]/ {
+ gsub("[][]", "", $1);
+ section=$1
+ next
+ }
+ /^ *\#/ {
+ next
+ }
+ /^[^ \t]+/ {
+ # get offset of first '=' in $0
+ eq_idx = index($0, "=")
+ # extract attr & value from $0
+ attr = substr($0, 1, eq_idx - 1)
+ value = substr($0, eq_idx + 1)
+ # only need to strip trailing whitespace from attr
+ sub(/[ \t]*$/, "", attr)
+ # need to strip leading & trailing whitespace from value
+ sub(/^[ \t]*/, "", value)
+ sub(/[ \t]*$/, "", value)
+
+ # cfg_attr_count: number of config lines per [section, attr]
+ # cfg_attr: three dimensional array to keep all the config lines per [section, attr]
+ # cfg_section: keep the section names in the same order as they appear in local.conf
+ # cfg_sec_attr_name: keep the attr names in the same order as they appear in local.conf
+ if (! (section, attr) in cfg_attr_count) {
+ if (section != last_section) {
+ cfg_section[section_count++] = section
+ last_section = section
+ }
+ attr_count = cfg_sec_attr_count[section_count - 1]++
+ cfg_sec_attr_name[section_count - 1, attr_count] = attr
+
+ cfg_attr[section, attr, 0] = value
+ cfg_attr_count[section, attr] = 1
+ } else {
+ lno = cfg_attr_count[section, attr]++
+ cfg_attr[section, attr, lno] = value
+ }
+ }
+ END {
+ # Process each section in order
+ for (sno = 0; sno < section_count; sno++) {
+ section = cfg_section[sno]
+ # The ini routines simply append a config item immediately
+ # after the section header. To keep the same order as defined
+ # in local.conf, invoke the ini routines in the reverse order
+ for (attr_no = cfg_sec_attr_count[sno] - 1; attr_no >=0; attr_no--) {
+ attr = cfg_sec_attr_name[sno, attr_no]
+ if (cfg_attr_count[section, attr] == 1)
+ print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, 0] "\""
+ else {
+ # For multiline, invoke the ini routines in the reverse order
+ count = cfg_attr_count[section, attr]
+ print "inidelete " configfile " " section " " attr
+ print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, count - 1] "\""
+ for (l = count -2; l >= 0; l--)
+ print "iniadd_literal " configfile " " section " " attr " \"" cfg_attr[section, attr, l] "\""
+ }
+ }
+ }
+ }
+ ' | while read a; do eval "$a"; done
+}
+
+
+# Merge all of the files specified by group
+# merge_config_group infile group [group ...]
+function merge_config_group {
+ local localfile=$1; shift
+ local matchgroups=$@
+
+ [[ -r $localfile ]] || return 0
+
+ local configfile group
+ for group in $matchgroups; do
+ for configfile in $(get_meta_section_files $localfile $group); do
+ if [[ -d $(dirname $(eval "echo $configfile")) ]]; then
+ merge_config_file $localfile $group $configfile
+ fi
+ done
+ done
+}
+
+
+# Restore xtrace
+$INC_META_XTRACE
+
+# Local variables:
+# mode: shell-script
+# End: