forked from storpool/sp-variant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp_variant.sh.j2
More file actions
450 lines (381 loc) · 7.86 KB
/
sp_variant.sh.j2
File metadata and controls
450 lines (381 loc) · 7.86 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/sh
#
# SPDX-FileCopyrightText: 2021 - 2024 StorPool <support@storpool.com>
# SPDX-License-Identifier: BSD-2-Clause
set -e
usage()
{
cat <<'EOUSAGE'
Usage: sp_variant command run [-N] category.command [arg...]
sp_variant detect
sp_variant features
sp_variant repo add
sp_variant show all
sp_variant show current
sp_variant show VARIANT
EOUSAGE
}
detect_from_os_release()
{
if [ ! -r /etc/os-release ]; then
return
fi
local output
# Maybe we should provide a sample os-release file for testing?
# shellcheck disable=SC1091,SC2153,SC2154
output="$(unset ID VERSION_ID; . /etc/os-release; printf -- '%s::%s\n' "$ID" "$VERSION_ID")"
local os_id="${output%%::*}" version_id="${output#*::}"
if [ -z "$os_id" ] || [ -z "$version_id" ]; then
return
fi
{% for name, var in variants|dictvsort %}
if [ "$os_id" = '{{ var.detect.os_id }}' ] && printf -- '%s\n' "$version_id" | grep -Eqe '{{ var.detect.os_version_regex.pattern|regexunx}}'; then
printf -- '%s\n' '{{ name }}'
return
fi
{% endfor %}
}
cmd_detect()
{
local res
if [ "$#" -ne 0 ]; then
usage 1>&2
exit 1
fi
res="$(detect_from_os_release)"
if [ -n "$res" ]; then
printf -- '%s\n' "$res"
return
fi
{% for name in order %}
if [ -r '{{ variants[name].detect.filename }}' ] && grep -Eqe '{{ variants[name].detect.regex.pattern|regexunx }}' -- '{{ variants[name].detect.filename }}'; then
printf -- '%s\n' '{{ name }}'
return
fi
{% endfor %}
echo "Could not detect the current host's build variant" 1>&2
exit 1
}
{% for name, var in variants_json|dictvsort %}
show_{{ name }}()
{
cat <<'EOVARIANT_JSON'
{{ var }}
EOVARIANT_JSON
}
{% endfor %}
cmd_show_all()
{
if [ "$#" -ne 0 ]; then
usage 1>&2
exit 1
fi
cat <<'EOPROLOGUE'
{
"format": {
"version": {
"major": {{ format_version[0] }},
"minor": {{ format_version[1] }}
}
},
"order": [
{%- for name in order %}
"{{ name }}"{% if loop.revindex > 1 %},{% endif %}
{%- endfor %}
],
"variants": {
EOPROLOGUE
{% for name in variants|vsort %}
printf -- ' "%s": ' '{{ name }}'
show_{{ name }}
{% if loop.revindex > 1 %}echo ','{% endif %}
{%- endfor %}
cat <<'EOEPILOGUE'
},
"version": "{{ version }}"
}
EOEPILOGUE
}
show_variant()
{
local variant="$1"
cat <<'EOPROLOGUE'
{
"format": {
"version": {
"major": {{ format_version[0] }},
"minor": {{ format_version[1] }}
}
},
"variant":
EOPROLOGUE
eval "'show_$variant'"
cat <<'EOEPILOGUE'
,
"version": "{{ version }}"
}
EOEPILOGUE
}
cmd_show_current()
{
if [ "$#" -ne 0 ]; then
usage 1>&2
exit 1
fi
local variant
variant="$(cmd_detect)"
[ -n "$variant" ]
show_variant "$variant"
}
run_command()
{
local name="$1" noop="$2" command="$3"
shift 3
local cmd_cat="${command%%.*}" cmd_item="${command#*.}"
if [ "$command" = "$cmd_cat" ] || [ "$cmd_item" != "${cmd_item%.*}" ]; then
echo "Invalid command specification '$command'" 1>&2
exit 1
fi
case "$name" in
{% for name, var in variants|dictvsort %}
{{ name }})
case "$cmd_cat" in
{% for cat_name in var.commands._fields|sort %}
{{ cat_name }})
case "$cmd_item" in
{% for cmd_name in var.commands|attr(cat_name)|attr("_fields")|sort %}
{{ cmd_name }})
# The commands are quoted exactly as much as necessary.
# shellcheck disable=SC2016
$noop {% for word in var.commands|attr(cat_name)|attr(cmd_name) %}'{{ word }}' {% endfor %} "$@"
;;
{% endfor %}
*)
echo "Invalid command '$cmd_item' in the '$cmd_cat' category" 1>&2
exit 1
;;
esac
;;
{% endfor %}
*)
echo "Invalid command category '$cmd_cat'" 1>&2
exit 1
;;
esac
;;
{% endfor %}
*)
echo "Internal error: invalid variant '$name'" 1>&2
exit 1
;;
esac
}
copy_file()
{
local src="$1" dstdir="$2"
if [ ! -f "$src" ]; then
echo "Internal error: no $src file to copy to $dstdir" 1>&2
exit 1
fi
if [ -z "$dstdir" ]; then
echo "Internal error: no destination specified for $src" 1>&2
exit 1
fi
if [ ! -d "$dstdir" ]; then
echo "Internal error: no $dstdir directory" 1>&2
exit 1
fi
local dst
dst="$dstdir/$(basename -- "$src")"
install -o root -g root -m 644 -- "$src" "$dst"
}
repo_add_extension()
{
local filename="$1" repotype="$2"
local basename="${filename%.*}"
local ext="${filename##*.}"
local suffix='-invalid'
case "$repotype" in
{% for rtype, rdef in repotypes|dictvsort %}
{{ rtype }})
suffix='{{ rdef.extension }}'
;;
{% endfor %}
*)
echo "Invalid repository type '$repotype'" 1>&2
exit 1
;;
esac
printf -- '%s%s.%s\n' "$basename" "$suffix" "$ext"
}
repo_add_yum()
{
local name="$1" vdir="$2" repotype="$3" yumdef="$4" keyring="$5"
yum --disablerepo='storpool-*' install -q -y ca-certificates
local yumbase repofile
yumbase="$(basename -- "$yumdef")"
repofile="$(repo_add_extension "$yumbase" "$repotype")"
[ -n "$repofile" ]
copy_file "$vdir/$repofile" /etc/yum.repos.d
local keybase
keybase="$(basename -- "$keyring")"
copy_file "$vdir/$keybase" /etc/pki/rpm-gpg
if [ -n "$(command -v rpmkeys || true)" ]; then
rpmkeys --import "/etc/pki/rpm-gpg/$(basename -- "$keybase")"
fi
yum --disablerepo='*' --enablerepo="storpool-$repotype" clean metadata
}
repo_add_deb()
{
local name="$1" vdir="$2" repotype="$3" srcdef="$4" keyring="$5" packages="$6"
# $packages is not quoted on purpose: there may be more than one.
# shellcheck disable=SC2086
run_command "$name" '' package.install $packages
local srcbase repofile
srcbase="$(basename -- "$srcdef")"
repofile="$(repo_add_extension "$srcbase" "$repotype")"
[ -n "$repofile" ]
copy_file "$vdir/$repofile" /etc/apt/sources.list.d
local keybase
keybase="$(basename -- "$keyring")"
copy_file "$vdir/$keybase" /usr/share/keyrings
apt-get update
}
cmd_repo_add()
{
local repodir repotype='contrib'
repodir="$(dirname -- "$0")"
local o
while getopts 'd:t:' o; do
case "$o" in
d)
repodir="$OPTARG"
;;
t)
repotype="$OPTARG"
;;
*)
usage 1>&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ "$#" -ne 0 ]; then
usage 1>&2
exit 1
fi
local variant
variant="$(cmd_detect)"
local vdir="$repodir/$variant"
if [ ! -d "$vdir" ]; then
echo "No $vdir directory" 1>&2
exit 1
fi
case "$variant" in
{% for name, var in variants|dictvsort %}
{{ name }})
{% if var.family == "debian" %}
repo_add_deb '{{ name }}' "$vdir" "$repotype" '{{ var.repo.sources }}' '{{ var.repo.keyring }}' '{{ var.repo.req_packages|join(" ") }}'
{% else %}
repo_add_yum '{{ name }}' "$vdir" "$repotype" '{{ var.repo.yumdef }}' '{{ var.repo.keyring }}'
{% endif %}
;;
{% endfor %}
*)
echo "Internal error: '$variant' should be recognized at this point" 1>&2
exit 1
;;
esac
install -o root -g root -m 755 -- "$0" /usr/sbin/sp_variant
}
cmd_command_run()
{
local noop=''
while getopts 'N' o; do
case "$o" in
N)
noop='echo'
;;
*)
usage 1>&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
local variant
variant="$(cmd_detect)"
[ -n "$variant" ]
run_command "$variant" "$noop" "$@"
}
cmd_command()
{
case "$1" in
run)
shift
cmd_command_run "$@"
;;
*)
usage 1>&2
exit 1
;;
esac
}
cmd_features()
{
echo 'Features: format={{ format_version[0] }}.{{ format_version[1] }} version={{ version }}'
}
case "$1" in
command)
shift
cmd_command "$@"
;;
detect)
shift
cmd_detect "$@"
;;
features)
shift
cmd_features "$@"
;;
repo)
shift
case "$1" in
add)
shift
cmd_repo_add "$@"
;;
*)
usage 1>&2
exit 1
;;
esac
;;
show)
shift
case "$1" in
all)
shift
cmd_show_all "$@"
;;
current)
shift
cmd_show_current "$@"
;;
{% for name in variants|vsort %}
{{ name }})
show_variant '{{ name }}'
;;
{% endfor %}
*)
usage 1>&2
exit 1
;;
esac
;;
*)
usage 1>&2
exit 1
;;
esac