-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmn_config_update
executable file
·309 lines (277 loc) · 10.1 KB
/
mn_config_update
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
. /medianet/lib/mn_includes
needroot
MODE_BOOTSTRAP=
# if we're bootstrapping, don't start services just yet, to avoid flooding
# the log with errors
if [[ $("$BASENAME" $0) == mn_config_bootstrap ]] ; then
MODE_BOOTSTRAP=1
fi
if [[ -n "$1" ]] ; then
CONFIG_FILE="$1"
else
CONFIG_FILE="$SYSTEM_CONFIG"
fi
if [[ ! -e "$CONFIG_FILE" ]] ; then
bail "$CONFIG_FILE does not exist."
fi
NOEDIT_PREFIX="# automatically created by "
NOEDIT_DYNAMIC="$PROGNAME from $CONFIG_FILE, "
NOEDIT_SUFFIX="do not edit!"
NOEDIT="${NOEDIT_PREFIX}${NOEDIT_DYNAMIC}${NOEDIT_SUFFIX}"
NONE="[none]\n"
# globals
MATCH="product description hostname location version"
# make nice headline:
FMATCH=
for i in $MATCH ; do FMATCH+="$i/" ; done
# shave off last character:
FMATCH=${FMATCH%?}
scream "Update $FMATCH:"
# make jq query
FMATCH=
for i in $MATCH ; do FMATCH+=".$i," ; done
FMATCH=${FMATCH%?}
DATA=`cat "$CONFIG_FILE" | "$JQ" -r "$FMATCH"`
# make shell variable suffix:
FMATCH=
for i in $MATCH ; do FMATCH+="`echo $i | "$TR" [a-z] [A-Z]` " ; done
CONFIG=
for i in $FMATCH; do
VAR="CONFIG_$i"
read VALUE
if [[ "$VALUE" = "null" ]] ; then
CONFIG+="$VAR=\"\"\n"
failure "$VAR undefined."
else
CONFIG+="$VAR=\"$VALUE\"\n"
fi
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n$CONFIG"
echo -n "Writing $CONFIG_INCLUDE..."
echo -e "$CONFIG" > "$CONFIG_INCLUDE" && success || failure
. $CONFIG_INCLUDE
# hostname
if [[ $CONFIG_HOSTNAME =~ [a-zA-Z0-9][a-zA-Z0-9-]{0,62} ]] ; then
echo -n "Updating /etc/hostname to $CONFIG_HOSTNAME..."
if [[ `cat /etc/hostname 2> /dev/null | "$TR" -d '[:space:]'` != "$CONFIG_HOSTNAME" ]] ; then
echo "$CONFIG_HOSTNAME" > /etc/hostname && success || failure
else
echo " not changed".
fi
echo -n "Updating /etc/hosts to $CONFIG_HOSTNAME..."
if [[ -n `grep $HOSTNAME /etc/hosts` ]] ; then
"$SED" -i "s/$HOSTNAME/$CONFIG_HOSTNAME/g" /etc/hosts && success || failure
else
echo -e "127.0.0.1\t$CONFIG_HOSTNAME" >> /etc/hosts && success || failure
fi
echo -n "Setting hostname to $CONFIG_HOSTNAME..."
"$HOSTNAMEBIN" "$CONFIG_HOSTNAME" && success || failure
else
echo -n "CONFIG_HOSTNAME is not a valid hostname..." ; failure
fi
# boot config
scream "Update boot configuration:"
CONFIG=
# we assume anything that is not a dtparam, dtoverlay or gpio can occur only once
# this becomes important if we later combine default settings with overrides
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.bootConfig | del(.dtoverlay) | del(.dtparam) | del(.gpio) | keys_unsorted[] as $k | "\($k)\n\(.[$k])"'`
while read KEY ; do
read VALUE
if [[ -z "$KEY" || -z "$VALUE" ]] ; then continue; fi
CONFIG+="$KEY=$VALUE\n"
done <<< "$DATA"
# the following boot parameters can occur multiple times. we preserve the original order and rely
# on the config.txt parser to do the right thing (latest takes precedence)
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.bootConfig.gpio[]?'`
while read VALUE ; do
[[ $VALUE ]] && CONFIG+="gpio=$VALUE\n"
done <<< "$DATA"
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.bootConfig.dtparam[]?'`
while read VALUE ; do
[[ $VALUE ]] && CONFIG+="dtparam=$VALUE\n"
done <<< "$DATA"
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.bootConfig.dtoverlay[]?'`
while read VALUE ; do
[[ $VALUE ]] && CONFIG+="dtoverlay=$VALUE\n"
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
echo -n "Updating $CONF_FILE_BOOT..."
# We need to replace everything after our $NOEDIT line. Search for the prefix:
SYSCONFIG=$("$SED" "/^${NOEDIT_PREFIX}/Q" < "$CONF_FILE_BOOT")
CONFIG="$SYSCONFIG\n$NOEDIT\n$CONFIG"
echo -en "$CONFIG" > "$CONF_FILE_BOOT" && success || failure
# mounts
CONFIG=
DATA=$( cat "$CONFIG_FILE" | "$JQ" -r '.mounts[]?' )
scream "Write $CONF_FILE_FSTAB and check for custom mounts:"
# always read default fstab
while read LINE ; do
CONFIG+="$LINE\n"
done < "$CONF_FILE_FSTAB_DEFAULT"
if [[ -n "$DATA" ]] ; then
while read LINE ; do
CONFIG+="$LINE\n"
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
fi
CONFIG="$NOEDIT\n$CONFIG"
echo -n "Writing $CONF_FILE_FSTAB..."
echo -en "$CONFIG" > "$CONF_FILE_FSTAB" && success || failure
# update systemd:
"$SYSTEMCTL" daemon-reload
# skip the rest if we're in early bootstrap:
if [[ "$MODE_BOOTSTRAP" -eq 1 ]] ; then
exit 0
fi
# systemd unit status
DATA=$( cat "$CONFIG_FILE" \
| "$JQ" -r '.systemdUnits[]? | select(.enabled == 1) | "\(.unit).\(.type)"' )
scream "Update systemd unit status and firewall rules:"
# compiling the list of units to iterate over is a bit tricky, because not everything is known:
# start with the system's list of medianet units:
UNITS=$SYSTEMD_MEDIANET_UNITS
# then add the currently active medianet template units:
UNITS+="$( "$SYSTEMCTL" list-unit-files --state enabled --no-legend --no-pager mn_*@* | "$SED" -e 's/^[[:space:]]*//' | "$CUT" -d ' ' -f 1 )
"
# finally, add all template units enabled in the config file:
UNITS+=$( "$GREP" '@' <<< "$DATA" )
# now get rid of duplicates and extra whitespace:
UNITS=$( echo "$UNITS" | "$SED" 's/^[[:space:]]//; s/[[space:]]$//' | "$SORT" | "$UNIQ" | $GREP -v '^$' )
for UNIT in $UNITS; do
# match up to the first dot or @ sign,
# to include template units:
UFWAPP=$( echo $UNIT | "$SED" 's/\([^@.]*\).*\..*/\1/' )
# is the unit in the list of enabled units from the config file?
"$GREP" --quiet "^${UNIT}$" <<< "$DATA" && {
# service should be enabled
echo -n "Enabling $UNIT..."
"$SYSTEMCTL" --quiet is-enabled "$UNIT" 2> /dev/null && {
echo " already enabled."
} || {
"$SYSTEMCTL" --quiet enable "$UNIT" && success || failure
}
if [[ -e "${CONF_DIR_UFW_MNAPPS}/${UFWAPP}" ]] ; then
echo -n "Found matching firewall ruleset for '$UFWAPP'. Enabling..."
"$UFW" allow "$UFWAPP" && success || failure
fi
} || {
# service should be disabled
echo -n "Disabling $UNIT..."
"$SYSTEMCTL" --quiet is-enabled "$UNIT" 2> /dev/null && {
"$SYSTEMCTL" --quiet disable "$UNIT" && success || failure
} || echo " already disabled."
if [[ -e "${CONF_DIR_UFW_MNAPPS}/${UFWAPP}" ]] ; then
"$GREP" --quiet "$UFWAPP" <<< "$DATA" && {
echo "Other template units are still using the firewall ruleset $UFWAPP. Not changing."
} || {
echo -n "No other templates using firewall ruleset $UFWAPP. Disabling..."
"$UFW" delete allow "$UFWAPP" && success || failure
}
fi
}
done
# systemd unit options
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.systemdUnits[]? | select(.enabled == 1) | select(.jackName or .execStartPre or .options) | "\(.unit)\n\(.type)\n\(.jackName)\n\(.execStartPre)\n\(.options)"'`
if [[ -n "$DATA" ]] ; then
scream "Update systemd unit options:"
while read UNIT ; do
CONFIG=
read TYPE
if [[ -z "$UNIT" || -z "$TYPE" ]] ; then continue ; fi
DROPIN_DIR="$SYSTEMD_UNIT_DIR"/"$UNIT"."$TYPE".d
DROPIN_FILE="$UNIT"."$TYPE".conf
if [[ ! -e "$DROPIN_DIR" ]] ; then
echo -n "Creating $DROPIN_DIR..."
mkdir "$DROPIN_DIR" && success || failure
fi
read JACKNAME
if [[ "$JACKNAME" != "null" ]] ; then
CONFIG+="Environment=\"JACKNAME=$JACKNAME\"\n"
fi
read EXECSTARTPRE
if [[ "$EXECSTARTPRE" != "null" ]] ; then
CONFIG+="ExecStartPre=$EXECSTARTPRE\n"
fi
read OPTIONS
if [[ "$OPTIONS" != "null" ]] ; then
CONFIG+="Environment=\"OPTIONS="
# deprecated hack to substitute %jackName% in the options string
# now hardcoded into service file for all services that support it
CONFIG+=`echo "$OPTIONS" | sed "s/%jackName%/\'$JACKNAME\'/g"`
CONFIG+="\"\n"
fi
echo "$UNIT options:"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n\n\n[Service]\n\n$CONFIG"
echo -n "Writing to $DROPIN_DIR/$DROPIN_FILE ..."
echo -e "$CONFIG" > "$DROPIN_DIR"/"$DROPIN_FILE" && success || failure
done <<< "$DATA"
fi
# systemd jack unit connections
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.systemdUnits[]? | select(.outPorts or .inPorts) | "\(.unit)\n\(.type)"'`
if [[ -n "$DATA" ]] ; then
scream "Update systemd jack unit connections:"
while read UNIT ; do
CONFIG=
read TYPE
if [[ -z "$UNIT" || -z "$TYPE" ]] ; then continue ; fi
DROPIN_DIR="$SYSTEMD_UNIT_DIR"/"$UNIT"."$TYPE".d
DROPIN_FILE="$UNIT"."$TYPE".connections
if [[ ! -e "$DROPIN_DIR" ]] ; then
echo -n "Creating $DROPIN_DIR..."
mkdir "$DROPIN_DIR" && success || failure
fi
COUT=`cat "$CONFIG_FILE" | "$JQ" --arg unit "$UNIT" -rjf "$PREFIX"/overlay/usr/local/lib/mn_config/output_connections.jq`
if [[ "$COUT" ]] ; then
CONFIG="CONNECTIONS_OUT=\"$COUT\n\"\n"
fi
CIN=`cat "$CONFIG_FILE" | "$JQ" --arg unit "$UNIT" -rjf "$PREFIX"/overlay/usr/local/lib/mn_config/input_connections.jq`
if [[ "$CIN" ]] ; then
CONFIG+="CONNECTIONS_IN=\"$CIN\n\"\n"
fi
echo "$UNIT connections:"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n\n$CONFIG"
echo -n "Writing to $DROPIN_DIR/$DROPIN_FILE ..."
echo -e "$CONFIG" > "$DROPIN_DIR"/"$DROPIN_FILE" && success || failure
done <<< "$DATA"
fi
scream "Update systemd daemon settings:"
echo -n "Performing 'systemctl daemon-reload'..."
$SYSTEMCTL daemon-reload && success || failure
# write config files
# jconvolver
scream "Update jconvolver configuration:"
CONFIG=
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.systemdUnits[]? | select(.unit == "mn_jconvolver" and .enabled == 1) | .config[]?'`
while read COMMAND ; do
CONFIG+="$COMMAND\n"
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n\n$CONFIG"
echo -n "Writing to $CONF_FILE_JCONVOLVER..."
echo -e "$CONFIG" > "$CONF_FILE_JCONVOLVER" && success || failure
# mod-host
scream "Update mod-host configuration:"
CONFIG=
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.systemdUnits[]? | select(.unit == "mn_mod-host" and .enabled == 1) | .config[]?'`
while read COMMAND ; do
CONFIG+="$COMMAND\n"
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n\n$CONFIG"
echo -n "Writing to $CONF_FILE_MODHOST..."
echo -e "$CONFIG" > "$CONF_FILE_MODHOST" && success || failure
# shairport-sync
scream "Update shairport-sync configuration:"
CONFIG=
DATA=`cat "$CONFIG_FILE" | "$JQ" -r '.systemdUnits[]? | select(.unit == "mn_shairport-sync" and .enabled == 1) | .config[]?'`
while read COMMAND ; do
CONFIG+="$COMMAND\n"
done <<< "$DATA"
echo -en "${CONFIG:-${NONE}}" | indent
CONFIG="$NOEDIT\n\n$CONFIG"
echo -n "Writing to $CONF_FILE_SHAIRPORT_SYNC..."
echo -e "$CONFIG" > "$CONF_FILE_SHAIRPORT_SYNC" && success || failure