From 2d10e95311544fee6d5e5e5efc457979bd94069e Mon Sep 17 00:00:00 2001 From: Lau Ernebjerg Josefsen <44977457+LauJosefsen@users.noreply.github.com> Date: Tue, 11 Mar 2025 11:44:06 +0100 Subject: [PATCH] Add an option to load/replace the servers.json file on each container startup. #8540 --- docs/en_US/container_deployment.rst | 8 +++++++ pkg/docker/entrypoint.sh | 37 ++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/docs/en_US/container_deployment.rst b/docs/en_US/container_deployment.rst index c241d26ae..369ffa5c9 100644 --- a/docs/en_US/container_deployment.rst +++ b/docs/en_US/container_deployment.rst @@ -134,6 +134,14 @@ Override the default file path for the server definition list. See the /pgadmin4/servers.json mapped file below for more information. See the format of the `Servers JSON file `_. +**PGADMIN_REPLACE_SERVERS_ON_STARTUP** + +*Default: null* + +By default, the server definitions are only loaded on first launch, i.e. when initializing the configuration database. See PGADMIN_SERVER_JSON_FILE above. + +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. + **PGADMIN_PREFERENCES_JSON_FILE** *Default: /pgadmin4/preferences.json* diff --git a/pkg/docker/entrypoint.sh b/pkg/docker/entrypoint.sh index da3105afa..631ae5a1e 100755 --- a/pkg/docker/entrypoint.sh +++ b/pkg/docker/entrypoint.sh @@ -72,6 +72,27 @@ if [ -n "${PGADMIN_CONFIG_CONFIG_DATABASE_URI}" ]; then 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)") fi +# DRY of the code to load the PGADMIN_SERVER_JSON_FILE +function load_server_json_file() { + export PGADMIN_SERVER_JSON_FILE="${PGADMIN_SERVER_JSON_FILE:-/pgadmin4/servers.json}" + + EXTRA_ARGS="" + + if [ "${PGADMIN_REPLACE_SERVERS_ON_STARTUP}" = "True" ]; then + EXTRA_ARGS="--replace" + fi + + if [ -f "${PGADMIN_SERVER_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 + /venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" ${EXTRA_ARGS} + else + /venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" --user "${PGADMIN_DEFAULT_EMAIL}" ${EXTRA_ARGS} + fi + fi +} + if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = "False" ]; then if [ -z "${PGADMIN_DEFAULT_EMAIL}" ] || { [ -z "${PGADMIN_DEFAULT_PASSWORD}" ] && [ -z "${PGADMIN_DEFAULT_PASSWORD_FILE}" ]; }; then 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}" = " # Importing pgadmin4 (from this script) is enough /venv/bin/python3 run_pgadmin.py - export PGADMIN_SERVER_JSON_FILE="${PGADMIN_SERVER_JSON_FILE:-/pgadmin4/servers.json}" export PGADMIN_PREFERENCES_JSON_FILE="${PGADMIN_PREFERENCES_JSON_FILE:-/pgadmin4/preferences.json}" # Pre-load any required servers - if [ -f "${PGADMIN_SERVER_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 - /venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" - else - /venv/bin/python3 /pgadmin4/setup.py load-servers "${PGADMIN_SERVER_JSON_FILE}" --user "${PGADMIN_DEFAULT_EMAIL}" - fi - fi + load_server_json_file + # Pre-load any required preferences if [ -f "${PGADMIN_PREFERENCES_JSON_FILE}" ]; then if [ "${PGADMIN_CONFIG_SERVER_MODE}" = "False" ]; then @@ -145,7 +158,9 @@ if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = " chmod 600 /var/lib/pgadmin/storage/${PGADMIN_USER_CONFIG_DIR}/.pgpass fi fi - +# If already initialised and PGADMIN_REPLACE_SERVERS_ON_STARTUP is set to true, then load the server json file. +elif [ "${PGADMIN_REPLACE_SERVERS_ON_STARTUP}" = "True" ]; then + load_server_json_file fi # Start Postfix to handle password resets etc.