blob: 703dc4d1758477adb4f84e78c654949ad7b02cb4 (
plain)
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
|
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2023 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Read a particular value from a key/value configuration file. Using this
# function introduces a dependency on awk.
config() {
config_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
}
config_subkeys() {
config_subkeys_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
}
config_for() {
local host=$1 ; shift
local key=$1 ; shift
# Use a variable definition test to define default, in order to ensure
# it is _not_ defined if no argument for it was passed, but _is_
# defined even if an empty string was passed.
test -v 1 && { local default=$1 ; shift ; }
local default
local file
local files
files=(
"$BASHTARD_ETCDIR/secrets"
"$BASHTARD_ETCDIR/hosts.d/$host"
"$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}"
"$BASHTARD_ETCDIR/defaults"
)
if [[ -n "$BASHTARD_PLAYBOOK" ]]
then
debug "bashtard/config_for" "BASHTARD_PLAYBOOK=$BASHTARD_PLAYBOOK, adding etc entries"
files+=(
"$(playbook_path "base")/etc/os.d/${BASHTARD_PLATFORM[key]}"
"$(playbook_path "base")/etc/defaults"
)
fi
files+=(
"$BASHTARD_SHAREDIR/os.d/${BASHTARD_PLATFORM[key]}"
"$BASHTARD_SHAREDIR/defaults"
)
# Check configuration files
for file in "${files[@]}"
do
debug "bashtard/config_for" "Checking for '$key' in '$file'"
[[ ! -f $file ]] && continue
# Check if the lookup is a reference variable, defined by using
# &= instead of just a single = as seperator. If this exists,
# do a new config_for lookup, this time using the value as the
# key for the new lookup.
value="$(awk -F= '$1 == "'"$key"'&" { print $0 }' "$file" | cut -d'=' -f 2-)"
if [[ -n $value ]]
then
debug "bashtard/config_for" "Found reference for $key to $value in $file"
printf "%s" "$(config_for "$host" "$value")"
return
fi
# Use awk to find the right line, then use cut to get the
# actual value. Cutting it out with awk _is_ possible, but
# comes with whitespace issues or having to deal with values
# containing the seperator (=), so using cut is much simpler
# and easier to understand.
value="$(awk -F= '$1 == "'"$key"'" { print $0 }' "$file" | cut -d'=' -f 2-)"
if [[ -n $value ]]
then
debug "bashtard/config_for" "Found $key=$value in $file"
printf "%s" "$value"
return
fi
done
# Return default value, if one has been defined
if test -v default
then
printf "%s" "$default"
return
fi
# Error
alert "bashtard/config_for" "No configuration value for $key"
}
config_subkeys_for() {
local host=$1 ; shift
local key=$1 ; shift
local file
local files
local results
files=(
"$BASHTARD_ETCDIR/secrets"
"$BASHTARD_ETCDIR/hosts.d/$host"
"$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}"
"$BASHTARD_ETCDIR/defaults"
)
if [[ -n "$BASHTARD_PLAYBOOK" ]]
then
debug "bashtard/config_for" "BASHTARD_PLAYBOOK=$BASHTARD_PLAYBOOK, adding etc entries"
files+=(
"$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/os.d/${BASHTARD_PLATFORM[key]}"
"$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/defaults"
)
fi
files+=(
"$BASHTARD_SHAREDIR/os.d/${BASHTARD_PLATFORM[key]}"
"$BASHTARD_SHAREDIR/defaults"
)
# Check configuration files
for file in "${files[@]}"
do
debug "bashtard/config_subkeys" "Checking for subkeys of '$key' in '$file'"
[[ ! -f $file ]] && continue
while read -r result
do
local subkey
subkey="$(awk -F. '{ print $1 }' <<< "${result#"$key."}")"
debug "bashtard/config_subkeys" "Found '$subkey' as subkey of '$key' through '$result'"
results+=("$subkey")
done < <(grep "^$key\." "$file" | awk -F= '{ print $1 }')
done
# Return unique results
for result in "${results[@]}"
do
printf "%s\n" "$result"
done | sort -u
}
|