Browse code

Merge "Support MultiStrOpt options in configuration file."

Jenkins authored on 2013/03/15 07:02:59
Showing 3 changed files
... ...
@@ -555,6 +555,56 @@ $option = $value
555 555
     fi
556 556
 }
557 557
 
558
+# Get a multiple line option from an INI file
559
+# iniget_multiline config-file section option
560
+function iniget_multiline() {
561
+    local file=$1
562
+    local section=$2
563
+    local option=$3
564
+    local values
565
+    values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
566
+    echo ${values}
567
+}
568
+
569
+# Set a multiple line option in an INI file
570
+# iniset_multiline config-file section option value1 value2 valu3 ...
571
+function iniset_multiline() {
572
+    local file=$1
573
+    local section=$2
574
+    local option=$3
575
+    shift 3
576
+    local values
577
+    for v in $@; do
578
+        # The later sed command inserts each new value in the line next to
579
+        # the section identifier, which causes the values to be inserted in
580
+        # the reverse order. Do a reverse here to keep the original order.
581
+        values="$v ${values}"
582
+    done
583
+    if ! grep -q "^\[$section\]" "$file"; then
584
+        # Add section at the end
585
+        echo -e "\n[$section]" >>"$file"
586
+    else
587
+        # Remove old values
588
+        sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
589
+    fi
590
+    # Add new ones
591
+    for v in $values; do
592
+        sed -i -e "/^\[$section\]/ a\\
593
+$option = $v
594
+" "$file"
595
+    done
596
+}
597
+
598
+# Append a new option in an ini file without replacing the old value
599
+# iniadd config-file section option value1 value2 value3 ...
600
+function iniadd() {
601
+    local file=$1
602
+    local section=$2
603
+    local option=$3
604
+    shift 3
605
+    local values="$(iniget_multiline $file $section $option) $@"
606
+    iniset_multiline $file $section $option $values
607
+}
558 608
 
559 609
 # is_service_enabled() checks if the service(s) specified as arguments are
560 610
 # enabled by the user in ``ENABLED_SERVICES``.
... ...
@@ -425,8 +425,7 @@ function create_nova_conf() {
425 425
     if is_service_enabled ceilometer; then
426 426
         iniset $NOVA_CONF DEFAULT instance_usage_audit "True"
427 427
         iniset $NOVA_CONF DEFAULT instance_usage_audit_period "hour"
428
-        iniset $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier"
429
-        iniset $NOVA_CONF DEFAULT notification_driver "ceilometer.compute.nova_notifier"
428
+        iniset_multiline $NOVA_CONF DEFAULT notification_driver "nova.openstack.common.notifier.rpc_notifier" "ceilometer.compute.nova_notifier"
430 429
     fi
431 430
 
432 431
 
... ...
@@ -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