From bf2ad7015d068f9a85c01813cea0aa79143b1d0f Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Mon, 9 Mar 2015 15:16:10 -0500 Subject: [PATCH] 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 --- doc/source/index.rst | 7 +- functions | 1 + functions-common | 199 ---------------- inc/ini-config | 223 ++++++++++++++++++ lib/config => inc/meta-config | 10 +- stack.sh | 2 +- tests/{test_ini.sh => test_ini_config.sh} | 4 +- tests/{test_config.sh => test_meta_config.sh} | 6 +- tools/build_docs.sh | 2 +- tox.ini | 1 + 10 files changed, 243 insertions(+), 212 deletions(-) create mode 100644 inc/ini-config rename lib/config => inc/meta-config (96%) rename tests/{test_ini.sh => test_ini_config.sh} (99%) rename tests/{test_config.sh => test_meta_config.sh} (99%) diff --git a/doc/source/index.rst b/doc/source/index.rst index cfde99132d..bac593de03 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -156,7 +156,6 @@ Scripts * `lib/ceilometer `__ * `lib/ceph `__ * `lib/cinder `__ -* `lib/config `__ * `lib/database `__ * `lib/dstat `__ * `lib/glance `__ @@ -188,6 +187,12 @@ Scripts * `extras.d/70-zaqar.sh `__ * `extras.d/80-tempest.sh `__ +* `inc/ini-config `__ +* `inc/meta-config `__ +* `inc/python `__ + +* `pkg/elasticsearch.sh `_ + Configuration ------------- diff --git a/functions b/functions index 79b2b37b4e..9adbfe7cf6 100644 --- a/functions +++ b/functions @@ -13,6 +13,7 @@ # Include the common functions FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd) source ${FUNC_DIR}/functions-common +source ${FUNC_DIR}/inc/ini-config source ${FUNC_DIR}/inc/python # Save trace setting diff --git a/functions-common b/functions-common index df69cbad16..4739e42e90 100644 --- a/functions-common +++ b/functions-common @@ -43,197 +43,6 @@ declare -A GITDIR TRACK_DEPENDS=${TRACK_DEPENDS:-False} -# 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 -} - -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 -} # Normalize config values to True or False # Accepts as False: 0 no No NO false False FALSE @@ -253,14 +62,6 @@ function trueorfalse { $xtrace } -function isset { - nounset=$(set +o | grep nounset) - set +o nounset - [[ -n "${!1+x}" ]] - result=$? - $nounset - return $result -} # Control Functions # ================= diff --git a/inc/ini-config b/inc/ini-config new file mode 100644 index 0000000000..0d6d169f8b --- /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/lib/config b/inc/meta-config similarity index 96% rename from lib/config rename to inc/meta-config index 31c6fa6e57..c8789bf816 100644 --- a/lib/config +++ b/inc/meta-config @@ -1,7 +1,9 @@ #!/bin/bash # -# lib/config - Configuration file manipulation functions - +# **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`` @@ -18,7 +20,7 @@ # file-name is the destination of the config file # Save trace setting -C_XTRACE=$(set +o | grep xtrace) +INC_META_XTRACE=$(set +o | grep xtrace) set +o xtrace @@ -176,7 +178,7 @@ function merge_config_group { # Restore xtrace -$C_XTRACE +$INC_META_XTRACE # Local variables: # mode: shell-script diff --git a/stack.sh b/stack.sh index bf9fc0118e..2060f2d65a 100755 --- a/stack.sh +++ b/stack.sh @@ -92,7 +92,7 @@ LAST_SPINNER_PID="" source $TOP_DIR/functions # Import config functions -source $TOP_DIR/lib/config +source $TOP_DIR/inc/meta-config # Import 'public' stack.sh functions source $TOP_DIR/lib/stack diff --git a/tests/test_ini.sh b/tests/test_ini_config.sh similarity index 99% rename from tests/test_ini.sh rename to tests/test_ini_config.sh index 106cc9507f..4a0ae33aeb 100755 --- a/tests/test_ini.sh +++ b/tests/test_ini_config.sh @@ -4,8 +4,8 @@ TOP=$(cd $(dirname "$0")/.. && pwd) -# Import common functions -source $TOP/functions +# Import config functions +source $TOP/inc/ini-config echo "Testing INI functions" diff --git a/tests/test_config.sh b/tests/test_meta_config.sh similarity index 99% rename from tests/test_config.sh rename to tests/test_meta_config.sh index 3252104bf1..9d65280b8c 100755 --- a/tests/test_config.sh +++ b/tests/test_meta_config.sh @@ -4,11 +4,9 @@ TOP=$(cd $(dirname "$0")/.. && pwd) -# Import common functions -source $TOP/functions - # Import config functions -source $TOP/lib/config +source $TOP/inc/ini-config +source $TOP/inc/meta-config # check_result() tests and reports the result values # check_result "actual" "expected" diff --git a/tools/build_docs.sh b/tools/build_docs.sh index 929d1e0e6c..2aa0a0ac04 100755 --- a/tools/build_docs.sh +++ b/tools/build_docs.sh @@ -81,7 +81,7 @@ for f in $(find . -name .git -prune -o \( -type f -name \*.sh -not -path \*shocc mkdir -p $FQ_HTML_BUILD/`dirname $f`; $SHOCCO $f > $FQ_HTML_BUILD/$f.html done -for f in $(find functions functions-common lib samples -type f -name \*); do +for f in $(find functions functions-common inc lib pkg samples -type f -name \*); do echo $f FILES+="$f " mkdir -p $FQ_HTML_BUILD/`dirname $f`; diff --git a/tox.ini b/tox.ini index a958ae7ba4..bc84928d95 100644 --- a/tox.ini +++ b/tox.ini @@ -20,6 +20,7 @@ commands = bash -c "find {toxinidir} \ -name \*.sh -or \ -name \*rc -or \ -name functions\* -or \ + -wholename \*/inc/\* \ # /inc files and -wholename \*/lib/\* \ # /lib files are shell, but \) \ # have no extension -print0 | xargs -0 bashate -v"