diff options
author | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2021-01-04 07:26:05 +0000 |
---|---|---|
committer | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2021-01-04 07:34:11 +0000 |
commit | 49acfb8a50661b74f64352914b48b259be6bef38 (patch) | |
tree | 5ece50430bdecc8b0d326e461494a85093bad580 | |
parent | 7654f42e2d36c49b388abc653c5017170eea3ce1 (diff) |
Docker image pull: Fix removing previously untagged images
... and update "pull" timestamps in tcwg-start-container.sh
Change-Id: Ib5638a9fb22c341527443fd3ff2f8431f7aa1fe0
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 9 | ||||
-rwxr-xr-x | tcwg-start-container.sh | 25 |
2 files changed, 20 insertions, 14 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index cc89a01e..84f4a5ea 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -180,14 +180,17 @@ if $cleanup_images; then stamp_dir=/home/shared/docker # Untag and prune images that haven't been used for 3 days or more. - for image in $(docker images --format "{{.Repository}}:{{.Tag}}"); do + for image_tuple in $(docker images --format "{{.ID}}:{{.Repository}}:{{.Tag}}"); do + image_id=$(echo "$image_tuple" | cut -d: -f 1) + image=$(echo "$image_tuple" | cut -d: -f 2,3) image_stamp="$stamp_dir/$(echo "$image" | tr "/:" "_")" remove_if_not_used_since=$(($(date +%s) - 3*24*60*60)) # Use negative comparison to handle non-existent stamp files. if ! [ "$(stat -c %Z "$image_stamp.use")" \ -gt $remove_if_not_used_since ] 2>/dev/null; then - # Untag the image. - docker rmi -f "$image" + # Untag the image. Use $image_id to handle previously-untagged + # images and other cases when we have no repo or tag reference. + docker rmi -f "$image_id" fi done # Prune untagged images. diff --git a/tcwg-start-container.sh b/tcwg-start-container.sh index 2c0efb2c..cc9b040b 100755 --- a/tcwg-start-container.sh +++ b/tcwg-start-container.sh @@ -8,6 +8,7 @@ convert_args_to_variables "$@" shift "$SHIFT_CONVERTED_ARGS" obligatory_variables container image +declare container image keep_existing="${keep_existing-true}" verbose="${verbose-true}" @@ -16,11 +17,22 @@ set -u if $verbose; then set -x; fi -# shellcheck disable=SC2154 +# See start-container-docker.sh for background on image stamp files. +# Note that there is no need to track pull requests, since these happen +# once a day anyway. +stamp_dir=/home/shared/docker +if ! [ -d "$stamp_dir" ]; then + sudo mkdir -p "$stamp_dir" || mkdir -p "$stamp_dir" + sudo chmod 0777 "$stamp_dir" || chmod 0777 "$stamp_dir" +fi +image_stamp="$stamp_dir/$(echo "$image" | tr "/:" "_")" + docker pull "$image" +rm -f "$image_stamp.pull" +touch "$image_stamp.pull" + rm_cnt="" -# shellcheck disable=SC2154 if docker stats --no-stream "$container" >/dev/null 2>&1; then case "$keep_existing" in true) exit 0 ;; @@ -53,15 +65,6 @@ docker run --rm $image start.sh > "$start_sh" bash "$start_sh" "$@" rm "$start_sh" -# See start-container-docker.sh for background on image stamp files. -# Note that there is no need to track pull requests, since these happen -# once a day anyway. -stamp_dir=/home/shared/docker -if ! [ -d "$stamp_dir" ]; then - sudo mkdir -p "$stamp_dir" || mkdir -p "$stamp_dir" - sudo chmod 0777 "$stamp_dir" || chmod 0777 "$stamp_dir" -fi -image_stamp="$stamp_dir/$(echo "$image" | tr "/:" "_")" rm -f "$image_stamp.use" touch "$image_stamp.use" |