-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwrapper.sh
55 lines (46 loc) · 1.87 KB
/
wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# exit as soon as any of these commands fail, this prevents starting a database without certificates
set -e
# Make sure there is a PGDATA variable available
if [ -z "$PGDATA" ]; then
echo "Missing PGDATA variable"
exit 1
fi
# Set up needed variables
SSL_DIR="/var/lib/postgresql/data/certs"
INIT_SSL_SCRIPT="/docker-entrypoint-initdb.d/init-ssl.sh"
POSTGRES_CONF_FILE="$PGDATA/postgresql.conf"
# Regenerate if the certificate is not a x509v3 certificate
if [ -f "$SSL_DIR/server.crt" ] && ! openssl x509 -noout -text -in "$SSL_DIR/server.crt" | grep -q "DNS:localhost"; then
echo "Did not find a x509v3 certificate, regenerating certificates..."
bash "$INIT_SSL_SCRIPT"
fi
# Regenerate if the certificate has expired or will expire
# 2592000 seconds = 30 days
if [ -f "$SSL_DIR/server.crt" ] && ! openssl x509 -checkend 2592000 -noout -in "$SSL_DIR/server.crt"; then
echo "Certificate has or will expire soon, regenerating certificates..."
bash "$INIT_SSL_SCRIPT"
fi
# Generate a certificate if the database was initialized but is missing a certificate
# Useful when going from the base postgres image to this ssl image
if [ -f "$POSTGRES_CONF_FILE" ] && [ ! -f "$SSL_DIR/server.crt" ]; then
echo "Database initialized without certificate, generating certificates..."
bash "$INIT_SSL_SCRIPT"
fi
# unset PGHOST to force psql to use Unix socket path
# this is specific to Railway and allows
# us to use PGHOST after the init
unset PGHOST
## unset PGPORT also specific to Railway
## since postgres checks for validity of
## the value in PGPORT we unset it in case
## it ends up being empty
unset PGPORT
# Call the entrypoint script with the
# appropriate PGHOST & PGPORT and redirect
# the output to stdout if LOG_TO_STDOUT is true
if [[ "$LOG_TO_STDOUT" == "true" ]]; then
/usr/local/bin/docker-entrypoint.sh "$@" 2>&1
else
/usr/local/bin/docker-entrypoint.sh "$@"
fi