-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-compose.js
More file actions
455 lines (404 loc) · 19 KB
/
generate-compose.js
File metadata and controls
455 lines (404 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env node
// ─────────────────────────────────────────────────────────────
// Single source of truth for all scaling values.
// Edit here — the YAML is fully derived from this config.
// Run: node generate-compose.js
// ─────────────────────────────────────────────────────────────
const fs = require("fs");
const path = require("path");
// Load .env if present (no extra dependencies needed)
const envFile = path.join(__dirname, ".env");
if (fs.existsSync(envFile)) {
fs.readFileSync(envFile, "utf8").split("\n").forEach((line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) return;
const idx = trimmed.indexOf("=");
if (idx === -1) return;
const key = trimmed.slice(0, idx).trim();
const val = trimmed.slice(idx + 1).trim();
if (!process.env[key]) process.env[key] = val; // don't override shell env
});
}
if (!process.env.AWS_ACCOUNT_ID) {
console.error("❌ AWS_ACCOUNT_ID is not set. Copy .env.example → .env and fill it in.");
process.exit(1);
}
const config = {
// ── pgbouncer-write (fronts primary Postgres — all writes) ─────────────────
pgbouncerWriteMaxReplicas: 2,
pgbouncerWritePoolSizePerPod: 20, // real server-side connections to postgres per pod
pgbouncerWriteHeadroom: 15, // reserved slack in postgres max_connections
pgbouncerWriteMaxClientConn: 1000, // client-side connections from API (pgbouncer queues these cheaply)
// ── pgbouncer-read (fronts postgres-replica — all reads) ───────────────────
pgbouncerReadMaxReplicas: 3,
pgbouncerReadPoolSizePerPod: 20, // real server-side connections to replica per pod
pgbouncerReadHeadroom: 15, // reserved slack in replica max_connections
pgbouncerReadMaxClientConn: 1000, // client-side connections from API (pgbouncer queues these cheaply)
// ── API ────────────────────────────────────────────────────────────────────
// These are independent of pgbouncer math — tune based on workload, not DB limits.
// PgBouncer decouples client connections from server connections.
apiMaxReplicas: 10,
apiWritePoolPerPod: 10, // concurrent write connections per API pod → pgbouncer (not postgres)
apiReadPoolPerPod: 20, // concurrent read connections per API pod → pgbouncer (not postgres)
// ── Images ─────────────────────────────────────────────────────────────────
pgbouncerImage: "gorspeed/pgbouncer:v1.0.5",
postgresPrimaryImage: "gorspeed/postgres-primary:v1.0.11", // built from postgres-primary/
postgresReplicaImage: "gorspeed/postgres-replica:v1.0.2", // built from postgres-replica/
apiImage: "gorspeed/paas-api:v1.0.4",
// ── AWS — loaded from .env, never hardcoded ────────────────────────────────
awsAccountId: process.env.AWS_ACCOUNT_ID,
// ── PITR — optional, loaded from .env ─────────────────────────────────────
// Set WALG_S3_PREFIX=s3://your-bucket/walg/prod in .env to enable WAL archiving.
// If not set, PITR is disabled and the deployment works exactly as before.
walgS3Prefix: process.env.WALG_S3_PREFIX || "",
walgAwsRegion: process.env.WALG_AWS_REGION || "ap-south-1",
// For S3 auth: either set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY in .env,
// or attach an IAM role to the EC2 instance (preferred in production).
walgAccessKeyId: process.env.WALG_AWS_ACCESS_KEY_ID || "",
walgSecretAccessKey: process.env.WALG_AWS_SECRET_ACCESS_KEY || "",
// ── Backup tuning — optional, defaults are production-safe ────────────────
backupCronSchedule: process.env.BACKUP_CRON_SCHEDULE || "0 2 * * *",
walgBackupRetentionFull: process.env.WALG_BACKUP_RETENTION_FULL || "7",
archiveTimeout: process.env.ARCHIVE_TIMEOUT || "60",
};
// ── Derived constraints ────────────────────────────────────────────────────────
// Only constraint that matters: pgbouncer server pool × max replicas < postgres max_connections.
// API pool sizes are independent — pgbouncer queues client connections, decoupling API from postgres.
const postgresMaxConn = (config.pgbouncerWriteMaxReplicas * config.pgbouncerWritePoolSizePerPod) + config.pgbouncerWriteHeadroom;
const postgresReplicaMaxConn = (config.pgbouncerReadMaxReplicas * config.pgbouncerReadPoolSizePerPod) + config.pgbouncerReadHeadroom;
const totalWritePgConn = config.pgbouncerWriteMaxReplicas * config.pgbouncerWritePoolSizePerPod;
const totalReadPgConn = config.pgbouncerReadMaxReplicas * config.pgbouncerReadPoolSizePerPod;
// ── Validation ────────────────────────────────────────────────────────────────
if (totalWritePgConn >= postgresMaxConn) {
console.error(`❌ Write constraint violated: pgbouncer-write(${config.pgbouncerWriteMaxReplicas} × ${config.pgbouncerWritePoolSizePerPod} = ${totalWritePgConn}) ≥ postgres max_connections(${postgresMaxConn})`);
process.exit(1);
}
if (totalReadPgConn >= postgresReplicaMaxConn) {
console.error(`❌ Read constraint violated: pgbouncer-read(${config.pgbouncerReadMaxReplicas} × ${config.pgbouncerReadPoolSizePerPod} = ${totalReadPgConn}) ≥ replica max_connections(${postgresReplicaMaxConn})`);
process.exit(1);
}
console.log(`✓ Write path: postgres max_connections=${postgresMaxConn} ≥ pgbouncer-write(${config.pgbouncerWriteMaxReplicas}×${config.pgbouncerWritePoolSizePerPod}=${totalWritePgConn}) + headroom(${config.pgbouncerWriteHeadroom})`);
console.log(`✓ Read path: replica max_connections=${postgresReplicaMaxConn} ≥ pgbouncer-read(${config.pgbouncerReadMaxReplicas}×${config.pgbouncerReadPoolSizePerPod}=${totalReadPgConn}) + headroom(${config.pgbouncerReadHeadroom})`);
console.log(`✓ pgbouncer-write MAX_CLIENT_CONN=${config.pgbouncerWriteMaxClientConn} (client-facing, decoupled from postgres)`);
console.log(`✓ pgbouncer-read MAX_CLIENT_CONN=${config.pgbouncerReadMaxClientConn} (client-facing, decoupled from postgres)\n`);
// ── Generate YAML ─────────────────────────────────────────────────────────────
const yaml = `version: "3.9"
# ─────────────────────────────────────────────────────────────
# OMNISTRATE PAAS SERVICE
# Architecture:
#
# api (writePool) → pgbouncer-write → postgres (primary, single writer)
# api (readPool) → pgbouncer-read → postgres-replica (streaming standby)
#
# ⚠️ DO NOT EDIT THIS FILE DIRECTLY.
# Generated by: node generate-compose.js
#
# SCALING CONSTRAINTS (auto-derived):
# Write: postgres max_connections=${postgresMaxConn} ≥ pgbouncer-write(${config.pgbouncerWriteMaxReplicas}×${config.pgbouncerWritePoolSizePerPod}=${totalWritePgConn}) + headroom(${config.pgbouncerWriteHeadroom})
# Read: replica max_connections=${postgresReplicaMaxConn} ≥ pgbouncer-read(${config.pgbouncerReadMaxReplicas}×${config.pgbouncerReadPoolSizePerPod}=${totalReadPgConn}) + headroom(${config.pgbouncerReadHeadroom})
# pgbouncer-write MAX_CLIENT_CONN=${config.pgbouncerWriteMaxClientConn} (client-facing, set independently of API pool)
# pgbouncer-read MAX_CLIENT_CONN=${config.pgbouncerReadMaxClientConn} (client-facing, set independently of API pool)
# ─────────────────────────────────────────────────────────────
services:
# ───────────────────────────────────────────
# 1. PostgreSQL Primary — write path only
# Streams WAL to postgres-replica continuously.
# ───────────────────────────────────────────
postgres:
image: ${config.postgresPrimaryImage}
command: >
postgres
-c max_connections=${postgresMaxConn}
-c wal_level=replica
-c max_wal_senders=5
-c max_replication_slots=3
environment:
- POSTGRES_USER=app
- POSTGRES_PASSWORD=\$var.dbPassword
- POSTGRES_DB=appdb
- REPLICATOR_PASSWORD=\$var.replicatorPassword
- WALG_S3_PREFIX=${config.walgS3Prefix}
- AWS_REGION=${config.walgAwsRegion}
${config.walgAccessKeyId ? `- AWS_ACCESS_KEY_ID=${config.walgAccessKeyId}` : "# AWS_ACCESS_KEY_ID — using IAM instance role"}
${config.walgSecretAccessKey ? `- AWS_SECRET_ACCESS_KEY=${config.walgSecretAccessKey}` : "# AWS_SECRET_ACCESS_KEY — using IAM instance role"}
- BACKUP_CRON_SCHEDULE=${config.backupCronSchedule}
- WALG_BACKUP_RETENTION_FULL=${config.walgBackupRetentionFull}
- ARCHIVE_TIMEOUT=${config.archiveTimeout}
expose:
- "5432"
volumes:
- source: pg_data
target: /var/lib/postgresql/data
type: volume
x-omnistrate-storage:
aws:
instanceStorageType: AWS::EBS_GP3
instanceStorageSizeGi: 20
instanceStorageIOPS: 3000
instanceStorageThroughputMiBps: 125
x-omnistrate-compute:
instanceTypes:
- cloudProvider: aws
name: t4g.small
x-omnistrate-capabilities: {}
x-omnistrate-mode-internal: true
x-omnistrate-actionhooks:
- scope: NODE
type: HEALTH_CHECK
commandTemplate: |
nc -z localhost 5432
x-omnistrate-api-params:
- key: dbPassword
name: Database Password
description: Password for the PostgreSQL app user
type: String
required: true
modifiable: false
export: false
- key: replicatorPassword
name: Replication Password
description: Password for the PostgreSQL streaming replication user
type: String
required: true
modifiable: false
export: false
# ───────────────────────────────────────────
# 2. PostgreSQL Replica — read path only
# Custom image runs pg_basebackup on first boot,
# then streams WAL from primary continuously.
# ───────────────────────────────────────────
postgres-replica:
image: ${config.postgresReplicaImage}
environment:
- PRIMARY_HOST=postgres
- REPLICATOR_PASSWORD=\$var.replicatorPassword
- POSTGRES_USER=app
- POSTGRES_PASSWORD=\$var.dbPassword
- POSTGRES_DB=appdb
- MAX_CONNECTIONS=${postgresReplicaMaxConn}
expose:
- "5432"
volumes:
- source: pg_replica_data
target: /var/lib/postgresql/data
type: volume
x-omnistrate-storage:
aws:
instanceStorageType: AWS::EBS_GP3
instanceStorageSizeGi: 20
instanceStorageIOPS: 3000
instanceStorageThroughputMiBps: 125
x-omnistrate-compute:
instanceTypes:
- cloudProvider: aws
name: t4g.small
x-omnistrate-capabilities: {}
x-omnistrate-mode-internal: true
x-omnistrate-actionhooks:
- scope: NODE
type: HEALTH_CHECK
commandTemplate: |
nc -z localhost 5432
x-omnistrate-api-params:
- key: replicatorPassword
name: Replication Password
description: Password for the PostgreSQL streaming replication user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres: replicatorPassword
- key: dbPassword
name: Database Password
description: Password for the PostgreSQL app user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres: dbPassword
depends_on:
- postgres
# ───────────────────────────────────────────
# 3. PgBouncer Write — fronts the primary
# MAX_CLIENT_CONN = ${config.pgbouncerWriteMaxClientConn}
# DEFAULT_POOL_SIZE = ${config.pgbouncerWritePoolSizePerPod} per pod
# ───────────────────────────────────────────
pgbouncer-write:
image: ${config.pgbouncerImage}
environment:
- DB_USER=app
- DB_PASSWORD=\$var.dbPassword
- DB_HOST=postgres
- DB_PORT=5432
- DB_NAME=appdb
- POOL_MODE=transaction
- MAX_CLIENT_CONN=${config.pgbouncerWriteMaxClientConn}
- DEFAULT_POOL_SIZE=${config.pgbouncerWritePoolSizePerPod}
- AUTH_TYPE=md5
expose:
- "5432"
x-omnistrate-compute:
instanceTypes:
- cloudProvider: aws
name: t4g.small
x-omnistrate-capabilities:
autoscaling:
minReplicas: 1
maxReplicas: ${config.pgbouncerWriteMaxReplicas}
overUtilizedThreshold: 70
overUtilizedMinutesBeforeScalingUp: 2
idleThreshold: 20
idleMinutesBeforeScalingDown: 5
x-omnistrate-mode-internal: true
x-omnistrate-api-params:
- key: dbPassword
name: Database Password
description: Password for the PostgreSQL app user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres: dbPassword
- key: replicatorPassword
name: Replication Password
description: Password for the PostgreSQL streaming replication user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres: replicatorPassword
depends_on:
- postgres
# ───────────────────────────────────────────
# 4. PgBouncer Read — fronts the replica
# MAX_CLIENT_CONN = ${config.pgbouncerReadMaxClientConn}
# DEFAULT_POOL_SIZE = ${config.pgbouncerReadPoolSizePerPod} per pod
# ───────────────────────────────────────────
pgbouncer-read:
image: ${config.pgbouncerImage}
environment:
- DB_USER=app
- DB_PASSWORD=\$var.dbPassword
- DB_HOST=postgres-replica
- DB_PORT=5432
- DB_NAME=appdb
- POOL_MODE=transaction
- MAX_CLIENT_CONN=${config.pgbouncerReadMaxClientConn}
- DEFAULT_POOL_SIZE=${config.pgbouncerReadPoolSizePerPod}
- AUTH_TYPE=md5
expose:
- "5432"
x-omnistrate-compute:
instanceTypes:
- cloudProvider: aws
name: t4g.small
x-omnistrate-capabilities:
autoscaling:
minReplicas: 1
maxReplicas: ${config.pgbouncerReadMaxReplicas}
overUtilizedThreshold: 70
overUtilizedMinutesBeforeScalingUp: 2
idleThreshold: 20
idleMinutesBeforeScalingDown: 5
x-omnistrate-mode-internal: true
x-omnistrate-api-params:
- key: dbPassword
name: Database Password
description: Password for the PostgreSQL app user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres-replica: dbPassword
- key: replicatorPassword
name: Replication Password
description: Password for the PostgreSQL streaming replication user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
postgres-replica: replicatorPassword
depends_on:
- postgres-replica
# ───────────────────────────────────────────
# 5. API — stateless, public
# writePool → pgbouncer-write → postgres
# readPool → pgbouncer-read → postgres-replica
# ───────────────────────────────────────────
api:
image: ${config.apiImage}
environment:
- DB_PASSWORD=\$var.dbPassword
- DB_WRITE_HOST=pgbouncer-write
- DB_READ_HOST=pgbouncer-read
- DB_PORT=5432
- DB_USER=app
- DB_NAME=appdb
- DB_WRITE_POOL_SIZE=${config.apiWritePoolPerPod}
- DB_READ_POOL_SIZE=${config.apiReadPoolPerPod}
- PORT=8080
- ENV=production
ports:
- "8080:8080"
x-omnistrate-compute:
instanceTypes:
- cloudProvider: aws
name: t4g.small
x-omnistrate-capabilities:
autoscaling:
minReplicas: 1
maxReplicas: ${config.apiMaxReplicas}
overUtilizedThreshold: 70
overUtilizedMinutesBeforeScalingUp: 2
idleThreshold: 20
idleMinutesBeforeScalingDown: 5
httpReverseProxy:
targetPort: 8080
x-omnistrate-actionhooks:
- scope: NODE
type: HEALTH_CHECK
commandTemplate: |
wget -qO- http://localhost:8080/health
x-omnistrate-api-params:
- key: dbPassword
name: Database Password
description: Password for the PostgreSQL app user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
pgbouncer-write: dbPassword
pgbouncer-read: dbPassword
- key: replicatorPassword
name: Replication Password
description: Password for the PostgreSQL streaming replication user
type: String
required: true
modifiable: false
export: false
parameterDependencyMap:
pgbouncer-write: replicatorPassword
pgbouncer-read: replicatorPassword
depends_on:
- pgbouncer-write
- pgbouncer-read
volumes:
pg_data:
pg_replica_data:
x-omnistrate-service-plan:
name: "PaaS Service"
tenancyType: "OMNISTRATE_DEDICATED_TENANCY"
deployment:
hostedDeployment:
awsAccountId: "${config.awsAccountId}"
`;
const outPath = path.join(__dirname, "omnistrate-compose.yaml");
fs.writeFileSync(outPath, yaml);
console.log(`✅ Written to ${outPath}`);