Skip to content

Commit bb35715

Browse files
sdaguefkakuma
andcommitted
add local.conf modifying functions
This adds a set of local.conf modifying functions which make it easier for consuming projects like devstack-gate to programatically add elements to local.conf structured files. Change-Id: I3427968c2bd43aba12b3619acc27f73c74f0dabb Co-Authored-By: fumihiko kakuma <[email protected]>
1 parent 01cbe7c commit bb35715

File tree

2 files changed

+639
-0
lines changed

2 files changed

+639
-0
lines changed

inc/ini-config

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,170 @@ function iniget_sections {
274274
$xtrace
275275
}
276276

277+
# Set a localrc var
278+
function localrc_set {
279+
local file=$1
280+
local group="local"
281+
local conf="localrc"
282+
local section=""
283+
local option=$2
284+
local value=$3
285+
localconf_set "$file" "$group" "$conf" "$section" "$option" "$value"
286+
}
287+
288+
# Check if local.conf has section.
289+
function localconf_has_section {
290+
local file=$1
291+
local group=$2
292+
local conf=$3
293+
local section=$4
294+
local sep
295+
sep=$(echo -ne "\x01")
296+
local line
297+
line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
298+
/\[${section}\]/p
299+
}" "$file")
300+
[ -n "$line" ]
301+
}
302+
303+
# Check if local.conf has option.
304+
function localconf_has_option {
305+
local file=$1
306+
local group=$2
307+
local conf=$3
308+
local section=$4
309+
local option=$5
310+
local sep
311+
sep=$(echo -ne "\x01")
312+
local line
313+
if [[ -z "$section" ]]; then
314+
line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
315+
/${option}[ \t]*=.*$/p
316+
}" "$file")
317+
else
318+
line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
319+
/\[${section}\]/,/\[\[.*\]\]\|\[.*\]/{
320+
/${option}[ \t]*=.*$/p}
321+
}" "$file")
322+
fi
323+
[ -n "$line" ]
324+
}
325+
326+
# Update option in local.conf.
327+
function localconf_update_option {
328+
local sudo=$1
329+
local file=$2
330+
local group=$3
331+
local conf=$4
332+
local section=$5
333+
local option=$6
334+
local value=$7
335+
local sep
336+
sep=$(echo -ne "\x01")
337+
if [[ -z "$section" ]]; then
338+
$sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
339+
s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
340+
}" "$file"
341+
else
342+
$sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
343+
/\[${section}\]/,/\[\[.*\]\]\|\[.*\]/s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
344+
}" "$file"
345+
fi
346+
}
347+
348+
# Add option in local.conf.
349+
function localconf_add_option {
350+
local sudo=$1
351+
local file=$2
352+
local group=$3
353+
local conf=$4
354+
local section=$5
355+
local option=$6
356+
local value=$7
357+
local sep
358+
sep=$(echo -ne "\x01")
359+
if [[ -z "$section" ]]; then
360+
$sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} a $option=$value" "$file"
361+
else
362+
$sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
363+
/\[${section}\]/ a $option=$value
364+
}" "$file"
365+
fi
366+
}
367+
368+
# Add section and option in local.conf.
369+
function localconf_add_section_and_option {
370+
local sudo=$1
371+
local file=$2
372+
local group=$3
373+
local conf=$4
374+
local section=$5
375+
local option=$6
376+
local value=$7
377+
local sep
378+
sep=$(echo -ne "\x01")
379+
$sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} {
380+
a [$section]
381+
a $option=$value
382+
}" "$file"
383+
}
384+
385+
# Set an option in a local.conf file.
386+
# localconf_set [-sudo] config-file group conf-name section option value
387+
# - if the file does not exist, it is created
388+
function localconf_set {
389+
local xtrace
390+
xtrace=$(set +o | grep xtrace)
391+
set +o xtrace
392+
local sep
393+
sep=$(echo -ne "\x01")
394+
local sudo=""
395+
if [ $1 == "-sudo" ]; then
396+
sudo="sudo "
397+
shift
398+
fi
399+
local file=$1
400+
local group=$2
401+
local conf=$3
402+
local section=$4
403+
local option=$5
404+
local value=$6
405+
406+
if [[ -z $group || -z $conf || -z $option || -z $value ]]; then
407+
$xtrace
408+
return
409+
fi
410+
411+
if ! grep -q "^\[\[${group}|${conf}\]\]" "$file" 2>/dev/null; then
412+
# Add meta section at the end if it does not exist
413+
echo -e "\n[[${group}|${conf}]]" | $sudo tee --append "$file" > /dev/null
414+
# Add section at the end
415+
if [[ -n "$section" ]]; then
416+
echo -e "[$section]" | $sudo tee --append "$file" > /dev/null
417+
fi
418+
# Add option at the end
419+
echo -e "$option=$value" | $sudo tee --append "$file" > /dev/null
420+
elif [[ -z "$section" ]]; then
421+
if ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
422+
# Add option
423+
localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
424+
else
425+
# Replace it
426+
localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
427+
fi
428+
elif ! localconf_has_section "$file" "$group" "$conf" "$section"; then
429+
# Add section and option in specified meta section
430+
localconf_add_section_and_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
431+
elif ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
432+
# Add option
433+
localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
434+
else
435+
# Replace it
436+
localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
437+
fi
438+
$xtrace
439+
}
440+
277441
# Restore xtrace
278442
$INC_CONF_TRACE
279443

0 commit comments

Comments
 (0)