postgres-entrypoint.sh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # This script replaces the default docker entrypoint for postgres in the
  3. # development environment.
  4. # Its job is to ensure postgres is properly configured to support the
  5. # Change Data Capture pipeline (by setting access permissions and installing
  6. # the replication plugin we use for CDC). Unfortunately the default
  7. # Postgres image does not allow this level of configurability so we need
  8. # to do it this way in order not to have to publish and maintain our own
  9. # Postgres image.
  10. #
  11. # This then, at the end, transfers control to the default entrypoint.
  12. set -e
  13. cdc_setup_hba_conf() {
  14. # Ensure pg-hba is properly configured to allow connections
  15. # to the replication slots.
  16. PG_HBA="$PGDATA/pg_hba.conf"
  17. if [ ! -f "$PG_HBA" ]; then
  18. echo "DB not initialized. Postgres will take care of pg_hba"
  19. elif [ "$(grep -c -E "^host\s+replication" "$PGDATA"/pg_hba.conf)" != 0 ]; then
  20. echo "Replication config already present in pg_hba. Not changing anything."
  21. else
  22. # Execute the same script we run on DB initialization
  23. /docker-entrypoint-initdb.d/init_hba.sh
  24. fi
  25. }
  26. install_wal2json() {
  27. # Install the latest version of wal2json if it is not already
  28. # present in /wal2json
  29. # If we cannot download the latest version from github the following
  30. # attempts are made:
  31. # - see if there is a valid version on the volume. Use that
  32. # - see if for any reason there is already a version in the postgres
  33. # lib directory
  34. # If not it is a bad day. And this stops the process.
  35. set +e
  36. LATEST_VERSION_FILE="/wal2json/latest.so"
  37. ARCH=$(uname -m)
  38. FILE_NAME="wal2json-Linux-$ARCH-musl.so"
  39. LATEST_VERSION=$(
  40. wget "https://api.github.com/repos/getsentry/wal2json/releases/latest" -O - |
  41. grep '"tag_name":' |
  42. sed -E 's/.*"([^"]+)".*/\1/'
  43. )
  44. if [[ $LATEST_VERSION ]]; then
  45. if [ ! -f "/wal2json/$LATEST_VERSION/$FILE_NAME" ]; then
  46. mkdir -p "/wal2json/$LATEST_VERSION"
  47. if wget \
  48. "https://github.com/getsentry/wal2json/releases/download/$LATEST_VERSION/$FILE_NAME" \
  49. -P "/wal2json/$LATEST_VERSION/"; then
  50. ln -sf "/wal2json/$LATEST_VERSION/$FILE_NAME" "$LATEST_VERSION_FILE"
  51. fi
  52. fi
  53. ln -sf "$LATEST_VERSION_FILE" /usr/local/lib/postgresql/wal2json.so
  54. elif [ -f $LATEST_VERSION_FILE ]; then
  55. # We did not manage to detect the latest version or we failed at downloading.
  56. # Try to failover
  57. REAL_FILE=$(realpath $LATEST_VERSION_FILE)
  58. echo "Cannot download latest version. Found $REAL_FILE on disk"
  59. ln -sf "$LATEST_VERSION_FILE" /usr/local/lib/postgresql/wal2json.so
  60. elif [ -f "/usr/local/lib/postgresql/wal2json.so" ]; then
  61. # Somehow our volume is not in a good state but there is still a version
  62. # in the library directory. We take that one.
  63. echo "Cannot download latest version. Found a version on disk"
  64. else
  65. echo "wal2json is not installed and cannot download latest version"
  66. exit 1
  67. fi
  68. set -e
  69. echo "wal2json installed"
  70. }
  71. echo "Setting up Change Data Capture"
  72. if [ "$1" = 'postgres' ]; then
  73. cdc_setup_hba_conf
  74. install_wal2json
  75. fi
  76. exec /docker-entrypoint.sh "$@"