diff --git a/functions b/functions index 8cb703c567..80d1f619da 100644 --- a/functions +++ b/functions @@ -553,6 +553,56 @@ $option = $value fi } +# Get a multiple line option from an INI file +# iniget_multiline config-file section option +function iniget_multiline() { + local file=$1 + local section=$2 + local option=$3 + local values + values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") + echo ${values} +} + +# Set a multiple line option in an INI file +# iniset_multiline config-file section option value1 value2 valu3 ... +function iniset_multiline() { + 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 +} + +# Append a new option in an ini file without replacing the old value +# iniadd config-file section option value1 value2 value3 ... +function iniadd() { + local file=$1 + local section=$2 + local option=$3 + shift 3 + local values="$(iniget_multiline $file $section $option) $@" + iniset_multiline $file $section $option $values +} # is_service_enabled() checks if the service(s) specified as arguments are # enabled by the user in ``ENABLED_SERVICES``. diff --git a/lib/nova b/lib/nova index 374979089d..89dc3f77ce 100644 --- a/lib/nova +++ b/lib/nova @@ -428,8 +428,7 @@ function create_nova_conf() { if is_service_enabled ceilometer; then iniset $NOVA_CONF DEFAULT instance_usage_audit "True" iniset $NOVA_CONF DEFAULT instance_usage_audit_period "hour" - iniset $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier" - iniset $NOVA_CONF DEFAULT notification_driver "ceilometer.compute.nova_notifier" + iniset_multiline $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier" "ceilometer.compute.nova_notifier" fi diff --git a/tests/functions.sh b/tests/functions.sh index 4fe644367d..27a6cfeec4 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -60,6 +60,10 @@ spaces = yes [ddd] empty = + +[eee] +multi = foo1 +multi = foo2 EOF # Test with spaces @@ -193,6 +197,34 @@ else echo "inicomment failed: $VAL" fi +# Test multiple line iniset/iniget +iniset_multiline test.ini eee multi bar1 bar2 + +VAL=$(iniget_multiline test.ini eee multi) +if [[ "$VAL" == "bar1 bar2" ]]; then + echo "OK: iniset_multiline" +else + echo "iniset_multiline failed: $VAL" +fi + +# Test iniadd with exiting values +iniadd test.ini eee multi bar3 +VAL=$(iniget_multiline test.ini eee multi) +if [[ "$VAL" == "bar1 bar2 bar3" ]]; then + echo "OK: iniadd" +else + echo "iniadd failed: $VAL" +fi + +# Test iniadd with non-exiting values +iniadd test.ini eee non-multi foobar1 foobar2 +VAL=$(iniget_multiline test.ini eee non-multi) +if [[ "$VAL" == "foobar1 foobar2" ]]; then + echo "OK: iniadd with non-exiting value" +else + echo "iniadd with non-exsting failed: $VAL" +fi + rm test.ini # Enabling/disabling services