OILS / deps / images.sh View on Github | oils.pub

361 lines, 143 significant
1#!/usr/bin/env bash
2#
3# Manage container images for Soil
4#
5# Usage:
6# deps/images.sh <function name>
7#
8# Dirs maybe to clear:
9#
10# $0 clean-all # Start from scratch
11#
12# Example:
13#
14# (1) Update LATEST_TAG
15#
16# (2) Bootstrapping Wedges
17#
18# $0 build wedge-bootstrap-debian-12
19#
20# (3) Building wedges:
21#
22# build/deps.sh fetch
23# build/deps.sh boxed-wedges
24# build/deps.sh boxed-spec-bin
25#
26# (4) Rebuild an image
27#
28# $0 build soil-debian-12 # populates apt cache. WHY DO I NEED THIS?
29# $0 build soil-test-image T # reuse package cache from apt-get
30# $0 smoke soil-test-image # smoke test
31#
32# (5) Update live version in 'soil/host-shim.sh live-image-tag'
33#
34# (6) Push Everything you Built
35#
36# $0 push wedge-bootstrap-debian-12 v-2025-04-30
37# $0 push soil-debian-12 v-2025-04-30
38# $0 push soil-test-image v-2025-04-30
39#
40# Our images:
41#
42# https://hub.docker.com/u/oilshell
43#
44# deps/images.sh list-tagged
45#
46
47set -o nounset
48set -o pipefail
49set -o errexit
50
51source deps/podman.sh
52
53DOCKER=${DOCKER:-docker}
54
55# Build with this tag
56readonly LATEST_TAG='v-2025-04-30b'
57
58clean-all() {
59 dirs='_build/wedge/tmp _build/wedge/binary _build/deps-source'
60 #rm -r -f $dirs
61 sudo rm -r -f $dirs
62}
63
64# BUGS in Docker.
65#
66# https://stackoverflow.com/questions/69173822/docker-build-uses-wrong-dockerfile-content-bug
67
68# NOTE: This also clears the exec.cachemount
69prune() {
70 sudo $DOCKER builder prune -f
71}
72
73# https://stackoverflow.com/questions/62834806/docker-buildkit-cache-location-size-and-id
74#
75# It lives somewhere in /var/lib/docker/overlay2
76
77show-cachemount() {
78 sudo $DOCKER system df -v --format '{{ .BuildCache | json }}' \
79 | jq '.[] | select(.CacheType == "exec.cachemount")' | tee _tmp/cachemount.txt
80
81 cat _tmp/cachemount.txt | jq -r '.ID' | while read id; do
82 sudo tree /var/lib/docker/overlay2/$id
83 sudo du --si -s /var/lib/docker/overlay2/$id
84 echo
85 done
86}
87
88tag-latest() {
89 local name=${1:-wedge-bootstrap-debian-12}
90 local tag_built_with=${2:-$LATEST_TAG}
91
92 set -x # 'docker tag' is annoyingly silent
93 sudo $DOCKER tag oilshell/$name:{$tag_built_with,latest}
94}
95
96build() {
97 local name=${1:-soil-dummy}
98 local use_cache=${2:-} # OFF by default
99
100 # set -x
101 local -a flags
102 if test -n "$use_cache"; then
103 flags=()
104 else
105 flags=('--no-cache=true')
106 fi
107 #flags+=('--progress=plain')
108
109 # Uh BuildKit is not the default on Linux!
110 # http://jpetazzo.github.io/2021/11/30/docker-build-container-images-antipatterns/
111 #
112 # It is more parallel and has colored output.
113
114 # TODO: use --authfile and more
115 #export-podman
116
117 # can't preserve the entire env: https://github.com/containers/buildah/issues/3887
118 #sudo --preserve-env=CONTAINERS_REGISTRIES_CONF --preserve-env=REGISTRY_AUTH_FILE \
119 sudo -E DOCKER_BUILDKIT=1 \
120 $DOCKER build "${flags[@]}" \
121 --tag "oilshell/$name:$LATEST_TAG" \
122 --file deps/Dockerfile.$name .
123
124 # Avoid hassle by also tagging it
125 tag-latest $name
126}
127
128list-images() {
129 for name in deps/Dockerfile.*; do
130 local image_id=${name//'deps/Dockerfile.'/}
131 if test "$image_id" = 'test-image'; then
132 continue
133 fi
134 echo $image_id
135 done
136}
137
138tag-all-latest() {
139 list-images | egrep -v 'wedge-builder|bootstrap' | while read image; do
140 local tag
141 tag=$(soil/host-shim.sh live-image-tag $image)
142
143 echo "$tag $image"
144
145 # syntax: source -> target
146 sudo $DOCKER tag oilshell/soil-$image:$tag oilshell/soil-$image:latest
147 done
148}
149
150push-all-latest() {
151 ### 'latest' can lag behind the tagged version, so push to catch up
152
153 # because our 'my-sizes' script fetches the latest manifest
154
155 list-images | grep -v 'wedge-builder|bootstrap' | while read image_id; do
156 echo "___ $image_id"
157 push $image_id latest
158 done
159}
160
161list-tagged() {
162 sudo $DOCKER images 'oilshell/*' #:v-*'
163}
164
165push() {
166 local name=${1:-soil-dummy}
167 local tag=${2:-$LATEST_TAG}
168
169 # TODO: replace with flags
170 #export-podman
171
172 local image="oilshell/$name:$tag"
173
174 set -x
175
176 # -E for export-podman vars
177 sudo -E $DOCKER push $image
178 #sudo -E $DOCKER --log-level=debug push $image
179
180 # Also push the 'latest' tag, to avoid getting out of sync
181 sudo -E $DOCKER push oilshell/$name:latest
182}
183
184smoke-test-script() {
185 echo '
186for file in /etc/debian_version /etc/lsb-release; do
187 if test -f $file; then
188 # spec/ble-idioms tests this
189 #grep -E "foo|^10" $file; echo grep=$?
190
191 echo $file
192 echo
193 cat $file
194 echo
195 else
196 echo "($file does not exist)"
197 fi
198done
199
200echo "bash $BASH_VERSION"
201
202git --version
203
204for name in python python2 python3; do
205 if which $name; then
206 $name -V
207 else
208 echo "$name not found"
209 fi
210done
211
212echo PATH=$PATH
213
214#curl https://op.oilshell.org/
215
216if true; then
217 python3 -c "import ssl; print(ssl)"
218 find /usr/lib | grep -i libssl
219 echo
220
221 dpkg -S /usr/lib/x86_64-linux-gnu/libssl.so.3
222 echo
223
224 #ls -l /usr/lib/x86_64-linux-gnu/libssl.so.1.1
225
226 apt-cache show libssl-dev
227
228 # find /lib | grep -i libssl
229 # echo
230 # find /usr/local | grep -i libssl
231 # echo
232 # python3-config --libs
233
234 # Useful command
235 # ldconfig -v #| grep ssl
236 # echo
237
238 #find / -name 'libssl.so*'
239fi
240'
241}
242
243smoke() {
244 sudo $0 _smoke "$@"
245}
246
247_smoke() {
248 ### Smoke test of container
249 local name=${1:-soil-dummy}
250 local tag=${2:-$LATEST_TAG}
251 local docker=${3:-$DOCKER}
252 local prefix=${4:-}
253
254 #sudo docker run oilshell/$name
255 #sudo docker run oilshell/$name python2 -c 'print("python2")'
256
257 # Need to point at registries.conf ?
258 #export-podman
259
260 $docker run ${prefix}oilshell/$name:$tag bash -c "$(smoke-test-script)"
261
262 # Python 2.7 build/prepare.sh requires this
263 #sudo docker run oilshell/$name python -V
264
265 #sudo docker run oilshell/$name python3 -c 'import pexpect; print(pexpect)'
266}
267
268smoke-podman() {
269 local name=${1:-dummy}
270
271 # 2025-04: I need to do 'podman login docker.io'
272 #
273 # Running without root
274
275 # need explicit docker.io prefix with podman
276 # smoke $name latest podman docker.io/
277
278 local tag='latest'
279 local prefix='docker.io/'
280 smoke-test-script | podman run -i ${prefix}oilshell/soil-$name:$tag bash
281}
282
283cmd() {
284 ### Run an arbitrary command
285 local name=${1:-soil-dummy}
286 local tag=${2:-$LATEST_TAG}
287
288 shift 2
289
290 sudo $DOCKER run oilshell/$name:$tag "$@"
291}
292
293utf8() {
294 # needed for a spec test, not the default on Debian
295 cmd ovm-tarball bash -c 'LC_ALL=en_US.UTF-8; echo $LC_ALL'
296}
297
298mount-test() {
299 local name=${1:-soil-dummy}
300
301 local -a argv
302 if test $# -le 1; then
303 argv=(sh -c 'ls -l /home/uke/oil')
304 else
305 argv=( "${@:2}" ) # index 2 not 1, weird shell behavior
306 fi
307
308 # mount Oil directory as /app
309 sudo $DOCKER run \
310 --mount "type=bind,source=$PWD,target=/home/uke/oil" \
311 oilshell/$name "${argv[@]}"
312}
313
314image-history() {
315 local image_id=${1:-soil-dummy}
316 local tag=${2:-latest}
317
318 local image="oilshell/$image_id"
319
320 sudo $DOCKER history $image:$tag
321}
322
323save() {
324 local image_id=${1:-soil-dummy}
325 local tag=${2:-latest}
326
327 local image="oilshell/$image_id"
328
329 mkdir -p _tmp/images
330 local out=_tmp/images/$image_id.tar
331
332 # Use > instead of -o so it doesn'th have root permissions
333 time sudo $DOCKER save $image:$tag > $out
334 ls -l -h $out
335}
336
337# This shows CREATED, command CREATED BY, size
338# It's a human readable size though
339#
340# This doesn't really have anything better
341# https://gist.github.com/MichaelSimons/fb588539dcefd9b5fdf45ba04c302db6
342#
343# It's annoying that the remote registry API is different than the local API.
344
345layers() {
346 local name=${1:-soil-dummy}
347 local tag=${2:-$LATEST_TAG}
348
349 local image="oilshell/$name:$tag"
350
351 # Gah this still prints 237M, not the exact number of bytes!
352 # --format ' {{ .Size }} '
353 sudo $DOCKER history --no-trunc $image
354
355 echo $'Size\tVirtual Size'
356 sudo $DOCKER inspect $image \
357 | jq --raw-output '.[0] | [.Size, .VirtualSize] | @tsv' \
358 | commas
359}
360
361"$@"