Skip to content

Commit 3c83000

Browse files
authored
Merge pull request #1 from piewared/feature/MAKE_TEMPORAL_OPTIONAL
Feature/make temporal optional
2 parents 321ae38 + 882353e commit 3c83000

35 files changed

+2099
-114
lines changed

.copier-answers.yml.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ project_slug: {{ project_slug }}
1212
python_version: '{{ python_version }}'
1313
use_postgres: {{ use_postgres }}
1414
use_redis: {{ use_redis }}
15+
use_temporal: {{ use_temporal }}
1516
version: {{ version }}

copier.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ use_redis:
125125
help: "Include Redis for caching and sessions?"
126126
default: yes
127127

128+
use_temporal:
129+
type: bool
130+
help: "Include Temporal for workflow orchestration?"
131+
default: yes
132+
128133
use_postgres:
129134
type: bool
130135
help: "Use PostgreSQL instead of SQLite?"

infra/docker/prod/postgres/admin-scripts/sync-passwords.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,18 @@ read_secret_value() {
5656
# Ensure required env vars exist (populated via docker-compose)
5757
require_env APP_DB_USER
5858
require_env APP_DB_RO_USER
59-
require_env TEMPORAL_DB_USER
59+
# TEMPORAL_DB_USER is optional - only required if Temporal is enabled
6060

6161
APP_USER_PASSWORD=$(read_secret_value POSTGRES_APP_USER_PW postgres_app_user_pw)
6262
APP_RO_PASSWORD=$(read_secret_value POSTGRES_APP_RO_PW postgres_app_ro_pw)
63-
TEMPORAL_PASSWORD=$(read_secret_value POSTGRES_TEMPORAL_PW postgres_temporal_pw)
6463
SUPERUSER_PASSWORD=$(read_secret_value POSTGRES_PASSWORD postgres_password)
6564

65+
# Temporal password is optional
66+
TEMPORAL_PASSWORD=""
67+
if [ -n "${TEMPORAL_DB_USER:-}" ]; then
68+
TEMPORAL_PASSWORD=$(read_secret_value POSTGRES_TEMPORAL_PW postgres_temporal_pw)
69+
fi
70+
6671
log "Synchronizing Postgres role passwords with secrets..."
6772

6873
PSQL_CMD=(psql -h 127.0.0.1 -U postgres -v ON_ERROR_STOP=1)
@@ -87,14 +92,13 @@ else
8792
fi
8893
fi
8994

95+
# Sync App and superuser passwords (always required)
9096
"${PSQL_CMD[@]}" \
9197
-d postgres \
9298
-v APP_USER="${APP_DB_USER}" \
9399
-v APP_RO_USER="${APP_DB_RO_USER}" \
94-
-v TEMPORAL_USER="${TEMPORAL_DB_USER}" \
95100
-v APP_USER_PASSWORD="${APP_USER_PASSWORD}" \
96101
-v APP_RO_USER_PASSWORD="${APP_RO_PASSWORD}" \
97-
-v TEMPORAL_USER_PASSWORD="${TEMPORAL_PASSWORD}" \
98102
-v SUPERUSER_PASSWORD="${SUPERUSER_PASSWORD}" <<'SQL'
99103
\set ON_ERROR_STOP on
100104
@@ -109,10 +113,23 @@ WHERE EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'APP_USER')
109113
SELECT format('ALTER ROLE %I WITH PASSWORD %L', :'APP_RO_USER', :'APP_RO_USER_PASSWORD')
110114
WHERE EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'APP_RO_USER')
111115
\gexec
116+
SQL
117+
118+
# Sync Temporal password only if Temporal is enabled
119+
if [ -n "${TEMPORAL_DB_USER:-}" ] && [ -n "${TEMPORAL_PASSWORD:-}" ]; then
120+
log "Syncing Temporal user password..."
121+
"${PSQL_CMD[@]}" \
122+
-d postgres \
123+
-v TEMPORAL_USER="${TEMPORAL_DB_USER}" \
124+
-v TEMPORAL_USER_PASSWORD="${TEMPORAL_PASSWORD}" <<'SQL'
125+
\set ON_ERROR_STOP on
112126
113127
SELECT format('ALTER ROLE %I WITH PASSWORD %L', :'TEMPORAL_USER', :'TEMPORAL_USER_PASSWORD')
114128
WHERE EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'TEMPORAL_USER')
115129
\gexec
116130
SQL
131+
else
132+
log "Temporal disabled, skipping Temporal password sync"
133+
fi
117134

118135
log "Password synchronization complete."

infra/docker/prod/postgres/init-scripts/01-init-app.sh

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
#!/bin/sh
22
set -eu
33

4-
# Required env (provided by your entrypoint/compose)
4+
# =============================================================================
5+
# PostgreSQL Initialization Script
6+
# =============================================================================
7+
# This script initializes:
8+
# 1. Application database (always) - roles, schema, and permissions
9+
# 2. Temporal databases (optional) - only if TEMPORAL_DB_USER is set
10+
# =============================================================================
11+
12+
# Required env for App DB (provided by your entrypoint/compose)
513
: "${APP_DB:?missing APP_DB}"
6-
: "${APP_DB_OWNER:?missing APP_DB_OWNER}" # NOLOGIN owner (app)
14+
: "${APP_DB_OWNER:?missing APP_DB_OWNER}" # NOLOGIN owner (app)
715
: "${APP_DB_USER:?missing APP_DB_USER}" # app_user (LOGIN)
8-
: "${APP_DB_RO_USER:?missing APP_DB_RO_USER}" # app_ro (LOGIN)
9-
: "${TEMPORAL_DB_USER:?missing TEMPORAL_DB_USER}" # e.g., temporal_user
10-
: "${POSTGRES_APP_USER_PW:?missing POSTGRES_APP_USER_PW}" # password for app user
11-
: "${POSTGRES_APP_RO_PW:?missing POSTGRES_APP_RO_PW}" # password for read-only user
12-
: "${POSTGRES_TEMPORAL_PW:?missing POSTGRES_TEMPORAL_PW}"
13-
14-
# Optional override for Temporal owner (NOLOGIN). Default: "<temporal_user>_owner"
15-
TEMPORAL_DB_OWNER="${TEMPORAL_DB_OWNER:-${TEMPORAL_DB_USER}_owner}"
16-
17-
# Database names (override if you like)
18-
TEMPORAL_DB="${TEMPORAL_DB:-temporal}"
19-
TEMPORAL_VIS_DB="${TEMPORAL_VIS_DB:-temporal_visibility}"
16+
: "${APP_DB_RO_USER:?missing APP_DB_RO_USER}" # app_ro (LOGIN)
17+
: "${POSTGRES_APP_USER_PW:?missing POSTGRES_APP_USER_PW}" # password for app user
18+
: "${POSTGRES_APP_RO_PW:?missing POSTGRES_APP_RO_PW}" # password for read-only user
2019

2120
APP_SCHEMA="${APP_SCHEMA:-app}"
2221

23-
echo "==> Initializing roles/db for ${APP_DB} and Temporal"
22+
# =============================================================================
23+
# App Database Initialization
24+
# =============================================================================
25+
echo "==> Initializing roles/db for ${APP_DB}"
2426

2527
psql -v ON_ERROR_STOP=1 \
2628
-v APP_DB="${APP_DB}" \
@@ -29,11 +31,6 @@ psql -v ON_ERROR_STOP=1 \
2931
-v APP_RO_USER="${APP_DB_RO_USER}" \
3032
-v APP_USER_PASSWORD="${POSTGRES_APP_USER_PW}" \
3133
-v APP_RO_USER_PASSWORD="${POSTGRES_APP_RO_PW}" \
32-
-v TEMPORAL_OWNER_USER="${TEMPORAL_DB_OWNER}" \
33-
-v TEMPORAL_USER="${TEMPORAL_DB_USER}" \
34-
-v TEMPORAL_USER_PASSWORD="${POSTGRES_TEMPORAL_PW}" \
35-
-v TEMPORAL_DB="${TEMPORAL_DB}" \
36-
-v TEMPORAL_VIS_DB="${TEMPORAL_VIS_DB}" \
3734
-v APP_SCHEMA="${APP_SCHEMA}" <<'PSQL'
3835
\set ON_ERROR_STOP on
3936
@@ -112,7 +109,32 @@ SELECT format(
112109
)\gexec
113110
114111
\echo === App DB/roles initialized (3-role pattern) ===
112+
PSQL
113+
114+
echo "==> App database initialization completed"
115+
116+
# =============================================================================
117+
# Temporal Database Initialization (Optional)
118+
# =============================================================================
119+
# Only run if TEMPORAL_DB_USER is set (indicates Temporal is enabled)
115120

121+
if [ -n "${TEMPORAL_DB_USER:-}" ] && [ -n "${POSTGRES_TEMPORAL_PW:-}" ]; then
122+
echo "==> Temporal enabled, initializing Temporal roles/databases"
123+
124+
# Optional override for Temporal owner (NOLOGIN). Default: "<temporal_user>_owner"
125+
TEMPORAL_DB_OWNER="${TEMPORAL_DB_OWNER:-${TEMPORAL_DB_USER}_owner}"
126+
127+
# Database names (override if you like)
128+
TEMPORAL_DB="${TEMPORAL_DB:-temporal}"
129+
TEMPORAL_VIS_DB="${TEMPORAL_VIS_DB:-temporal_visibility}"
130+
131+
psql -v ON_ERROR_STOP=1 \
132+
-v TEMPORAL_OWNER_USER="${TEMPORAL_DB_OWNER}" \
133+
-v TEMPORAL_USER="${TEMPORAL_DB_USER}" \
134+
-v TEMPORAL_USER_PASSWORD="${POSTGRES_TEMPORAL_PW}" \
135+
-v TEMPORAL_DB="${TEMPORAL_DB}" \
136+
-v TEMPORAL_VIS_DB="${TEMPORAL_VIS_DB}" <<'PSQL'
137+
\set ON_ERROR_STOP on
116138
117139
\echo === Creating Temporal roles and databases ===
118140
@@ -204,4 +226,9 @@ SELECT format(
204226
205227
PSQL
206228

229+
echo "==> Temporal database initialization completed"
230+
else
231+
echo "==> Temporal disabled (TEMPORAL_DB_USER not set), skipping Temporal database setup"
232+
fi
233+
207234
echo "==> Init completed"

infra/docker/prod/postgres/verify-init.sh

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ APP_DB="${APP_DB:-${APP_DB:-appdb}}"
66
APP_USER="${APP_DB_USER:-${APP_DB_USER:-appuser}}"
77
APP_RO="${APP_DB_RO_USER:-${APP_DB_RO_USER:-appreadonly}}"
88
APP_OWNER="${APP_DB_OWNER:-${APP_DB_OWNER:-owner}}"
9-
TEMPORAL_DB="${TEMPORAL_DB:-${TEMPORAL_DB:-temporal}}"
10-
TEMPORAL_DB_USER="${TEMPORAL_DB_USER:-${TEMPORAL_DB_USER:-temporaluser}}"
11-
TEMPORAL_DB_OWNER="${TEMPORAL_DB_OWNER:-${TEMPORAL_DB_USER}_owner}"
129
APP_SCHEMA="${APP_SCHEMA:-app}"
1310

11+
# Temporal variables - only set if TEMPORAL_DB_USER is provided (no defaults)
12+
# When Temporal is disabled, these should be empty/unset
13+
TEMPORAL_DB="${TEMPORAL_DB:-}"
14+
TEMPORAL_DB_USER="${TEMPORAL_DB_USER:-}"
15+
TEMPORAL_DB_OWNER="${TEMPORAL_DB_OWNER:-${TEMPORAL_DB_USER:+${TEMPORAL_DB_USER}_owner}}"
16+
1417
# Comma-separated list of subnets that SHOULD be allowed via hostssl
1518
# e.g. "172.30.50.0/24,10.10.0.0/16"
1619
ALLOWED_SUBNETS="${ALLOWED_SUBNETS:-172.30.50.0/24}"
@@ -31,7 +34,11 @@ warn(){ printf "⚠️ %s\n" "$*"; }
3134

3235
echo "== Verifying Postgres (host: postgres) =="
3336
echo " DB=$APP_DB OWNER=$APP_OWNER USER=$APP_USER RO=$APP_RO SCHEMA=$APP_SCHEMA"
34-
echo " Temporal DB=$TEMPORAL_DB USER=$TEMPORAL_DB_USER OWNER=$TEMPORAL_DB_OWNER"
37+
if [ -n "${TEMPORAL_DB_USER:-}" ]; then
38+
echo " Temporal DB=$TEMPORAL_DB USER=$TEMPORAL_DB_USER OWNER=$TEMPORAL_DB_OWNER"
39+
else
40+
echo " Temporal: disabled"
41+
fi
3542
echo
3643

3744
# --- Core: roles / db / schema / grants ---------------------------------------
@@ -53,20 +60,23 @@ echo
5360
|| bad "Role ${APP_OWNER} should be NOLOGIN"
5461
ok "App DB role login attributes look correct (${APP_USER}/${APP_RO} LOGIN, ${APP_OWNER} NOLOGIN)"
5562

56-
# Temporal DB roles exist?
63+
# Temporal DB roles exist? (only check if Temporal is enabled)
64+
if [ -n "${TEMPORAL_DB_USER:-}" ]; then
65+
[ "$($PSQL -c "SELECT COUNT(*) FROM pg_roles WHERE rolname='${TEMPORAL_DB_USER}';")" = "1" ] \
66+
|| bad "Role ${TEMPORAL_DB_USER} does not exist"
67+
[ "$($PSQL -c "SELECT COUNT(*) FROM pg_roles WHERE rolname='${TEMPORAL_DB_OWNER}';")" = "1" ] \
68+
|| bad "Role ${TEMPORAL_DB_OWNER} does not exist"
5769

58-
[ "$($PSQL -c "SELECT COUNT(*) FROM pg_roles WHERE rolname='${TEMPORAL_DB_USER}';")" = "1" ] \
59-
|| bad "Role ${TEMPORAL_DB_USER} does not exist"
60-
[ "$($PSQL -c "SELECT COUNT(*) FROM pg_roles WHERE rolname='${TEMPORAL_DB_OWNER}';")" = "1" ] \
61-
|| bad "Role ${TEMPORAL_DB_OWNER} does not exist"
70+
# LOGIN/NOLOGIN checks
71+
[ "$($PSQL -c "SELECT rolcanlogin FROM pg_roles WHERE rolname='${TEMPORAL_DB_USER}';")" = "t" ] \
72+
|| bad "Role ${TEMPORAL_DB_USER} missing LOGIN"
73+
[ "$($PSQL -c "SELECT rolcanlogin FROM pg_roles WHERE rolname='${TEMPORAL_DB_OWNER}';")" = "f" ] \
74+
|| bad "Role ${TEMPORAL_DB_OWNER} should be NOLOGIN"
6275

63-
# LOGIN/NOLOGIN checks
64-
[ "$($PSQL -c "SELECT rolcanlogin FROM pg_roles WHERE rolname='${TEMPORAL_DB_USER}';")" = "t" ] \
65-
|| bad "Role ${TEMPORAL_DB_USER} missing LOGIN"
66-
[ "$($PSQL -c "SELECT rolcanlogin FROM pg_roles WHERE rolname='${TEMPORAL_DB_OWNER}';")" = "f" ] \
67-
|| bad "Role ${TEMPORAL_DB_OWNER} should be NOLOGIN"
68-
69-
ok "Temporal DB role login attributes look correct (${TEMPORAL_DB_USER} LOGIN, ${TEMPORAL_DB_OWNER} NOLOGIN)"
76+
ok "Temporal DB role login attributes look correct (${TEMPORAL_DB_USER} LOGIN, ${TEMPORAL_DB_OWNER} NOLOGIN)"
77+
else
78+
ok "Temporal disabled, skipping Temporal role checks"
79+
fi
7080

7181
# Database ownership
7282
OWNER="$($PSQL -c "SELECT pg_get_userbyid(datdba) FROM pg_database WHERE datname='${APP_DB}';")"

infra/helm/api-forge/templates/deployments/postgres.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ spec:
3131
type: RuntimeDefault
3232
containers:
3333
- name: postgres
34-
image: {{ .Values.postgres.image | default "app_data_postgres_image:latest" }}
34+
image: {{ .Values.postgres.image.repository }}:{{ .Values.postgres.image.tag | default "latest" }}
3535
imagePullPolicy: {{ .Values.global.imagePullPolicy | default "IfNotPresent" }}
3636
command: ["/opt/entry/start-scripts/universal-entrypoint.sh"]
3737
args: ["/bin/sh", "-lc", "/opt/entry/start-scripts/pg-start.sh"]

infra/helm/api-forge/templates/deployments/redis.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ spec:
3030
type: RuntimeDefault
3131
containers:
3232
- name: redis
33-
image: {{ .Values.redis.image | default "app_data_redis_image:latest" }}
33+
image: {{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag | default "latest" }}
3434
imagePullPolicy: {{ .Values.global.imagePullPolicy | default "IfNotPresent" }}
3535
env:
3636
- name: TZ

infra/helm/api-forge/templates/deployments/temporal.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ spec:
3131
type: RuntimeDefault
3232
containers:
3333
- name: temporal
34-
image: {{ .Values.temporal.image | default "my-temporal-server:latest" }}
34+
image: {{ .Values.temporal.image.repository }}:{{ .Values.temporal.image.tag | default "latest" }}
3535
imagePullPolicy: {{ .Values.global.imagePullPolicy | default "IfNotPresent" }}
3636
command: ["/universal-entrypoint.sh"]
3737
args: ["/opt/entry/scripts/entrypoint.sh"]

infra/helm/api-forge/templates/jobs/postgres-verifier.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ metadata:
88
app.kubernetes.io/name: postgres-verifier
99
app.kubernetes.io/component: database-verification
1010
app.kubernetes.io/part-of: api-forge
11+
annotations:
12+
# Run on install and upgrade, delete previous job before creating new one
13+
"helm.sh/hook": post-install,post-upgrade
14+
"helm.sh/hook-weight": "10"
15+
"helm.sh/hook-delete-policy": before-hook-creation
1116
spec:
1217
# Keep successful job for 30 minutes for debugging
1318
ttlSecondsAfterFinished: 1800

infra/helm/api-forge/templates/jobs/temporal-namespace-init.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ metadata:
88
app.kubernetes.io/name: temporal-namespace-init
99
app.kubernetes.io/component: workflow-engine-init
1010
app.kubernetes.io/part-of: api-forge
11+
annotations:
12+
# Run on install and upgrade, delete previous job before creating new one
13+
# Weight 15 ensures this runs after schema-setup (weight 5) and verifier (weight 10)
14+
"helm.sh/hook": post-install,post-upgrade
15+
"helm.sh/hook-weight": "15"
16+
"helm.sh/hook-delete-policy": before-hook-creation
1117
spec:
1218
# Keep successful job for 30 minutes for debugging
1319
ttlSecondsAfterFinished: 1800

0 commit comments

Comments
 (0)