From 326d1e9250336cc380b2026131349da976749976 Mon Sep 17 00:00:00 2001 From: Matt Cruz Date: Thu, 5 Dec 2024 14:30:18 -0500 Subject: [PATCH] Move Postgres Replica command to separate file for better readability --- .github/workflows/prebuild_postgres_replica.yml | 9 ++++++++- .github/workflows/test.yml | 8 +++++++- docker/postgres_replica/cmd.sh | 9 +++++++++ postgres_replica.dockerfile | 14 +++----------- 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 docker/postgres_replica/cmd.sh diff --git a/.github/workflows/prebuild_postgres_replica.yml b/.github/workflows/prebuild_postgres_replica.yml index f907cf1..bf7169a 100644 --- a/.github/workflows/prebuild_postgres_replica.yml +++ b/.github/workflows/prebuild_postgres_replica.yml @@ -4,6 +4,7 @@ on: pull_request: paths: - 'postgres_replica.dockerfile' + - 'docker/postgres_replica/cmd.sh' jobs: export_variables: @@ -22,7 +23,13 @@ jobs: - name: Calculate SHA256 for postgres_replica.dockerfile id: calculate_replica_sha - run: echo "REPLICA_SHA=$(sha256sum postgres_replica.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT + run: | + checksum () { + sha256sum $@ | awk '{print substr($1, 1, 12)}' + } + REPLICA_DOCKERFILE_SHA=$(checksum postgres_replica.dockerfile) + REPLICA_CMD_SHA=$(checksum docker/postgres_replica/cmd.sh) + echo "REPLICA_SHA=$(echo \"${REPLICA_DOCKERFILE_SHA}-${REPLICA_CMD_SHA}\" | checksum)" >> $GITHUB_OUTPUT prebuild_replica: needs: export_variables diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41f69f3..24eab39 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,7 +30,13 @@ jobs: - name: Calculate SHA256 for postgres_replica.dockerfile id: calculate_replica_sha - run: echo "REPLICA_SHA=$(sha256sum postgres_replica.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT + run: | + checksum () { + sha256sum $@ | awk '{print substr($1, 1, 12)}' + } + REPLICA_DOCKERFILE_SHA=$(checksum postgres_replica.dockerfile) + REPLICA_CMD_SHA=$(checksum docker/postgres_replica/cmd.sh) + echo "REPLICA_SHA=$(echo \"${REPLICA_DOCKERFILE_SHA}-${REPLICA_CMD_SHA}\" | checksum)" >> $GITHUB_OUTPUT rubocop: runs-on: ubuntu-latest diff --git a/docker/postgres_replica/cmd.sh b/docker/postgres_replica/cmd.sh new file mode 100644 index 0000000..3861c16 --- /dev/null +++ b/docker/postgres_replica/cmd.sh @@ -0,0 +1,9 @@ +until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=$PRIMARY_REPLICATION_SLOT --host=$PRIMARY_DATABASE_HOST --port=$PRIMARY_DATABASE_PORT +do + echo 'Waiting for primary to connect...' + sleep 1s +done + +echo 'Backup done, starting replica...' +chmod 0700 /var/lib/postgresql/data +postgres diff --git a/postgres_replica.dockerfile b/postgres_replica.dockerfile index 7b6975e..b5c5135 100644 --- a/postgres_replica.dockerfile +++ b/postgres_replica.dockerfile @@ -4,20 +4,12 @@ ENV PRIMARY_DATABASE_HOST=localhost ENV PRIMARY_DATABASE_PORT=5432 ENV PRIMARY_REPLICATION_SLOT=replication_slot -USER root -RUN printf '' > cmd.sh -RUN echo 'until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=$PRIMARY_REPLICATION_SLOT --host=$PRIMARY_DATABASE_HOST --port=$PRIMARY_DATABASE_PORT' >> cmd.sh -RUN echo 'do' >> cmd.sh -RUN echo "echo 'Waiting for primary to connect...'" >> cmd.sh -RUN echo 'sleep 1s' >> cmd.sh -RUN echo 'done' >> cmd.sh -RUN echo "echo 'Backup done, starting replica...'" >> cmd.sh -RUN echo 'chmod 0700 /var/lib/postgresql/data' >> cmd.sh -RUN echo 'postgres' >> cmd.sh +COPY docker/postgres_replica/cmd.sh cmd.sh +USER root RUN chown -R postgres:postgres cmd.sh USER postgres -RUN chmod u+rwx cmd.sh +RUN chmod u+x cmd.sh CMD [ "./cmd.sh" ]