-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmeta-config
223 lines (197 loc) · 7.54 KB
/
meta-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
#
# **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``
# Meta-config files contain multiple INI-style configuration files
# using a specific new section header to delimit them:
#
# [[group-name|file-name]]
#
# group-name refers to the group of configuration file changes to be processed
# at a particular time. These are called phases in ``stack.sh`` but
# group here as these functions are not DevStack-specific.
#
# file-name is the destination of the config file
# Save trace setting
_XTRACE_INC_META=$(set +o | grep xtrace)
set +o xtrace
# Allow the awk command to be overridden on legacy platforms
CONFIG_AWK_CMD=${CONFIG_AWK_CMD:-awk}
# Get the section for the specific group and config file
# get_meta_section infile group configfile
function get_meta_section {
local file=$1
local matchgroup=$2
local configfile=$3
[[ -r $file ]] || return 0
[[ -z $configfile ]] && return 0
$CONFIG_AWK_CMD -v matchgroup=$matchgroup -v configfile=$configfile '
BEGIN { group = "" }
/^\[\[.+\|.*\]\]/ {
gsub("[][]", "", $1);
split($1, a, "|");
if (a[1] == matchgroup && a[2] == configfile) {
group=a[1]
} else {
group=""
}
next
}
{
if (group != "")
print $0
}
' $file
}
# Get a list of config files for a specific group
# get_meta_section_files infile group
function get_meta_section_files {
local file=$1
local matchgroup=$2
[[ -r $file ]] || return 0
$CONFIG_AWK_CMD -v matchgroup=$matchgroup '
/^\[\[.+\|.*\]\]/ {
gsub("[][]", "", $1);
split($1, a, "|");
if (a[1] == matchgroup)
print a[2]
}
' $file
}
# Merge the contents of a meta-config file into its destination config file
# If configfile does not exist it will be created.
# merge_config_file infile group configfile
function merge_config_file {
local file=$1
local matchgroup=$2
local configfile=$3
# note, configfile might be a variable (note the iniset, etc
# created in the mega-awk below is "eval"ed too, so we just leave
# it alone.
local real_configfile
real_configfile=$(eval echo $configfile)
if [ ! -f $real_configfile ]; then
mkdir -p $(dirname $real_configfile) || die $LINENO "could not create the directory of $real_configfile ($configfile)"
touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
fi
get_meta_section $file $matchgroup $configfile | \
$CONFIG_AWK_CMD -v configfile=$configfile '
BEGIN {
section = ""
last_section = ""
section_count = 0
}
/^\[.+\]/ {
gsub("[][]", "", $1);
section=$1
next
}
/^ *\#/ {
next
}
/^[^ \t]+/ {
# get offset of first '=' in $0
eq_idx = index($0, "=")
# extract attr & value from $0
attr = substr($0, 1, eq_idx - 1)
value = substr($0, eq_idx + 1)
# only need to strip trailing whitespace from attr
sub(/[ \t]*$/, "", attr)
# need to strip leading & trailing whitespace from value
sub(/^[ \t]*/, "", value)
sub(/[ \t]*$/, "", value)
# cfg_attr_count: number of config lines per [section, attr]
# cfg_attr: three dimensional array to keep all the config lines per [section, attr]
# cfg_section: keep the section names in the same order as they appear in local.conf
# cfg_sec_attr_name: keep the attr names in the same order as they appear in local.conf
if (! (section, attr) in cfg_attr_count) {
if (section != last_section) {
cfg_section[section_count++] = section
last_section = section
}
attr_count = cfg_sec_attr_count[section_count - 1]++
cfg_sec_attr_name[section_count - 1, attr_count] = attr
cfg_attr[section, attr, 0] = value
cfg_attr_count[section, attr] = 1
} else {
lno = cfg_attr_count[section, attr]++
cfg_attr[section, attr, lno] = value
}
}
END {
# Process each section in order
for (sno = 0; sno < section_count; sno++) {
section = cfg_section[sno]
# The ini routines simply append a config item immediately
# after the section header. To keep the same order as defined
# in local.conf, invoke the ini routines in the reverse order
for (attr_no = cfg_sec_attr_count[sno] - 1; attr_no >=0; attr_no--) {
attr = cfg_sec_attr_name[sno, attr_no]
if (cfg_attr_count[section, attr] == 1)
print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, 0] "\""
else {
# For multiline, invoke the ini routines in the reverse order
count = cfg_attr_count[section, attr]
print "inidelete " configfile " " section " " attr
print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, count - 1] "\""
for (l = count -2; l >= 0; l--)
print "iniadd_literal " configfile " " section " " attr " \"" cfg_attr[section, attr, l] "\""
}
}
}
}
' | while read a; do eval "$a"; done
}
# Merge all of the files specified by group
# merge_config_group infile group [group ...]
function merge_config_group {
local localfile=$1; shift
local matchgroups=$@
[[ -r $localfile ]] || return 0
local configfile group
for group in $matchgroups; do
for configfile in $(get_meta_section_files $localfile $group); do
local realconfigfile
local dir
realconfigfile=$(eval "echo $configfile")
if [[ -z $realconfigfile ]]; then
warn $LINENO "unknown config file specification: $configfile is undefined"
break
fi
dir=$(dirname $realconfigfile)
if [[ -d $dir ]]; then
merge_config_file $localfile $group $configfile
else
die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)"
fi
done
done
}
function extract_localrc_section {
local configfile=$1 # top_dir/local.conf
local localrcfile=$2 # top_dir/localrc
local localautofile=$3 # top_dir/.localrc.auto
if [[ -r $configfile ]]; then
LRC=$(get_meta_section_files $configfile local)
for lfile in $LRC; do
if [[ "$lfile" == "localrc" ]]; then
if [[ -r $localrcfile ]]; then
echo "localrc and local.conf:[[local]] both exist, using localrc"
else
echo "# Generated file, do not edit" >$localautofile
get_meta_section $configfile local $lfile >>$localautofile
fi
fi
done
fi
}
# Restore xtrace
$_XTRACE_INC_META
# Local variables:
# mode: shell-script
# End: