2018-04-04 10:18:17 -05:00
#!/bin/sh
2022-03-21 06:19:33 -05:00
# Fixup the passwd file, in case we're on OpenShift
2022-08-11 03:30:36 -05:00
if ! whoami > /dev/null 2>& 1; then
if [ " $( id -u) " -ne 5050 ] ; then
2022-03-21 06:19:33 -05:00
if [ -w /etc/passwd ] ; then
echo " ${ USER_NAME :- pgadminr } :x: $( id -u) :0: ${ USER_NAME :- pgadminr } user: ${ HOME } :/sbin/nologin " >> /etc/passwd
fi
fi
fi
2019-12-08 23:39:46 -06:00
# Populate config_distro.py. This has some default config, as well as anything
2019-08-22 09:24:04 -05:00
# provided by the user through the PGADMIN_CONFIG_* environment variables.
2019-12-08 23:39:46 -06:00
# Only update the file on first launch. The empty file is created during the
# container build so it can have the required ownership.
2022-08-11 03:30:36 -05:00
if [ " $( wc -m /pgadmin4/config_distro.py | awk '{ print $1 }' ) " = "0" ] ; then
2019-08-22 09:24:04 -05:00
cat << EOF > /pgadmin4/config_distro.py
2021-02-01 11:26:39 -06:00
CA_FILE = '/etc/ssl/certs/ca-certificates.crt'
2021-02-02 07:45:49 -06:00
LOG_FILE = '/dev/null'
2019-08-22 09:24:04 -05:00
HELP_PATH = '../../docs'
DEFAULT_BINARY_PATHS = {
2023-09-26 03:15:08 -05:00
'pg' : '/usr/local/pgsql-16' ,
'pg-16' : '/usr/local/pgsql-16' ,
2022-10-17 04:02:45 -05:00
'pg-15' : '/usr/local/pgsql-15' ,
2021-10-04 05:42:45 -05:00
'pg-14' : '/usr/local/pgsql-14' ,
2021-06-15 08:19:31 -05:00
'pg-13' : '/usr/local/pgsql-13' ,
2023-09-26 03:15:08 -05:00
'pg-12' : '/usr/local/pgsql-12'
2019-08-22 09:24:04 -05:00
}
EOF
# This is a bit kludgy, but necessary as the container uses BusyBox/ash as
# it's shell and not bash which would allow a much cleaner implementation
2023-09-04 02:36:15 -05:00
for var in $( env | grep "^PGADMIN_CONFIG_" | cut -d "=" -f 1) ; do
2022-08-11 03:30:36 -05:00
# shellcheck disable=SC2086
# shellcheck disable=SC2046
2019-08-22 09:24:04 -05:00
echo ${ var #PGADMIN_CONFIG_ } = $( eval " echo \$ $var " ) >> /pgadmin4/config_distro.py
done
fi
2018-04-04 10:18:17 -05:00
if [ ! -f /var/lib/pgadmin/pgadmin4.db ] ; then
2022-08-11 03:30:36 -05:00
if [ -z " ${ PGADMIN_DEFAULT_EMAIL } " ] || { [ -z " ${ PGADMIN_DEFAULT_PASSWORD } " ] && [ -z " ${ PGADMIN_DEFAULT_PASSWORD_FILE } " ] ; } ; then
2022-06-15 00:37:56 -05:00
echo 'You need to define the PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD or PGADMIN_DEFAULT_PASSWORD_FILE environment variables.'
2021-02-26 10:57:03 -06:00
exit 1
fi
2022-08-11 03:30:36 -05:00
if ! echo " ${ PGADMIN_DEFAULT_EMAIL } " | grep -E " ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,} $" > /dev/null; then
2021-02-26 10:57:03 -06:00
echo " ' ${ PGADMIN_DEFAULT_EMAIL } ' does not appear to be a valid email address. Please reset the PGADMIN_DEFAULT_EMAIL environment variable and try again. "
2018-04-04 10:18:17 -05:00
exit 1
fi
2022-06-15 00:37:56 -05:00
# Read secret contents
if [ -n " ${ PGADMIN_DEFAULT_PASSWORD_FILE } " ] ; then
2022-08-11 03:30:36 -05:00
PGADMIN_DEFAULT_PASSWORD = $( cat " ${ PGADMIN_DEFAULT_PASSWORD_FILE } " )
export PGADMIN_DEFAULT_PASSWORD
2022-06-15 00:37:56 -05:00
fi
2018-04-04 10:18:17 -05:00
# Set the default username and password in a
# backwards compatible way
2022-08-11 03:30:36 -05:00
export PGADMIN_SETUP_EMAIL = " ${ PGADMIN_DEFAULT_EMAIL } "
export PGADMIN_SETUP_PASSWORD = " ${ PGADMIN_DEFAULT_PASSWORD } "
2018-04-04 10:18:17 -05:00
# Initialize DB before starting Gunicorn
# Importing pgadmin4 (from this script) is enough
2021-02-09 07:12:19 -06:00
/venv/bin/python3 run_pgadmin.py
2018-12-05 11:16:46 -06:00
2022-08-11 03:30:36 -05:00
export PGADMIN_SERVER_JSON_FILE = " ${ PGADMIN_SERVER_JSON_FILE :- /pgadmin4/servers.json } "
2024-02-27 00:41:59 -06:00
export PGADMIN_PREFERENCES_JSON_FILE = " ${ PGADMIN_PREFERENCES_JSON_FILE :- /pgadmin4/preferences.json } "
2022-08-11 03:30:36 -05:00
2018-12-05 11:16:46 -06:00
# Pre-load any required servers
2019-06-27 09:56:37 -05:00
if [ -f " ${ PGADMIN_SERVER_JSON_FILE } " ] ; then
2019-12-17 01:45:04 -06:00
# When running in Desktop mode, no user is created
# so we have to import servers anonymously
if [ " ${ PGADMIN_CONFIG_SERVER_MODE } " = "False" ] ; then
2023-12-21 00:37:26 -06:00
/venv/bin/python3 /pgadmin4/setup.py load-servers " ${ PGADMIN_SERVER_JSON_FILE } "
2019-12-17 01:45:04 -06:00
else
2023-12-21 00:37:26 -06:00
/venv/bin/python3 /pgadmin4/setup.py load-servers " ${ PGADMIN_SERVER_JSON_FILE } " --user " ${ PGADMIN_DEFAULT_EMAIL } "
2019-12-17 01:45:04 -06:00
fi
2018-12-05 11:16:46 -06:00
fi
2024-02-15 03:28:31 -06:00
if [ -f " ${ PGADMIN_PREFERENCES_JSON_FILE } " ] ; then
# When running in Desktop mode, no user is created
# so we have to import servers anonymously
if [ " ${ PGADMIN_CONFIG_SERVER_MODE } " = "False" ] ; then
DESKTOP_USER = $( cd /pgadmin4 && /venv/bin/python3 -c 'import config; print(config.DESKTOP_USER)' )
/venv/bin/python3 /pgadmin4/setup.py set-prefs " ${ DESKTOP_USER } " --input-file " ${ PGADMIN_PREFERENCES_JSON_FILE } "
else
/venv/bin/python3 /pgadmin4/setup.py set-prefs " ${ PGADMIN_DEFAULT_EMAIL } " --input-file " ${ PGADMIN_PREFERENCES_JSON_FILE } "
fi
fi
2018-04-04 10:18:17 -05:00
fi
2018-12-05 08:44:23 -06:00
# Start Postfix to handle password resets etc.
2022-08-11 03:30:36 -05:00
if [ -z " ${ PGADMIN_DISABLE_POSTFIX } " ] ; then
2021-01-20 05:50:05 -06:00
sudo /usr/sbin/postfix start
fi
2018-12-05 08:44:23 -06:00
2019-03-01 05:55:17 -06:00
# Get the session timeout from the pgAdmin config. We'll use this (in seconds)
# to define the Gunicorn worker timeout
2021-02-09 07:12:19 -06:00
TIMEOUT = $( cd /pgadmin4 && /venv/bin/python3 -c 'import config; print(config.SESSION_EXPIRATION_TIME * 60 * 60 * 24)' )
2019-03-01 05:55:17 -06:00
2018-04-04 10:18:17 -05:00
# NOTE: currently pgadmin can run only with 1 worker due to sessions implementation
# Using --threads to have multi-threaded single-process worker
2024-03-26 07:52:29 -05:00
if [ -n " ${ PGADMIN_ENABLE_SOCK } " ] ; then
BIND_ADDRESS = "unix:/run/pgadmin/pgadmin.sock"
else
if [ -n " ${ PGADMIN_ENABLE_TLS } " ] ; then
BIND_ADDRESS = " ${ PGADMIN_LISTEN_ADDRESS :- [ : : ] } : ${ PGADMIN_LISTEN_PORT :- 443 } "
else
BIND_ADDRESS = " ${ PGADMIN_LISTEN_ADDRESS :- [ : : ] } : ${ PGADMIN_LISTEN_PORT :- 80 } "
fi
fi
2022-08-11 03:30:36 -05:00
if [ -n " ${ PGADMIN_ENABLE_TLS } " ] ; then
2024-03-26 07:52:29 -05:00
exec /venv/bin/gunicorn --limit-request-line " ${ GUNICORN_LIMIT_REQUEST_LINE :- 8190 } " --timeout " ${ TIMEOUT } " --bind " ${ BIND_ADDRESS } " -w 1 --threads " ${ GUNICORN_THREADS :- 25 } " --access-logfile " ${ GUNICORN_ACCESS_LOGFILE :- - } " --keyfile /certs/server.key --certfile /certs/server.cert -c gunicorn_config.py run_pgadmin:app
2018-04-04 10:18:17 -05:00
else
2024-03-26 07:52:29 -05:00
exec /venv/bin/gunicorn --limit-request-line " ${ GUNICORN_LIMIT_REQUEST_LINE :- 8190 } " --timeout " ${ TIMEOUT } " --bind " ${ BIND_ADDRESS } " -w 1 --threads " ${ GUNICORN_THREADS :- 25 } " --access-logfile " ${ GUNICORN_ACCESS_LOGFILE :- - } " -c gunicorn_config.py run_pgadmin:app
2018-04-04 10:18:17 -05:00
fi