diff options
-rwxr-xr-x | start-container-docker.sh | 42 | ||||
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 17 |
2 files changed, 57 insertions, 2 deletions
diff --git a/start-container-docker.sh b/start-container-docker.sh index 6fbec8e4..bc3f6145 100755 --- a/start-container-docker.sh +++ b/start-container-docker.sh @@ -215,6 +215,33 @@ fi image=linaro/ci-${container_arch}-tcwg-build-ubuntu:${distro} +stamp_dir=/home/shared/docker +if ! [ -d "$stamp_dir" ]; then + sudo mkdir -p "$stamp_dir" + sudo chmod 0777 "$stamp_dir" +fi + +# We use two stamp files per image: +# - $image_stamp.pull -- time of last image pull +# - $image_stamp.use -- time of last image use +image_stamp="$stamp_dir/$(echo "$image" | tr "/:" "_")" + +# We attempt to run all our builds using current versions of docker images. +# Unfortunately, now that dockerhub limits pull requests, we need to be more +# considerate to when we pull the image or attempt to use a local copy. +# Also note that "docker run" below will automatically pull the image if there +# is no local copy. +pull_image=false + +# For starters, let's try to pull images once a day. This guarantees +# that any change to master docker images will be deployed within a day. +pull_if_older_than=$(($(date +%s) - 1*24*60*60)) +# Use negative comparison to handle non-existent stamp files. +if ! [ "$(stat -c %Z "$image_stamp.pull" 2>/dev/null)" \ + -gt $pull_if_older_than ]; then + pull_image=true +fi + # Avoid connexion sharing because of race conditions with parallel # builds SSH="ssh -S none" @@ -224,7 +251,15 @@ SSH="ssh -S none" # Instead of: # "foo bar docker" <...> DOCKER="$dryruncmd $SSH $session_host docker" -$DOCKER pull $image || ssh_error $? + +if $pull_image; then + $DOCKER pull $image || ssh_error $? + # Remove the stamp to avoid permission issues (we have rwx permissions + # for all on the directory, so we can always remove a file, but only + # owner can modify files. + rm -f "$image_stamp.pull" + touch "$image_stamp.pull" +fi SECURITY="--cap-add=SYS_PTRACE" # We need this because of a bug in libgo's configure script: @@ -367,6 +402,11 @@ if $ssh_info; then ssh_info_opt="ssh_host=${user}$session_host ssh_port=$session_port" fi +# Update the time of image use, so that we don't remove the image in +# tcwg-cleanup-stale-containers . +rm -f "$image_stamp.use" +touch "$image_stamp.use" + # Restore stdout/stderr exec 1>&3 2>&4 diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index 2fa246cf..fe626621 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -176,7 +176,22 @@ else fi if $cleanup_images; then - docker image prune -a -f + # See start-container-docker.sh for background on image stamp files. + 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 + 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" 2>/dev/null)" \ + -gt $remove_if_not_used_since ]; then + # Untag the image. + docker rmi "$image" + fi + done + # Prune untagged images. + docker image prune -f else echo "DRY_RUN: NOT REMOVING UNUSED IMAGES" fi |