|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -eu |
3 | 3 |
|
| 4 | +DATABASE_URL="postgresql://${CS_DATABASE__USERNAME}:${CS_DATABASE__PASSWORD}@${CS_DATABASE__HOST}:${CS_DATABASE__PORT}/${CS_DATABASE__NAME}" |
| 5 | + |
| 6 | +postgres_ready () { |
| 7 | + psql ${DATABASE_URL} -c "SELECT 1" > /dev/null 2>&1 |
| 8 | +} |
| 9 | + |
| 10 | +wait_for_postgres_or_exit() { |
| 11 | + host=${CS_DATABASE__HOST} |
| 12 | + port=${CS_DATABASE__PORT} |
| 13 | + max_retries=20 |
| 14 | + interval=0.5 |
| 15 | + attempt=1 |
| 16 | + echo "Testing presence of PostgreSQL at ${host}:${port} with a maximum of ${max_retries} retries" |
| 17 | + |
| 18 | + until postgres_ready |
| 19 | + do |
| 20 | + if [ $attempt -lt $max_retries ]; then |
| 21 | + echo "Waiting for ${host}:${port}" |
| 22 | + sleep $interval |
| 23 | + attempt=$(expr $attempt + 1) |
| 24 | + else |
| 25 | + echo "Unable to connect to ${host}:${port} after ${max_retries} attempts" |
| 26 | + exit 64 |
| 27 | + fi |
| 28 | + done |
| 29 | + echo "Connected to ${host}:${port} after ${attempt} attempts" |
| 30 | +} |
| 31 | + |
4 | 32 | : "${CS_DATABASE__AWS_BUNDLE_PATH:=./aws-rds-global-bundle.pem}" |
5 | 33 |
|
6 | 34 | # Optionally pull in the AWS RDS global certificate bundle. This is required |
@@ -30,4 +58,31 @@ case "${CS_DATABASE__INSTALL_AWS_RDS_CERT_BUNDLE:-}" in |
30 | 58 | ;; |
31 | 59 | esac |
32 | 60 |
|
| 61 | +# Optionally install EQL in the target database |
| 62 | +case "${CS_DATABASE__INSTALL_EQL:-}" in |
| 63 | + "true") ;& |
| 64 | + "yes") ;& |
| 65 | + "1") |
| 66 | + >&2 echo "Installing EQL in target PostgreSQL database..." |
| 67 | + |
| 68 | + if [ ! -f "/opt/cipherstash-eql.sql" ]; then |
| 69 | + >&2 echo "error: unable to find EQL installer at: /opt/cipherstash-eql.sql" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + |
| 73 | + # Wait for postgres to become available |
| 74 | + wait_for_postgres_or_exit |
| 75 | + |
| 76 | + # Attempt to install EQL |
| 77 | + psql --file=/opt/cipherstash-eql.sql --quiet $DATABASE_URL > /dev/null 2>&1 |
| 78 | + if [ $? != 0 ]; then |
| 79 | + >&2 echo "error: unable to install EQL in target PostgreSQL database!" |
| 80 | + exit 2 |
| 81 | + fi |
| 82 | + ;; |
| 83 | + *) |
| 84 | + >&2 echo "Not installing EQL in target PostgreSQL database." |
| 85 | + ;; |
| 86 | +esac |
| 87 | + |
33 | 88 | exec cipherstash-proxy "$@" |
0 commit comments