From 1882d54625d6a9a6332e63e5645e3495273914cd Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 21 Oct 2021 12:31:18 +0900 Subject: [PATCH] Add our own image of postgres for migrations --- Postgres/13.4/linuxamd64.Dockerfile | 16 +++++ Postgres/13.4/linuxarm32v7.Dockerfile | 23 +++++++ Postgres/13.4/linuxarm64v8.Dockerfile | 16 +++++ Postgres/13.4/migrate-docker-entrypoint.sh | 76 ++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 Postgres/13.4/linuxamd64.Dockerfile create mode 100644 Postgres/13.4/linuxarm32v7.Dockerfile create mode 100644 Postgres/13.4/linuxarm64v8.Dockerfile create mode 100755 Postgres/13.4/migrate-docker-entrypoint.sh diff --git a/Postgres/13.4/linuxamd64.Dockerfile b/Postgres/13.4/linuxamd64.Dockerfile new file mode 100644 index 0000000..b4da07a --- /dev/null +++ b/Postgres/13.4/linuxamd64.Dockerfile @@ -0,0 +1,16 @@ +FROM postgres:13.4 + +ENV PREVIOUS_VERSION 9.6 +RUN cp /etc/apt/sources.list.d/pgdg.list /etc/apt/sources.list.d/pgdg.list.backup && \ + sed -i "s/$/ $PREVIOUS_VERSION/" /etc/apt/sources.list.d/pgdg.list && \ + apt-get update && \ + apt-get install -y --no-install-recommends \ + postgresql-$PREVIOUS_VERSION \ + postgresql-contrib-$PREVIOUS_VERSION && \ + rm /etc/apt/sources.list.d/pgdg.list && mv /etc/apt/sources.list.d/pgdg.list.backup /etc/apt/sources.list.d/pgdg.list && \ + rm -rf /var/lib/apt/lists/* + +COPY migrate-docker-entrypoint.sh /migrate-docker-entrypoint.sh + +ENTRYPOINT ["/migrate-docker-entrypoint.sh"] +CMD ["postgres"] \ No newline at end of file diff --git a/Postgres/13.4/linuxarm32v7.Dockerfile b/Postgres/13.4/linuxarm32v7.Dockerfile new file mode 100644 index 0000000..475224b --- /dev/null +++ b/Postgres/13.4/linuxarm32v7.Dockerfile @@ -0,0 +1,23 @@ +FROM --platform=arm postgres:13.4 + +# Postgres doesn't ship packages for 9.6 +ENV PREVIOUS_VERSION 9.6 +RUN FALLBACK="http://aois.blob.core.windows.net/public/$PREVIOUS_VERSION-$(uname -m).tar.gz" && \ + FALLBACK_SHARE="http://aois.blob.core.windows.net/public/share-$PREVIOUS_VERSION-$(uname -m).tar.gz" && \ + apt-get update && apt-get install --no-install-recommends -y wget && \ + rm -rf /var/lib/apt/lists/* && \ + cd /usr/lib/postgresql && \ + wget $FALLBACK && \ + echo "50a98f90ad9c61d0b5b5ccb9c984ca48bd6e6f331ed17eb87086a2c4290c75c6 9.6-armv7l.tar.gz" | sha256sum -c - && \ + tar -xvf *.tar.gz && \ + rm -f *.tar.gz && \ + cd /usr/share/postgresql && \ + wget $FALLBACK_SHARE && \ + echo "10c8c66d97fcb1cd9b22334118ba5afd495147c94f3a03409ca98da54643b433 share-9.6-armv7l.tar.gz" | sha256sum -c - && \ + tar -xvf *.tar.gz && \ + rm -f *.tar.gz + +COPY migrate-docker-entrypoint.sh /migrate-docker-entrypoint.sh + +ENTRYPOINT ["/migrate-docker-entrypoint.sh"] +CMD ["postgres"] \ No newline at end of file diff --git a/Postgres/13.4/linuxarm64v8.Dockerfile b/Postgres/13.4/linuxarm64v8.Dockerfile new file mode 100644 index 0000000..093f394 --- /dev/null +++ b/Postgres/13.4/linuxarm64v8.Dockerfile @@ -0,0 +1,16 @@ +FROM --platform=arm64 postgres:13.4 + +ENV PREVIOUS_VERSION 9.6 +RUN cp /etc/apt/sources.list.d/pgdg.list /etc/apt/sources.list.d/pgdg.list.backup && \ + sed -i "s/$/ $PREVIOUS_VERSION/" /etc/apt/sources.list.d/pgdg.list && \ + apt-get update && \ + apt-get install -y --no-install-recommends \ + postgresql-$PREVIOUS_VERSION \ + postgresql-contrib-$PREVIOUS_VERSION && \ + rm /etc/apt/sources.list.d/pgdg.list && mv /etc/apt/sources.list.d/pgdg.list.backup /etc/apt/sources.list.d/pgdg.list && \ + rm -rf /var/lib/apt/lists/* + +COPY migrate-docker-entrypoint.sh /migrate-docker-entrypoint.sh + +ENTRYPOINT ["/migrate-docker-entrypoint.sh"] +CMD ["postgres"] \ No newline at end of file diff --git a/Postgres/13.4/migrate-docker-entrypoint.sh b/Postgres/13.4/migrate-docker-entrypoint.sh new file mode 100755 index 0000000..6abe1a3 --- /dev/null +++ b/Postgres/13.4/migrate-docker-entrypoint.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +set -Eeo pipefail +shopt -s extglob + +CURRENT_PGVERSION="" +EXPECTED_PGVERSION="$PG_MAJOR" +if [[ -f "/var/lib/postgresql/data/PG_VERSION" ]]; then + CURRENT_PGVERSION="$(cat /var/lib/postgresql/data/PG_VERSION)" +fi + +if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \ + [[ "$CURRENT_PGVERSION" != "" ]]; then + + if ! [ -f "/usr/lib/postgresql/$CURRENT_PGVERSION/bin/pg_upgrade" ]; then + sed -i "s/$/ $CURRENT_PGVERSION/" /etc/apt/sources.list.d/pgdg.list + if ! apt-get update; then + echo "apt-get update failed. Are you using raspberry pi 4? If yes, please follow https://blog.samcater.com/fix-workaround-rpi4-docker-libseccomp2-docker-20/" + exit 1 + fi + if ! apt-get install -y --no-install-recommends \ + postgresql-$CURRENT_PGVERSION \ + postgresql-contrib-$CURRENT_PGVERSION; then + # On arm32, postgres doesn't ship those packages, so we download + # the binaries from an archive we built from the postgres 9.6.20 image's binaries + FALLBACK="https://aois.blob.core.windows.net/public/$CURRENT_PGVERSION-$(uname -m).tar.gz" + FALLBACK_SHARE="https://aois.blob.core.windows.net/public/share-$CURRENT_PGVERSION-$(uname -m).tar.gz" + echo "Failure to install postgresql-$CURRENT_PGVERSION and postgresql-contrib-$CURRENT_PGVERSION trying fallback $FALLBACK" + apt-get install -y wget + pushd . > /dev/null + cd /usr/lib/postgresql + wget $FALLBACK + tar -xvf *.tar.gz + rm -f *.tar.gz + cd /usr/share/postgresql + wget $FALLBACK_SHARE + tar -xvf *.tar.gz + rm -f *.tar.gz + popd > /dev/null + echo "Successfully installed PG utilities via the fallback" + fi + else + echo "Migration binaries already present on the image" + fi + + export PGBINOLD="/usr/lib/postgresql/$CURRENT_PGVERSION/bin" + export PGDATABASE="/var/lib/postgresql/data" + export PGDATAOLD="/var/lib/postgresql/data/$CURRENT_PGVERSION" + export PGDATANEW="/var/lib/postgresql/data/$EXPECTED_PGVERSION" + + mkdir -p "$PGDATANEW" "$PGDATAOLD" + find "$PGDATABASE" -maxdepth 1 -mindepth 1 \ + -not -wholename "$PGDATAOLD" \ + -not -wholename "$PGDATANEW" \ + -exec mv {} "$PGDATAOLD/" \; + + chmod 700 "$PGDATAOLD" "$PGDATANEW" + chown postgres . + chown -R postgres "$PGDATAOLD" "$PGDATANEW" "$PGDATABASE" + if [ ! -s "$PGDATANEW/PG_VERSION" ]; then + PGDATA="$PGDATANEW" eval "gosu postgres initdb $POSTGRES_INITDB_ARGS" + fi + + gosu postgres pg_upgrade + rm $PGDATANEW/*.conf + mv $PGDATANEW/* "$PGDATABASE" + mv $PGDATAOLD/*.conf "$PGDATABASE" + rm -r "$PGDATANEW" + ./delete_old_cluster.sh + rm ./analyze_new_cluster.sh +fi + +if [ -f "docker-entrypoint.sh" ]; then + exec ./docker-entrypoint.sh "$@" +else + exec docker-entrypoint.sh "$@" +fi \ No newline at end of file