Browse code

Support MultiStrOpt options in configuration file.

Fixed bug #1136964.

1. Added 3 ini functions to support MultiStrOpt:
Function "iniset_multiline config-file section option value1 value2
value3 ..." sets a MultiStrOpt option in an ini file.

Function "iniget_multiline config-file section option" gets the
MultiStrOpt option values.

Function "iniadd config-file section option value1 value2 value3..."
appends an option without relacing the old values, which would result
the option to be MultiStrOpt.

2. Modified the nova configuation to correctly enable notification for
ceilometer.

Change-Id: I1c27db1a6e58b35bc4428e761f40627988f69e37

Lianhao Lu authored on 2013/03/01 16:54:02
Showing 3 changed files
... ...
@@ -553,6 +553,56 @@ $option = $value
553 553
     fi
554 554
 }
555 555
 
556
+# Get a multiple line option from an INI file
557
+# iniget_multiline config-file section option
558
+function iniget_multiline() {
559
+    local file=$1
560
+    local section=$2
561
+    local option=$3
562
+    local values
563
+    values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
564
+    echo ${values}
565
+}
566
+
567
+# Set a multiple line option in an INI file
568
+# iniset_multiline config-file section option value1 value2 valu3 ...
569
+function iniset_multiline() {
570
+    local file=$1
571
+    local section=$2
572
+    local option=$3
573
+    shift 3
574
+    local values
575
+    for v in $@; do
576
+        # The later sed command inserts each new value in the line next to
577
+        # the section identifier, which causes the values to be inserted in
578
+        # the reverse order. Do a reverse here to keep the original order.
579
+        values="$v ${values}"
580
+    done
581
+    if ! grep -q "^\[$section\]" "$file"; then
582
+        # Add section at the end
583
+        echo -e "\n[$section]" >>"$file"
584
+    else
585
+        # Remove old values
586
+        sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
587
+    fi
588
+    # Add new ones
589
+    for v in $values; do
590
+        sed -i -e "/^\[$section\]/ a\\
591
+$option = $v
592
+" "$file"
593
+    done
594
+}
595
+
596
+# Append a new option in an ini file without replacing the old value
597
+# iniadd config-file section option value1 value2 value3 ...
598
+function iniadd() {
599
+    local file=$1
600
+    local section=$2
601
+    local option=$3
602
+    shift 3
603
+    local values="$(iniget_multiline $file $section $option) $@"
604
+    iniset_multiline $file $section $option $values
605
+}
556 606
 
557 607
 # is_service_enabled() checks if the service(s) specified as arguments are
558 608
 # enabled by the user in ``ENABLED_SERVICES``.
... ...
@@ -428,8 +428,7 @@ function create_nova_conf() {
428 428
     if is_service_enabled ceilometer; then
429 429
         iniset $NOVA_CONF DEFAULT instance_usage_audit "True"
430 430
         iniset $NOVA_CONF DEFAULT instance_usage_audit_period "hour"
431
-        iniset $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier"
432
-        iniset $NOVA_CONF DEFAULT notification_driver "ceilometer.compute.nova_notifier"
431
+        iniset_multiline $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier" "ceilometer.compute.nova_notifier"
433 432
     fi
434 433
 
435 434
 
... ...
@@ -60,6 +60,10 @@ spaces  =  yes
60 60
 
61 61
 [ddd]
62 62
 empty =
63
+
64
+[eee]
65
+multi = foo1
66
+multi = foo2
63 67
 EOF
64 68
 
65 69
 # Test with spaces
... ...
@@ -193,6 +197,34 @@ else
193 193
     echo "inicomment failed: $VAL"
194 194
 fi
195 195
 
196
+# Test multiple line iniset/iniget
197
+iniset_multiline test.ini eee multi bar1 bar2
198
+
199
+VAL=$(iniget_multiline test.ini eee multi)
200
+if [[ "$VAL" == "bar1 bar2" ]]; then
201
+    echo "OK: iniset_multiline"
202
+else
203
+    echo "iniset_multiline failed: $VAL"
204
+fi
205
+
206
+# Test iniadd with exiting values
207
+iniadd test.ini eee multi bar3
208
+VAL=$(iniget_multiline test.ini eee multi)
209
+if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
210
+    echo "OK: iniadd"
211
+else
212
+    echo "iniadd failed: $VAL"
213
+fi
214
+
215
+# Test iniadd with non-exiting values
216
+iniadd test.ini eee non-multi foobar1 foobar2
217
+VAL=$(iniget_multiline test.ini eee non-multi)
218
+if [[ "$VAL" == "foobar1 foobar2" ]]; then
219
+    echo "OK: iniadd with non-exiting value"
220
+else
221
+    echo "iniadd with non-exsting failed: $VAL"
222
+fi
223
+
196 224
 rm test.ini
197 225
 
198 226
 # Enabling/disabling services