Browse Source

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.
rallly
Valentin Lab 5 months ago
parent
commit
be952660ce
  1. 40
      postgres/actions/upgrade

40
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"

Loading…
Cancel
Save