From 5043ec95fa89794f92a23f66f01a4757385e91bd Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Mon, 16 Sep 2024 19:06:39 +1000 Subject: [PATCH 1/2] Docker Library - manifest cleanup jq --arg x v implies x is always a string. This doesn't work when its compared to a number as as such all images are cleared. Make removal around Dangling match the comment. Ensure that buildah images removed have a mariadb name (otherwise it picks up ubuntu/mysql images in repo). Simplify other select expressions to include more. Yes, buildah and podman put different capitalization on .Id/.id files in json format --- scripts/docker-library-manifest.sh | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts/docker-library-manifest.sh b/scripts/docker-library-manifest.sh index aa89cfd3..2a4a89d1 100755 --- a/scripts/docker-library-manifest.sh +++ b/scripts/docker-library-manifest.sh @@ -202,17 +202,32 @@ if (($(buildah manifest inspect "$devmanifest" | jq '.manifests | length') >= ex buildah images # lost and forgotten (or just didn't make enough manifest items - build failure on an arch) lastweek=$(date +%s --date='1 week ago') + # note - jq args are treated as strings and need to be cast tonumber to make the comparible. + # clear buildah images - buildah images --json | jq --arg lastweek "$lastweek" '.[] | .id as $id | select(.created <= $lastweek ) | $id' | xargs --no-run-if-empty podman rmi --force || echo "had trouble removing buildah images" + buildah images --json | + jq --arg lastweek "$lastweek" '.[] | select(.created <= ( $lastweek | tonumber ) and any( .names[]? ; startswith("localhost/mariadb")) ) | .id' | + xargs --no-run-if-empty buildah rmi --force || echo "had trouble removing buildah images" # old ubuntu and base images that got updated so are Dangling - podman images --format=json | jq --arg lastweek "$lastweek" '.[] | .Id as $id | select(.Created <= $lastweek ) | any( .Names[]? ; startswith("mariadb")) | $id' | xargs --no-run-if-empty podman rmi --force || echo "continuing cleanup anyway" - # clean buildah containers + podman images --format=json | + jq --arg lastweek "$lastweek" '.[] | select(.Created <= ( $lastweek | tonumber ) and .Dangling? ) | .Id' | + xargs --no-run-if-empty podman rmi --force || echo "continuing cleanup anyway" + + # clean buildah containers (nothing should be running) buildah containers --format "{{.ContainerID}}" | xargs --no-run-if-empty buildah rm || echo "had trouble cleaning containers" + # clean images - buildah images --json | jq --arg lastweek "$lastweek" '.[] | select(.readonly ==false) | select(.created <= $lastweek) | select( .names == null) | .id' | xargs --no-run-if-empty buildah rmi || echo "had trouble cleaning images" + buildah images --json | + jq --arg lastweek "$lastweek" '.[] | select(.readonly ==false and .created <= ( $lastweek | tonumber ) and .names == null) | .id' | + xargs --no-run-if-empty buildah rmi || echo "had trouble cleaning images" + # clean manifests - buildah images --json | jq --arg lastweek "$lastweek" '.[] | select(.readonly ==false) | select(.created <= $lastweek) | select( try .names[0]? catch "" | startswith("localhost/mariadb-") ) | .id' | xargs --no-run-if-empty buildah manifest rm || echo "trouble cleaning manifests" + buildah images --json | + jq --arg lastweek "$lastweek" '.[] | select(.readonly ==false and .created <= ( $lastweek | tonumber ) and ( try .names[0]? catch "" | startswith("localhost/mariadb-") )) | .id' | + xargs --no-run-if-empty buildah manifest rm || echo "trouble cleaning manifests" + + # what's left? buildah images fi From ecfa1cbdde85da8108819584c8d3708dbe02171a Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Mon, 16 Sep 2024 19:35:00 +1000 Subject: [PATCH 2/2] spell fix --- scripts/docker-library-manifest.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/docker-library-manifest.sh b/scripts/docker-library-manifest.sh index 2a4a89d1..c75226d0 100755 --- a/scripts/docker-library-manifest.sh +++ b/scripts/docker-library-manifest.sh @@ -202,7 +202,7 @@ if (($(buildah manifest inspect "$devmanifest" | jq '.manifests | length') >= ex buildah images # lost and forgotten (or just didn't make enough manifest items - build failure on an arch) lastweek=$(date +%s --date='1 week ago') - # note - jq args are treated as strings and need to be cast tonumber to make the comparible. + # note - jq args are treated as strings and need to be cast tonumber to make the value comparable. # clear buildah images buildah images --json |