From be952660cef6b2a8fbb2e678496df0c2a596d31b Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sun, 10 Dec 2023 23:26:42 +0100 Subject: [PATCH] fix: [postgres] prevent failure on slower hosts Using ``docker stop CONTAINER`` followed by a ``docker rmi IMAGE`` where IMAGE is the image of CONTAINER may fail. To ensure that it won't fail, we have to wait for the disappearance of the container in the container list. --- postgres/actions/upgrade | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/postgres/actions/upgrade b/postgres/actions/upgrade index 06bfbc0..ad0656d 100755 --- a/postgres/actions/upgrade +++ b/postgres/actions/upgrade @@ -101,6 +101,36 @@ postgres:container:version() { echo "$image_version" } +## XXXvlab: there's an issue with using ``docker stop ..``, then +## ``docker rmi ..`` on the stopped container's image. It can fail on +## some VPS (and on dev platform) saying the image is still in use. +docker:container:stop() { + local container=$1 timeout=${2:-60} + + ## Stop the container + if ! out=$(docker stop "$container"); then + err "Failed to stop container $container" + echo "$out" | prefix " | " >&2 + return 1 + fi + + local start=$SECONDS + + # Wait for the container to stop + while [ -n "$( + curl -s --unix-socket /var/run/docker.sock \ + http://localhost/containers/json?all=1 | + jq -r '.[] | .Id[:12] , .Id , ( .Names | .[] | ltrimstr("/"))' | grep ^"$container"$)" ]; do + if [ $(($SECONDS - start)) -ge $timeout ]; then + err "Timeout waiting for container '$container' to stop." >&2 + return 1 + fi + sleep 0.1 + done + return 0 +} + + postgres:container:psql () { local container="$1" docker exec -i "$container" psql 2>&1 @@ -290,9 +320,8 @@ while [[ "${#upgrade_path[@]}" != 0 ]]; do break fi - out=$(docker stop "$migration_container" 2>&1) || { - err "Failed to stop the dump container $migration_container for $current_image_version:" - printf "%s\n" "$out" | prefix " ${GRAY}|${NORMAL} " + docker:container:stop "$migration_container" || { + err "Failed to stop the dump container $migration_container for $current_image_version" break 3 } expected_stop="1" @@ -328,6 +357,8 @@ while [[ "${#upgrade_path[@]}" != 0 ]]; do out=$(docker rmi "$current_image" 2>&1) || { err "Failed to remove image $current_image" printf "%s\n" "$out" | prefix " ${GRAY}|${NORMAL} " + sleep 2 + docker start "$migration_container" >&2 break 2 } } @@ -390,9 +421,8 @@ while [[ "${#upgrade_path[@]}" != 0 ]]; do current_image="$next_image" current_image_version="$image_version" - out=$(docker stop "$migration_container" 2>&1) || { + docker:container:stop "$migration_container" || { err "Failed to stop the migration container $migration_container for $image_version:" - printf "%s\n" "$out" | prefix " ${GRAY}|${NORMAL} " break 3 } expected_stop="1"