generated from ublue-os/image-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Justfile
388 lines (348 loc) · 12 KB
/
Justfile
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
my_image := "bos"
my_image_styled := "bOS"
repo_name := "bos"
repo_organization := "bsherman"
rechunker_image := "ghcr.io/hhd-dev/rechunk:v1.0.1"
images := '(
[bazzite]="bazzite-gnome"
[bazzite-deck]="bazzite-deck-gnome"
[bluefin]="bluefin"
[bluefin-dx]="bluefin-dx"
[ucore-minimal]="ucore-minimal"
[ucore]="ucore"
[ucore-hci]="ucore-hci"
)'
flavors := '(
[main]=main
[nvidia]=nvidia
)'
tags := '(
[stable]=stable
[beta]=beta
[testing]=testing
)'
[private]
default:
@just --list
# Check Just Syntax
[group('Just')]
check:
#!/usr/bin/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/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/bash
set -eoux pipefail
rm -f previous.manifest.json
find *_build* -exec rm -rf {} \;
# Sudo Clean
[group('Utility')]
sudo-clean:
#!/usr/bin/bash
set -eoux pipefail
just sudoif "rm -f previous.manifest.json"
just sudoif "find *_build* -exec rm -rf {} \;"
# Check if valid combo
[group('Utility')]
[private]
validate image="" tag="" flavor="":
#!/usr/bin/bash
set -eoux pipefail
declare -A images={{ images }}
declare -A tags={{ tags }}
declare -A flavors={{ flavors }}
image={{ image }}
tag={{ tag }}
flavor={{ flavor }}
checkimage="${images[${image}]-}"
checktag="${tags[${tag}]-}"
checkflavor="${flavors[${flavor}]-}"
# Validity Checks
if [[ -z "$checkimage" ]]; then
echo "Invalid Image..."
exit 1
fi
if [[ -z "$checkflavor" ]]; then
echo "Invalid flavor..."
exit 1
fi
if [[ -z "$checktag" ]]; then
echo "Invalid tag..."
exit 1
fi
if [[ "$checkimage" =~ bazzite ]]; then
if [[ "$checktag" != stable ]]; then
echo "Bazzite only builds stable tag..."
exit 1
fi
if [[ "$checkflavor" != main ]]; then
echo "Bazzite only builds main flavor..."
exit 1
fi
fi
if [[ "$checkimage" =~ bluefin ]]; then
if [[ "$checktag" =~ testing ]]; then
echo "Bluefin does not build testing tag..."
exit 1
fi
fi
if [[ "$checkimage" =~ ucore ]]; then
if [[ "$checktag" =~ beta ]]; then
echo "uCore does not build beta tag..."
exit 1
fi
fi
# Generate container args, etc
[group('Utility')]
[private]
gen-build-src-dst image="" tag="" flavor="":
#!/usr/bin/bash
set -eou pipefail
declare -A images={{ images }}
declare -A tags={{ tags }}
declare -A flavors={{ flavors }}
image={{ image }}
tag={{ tag }}
flavor={{ flavor }}
srcimage="${images[${image}]-}"
srctag="${tags[${tag}]-}"
srcflavor="${flavors[${flavor}]-}"
# Validate
just validate "${image}" "${tag}" "${flavor}"
# Image Name (the SOURCE image)
if [[ "${flavor}" =~ main || "${image}" =~ ucore ]]; then
# image name is what was required if it's a main flavor
# OR if it's ucore, since ucore has a different tagging for nivida rather than image
source_image="${srcimage}"
else
source_image="${srcimage}-${srcflavor}"
fi
# Tag Version (the SOURCE tag)
if [[ "${image}" =~ ucore ]]; then
if [[ "${image}" == ucore-minimal ]]; then
if [[ "${flavor}" =~ main ]]; then
source_tag="${srctag}"
else
source_tag="${srctag}-${srcflavor}"
fi
else
if [[ "${flavor}" =~ main ]]; then
source_tag="${srctag}-zfs"
else
source_tag="${srctag}-${srcflavor}-zfs"
fi
fi
elif [[ "${image}" =~ bluefin && "${tag}" == stable ]]; then
source_tag="${srctag}-daily"
else
source_tag="${srctag}"
fi
# My Tag (the tag I publish for my image)
my_tag_flavor="" # a way to inject flavor for ucore using different tag style
if [[ "${flavor}" != main ]]; then
my_tag_flavor="-${flavor}"
fi
if [[ "${tag}" =~ stable ]]; then
my_tag="${image}${my_tag_flavor}"
else
my_tag="${image}${my_tag_flavor}-${tag}"
fi
echo "${source_image} ${source_tag} {{ my_image }} ${my_tag}"
# sudoif bash function
[group('Utility')]
[private]
sudoif command *args:
#!/usr/bin/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 }}
# Build Image
[group('Image')]
build image="bluefin" tag="stable" flavor="main" rechunk="0":
#!/usr/bin/bash
set -eoux pipefail
image={{ image }}
tag={{ tag }}
flavor={{ flavor }}
# Validate is handled by gen-build-src-dst
build_src_dst=($(just gen-build-src-dst "${image}" "${tag}" "${flavor}"))
src_img=${build_src_dst[0]}
src_tag=${build_src_dst[1]}
dst_img=${build_src_dst[2]}
dst_tag=${build_src_dst[3]}
# Build Arguments
BUILD_ARGS=()
BUILD_ARGS+=("--build-arg" "BASE_IMAGE=${src_img}")
BUILD_ARGS+=("--build-arg" "IMAGE=${src_img}")
BUILD_ARGS+=("--build-arg" "TAG_VERSION=${src_tag}")
# Labels
LABELS=()
LABELS+=("--label" "io.artifacthub.package.readme-url=https://raw.githubusercontent.com/{{ repo_organization }}/{{ repo_name }}/refs/heads/main/README.md")
LABELS+=("--label" "org.opencontainers.image.title={{ my_image_styled }}")
LABELS+=("--label" "org.opencontainers.image.description=This {{ my_image_styled }} is {{ repo_organization }}'s customized image of ghcr.io/ublue-os/${src_img}:${src_tag}")
# Build Image
podman build \
"${BUILD_ARGS[@]}" \
"${LABELS[@]}" \
--tag "${dst_img}:${dst_tag}" \
.
# Rechunk
if [[ "{{ rechunk }}" == "1" ]]; then
just rechunk "${image}" "${tag}" "${flavor}"
fi
# Build Image and Rechunk
[group('Image')]
build-rechunk image="bluefin" tag="stable" flavor="main":
@just build {{ image }} {{ tag }} {{ flavor }} 1
# Rechunk Image
[group('Image')]
[private]
rechunk image="bluefin" tag="stable" flavor="main":
#!/usr/bin/bash
set -eoux pipefail
image={{ image }}
tag={{ tag }}
flavor={{ flavor }}
# Validate is handled by gen-build-src-dst
build_src_dst=($(just gen-build-src-dst "${image}" "${tag}" "${flavor}"))
src_img=${build_src_dst[0]}
src_tag=${build_src_dst[1]}
dst_img=${build_src_dst[2]}
dst_tag=${build_src_dst[3]}
# debugging
just sudoif podman images
# Check if image is already built
ID=$(podman images --filter reference=localhost/"${dst_img}":"${dst_tag}" --format "'{{ '{{.ID}}' }}'")
if [[ -z "$ID" ]]; then
just build "${image}" "${tag}" "${flavor}"
fi
# Load into Rootful Podman
ID=$(just sudoif podman images --filter reference=localhost/"${dst_img}":"${dst_tag}" --format "'{{ '{{.ID}}' }}'")
if [[ -z "$ID" ]]; then
just sudoif podman image scp ${UID}@localhost::localhost/"${dst_img}":"${dst_tag}" root@localhost::localhost/"${dst_img}":"${dst_tag}"
fi
# Prep Container
CREF=$(just sudoif podman create localhost/"${dst_img}":"${dst_tag}" bash)
MOUNT=$(just sudoif podman mount "${CREF}")
OUT_NAME="${dst_img}_build"
# Fedora Version
fedora_version=$(just sudoif podman inspect $CREF | jq -r '.[].Config.Labels["ostree.linux"]' | grep -oP 'fc\K[0-9]+')
# Cleanup space needed for Github Action runner lack of space
ID=$(just sudoif podman images --filter reference=ghcr.io/ublue-os/"${src_img}":${src_tag} --format "'{{ '{{.ID}}' }}'")
if [[ -n "$ID" ]]; then
just sudoif podman rmi "$ID"
fi
# Run Rechunker's Prune
just sudoif podman run --rm \
--pull=newer \
--security-opt label=disable \
--volume "$MOUNT":/var/tree \
--env TREE=/var/tree \
--user 0:0 \
"{{ rechunker_image }}" \
/sources/rechunk/1_prune.sh
# Run Rechunker's Create
just sudoif podman run --rm \
--security-opt label=disable \
--volume "$MOUNT":/var/tree \
--volume "cache_ostree:/var/ostree" \
--env TREE=/var/tree \
--env REPO=/var/ostree/repo \
--env RESET_TIMESTAMP=1 \
--user 0:0 \
"{{ rechunker_image }}" \
/sources/rechunk/2_create.sh
# Cleanup Temp Container Reference
just sudoif podman unmount "$CREF"
just sudoif podman rm "$CREF"
# Run Rechunker
just sudoif podman run --rm \
--pull=newer \
--security-opt label=disable \
--volume "$PWD:/workspace" \
--volume "$PWD:/var/git" \
--volume cache_ostree:/var/ostree \
--env REPO=/var/ostree/repo \
--env PREV_REF=ghcr.io/{{ repo_organization }}/"${dst_img}":"${dst_tag}" \
--env OUT_NAME="$OUT_NAME" \
--env LABELS="org.opencontainers.image.title={{ my_image_styled }}$'\n'org.opencontainers.image.version=${fedora_version}-$(date +%Y%m%d-%H:%M:%S)$'\n''io.artifacthub.package.readme-url=https://raw.githubusercontent.com/{{ repo_organization }}/{{ repo_name }}/refs/heads/main/README.md'$'\n'" \
--env "DESCRIPTION='This {{ my_image_styled }} is a {{ repo_organization }} customized version of ghcr.io/ublue-os/${src_img}:${src_tag}'" \
--env VERSION_FN=/workspace/version.txt \
--env OUT_REF="oci:$OUT_NAME" \
--env GIT_DIR="/var/git" \
--user 0:0 \
"{{ rechunker_image }}" \
/sources/rechunk/3_chunk.sh
# Cleanup
just sudoif "find ${OUT_NAME} -type d -exec chmod 0755 {} \;" || true
just sudoif "find ${OUT_NAME}* -type f -exec chmod 0644 {} \;" || true
if [[ "${UID}" -gt 0 ]]; then
just sudoif chown ${UID}:${GROUPS} -R "${PWD}"
fi
just sudoif podman volume rm cache_ostree
just sudoif podman rmi localhost/"${dst_img}":"${dst_tag}"
# Load Image into Podman Store
IMAGE=$(podman pull oci:"${PWD}"/"${OUT_NAME}")
podman tag ${IMAGE} localhost/"${dst_img}":"${dst_tag}"
# Run Container
[group('Image')]
run image="bluefin" tag="stable" flavor="main":
#!/usr/bin/bash
set -eoux pipefail
image={{ image }}
tag={{ tag }}
flavor={{ flavor }}
# Validate is handled by gen-build-src-dst
build_src_dst=($(just gen-build-src-dst "${image}" "${tag}" "${flavor}"))
src_img=${build_src_dst[0]}
src_tag=${build_src_dst[1]}
dst_img=${build_src_dst[2]}
dst_tag=${build_src_dst[3]}
# Check if image exists
ID=$(podman images --filter reference=localhost/"${dst_img}":"${dst_tag}" --format "'{{ '{{.ID}}' }}'")
if [[ -z "$ID" ]]; then
just build "$image" "$tag" "$flavor"
fi
# Run Container
podman run -it --rm localhost/"${dst_img}":"${dst_tag}" bash
# Get Fedora Version of an image
[group('Utility')]
fedora_version image="bluefin" tag="stable" flavor="main":
#!/usr/bin/bash
set -eou pipefail
just validate {{ image }} {{ tag }} {{ flavor }}
if [[ ! -f /tmp/manifest.json ]]; then
if [[ "{{ tag }}" =~ stable ]]; then
# CoreOS does not uses cosign
skopeo inspect --retry-times 3 docker://quay.io/fedora/fedora-coreos:stable > /tmp/manifest.json
else
skopeo inspect --retry-times 3 docker://ghcr.io/ublue-os/base-main:"{{ tag }}" > /tmp/manifest.json
fi
fi
fedora_version=$(jq -r '.Labels["ostree.linux"]' < /tmp/manifest.json | grep -oP 'fc\K[0-9]+')
echo "${fedora_version}"