Skip to content

Commit f9b3359

Browse files
committed
incorporated the changes of forked repo
1 parent 9947034 commit f9b3359

File tree

2 files changed

+127
-15
lines changed

2 files changed

+127
-15
lines changed

registry/AJ0070/modules/pgadmin/main.tf

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,36 @@ variable "subdomain" {
2323
default = true
2424
}
2525

26+
variable "config" {
27+
type = any
28+
description = "A map of pgAdmin configuration settings."
29+
default = {
30+
DEFAULT_EMAIL = "[email protected]"
31+
DEFAULT_PASSWORD = "coderPASSWORD"
32+
SERVER_MODE = false
33+
MASTER_PASSWORD_REQUIRED = false
34+
LISTEN_ADDRESS = "127.0.0.1"
35+
}
36+
}
37+
2638
data "coder_workspace" "me" {}
39+
data "coder_workspace_owner" "me" {}
2740

2841
resource "coder_app" "pgadmin" {
2942
count = data.coder_workspace.me.start_count
3043
agent_id = var.agent_id
3144
display_name = "pgAdmin"
3245
slug = "pgadmin"
3346
icon = "/icon/postgres.svg"
34-
url = var.subdomain ? "https://pgadmin-${data.coder_workspace.me.id}.${data.coder_workspace.me.access_url}" : "http://localhost:${var.port}"
47+
url = local.url
48+
subdomain = var.subdomain
3549
share = "owner"
50+
51+
healthcheck {
52+
url = local.healthcheck_url
53+
interval = 5
54+
threshold = 6
55+
}
3656
}
3757

3858
resource "coder_script" "pgadmin" {
@@ -41,7 +61,48 @@ resource "coder_script" "pgadmin" {
4161
icon = "/icon/postgres.svg"
4262
run_on_start = true
4363
script = templatefile("${path.module}/run.sh", {
44-
PORT = var.port,
45-
LOG_PATH = "/tmp/pgadmin.log"
64+
PORT = var.port,
65+
LOG_PATH = "/tmp/pgadmin.log",
66+
SERVER_BASE_PATH = local.server_base_path,
67+
CONFIG = local.config_content,
68+
PGADMIN_DATA_DIR = local.pgadmin_data_dir,
69+
PGADMIN_LOG_DIR = local.pgadmin_log_dir,
70+
PGADMIN_VENV_DIR = local.pgadmin_venv_dir
4671
})
72+
}
73+
74+
locals {
75+
server_base_path = var.subdomain ? "" : format("/@%s/%s/apps/%s", data.coder_workspace_owner.me.name, data.coder_workspace.me.name, "pgadmin")
76+
url = "http://localhost:${var.port}${local.server_base_path}"
77+
healthcheck_url = "http://localhost:${var.port}${local.server_base_path}/"
78+
79+
# pgAdmin data directories (user-local paths)
80+
pgadmin_data_dir = "$HOME/.pgadmin"
81+
pgadmin_log_dir = "$HOME/.pgadmin/logs"
82+
pgadmin_venv_dir = "$HOME/.pgadmin/venv"
83+
84+
base_config = merge(var.config, {
85+
LISTEN_PORT = var.port
86+
# Override paths for user installation
87+
DATA_DIR = local.pgadmin_data_dir
88+
LOG_FILE = "${local.pgadmin_log_dir}/pgadmin4.log"
89+
SQLITE_PATH = "${local.pgadmin_data_dir}/pgadmin4.db"
90+
SESSION_DB_PATH = "${local.pgadmin_data_dir}/sessions"
91+
STORAGE_DIR = "${local.pgadmin_data_dir}/storage"
92+
# Disable initial setup prompts for automated deployment
93+
SETUP_AUTH = false
94+
})
95+
96+
config_with_path = var.subdomain ? local.base_config : merge(local.base_config, {
97+
APPLICATION_ROOT = local.server_base_path
98+
})
99+
100+
config_content = join("\n", [
101+
for key, value in local.config_with_path :
102+
format("%s = %s", key,
103+
can(regex("^(true|false)$", tostring(value))) ? (value ? "True" : "False") :
104+
can(tonumber(value)) ? tostring(value) :
105+
format("'%s'", tostring(value))
106+
)
107+
])
47108
}
Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,76 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
24

35
PORT=${PORT}
46
LOG_PATH=${LOG_PATH}
7+
SERVER_BASE_PATH=${SERVER_BASE_PATH}
58

69
BOLD='\033[0;1m'
710

811
printf "$${BOLD}Installing pgAdmin!\n"
912

10-
if ! command -v pip > /dev/null 2>&1; then
11-
echo "pip is not installed"
12-
echo "Please install pip in your Dockerfile/VM image before using this module"
13-
exit 1
13+
# Check if Python 3 is available
14+
if ! command -v python3 > /dev/null 2>&1; then
15+
echo "⚠️ Warning: Python 3 is not installed. Please install Python 3 before using this module."
16+
exit 0
1417
fi
1518

16-
if ! command -v pgadmin4 > /dev/null 2>&1; then
17-
pip install pgadmin4-web
18-
echo "pgAdmin has been installed\n\n"
19+
# Setup pgAdmin directories (from Terraform configuration)
20+
PGADMIN_DATA_DIR="${PGADMIN_DATA_DIR}"
21+
PGADMIN_LOG_DIR="${PGADMIN_LOG_DIR}"
22+
PGADMIN_VENV_DIR="${PGADMIN_VENV_DIR}"
23+
24+
printf "Setting up pgAdmin directories...\n"
25+
mkdir -p "$PGADMIN_DATA_DIR"
26+
mkdir -p "$PGADMIN_LOG_DIR"
27+
28+
# Check if pgAdmin virtual environment already exists and is working
29+
if [ -f "$PGADMIN_VENV_DIR/bin/pgadmin4" ] && [ -f "$PGADMIN_VENV_DIR/bin/activate" ]; then
30+
printf "🥳 pgAdmin virtual environment already exists\n\n"
1931
else
20-
echo "pgAdmin is already installed\n\n"
32+
printf "Creating Python virtual environment for pgAdmin...\n"
33+
if ! python3 -m venv "$PGADMIN_VENV_DIR"; then
34+
echo "⚠️ Warning: Failed to create virtual environment"
35+
exit 0
36+
fi
37+
38+
printf "Installing pgAdmin 4 in virtual environment...\n"
39+
if ! "$PGADMIN_VENV_DIR/bin/pip" install pgadmin4; then
40+
echo "⚠️ Warning: Failed to install pgAdmin4"
41+
exit 0
42+
fi
43+
44+
printf "🥳 pgAdmin has been installed successfully\n\n"
2145
fi
2246

23-
echo "Starting pgAdmin in background..."
24-
echo "check logs at $${LOG_PATH}"
25-
pgadmin4 > $${LOG_PATH} 2>&1 &
47+
printf "$${BOLD}Configuring pgAdmin...\n"
48+
49+
if [ -f "$PGADMIN_VENV_DIR/bin/pgadmin4" ]; then
50+
# pgAdmin installs to a predictable location in the virtual environment
51+
PYTHON_VERSION=$("$PGADMIN_VENV_DIR/bin/python" -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
52+
PGADMIN_INSTALL_DIR="$PGADMIN_VENV_DIR/lib/python$PYTHON_VERSION/site-packages/pgadmin4"
53+
54+
# Create pgAdmin config file in the correct location (next to config.py)
55+
cat > "$PGADMIN_INSTALL_DIR/config_local.py" << EOF
56+
# pgAdmin configuration for Coder workspace
57+
${CONFIG}
58+
EOF
59+
60+
printf "📄 Config written to $PGADMIN_INSTALL_DIR/config_local.py\n"
61+
62+
printf "$${BOLD}Starting pgAdmin in background...\n"
63+
printf "📝 Check logs at $${LOG_PATH}\n"
64+
printf "🌐 Serving at http://localhost:${PORT}${SERVER_BASE_PATH}\n"
65+
66+
# Create required directories
67+
mkdir -p "$PGADMIN_DATA_DIR/sessions"
68+
mkdir -p "$PGADMIN_DATA_DIR/storage"
69+
70+
# Start pgadmin4 from the virtual environment with proper environment
71+
cd "$PGADMIN_DATA_DIR"
72+
PYTHONPATH="$PGADMIN_INSTALL_DIR:$${PYTHONPATH:-}" "$PGADMIN_VENV_DIR/bin/pgadmin4" > "$${LOG_PATH}" 2>&1 &
73+
else
74+
printf "⚠️ Warning: pgAdmin4 virtual environment not found\n"
75+
printf "📝 Installation may have failed - check logs above\n"
76+
fi

0 commit comments

Comments
 (0)