Skip to content

Commit 2d10e95

Browse files
authored
Add an option to load/replace the servers.json file on each container startup. #8540
1 parent f9af745 commit 2d10e95

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

docs/en_US/container_deployment.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ Override the default file path for the server definition list. See the
134134
/pgadmin4/servers.json mapped file below for more information. See the format
135135
of the `Servers JSON file <https://www.pgadmin.org/docs/pgadmin4/latest/import_export_servers.html#json-format>`_.
136136

137+
**PGADMIN_REPLACE_SERVERS_ON_STARTUP**
138+
139+
*Default: null*
140+
141+
By default, the server definitions are only loaded on first launch, i.e. when initializing the configuration database. See PGADMIN_SERVER_JSON_FILE above.
142+
143+
If this variable is set to True, the server definitions will be replaced on every launch. This can be used to declaratively manage the server definitions.
144+
137145
**PGADMIN_PREFERENCES_JSON_FILE**
138146

139147
*Default: /pgadmin4/preferences.json*

pkg/docker/entrypoint.sh

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ if [ -n "${PGADMIN_CONFIG_CONFIG_DATABASE_URI}" ]; then
7272
external_config_db_exists=$(cd /pgadmin4/pgadmin/utils && /venv/bin/python3 -c "from check_external_config_db import check_external_config_db; val = check_external_config_db("${PGADMIN_CONFIG_CONFIG_DATABASE_URI}"); print(val)")
7373
fi
7474

75+
# DRY of the code to load the PGADMIN_SERVER_JSON_FILE
76+
function load_server_json_file() {
77+
export PGADMIN_SERVER_JSON_FILE="${PGADMIN_SERVER_JSON_FILE:-/pgadmin4/servers.json}"
78+
79+
EXTRA_ARGS=""
80+
81+
if [ "${PGADMIN_REPLACE_SERVERS_ON_STARTUP}" = "True" ]; then
82+
EXTRA_ARGS="--replace"
83+
fi
84+
85+
if [ -f "${PGADMIN_SERVER_JSON_FILE}" ]; then
86+
# When running in Desktop mode, no user is created
87+
# so we have to import servers anonymously
88+
if [ "${PGADMIN_CONFIG_SERVER_MODE}" = "False" ]; then
89+
/venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" ${EXTRA_ARGS}
90+
else
91+
/venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" --user "${PGADMIN_DEFAULT_EMAIL}" ${EXTRA_ARGS}
92+
fi
93+
fi
94+
}
95+
7596
if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = "False" ]; then
7697
if [ -z "${PGADMIN_DEFAULT_EMAIL}" ] || { [ -z "${PGADMIN_DEFAULT_PASSWORD}" ] && [ -z "${PGADMIN_DEFAULT_PASSWORD_FILE}" ]; }; then
7798
echo 'You need to define the PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD or PGADMIN_DEFAULT_PASSWORD_FILE environment variables.'
@@ -111,19 +132,11 @@ if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = "
111132
# Importing pgadmin4 (from this script) is enough
112133
/venv/bin/python3 run_pgadmin.py
113134

114-
export PGADMIN_SERVER_JSON_FILE="${PGADMIN_SERVER_JSON_FILE:-/pgadmin4/servers.json}"
115135
export PGADMIN_PREFERENCES_JSON_FILE="${PGADMIN_PREFERENCES_JSON_FILE:-/pgadmin4/preferences.json}"
116136

117137
# Pre-load any required servers
118-
if [ -f "${PGADMIN_SERVER_JSON_FILE}" ]; then
119-
# When running in Desktop mode, no user is created
120-
# so we have to import servers anonymously
121-
if [ "${PGADMIN_CONFIG_SERVER_MODE}" = "False" ]; then
122-
/venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}"
123-
else
124-
/venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" --user "${PGADMIN_DEFAULT_EMAIL}"
125-
fi
126-
fi
138+
load_server_json_file
139+
127140
# Pre-load any required preferences
128141
if [ -f "${PGADMIN_PREFERENCES_JSON_FILE}" ]; then
129142
if [ "${PGADMIN_CONFIG_SERVER_MODE}" = "False" ]; then
@@ -145,7 +158,9 @@ if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = "
145158
chmod 600 /var/lib/pgadmin/storage/${PGADMIN_USER_CONFIG_DIR}/.pgpass
146159
fi
147160
fi
148-
161+
# If already initialised and PGADMIN_REPLACE_SERVERS_ON_STARTUP is set to true, then load the server json file.
162+
elif [ "${PGADMIN_REPLACE_SERVERS_ON_STARTUP}" = "True" ]; then
163+
load_server_json_file
149164
fi
150165

151166
# Start Postfix to handle password resets etc.

0 commit comments

Comments
 (0)