Skip to content

Commit

Permalink
Move Postgres Replica command to separate file for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuscruz committed Dec 5, 2024
1 parent 9b97ef0 commit 326d1e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/prebuild_postgres_replica.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
paths:
- 'postgres_replica.dockerfile'
- 'docker/postgres_replica/cmd.sh'

jobs:
export_variables:
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docker/postgres_replica/cmd.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 3 additions & 11 deletions postgres_replica.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]

0 comments on commit 326d1e9

Please sign in to comment.