forked from ublue-os/bluefin-lts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustfile
More file actions
396 lines (342 loc) · 13.6 KB
/
Justfile
File metadata and controls
396 lines (342 loc) · 13.6 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
export repo_organization := env("GITHUB_REPOSITORY_OWNER", "ublue-os")
export image_name := env("IMAGE_NAME", "bluefin")
export centos_version := env("CENTOS_VERSION", "stream10")
export default_tag := env("DEFAULT_TAG", "lts")
export bib_image := env("BIB_IMAGE", "quay.io/centos-bootc/bootc-image-builder:latest")
export coreos_stable_version := env("COREOS_STABLE_VERSION", "42")
export common_image := env("COMMON_IMAGE", "ghcr.io/projectbluefin/common:latest")
export brew_image := env("BREW_IMAGE", "ghcr.io/ublue-os/brew:latest")
alias build-vm := build-qcow2
alias rebuild-vm := rebuild-qcow2
alias run-vm := run-vm-qcow2
[private]
default:
@just --list
# Check Just Syntax
[group('Just')]
check:
#!/usr/bin/env bash
find . -type f -name "*.just" | while read -r file; do
echo "Checking syntax: $file"
just --unstable --fmt --check -f $file
done
echo "Checking syntax: Justfile"
just --unstable --fmt --check -f Justfile
# Fix Just Syntax
[group('Just')]
fix:
#!/usr/bin/env bash
find . -type f -name "*.just" | while read -r file; do
echo "Checking syntax: $file"
just --unstable --fmt -f $file
done
echo "Checking syntax: Justfile"
just --unstable --fmt -f Justfile || { exit 1; }
# Clean Repo
[group('Utility')]
clean:
#!/usr/bin/env bash
set -eoux pipefail
touch _build
find *_build* -exec rm -rf {} \;
rm -f previous.manifest.json
rm -f changelog.md
rm -f output.env
# Sudo Clean Repo
[group('Utility')]
[private]
sudo-clean:
just sudoif just clean
# sudoif bash function
[group('Utility')]
[private]
sudoif command *args:
#!/usr/bin/env bash
function sudoif(){
if [[ "${UID}" -eq 0 ]]; then
"$@"
elif [[ "$(command -v sudo)" && -n "${SSH_ASKPASS:-}" ]] && [[ -n "${DISPLAY:-}" || -n "${WAYLAND_DISPLAY:-}" ]]; then
/usr/bin/sudo --askpass "$@" || exit 1
elif [[ "$(command -v sudo)" ]]; then
/usr/bin/sudo "$@" || exit 1
else
exit 1
fi
}
sudoif {{ command }} {{ args }}
# This Justfile recipe builds a container image using Podman.
#
# Arguments:
# $target_image - The tag you want to apply to the image (default: bluefin).
# $tag - The tag for the image (default: lts).
# $dx - Enable DX (default: "0").
# $gdx - Enable GDX (default: "0").
#
# DX:
# Developer Experience (DX) is a feature that allows you to install the latest developer tools for your system.
# Packages include VScode, Docker, Distrobox, and more.
# GDX: https://docs.projectbluefin.io/gdx/
# GPU Developer Experience (GDX) creates a base as an AI and Graphics platform.
# Installs Nvidia drivers, CUDA, and other tools.
#
# The script constructs the version string using the tag and the current date.
# If the git working directory is clean, it also includes the short SHA of the current HEAD.
#
# just build $target_image $tag $dx $gdx $hwe
#
# Example usage:
# just build bluefin lts 1 0 1
#
# This will build an image 'bluefin:lts' with DX and HWE enabled.
#
[private]
_ensure-yq:
#!/usr/bin/env bash
if ! command -v yq &> /dev/null; then
echo "Missing requirement: 'yq' is not installed."
echo "Please install yq (e.g. 'brew install yq')"
exit 1
fi
# Build the image using the specified parameters
build $target_image=image_name $tag=default_tag $dx="0" $gdx="0" $hwe="0" $kernel_pin="": _ensure-yq
#!/usr/bin/env bash
# Get Version
ver="${tag}-${centos_version}.$(date +%Y%m%d)"
common_image_sha=$(yq -r '.images[] | select(.name == "common") | .digest' image-versions.yaml)
common_image_ref="${common_image}@${common_image_sha}"
brew_image_sha=$(yq -r '.images[] | select(.name == "brew") | .digest' image-versions.yaml)
brew_image_ref="${brew_image}@${brew_image_sha}"
BUILD_ARGS=()
BUILD_ARGS+=("--build-arg" "COMMON_IMAGE_REF=${common_image_ref}")
BUILD_ARGS+=("--build-arg" "BREW_IMAGE_REF=${brew_image_ref}")
BUILD_ARGS+=("--build-arg" "MAJOR_VERSION=${centos_version}")
BUILD_ARGS+=("--build-arg" "IMAGE_NAME=${image_name}")
BUILD_ARGS+=("--build-arg" "IMAGE_VENDOR=${repo_organization}")
BUILD_ARGS+=("--build-arg" "ENABLE_DX=${dx}")
BUILD_ARGS+=("--build-arg" "ENABLE_GDX=${gdx}")
BUILD_ARGS+=("--build-arg" "ENABLE_HWE=${hwe}")
# Select akmods source tag for mounted ZFS/NVIDIA images
ARCH=$(uname -m)
if [[ "${hwe}" -eq "1" || "${gdx}" -eq "1" ]]; then
AKMODS_BASE="coreos-stable-${coreos_stable_version}"
else
AKMODS_BASE="centos-10"
fi
if [[ -n "${kernel_pin}" ]]; then
BUILD_ARGS+=("--build-arg" "AKMODS_VERSION=${AKMODS_BASE}-${kernel_pin}.${ARCH}")
else
BUILD_ARGS+=("--build-arg" "AKMODS_VERSION=${AKMODS_BASE}")
fi
if [[ -z "$(git status -s)" ]]; then
BUILD_ARGS+=("--build-arg" "SHA_HEAD_SHORT=$(git rev-parse --short HEAD)")
fi
echo "Building image ${target_image}:${tag} with args: ${BUILD_ARGS[*]}"
podman build \
"${BUILD_ARGS[@]}" \
--pull=newer \
--tag "${target_image}:${tag}" \
.
# Command: _rootful_load_image
# Description: This script checks if the current user is root or running under sudo. If not, it attempts to resolve the image tag using podman inspect.
# If the image is found, it loads it into rootful podman. If the image is not found, it pulls it from the repository.
#
# Parameters:
# $target_image - The name of the target image to be loaded or pulled.
# $tag - The tag of the target image to be loaded or pulled. Default is 'default_tag'.
#
# Example usage:
# _rootful_load_image my_image latest
#
# Steps:
# 1. Check if the script is already running as root or under sudo.
# 2. Check if target image is in the non-root podman container storage)
# 3. If the image is found, load it into rootful podman using podman scp.
# 4. If the image is not found, pull it from the remote repository into reootful podman.
rootful_load_image $target_image=image_name $tag=default_tag:
#!/usr/bin/env bash
set -eoux pipefail
# Check if already running as root or under sudo
if [[ -n "${SUDO_USER:-}" || "${UID}" -eq "0" ]]; then
echo "Already root or running under sudo, no need to load image from user podman."
exit 0
fi
# Try to resolve the image tag using podman inspect
set +e
resolved_tag=$(podman inspect -t image "${target_image}:${tag}" | jq -r '.[].RepoTags.[0]')
return_code=$?
set -e
if [[ $return_code -eq 0 ]]; then
# If the image is found, load it into rootful podman
ID=$(just sudoif podman images --filter reference="${target_image}:${tag}" --format "'{{ '{{.ID}}' }}'")
if [[ -z "$ID" ]]; then
# If the image ID is not found, copy the image from user podman to root podman
COPYTMP=$(mktemp -p "${PWD}" -d -t _build_podman_scp.XXXXXXXXXX)
just sudoif TMPDIR=${COPYTMP} podman image scp ${UID}@localhost::"${target_image}:${tag}" root@localhost::"${target_image}:${tag}"
rm -rf "${COPYTMP}"
fi
else
# If the image is not found, pull it from the repository
just sudoif podman pull "${target_image}:${tag}"
fi
# Build a bootc bootable image using Bootc Image Builder (BIB)
# Converts a container image to a bootable image
# Parameters:
# target_image: The name of the image to build (ex. localhost/fedora)
# tag: The tag of the image to build (ex. latest)
# type: The type of image to build (ex. qcow2, raw, iso)
# config: The configuration file to use for the build (default: image.toml)
# Example: just _rebuild-bib localhost/fedora latest qcow2 image.toml
_build-bib $target_image $tag $type $config:
#!/usr/bin/env bash
set -euo pipefail
mkdir -p "output"
echo "Cleaning up previous build"
if [[ $type == iso ]]; then
sudo rm -rf "output/bootiso" || true
else
sudo rm -rf "output/${type}" || true
fi
args="--type ${type} "
args+="--use-librepo=True"
just sudoif podman run \
--rm \
-it \
--privileged \
--pull=newer \
--net=host \
--security-opt label=type:unconfined_t \
-v $(pwd)/${config}:/config.toml:ro \
-v $(pwd)/output:/output \
-v /var/lib/containers/storage:/var/lib/containers/storage \
"${bib_image}" \
${args} \
"${target_image}:${tag}"
sudo chown -R $USER:$USER output
# Podman build's the image from the Containerfile and creates a bootable image
# Parameters:
# target_image: The name of the image to build (ex. localhost/fedora)
# tag: The tag of the image to build (ex. latest)
# type: The type of image to build (ex. qcow2, raw, iso)
# config: The configuration file to use for the build (deafult: image.toml)
# Example: just _rebuild-bib localhost/fedora latest qcow2 image.toml
_rebuild-bib $target_image $tag $type $config: (build target_image tag) && (_build-bib target_image tag type config)
# Build a QCOW2 virtual machine image
[group('Build Virtal Machine Image')]
build-qcow2 $target_image=("localhost/" + image_name) $tag=default_tag: && (_build-bib target_image tag "qcow2" "image.toml")
# Build a RAW virtual machine image
[group('Build Virtal Machine Image')]
build-raw $target_image=("localhost/" + image_name) $tag=default_tag: && (_build-bib target_image tag "raw" "image.toml")
# Build an ISO virtual machine image
[group('Build Virtal Machine Image')]
build-iso $target_image=("localhost/" + image_name) $tag=default_tag:
#!/usr/bin/env bash
set -eoux pipefail
# Determine Repo
REPO="local"
if [[ "{{ target_image }}" =~ ghcr.io ]]; then
REPO="ghcr"
fi
# Determine Variant
VARIANT="bluefin"
if [[ "{{ tag }}" =~ lts ]]; then
VARIANT="lts"
fi
# Determine Flavor
FLAVOR="base"
if [[ "{{ target_image }}" =~ -dx ]]; then
FLAVOR="dx"
fi
if [[ "{{ target_image }}" =~ -gdx ]]; then
FLAVOR="gdx"
fi
echo "Delegating to projectbluefin/iso..."
echo "Variant: $VARIANT"
echo "Flavor: $FLAVOR"
echo "Repo: $REPO"
# Clone and Build
BUILD_ROOT="_iso_build"
rm -rf "$BUILD_ROOT"
git clone https://github.com/projectbluefin/iso.git "$BUILD_ROOT"
pushd "$BUILD_ROOT"
just local-iso "$VARIANT" "$FLAVOR" "$REPO"
popd
# Copy Artifacts
mv "$BUILD_ROOT"/*.iso .
rm -rf "$BUILD_ROOT"
# Rebuild a QCOW2 virtual machine image
[group('Build Virtal Machine Image')]
rebuild-qcow2 $target_image=("localhost/" + image_name) $tag=default_tag: && (_rebuild-bib target_image tag "qcow2" "image.toml")
# Rebuild a RAW virtual machine image
[group('Build Virtal Machine Image')]
rebuild-raw $target_image=("localhost/" + image_name) $tag=default_tag: && (_rebuild-bib target_image tag "raw" "image.toml")
# Rebuild an ISO virtual machine image
[group('Build Virtal Machine Image')]
rebuild-iso $target_image=("localhost/" + image_name) $tag=default_tag: && (_rebuild-bib target_image tag "iso" "iso.toml")
# Run a virtual machine with the specified image type and configuration
_run-vm $target_image $tag $type $config $iso_file="":
#!/usr/bin/env bash
set -eoux pipefail
# Determine the image file based on the type
if [[ -n "$iso_file" ]]; then
image_file="$iso_file"
elif [[ $type == iso ]]; then
image_file="output/bootiso/install.iso"
else
image_file="output/${type}/disk.${type}"
fi
# Build the image if it does not exist (skip if custom iso_file provided)
if [[ ! -f "${image_file}" ]]; then
if [[ -n "$iso_file" ]]; then
echo "ISO not found at $iso_file. Please build it first or specify a valid ISO path."
exit 1
fi
just "build-${type}" "$target_image" "$tag"
fi
# Determine an available port to use
port=8006
while grep -q :${port} <<< $(ss -tunalp); do
port=$(( port + 1 ))
done
echo "Using Web Port: ${port}"
echo "Connect via Web: http://localhost:${port}"
# Set up the arguments for running the VM
run_args=()
run_args+=(--rm --privileged)
run_args+=(--pull=newer)
run_args+=(--publish "127.0.0.1:${port}:8006")
run_args+=(--env "CPU_CORES=4")
run_args+=(--env "RAM_SIZE=4G")
run_args+=(--env "DISK_SIZE=64G")
run_args+=(--env "TPM=Y")
run_args+=(--env "GPU=Y")
run_args+=(--device=/dev/kvm)
# Add SSH port forwarding for all VM types
ssh_port=$(( port + 1 ))
while grep -q :${ssh_port} <<< $(ss -tunalp); do
ssh_port=$(( ssh_port + 1 ))
done
echo "Using SSH Port: ${ssh_port}"
echo "Connect via SSH: ssh user@localhost -p ${ssh_port}"
run_args+=(--publish "127.0.0.1:${ssh_port}:22")
run_args+=(--env "USER_PORTS=22")
run_args+=(--env "NETWORK=user")
run_args+=(--volume "${PWD}/${image_file}":"/boot.${type}")
run_args+=(ghcr.io/qemus/qemu)
# Run the VM and open the browser to connect
(sleep 5 && xdg-open "http://localhost:${port}") &
podman run "${run_args[@]}"
# Run a virtual machine from a QCOW2 image
[group('Run Virtal Machine')]
run-vm-qcow2 $target_image=("localhost/" + image_name) $tag=default_tag: && (_run-vm target_image tag "qcow2" "image.toml")
# Run a virtual machine from a RAW image
[group('Run Virtal Machine')]
run-vm-raw $target_image=("localhost/" + image_name) $tag=default_tag: && (_run-vm target_image tag "raw" "image.toml")
# Run a virtual machine from an ISO
[group('Run Virtal Machine')]
run-vm-iso $iso_file="output/bootiso/install.iso": && (_run-vm "" "" "iso" "" iso_file)
# Runs shell check on all Bash scripts
lint:
/usr/bin/find . -iname "*.sh" -type f -exec shellcheck "{}" ';'
# Runs shfmt on all Bash scripts
format:
/usr/bin/find . -iname "*.sh" -type f -exec shfmt --write "{}" ';'