1+ terraform {
2+ required_providers {
3+ coder = {
4+ source = " coder/coder"
5+ }
6+ }
7+ }
8+
9+ variable "agent_id" {
10+ type = string
11+ description = " The agent to install pgAdmin on."
12+ }
13+
14+ variable "port" {
15+ type = number
16+ description = " The port to run pgAdmin on."
17+ default = 5050
18+ }
19+
20+ variable "subdomain" {
21+ type = bool
22+ description = " If true, the app will be served on a subdomain."
23+ default = true
24+ }
25+
26+ variable "config" {
27+ type = any
28+ description = " A map of pgAdmin configuration settings."
29+ default = {
30+ DEFAULT_EMAIL = " admin@coder.com"
31+ DEFAULT_PASSWORD = " coderPASSWORD"
32+ SERVER_MODE = false
33+ MASTER_PASSWORD_REQUIRED = false
34+ LISTEN_ADDRESS = " 127.0.0.1"
35+ }
36+ }
37+
38+ data "coder_workspace" "me" {}
39+ data "coder_workspace_owner" "me" {}
40+
41+ resource "coder_app" "pgadmin" {
42+ count = data. coder_workspace . me . start_count
43+ agent_id = var. agent_id
44+ display_name = " pgAdmin"
45+ slug = " pgadmin"
46+ icon = " /icon/pgadmin.svg"
47+ url = local. url
48+ subdomain = var. subdomain
49+ share = " owner"
50+
51+ healthcheck {
52+ url = local. healthcheck_url
53+ interval = 5
54+ threshold = 6
55+ }
56+ }
57+
58+ resource "coder_script" "pgadmin" {
59+ agent_id = var. agent_id
60+ display_name = " Install and run pgAdmin"
61+ icon = " /icon/pgadmin.svg"
62+ run_on_start = true
63+ script = templatefile (" ${ path . module } /run.sh" , {
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
71+ })
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+ ])
108+ }
0 commit comments