-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdev.sh
executable file
·518 lines (441 loc) · 13.7 KB
/
dev.sh
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/bin/bash
set -eo pipefail
HERE=$(cd $(dirname $0); pwd)
: ${NAMESPACE:=substrate-nobody}
CUE_VERSION="0.11.0"
CUE_PREFIX=cue_v${CUE_VERSION}_
CUE_NATIVE_SUFFIX=$(uname -s | tr "[:upper:]" "[:lower:]")_$(uname -m)
# HACK use amd64
case "$CUE_NATIVE_SUFFIX" in
linux_x86_64)
CUE_NATIVE_SUFFIX=linux_amd64
;;
esac
CUE_NATIVE=$HERE/tools/cue/${CUE_PREFIX}${CUE_NATIVE_SUFFIX}
GO_VERSION="1.23.4"
GO_PREFIX=go-v${GO_VERSION}-
GO_NATIVE_SUFFIX=$(uname -s | tr "[:upper:]" "[:lower:]")-$(uname -m)
# HACK use amd64
case "$GO_NATIVE_SUFFIX" in
linux-x86_64)
GO_NATIVE_SUFFIX=linux-amd64
;;
esac
GO_NATIVE=$HERE/tools/go/${GO_PREFIX}${GO_NATIVE_SUFFIX}
BUILDX_VERSION="0.19.1"
BUILDX_PREFIX=buildx-v${BUILDX_VERSION}.
BUILDX_NATIVE_SUFFIX=$(uname -s | tr "[:upper:]" "[:lower:]")_$(uname -m)
# HACK use amd64
case "$BUILDX_NATIVE_SUFFIX" in
linux_x86_64)
BUILDX_NATIVE_SUFFIX=linux-amd64
;;
esac
BUILDX_NATIVE=$HERE/tools/buildx/${BUILDX_PREFIX}${BUILDX_NATIVE_SUFFIX}
TXTAR_VERSION="v0.0.0-20241009180824-f66d83c29e7c"
TXTAR_PREFIX=txtar-${TXTAR_VERSION}_
TXTAR_NATIVE_SUFFIX=$(uname -s | tr "[:upper:]" "[:lower:]")_$(uname -m)
TXTAR_NATIVE=$HERE/tools/txtar/${TXTAR_PREFIX}${TXTAR_NATIVE_SUFFIX}
if [ -z "$PODMAN" ]; then
PODMAN=$(PATH=/opt/podman/bin:$PATH which podman)
fi
if [ -z "$DOCKER" ]; then
DOCKER=$(PATH=~/.docker/bin:$PATH which docker)
fi
# TODO don't hardcode this here.
SUBSTRATEOS_IMAGE=ghcr.io/ajbouh/substrate:substrate-substrateos
SUBSTRATEOS_OVERLAY_IMAGE=ghcr.io/ajbouh/substrate:substrate-substrateos-overlay
ensure_cue() {
# lazily unpack and download a native version of cue
if [ ! -f $CUE_NATIVE ]; then
CUE_ARTIFACT=$HERE/tools/cue/${CUE_PREFIX}${CUE_NATIVE_SUFFIX}.tar.gz
if [ ! -f $CUE_ARTIFACT ]; then
mkdir -p $(dirname $CUE_ARTIFACT)
curl -L https://github.com/cue-lang/cue/releases/download/v$CUE_VERSION/$(basename $CUE_ARTIFACT) > $CUE_ARTIFACT
fi
if [ -f $CUE_ARTIFACT ]; then
tar -xv --to-stdout -f $CUE_ARTIFACT cue > $CUE_NATIVE
chmod +x $CUE_NATIVE
fi
fi
}
CUE_DEV_DIR=./defs
cue() {
ensure_cue
cd >/dev/null $CUE_DEV_DIR
if [ -f $CUE_NATIVE ]; then
$CUE_NATIVE "$@"
fi
cd >/dev/null -
}
ensure_txtar() {
if [ ! -f $TXTAR_NATIVE ]; then
$PODMAN build \
-f $HERE/images/txtar/Dockerfile \
--target=txtar \
--build-arg=TXTAR_VERSION=$TXTAR_VERSION \
--build-arg=TXTAR_BASENAME=$(basename $TXTAR_NATIVE) \
--output type=local,dest=$(dirname $TXTAR_NATIVE) \
$HERE
chmod +x $TXTAR_NATIVE
fi
}
txtar() {
ensure_txtar
$TXTAR_NATIVE "$@"
}
ensure_go() {
# lazily unpack and download a native version of cue
if [ ! -e $GO_NATIVE ]; then
GO_ARTIFACT=$HERE/tools/go/${GO_PREFIX}${GO_NATIVE_SUFFIX}.tar.gz
if [ ! -f $GO_ARTIFACT ]; then
mkdir -p $(dirname $GO_ARTIFACT)
curl -L https://go.dev/dl/go${GO_VERSION}.${GO_NATIVE_SUFFIX}.tar.gz > $GO_ARTIFACT
fi
if [ -f $GO_ARTIFACT ]; then
rm -rf $GO_NATIVE.tmp
mkdir $GO_NATIVE.tmp
tar -C $GO_NATIVE.tmp -xv -f $GO_ARTIFACT --strip-components=1
mv $GO_NATIVE.tmp $GO_NATIVE
fi
fi
}
go() {
ensure_go
$GO_NATIVE/bin/go "$@"
}
go_exec() {
ensure_go
exec $GO_NATIVE/bin/go "$@"
}
ensure_buildx() {
# lazily unpack and download a native version of buildx
if [ ! -f $BUILDX_NATIVE ]; then
mkdir -p $(dirname $BUILDX_NATIVE)
curl -L https://github.com/docker/buildx/releases/download/v$BUILDX_VERSION/$(basename $BUILDX_NATIVE) > $BUILDX_NATIVE
chmod +x $BUILDX_NATIVE
fi
}
buildx() {
ensure_buildx
$BUILDX_NATIVE "$@"
}
sudo_buildx() {
ensure_buildx
sudo env DOCKER_HOST=unix:///run/podman/podman.sock $BUILDX_NATIVE "$@"
}
. $HERE/tools/functions/cue.sh
docker_compose() {
docker_compose_yml=$1
shift
$DOCKER compose \
-p $(basename $docker_compose_yml .yml) \
--project-directory $HERE \
-f $docker_compose_yml "$@"
}
pick_docker_compose_yml() {
suffix=$1
echo $HERE/.gen/docker/$NAMESPACE-$suffix.yml
}
build_images_while_blocking() {
docker_compose_file="$1"
shift
buildx_bake_output_arg=$1
shift
blocking=$1
shift
images="$@"
# regenerate the docker_compose file without these "blocked" tags
write_rendered_cue_dev_expr_as yaml $docker_compose_file \
-t "buildx_bake_docker_compose_block_tags=$blocking" \
-t "buildx_bake_docker_compose_focus=$images" \
-t "resolve_image_ids=false" \
-e 'overlay.buildx_bake_docker_compose'
sudo_buildx bake \
--progress=tty \
-f $docker_compose_file \
--metadata-file=${docker_compose_file}.metadata.json \
"$buildx_bake_output_arg" \
>&2
}
build_imagespecs() {
docker_compose_file="$1"
shift
buildx_bake_output_arg=$1
shift
images="$@"
# `buildx bake`` is a very fast tool for building images, but it needlessly sends the tarball to the docker socket no matter what, even if already present.
# We should be using a more intelligent incremental loading approach, but we don't have that right now.
# In the meantime, we avoid loading resourcedir images whose tags are already present in our image store. This can result in unexpected behavior if
# the contents of a resourcedir image tag change at all from build to build. Ideally the tag name captures all information used to construct the
# resourcedir so this should happen rarely if at all. This edge case will be avoided completely once we use a proper incremental loading
# strategy.
resourcedir_image_tags=$(print_cue_dev_expr_as_text_lines " " 'overlay.resourcedir_image_tags' \
-t "buildx_bake_docker_compose_focus=$images" \
-t "resolve_image_ids=false")
# accumulate ones that are present as "blocked"
for image_tag in $resourcedir_image_tags; do
if ! sudo $PODMAN image exists $image_tag 1>&2; then
# build needed resourcedirs one by one directly via podman
# this avoids starving resourcedir fetches of bandwidth and avoids the wasted copies in the buildx bake approach
sudo podman build $(print_cue_dev_expr_as text \
-t "resolve_image_ids=false" \
-e "overlay.imagespec_by_image_tag[\"$image_tag\"].#podman_build_options" )
fi
done
# build everything else in parallel without resourcedirs
build_images_while_blocking $docker_compose_file $buildx_bake_output_arg "$resourcedir_image_tags" "$images"
}
set_os_vars() {
CUE_DEV_DEFS="defs"
USE_VARSET="substrateos"
BUILD_SOURCE_DIRECTORY="$HERE"
SUBSTRATE_USER=$(id -un)
SUBSTRATE_GROUP=$(id -gn)
SUBSTRATE_HOME="$HOME"
SUBSTRATE_SOURCE="$HERE"
SUBSTRATE_LIVE_EDIT=false
SUBSTRATE_USE_BOOTC_STORAGE=true
}
set_live_edit_vars() {
SUBSTRATE_LIVE_EDIT=true
}
set_docker_vars() {
CUE_DEV_DEFS="defs"
USE_VARSET="docker_compose"
BUILD_SOURCE_DIRECTORY="$HERE"
SUBSTRATE_LIVE_EDIT=false
}
systemd_logs() {
journalctl -xfeu substrate.service
}
write_image_id_files() {
dst=$1
buildx_bake_metadata_file="$2"
shift 2
set +x
print_cue_dev_expr_as text \
-t "buildx_bake_metadata=$(cat $buildx_bake_metadata_file)" \
-t "resolve_image_ids=false" \
-e 'overlay.buildx_bake_image_ids_txtar.#out' | tee /dev/stderr | (cd $dst; txtar -x)
set -x
}
write_ready_file() {
echo 1>&2 "write_ready_file()"
touch $CUE_DEV_DIR/ready
}
build_images() {
docker_compose_file=$1
shift
images=$@
# This is less efficient than we'd like, but it works. We don't want to reload all tarballs on every build.
build_imagespecs $docker_compose_file '--set=*.output=type=docker,oci-mediatypes=true' $images
}
systemd_reload() {
# always include substrateos-overlay, since that's how we (re)generate systemd units
images="$@"
# If we are on a machine that *does not* have bootc storage, then do not reference it in generated quadlets.
if ! [ -e /usr/lib/bootc/storage ]; then
SUBSTRATE_USE_BOOTC_STORAGE=false
fi
docker_compose_file=$(pick_docker_compose_yml substrate)
build_images $docker_compose_file $images
write_image_id_files $PWD "$docker_compose_file.metadata.json" $images
# build overlay after we write image id files
sudo podman build \
--build-arg "NAMESPACE=$NAMESPACE" \
--build-arg "SUBSTRATE_LIVE_EDIT=$SUBSTRATE_LIVE_EDIT" \
--build-arg "CUE_DEV_DEFS=$CUE_DEV_DEFS" \
--build-arg "SUBSTRATE_USER=$SUBSTRATE_USER" \
--build-arg "SUBSTRATE_GROUP=$SUBSTRATE_GROUP" \
--build-arg "SUBSTRATE_HOME=$SUBSTRATE_HOME" \
--build-arg "SUBSTRATE_SOURCE=$SUBSTRATE_SOURCE" \
--build-arg "SUBSTRATE_BUILD_FOCUS=$images" \
--build-arg "SUBSTRATE_USE_BOOTC_STORAGE=$SUBSTRATE_USE_BOOTC_STORAGE" \
--build-arg "SUBSTRATE_RESOLVE_IMAGE_IDS=true" \
--target overlay \
-t $SUBSTRATEOS_OVERLAY_IMAGE \
-f images/substrateos/Containerfile \
.
# show overrides before we replace anything (as a sort of poor man's backup)
systemd-delta --no-pager
# reconstruct the parts of the overlay we can safely reload.
RELOAD_OVERLAY_BASEDIR=images/substrateos/gen/reload
sudo rm -rf $RELOAD_OVERLAY_BASEDIR
mkdir -p $RELOAD_OVERLAY_BASEDIR
# Export just the files we can load into an existing system
# NOTE in the future we might want to use the tmp mount /usr override that bootc can provide.
SUBSTRATEOS_OVERLAY_IMAGE_DIR=$(sudo $PODMAN image mount $SUBSTRATEOS_OVERLAY_IMAGE)
stage_dir() {
src=$1
dst=$2
echo >&2 "staging ${SUBSTRATEOS_OVERLAY_IMAGE_DIR}$src"
(cd ${SUBSTRATEOS_OVERLAY_IMAGE_DIR}$src; find .)
sudo mkdir -p ${RELOAD_OVERLAY_BASEDIR}${dst}
sudo rsync -i -v -a ${SUBSTRATEOS_OVERLAY_IMAGE_DIR}$src ${RELOAD_OVERLAY_BASEDIR}${dst}
}
# /etc is writable so this is fine
stage_dir /etc/. /etc/
# systemd overrides go in /etc
stage_dir /usr/share/containers/systemd/. /etc/containers/systemd/
stage_dir /usr/lib/systemd/. /etc/systemd/
sudo $PODMAN image unmount $SUBSTRATEOS_OVERLAY_IMAGE
sudo find $RELOAD_OVERLAY_BASEDIR
# use rsync to avoid writing files that don't change.
sudo rsync -i -v -a --no-times --checksum $RELOAD_OVERLAY_BASEDIR/* /
sudo systemctl daemon-reload
# show overrides
systemd-delta --no-pager
# HB it would be better to run this on the overlay dir *before* we copy it. how do we do that?
/usr/libexec/podman/quadlet --dryrun
# after writing our presets, ask systemd to respect them!
sudo systemctl preset-all
systemctl_start_units="$(print_cue_dev_expr_as text \
-t "buildx_bake_docker_compose_focus=$images" \
-e "overlay.systemd_units_to_start_text")"
if [ -n "$systemctl_start_units" ]; then
sudo systemctl start $systemctl_start_units
fi
set +x
write_ready_file
echo "Visit: https://$(hostname -f)"
}
os_installer() {
OS_BUNDLED_IMAGES="events tool-call nvml sigar new-space spaceview mdns template-treehouse substrate"
docker_compose_file=$(pick_docker_compose_yml substrate)
build_images $docker_compose_file $OS_BUNDLED_IMAGES
sudo podman build \
--build-arg "NAMESPACE=$NAMESPACE" \
--build-arg "SUBSTRATE_LIVE_EDIT=$SUBSTRATE_LIVE_EDIT" \
--build-arg "CUE_DEV_DEFS=$CUE_DEV_DEFS" \
--build-arg "SUBSTRATE_USER=$SUBSTRATE_USER" \
--build-arg "SUBSTRATE_GROUP=$SUBSTRATE_GROUP" \
--build-arg "SUBSTRATE_HOME=$SUBSTRATE_HOME" \
--build-arg "SUBSTRATE_SOURCE=$SUBSTRATE_SOURCE" \
--build-arg "SUBSTRATE_BUILD_FOCUS=$OS_BUNDLED_IMAGES" \
--build-arg "SUBSTRATE_USE_BOOTC_STORAGE=true" \
--build-arg "SUBSTRATE_RESOLVE_IMAGE_IDS=false" \
--target dist \
-t $SUBSTRATEOS_IMAGE \
-f images/substrateos/Containerfile \
.
exec sudo env OS_IMAGE_TAG=$SUBSTRATEOS_IMAGE images/substrateos/overlay/usr/bin/substrateos-installer "$@"
}
set +x
case "$1" in
buildx)
shift
buildx "$@"
;;
cue)
shift
cue "$@"
;;
go)
shift
go "$@"
;;
txtar)
shift
txtar "$@"
;;
efibootmgr)
shift
efibootmgr "$@"
;;
efibootmgr-bootnext)
shift
efibootmgr --bootnext "$@"
;;
p9ufs)
shift
go_exec run -a ./images/p9ufs/main.go "$@"
;;
expr-dump)
shift
set_os_vars
set_live_edit_vars
set +x
print_cue_dev_expr_as cue -e "$@"
;;
ready)
shift
set_os_vars
write_ready_file
;;
reload|systemd-reload)
shift
set_os_vars
set_live_edit_vars
systemd_reload "$@"
;;
systemd-logs)
shift
set_os_vars
set_live_edit_vars
systemd_logs
;;
systemd-reload-follow|srf)
shift
set_os_vars
set_live_edit_vars
systemd_reload "$@"
systemd_logs
;;
os-shell)
shift
set_os_vars
os_installer shell "$@"
;;
os-install)
shift
set_os_vars
os_installer install "$@"
;;
buildx-bake)
shift
set_docker_vars
check_cue_dev_expr_as_cue
DOCKER_COMPOSE_FILE=$(pick_docker_compose_yml substrate)
write_rendered_cue_dev_expr_as yaml $DOCKER_COMPOSE_FILE -e 'overlay.buildx_bake_docker_compose'
sudo_buildx bake \
--progress=tty \
-f $DOCKER_COMPOSE_FILE "$@"
;;
test)
shift
if [ $# -eq 0 ]; then
COMPOSE_PROFILES="tests"
else
COMPOSE_PROFILES="tests.$1"
shift
for t in "$@"; do
COMPOSE_PROFILES="$profiles,tests.$t"
done
fi
set_docker_vars
check_cue_dev_expr_as_cue
DOCKER_COMPOSE_FILE=$(pick_docker_compose_yml substrate)
write_rendered_cue_dev_expr_as yaml $DOCKER_COMPOSE_FILE -e 'overlay.docker_compose'
export COMPOSE_PROFILES
docker_compose $DOCKER_COMPOSE_FILE up \
--always-recreate-deps \
--remove-orphans \
--force-recreate \
--abort-on-container-exit \
--build
;;
run-test)
shift
set_docker_vars
check_cue_dev_expr_as_cue
DOCKER_COMPOSE_FILE=$(make_docker_compose_yml substrate 'overlay.docker_compose')
export COMPOSE_PROFILES
docker_compose $DOCKER_COMPOSE_FILE run \
--remove-orphans \
--build \
--rm \
-it \
tests.$1
;;
esac