devstack/lib/config
Fergal Mc Carthy cc87c2871d local.conf processing doesn't handle '=' in values
When attempting to add a libvirt section with a volume_drivers entry
to $NOVA_CONF, via a post-config block in the local.conf file, I
encountered problems; the value for this attribute takes the form

    driver=python.import.path.to.driver

but the value actually populated in the $NOVA_CONF was truncated at the
equals.

Taking the iscsi driver setting specified in the official nova.conf
documentation as an example, if I have the following in my local.conf
file:

[[post-config|$NOVA_CONF]]
[libvirt]
volume_drivers = iscsi=nova.virt.libvirt.volume.LibvirtISCSIVolumeDriver

I will see that the generated $NOVA_CONF has the following:

[libvirt]
volume_driver = iscsi

This occurs because the existing handling for a post-config setion, as
implemented in merge_config_file(), splits the line on the equals sign,
and then uses the first and seconds elements of the resulting array as
attribute name and value respectively.

However when an equals occurs as part of the value this results in the
value being truncated at the first equals in the value.

The fix I've implemented, based upon review feedback, extracts the
contents of $0 before the first equals as the attr name, and extracts
the remainder after the equals as the value. Then it strips the leading
and trailing whitespaces from both as appropriate.

I've also added test5 to tests/test_config.sh to test for, and verify,
correct operation when this scenario is encountered.  Similarly I've
added test6 to ensure that trailing spaces in values are stripped
correctly.

Change-Id: Id0cb1e6e1cece21bc5dbf427c4d756af86fbd927
Closes-Bug: #1374482
2014-10-10 08:25:38 -04:00

141 lines
3.8 KiB
Plaintext

# lib/config - Configuration file manipulation functions
# 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
C_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
# note in the awk below, \x27 is ascii for ' -- this avoids
# having to do nasty quoting games
get_meta_section $file $matchgroup $configfile | \
$CONFIG_AWK_CMD -v configfile=$configfile '
BEGIN { section = "" }
/^\[.+\]/ {
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)
print "iniset " configfile " " section " " attr " \x27" value "\x27"
}
' | 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
$C_XTRACE
# Local variables:
# mode: shell-script
# End: