-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-parent.ts
More file actions
461 lines (397 loc) · 19.6 KB
/
init-parent.ts
File metadata and controls
461 lines (397 loc) · 19.6 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
456
457
458
459
460
461
#!/usr/bin/env -S npx tsx
/**
* init-parent.ts
*
* Interactive scaffolder for the "private parent repo + Hyveon
* submodule" deployment pattern documented at
* https://codercoco.github.io/Hyveon/guides/submodule/.
*
* Run from the parent (private) repo root:
*
* cd your-private-games
* git submodule add https://github.com/CoderCoco/Hyveon.git
* (cd Hyveon/scripts && npm install)
* npx --prefix Hyveon/scripts tsx Hyveon/scripts/init-parent.ts
*
* The script writes (or refuses to overwrite without --force):
* - Makefile wrapper around the submodule's Makefile
* - terraform.tfvars skeleton populated from your answers
* - .env API_TOKEN for the management app (gitignored)
* - .gitignore covers .env, .make/, terraform.tfstate*, etc.
*
* It NEVER reads or modifies anything inside the submodule.
*/
import { createInterface, type Interface } from 'node:readline/promises';
import { existsSync, readFileSync, writeFileSync, mkdirSync, statSync } from 'node:fs';
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { stdin as input, stdout as output, argv, cwd, exit } from 'node:process';
import { randomBytes } from 'node:crypto';
interface Answers {
parentDir: string;
submoduleDir: string;
submoduleName: string;
projectName: string;
awsRegion: string;
hostedZone: string;
apiToken: string;
configureDiscord: boolean;
discordApplicationId?: string;
discordBotToken?: string;
discordPublicKey?: string;
}
const FORCE = argv.includes('--force');
// ─────────────────────────────────────────────────────────────────────────────
// Path detection
// ─────────────────────────────────────────────────────────────────────────────
/** Walk up from `start` until a directory containing `.gitmodules` is found. */
function findParentRepoRoot(start: string): string | null {
let dir = resolve(start);
while (true) {
if (existsSync(join(dir, '.gitmodules'))) return dir;
const parent = dirname(dir);
if (parent === dir) return null;
dir = parent;
}
}
/** Best-effort guess of the submodule path inside the parent repo. */
function detectSubmodulePath(parentDir: string, scriptDir: string): string {
// If the script lives at <parent>/<submodule>/scripts/init-parent.ts,
// the submodule directory name is the immediate parent of `scripts/`.
const submoduleRoot = dirname(scriptDir);
const rel = relative(parentDir, submoduleRoot);
if (rel && !rel.startsWith('..') && !isAbsolute(rel)) return rel;
// Fall back to parsing .gitmodules.
const gm = join(parentDir, '.gitmodules');
if (existsSync(gm)) {
const m = readFileSync(gm, 'utf8').match(/path\s*=\s*(\S+)/);
if (m) return m[1];
}
return 'Hyveon';
}
// ─────────────────────────────────────────────────────────────────────────────
// Prompting
// ─────────────────────────────────────────────────────────────────────────────
async function ask(rl: Interface, label: string, def?: string): Promise<string> {
const suffix = def === undefined ? ': ' : ` [${def}]: `;
const raw = (await rl.question(label + suffix)).trim();
return raw || def || '';
}
async function askBool(rl: Interface, label: string, def: boolean): Promise<boolean> {
const hint = def ? 'Y/n' : 'y/N';
const raw = (await rl.question(`${label} (${hint}): `)).trim().toLowerCase();
if (!raw) return def;
return raw.startsWith('y');
}
async function askRequired(rl: Interface, label: string, def?: string): Promise<string> {
while (true) {
const v = await ask(rl, label, def);
if (v) return v;
output.write(' ↳ a value is required.\n');
}
}
// ─────────────────────────────────────────────────────────────────────────────
// File generators
// ─────────────────────────────────────────────────────────────────────────────
/**
* Mirrors the structure documented in the Makefile-driven submodule pattern:
* setup → init submodule, run setup.sh, stamp its sha
* plan → copy tfvars in, delegate to submodule's `make tf-plan`
* apply → same, delegate to `make tf-apply`
* update → bump submodule, rerun setup.sh only if its sha changed
* dev → pull live tfstate into .make/, then `make dev` in submodule
*
* API_TOKEN is loaded from .env (gitignored) — never hardcoded.
*/
export function renderMakefile(a: Answers): string {
return `SHELL := /usr/bin/env bash
.SHELLFLAGS := -eu -o pipefail -c
REPO_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
SUBMODULE := $(REPO_ROOT)/${a.submoduleDir}
TF_DIR := $(SUBMODULE)/terraform
TFVARS := $(REPO_ROOT)/terraform.tfvars
STAMP_DIR := $(REPO_ROOT)/.make
SETUP_STAMP := $(STAMP_DIR)/setup.stamp
# Load API_TOKEN (and any other K=V) from .env without leaking it into git.
ifneq (,$(wildcard $(REPO_ROOT)/.env))
include $(REPO_ROOT)/.env
export
endif
.PHONY: help setup plan apply update dev copy-tfvars
# ── Help ─────────────────────────────────────────────────────────────────────
help:
\t@echo "${a.projectName} — submodule deployment wrapper"
\t@echo ""
\t@echo " make setup One-time bootstrap: init submodule, install deps, terraform init"
\t@echo " make plan Copy tfvars into submodule then terraform plan"
\t@echo " make apply Copy tfvars into submodule then terraform apply"
\t@echo " make update Pull latest ${a.submoduleDir}/main; rerun setup.sh if changed"
\t@echo " make dev Start dev servers (Nest :3001 + Vite :5173)"
# ── Stamp dir ────────────────────────────────────────────────────────────────
$(STAMP_DIR):
\t@mkdir -p $@
# ── One-time setup ───────────────────────────────────────────────────────────
setup: | $(STAMP_DIR)
\tgit submodule update --init --recursive
\tbash $(SUBMODULE)/setup.sh
\t@sha256sum $(SUBMODULE)/setup.sh | cut -d' ' -f1 > $(SETUP_STAMP)
# ── Copy tfvars into the submodule terraform dir ─────────────────────────────
$(TF_DIR)/terraform.tfvars: $(TFVARS)
\tcp $(TFVARS) $@
# Force a fresh copy on every plan/apply so stale vars can't slip through.
copy-tfvars: $(TFVARS)
\tcp $(TFVARS) $(TF_DIR)/terraform.tfvars
# ── Terraform targets ────────────────────────────────────────────────────────
plan: copy-tfvars
\t$(MAKE) -C $(SUBMODULE) tf-plan
apply: copy-tfvars
\t$(MAKE) -C $(SUBMODULE) tf-apply
# ── Submodule update with idempotent setup.sh re-run ─────────────────────────
update: | $(STAMP_DIR)
\tgit submodule update --remote --merge $(SUBMODULE)
\t@CURRENT=$$(sha256sum $(SUBMODULE)/setup.sh | cut -d' ' -f1); \\
\t PREVIOUS=$$(cat $(SETUP_STAMP) 2>/dev/null || echo ""); \\
\t if [ "$$CURRENT" != "$$PREVIOUS" ]; then \\
\t echo "setup.sh changed — clearing .terraform/ and rerunning..."; \\
\t rm -rf $(TF_DIR)/.terraform; \\
\t bash $(SUBMODULE)/setup.sh; \\
\t echo "$$CURRENT" > $(SETUP_STAMP); \\
\t else \\
\t echo "setup.sh unchanged — skipping."; \\
\t fi
\t@echo ""
\t@echo "Submodule updated. Commit the new pointer when ready:"
\t@echo " git add ${a.submoduleDir} && git commit -m 'chore: bump ${a.submoduleDir}'"
# ── Dev server ───────────────────────────────────────────────────────────────
# Pull live tfstate so embed-tfstate has something to read; falls back to null
# when the backend isn't reachable yet (e.g. before the first apply).
dev: | $(STAMP_DIR)
\tterraform -chdir=$(TF_DIR) state pull > $(STAMP_DIR)/tfstate.json 2>/dev/null || echo 'null' > $(STAMP_DIR)/tfstate.json
\trm -f $(SUBMODULE)/app/packages/*/tsconfig*.tsbuildinfo
\tTF_STATE_PATH=$(STAMP_DIR)/tfstate.json $(MAKE) -C $(SUBMODULE) dev
`;
}
/**
* Skeleton tfvars derived from the public terraform.tfvars.example shape. We
* fill in the few things we just asked the user about and leave the rest as
* commented examples.
*/
export function renderTfvars(a: Answers): string {
const discordBlock =
a.configureDiscord && a.discordApplicationId && a.discordBotToken && a.discordPublicKey
? `discord_application_id = "${a.discordApplicationId}"
discord_bot_token = "${a.discordBotToken}"
discord_public_key = "${a.discordPublicKey}"
`
: `# discord_application_id = "1234567890"
# discord_bot_token = "MTIz...xyz"
# discord_public_key = "0123abc..."
`;
return `# ${a.projectName} — Terraform variables.
# Commit this file to your private parent repo. The wrapper Makefile copies it
# into ${a.submoduleDir}/terraform/terraform.tfvars on every plan/apply, where
# the submodule's own .gitignore prevents it from being committed back.
aws_region = "${a.awsRegion}"
project_name = "${a.projectName}"
# Hosted zone in Route 53. {game}.${a.hostedZone} records are managed by Lambda.
hosted_zone_name = "${a.hostedZone}"
# Watchdog: auto-shuts down idle servers after (interval × idle_checks) minutes.
watchdog_interval_minutes = 15
watchdog_idle_checks = 4
watchdog_min_packets = 100
# acm_certificate_domain = "*.${a.hostedZone}"
# Discord bot credentials (optional — leave commented out to configure via the web UI).
${discordBlock}
# base_allowed_guilds = ["123456789012345678"]
# base_admin_user_ids = ["987654321098765432"]
# base_admin_role_ids = []
# Game server definitions. See ${a.submoduleDir}/terraform/terraform.tfvars.example
# for the full schema.
game_servers = {
# palworld = {
# image = "thijsvanloef/palworld-server-docker:latest"
# cpu = 2048
# memory = 8192
# ports = [
# { container = 8211, protocol = "udp" },
# { container = 27015, protocol = "udp" },
# ]
# environment = [
# { name = "PLAYERS", value = "8" },
# { name = "SERVER_NAME", value = "My Palworld Server" },
# ]
# volumes = [
# { name = "saves", container_path = "/palworld" },
# ]
# https = false
# }
}
`;
}
export function renderEnv(a: Answers): string {
return `# Bearer token for the management app (also used by docker compose).
# This file is gitignored — never commit it. Rotate by deleting and re-running
# \`init-parent.ts\` (or just generate a new hex string).
API_TOKEN=${a.apiToken}
`;
}
export function renderGitignore(a: Answers): string {
return `# ${a.projectName} — parent repo .gitignore
# Bearer token + any local environment overrides
.env
.env.*
!.env.example
# Make stamp dir (sha256 of submodule's setup.sh, cached tfstate.json, ...)
.make/
# Terraform local state, if you ever fall off the S3 backend
terraform.tfstate
terraform.tfstate.backup
*.tfvars.local
# Editor / OS noise
.DS_Store
.vscode/
.idea/
`;
}
// ─────────────────────────────────────────────────────────────────────────────
// IO helpers
// ─────────────────────────────────────────────────────────────────────────────
function writeIfSafe(path: string, contents: string): 'wrote' | 'skipped' | 'overwrote' {
if (existsSync(path) && !FORCE) {
return 'skipped';
}
const existed = existsSync(path);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, contents);
return existed ? 'overwrote' : 'wrote';
}
function status(path: string, action: 'wrote' | 'skipped' | 'overwrote', parentDir: string): void {
const rel = relative(parentDir, path) || path;
const tag =
action === 'wrote'
? ' +'
: action === 'overwrote'
? ' ~'
: ' ·';
const note = action === 'skipped' ? ' (exists — use --force to overwrite)' : '';
output.write(`${tag} ${rel}${note}\n`);
}
// ─────────────────────────────────────────────────────────────────────────────
// Validation
// ─────────────────────────────────────────────────────────────────────────────
function isValidProjectName(s: string): boolean {
// Used as part of S3 bucket names by setup.sh — keep it conservative.
return /^[a-z0-9][a-z0-9-]{1,30}[a-z0-9]$/.test(s);
}
function isValidRegion(s: string): boolean {
return /^[a-z]{2,3}-[a-z]+-\d$/.test(s);
}
function isValidDomain(s: string): boolean {
return /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/i.test(s);
}
// ─────────────────────────────────────────────────────────────────────────────
// Main
// ─────────────────────────────────────────────────────────────────────────────
async function main(): Promise<void> {
const scriptDir = dirname(fileURLToPath(import.meta.url));
const guessedParent = findParentRepoRoot(cwd()) ?? findParentRepoRoot(scriptDir) ?? cwd();
output.write('\n');
output.write(' Hyveon — submodule deployment scaffolder\n');
output.write(' ────────────────────────────────────────────────────\n');
output.write('\n');
output.write(` Parent repo: ${guessedParent}\n`);
output.write(` Script: ${relative(guessedParent, fileURLToPath(import.meta.url)) || fileURLToPath(import.meta.url)}\n`);
output.write('\n');
output.write(' This will write Makefile, terraform.tfvars, .env, and .gitignore in\n');
output.write(' the parent repo. Existing files are skipped unless you pass --force.\n');
output.write('\n');
const rl = createInterface({ input, output });
try {
const parentDir = await ask(rl, 'Parent repo path', guessedParent);
if (!existsSync(parentDir) || !statSync(parentDir).isDirectory()) {
output.write(`\n ✗ ${parentDir} is not a directory.\n`);
exit(1);
}
const submoduleDir = await ask(
rl,
'Submodule path (relative to parent repo)',
detectSubmodulePath(parentDir, scriptDir),
);
const submoduleName = submoduleDir.split('/').pop() || 'Hyveon';
let projectName = '';
while (!isValidProjectName(projectName)) {
projectName = await askRequired(rl, 'Project name (S3 bucket prefix; lowercase, dashes ok)', 'game-servers');
if (!isValidProjectName(projectName)) output.write(' ↳ must be 3–32 chars, lowercase letters/numbers/dashes.\n');
}
let awsRegion = '';
while (!isValidRegion(awsRegion)) {
awsRegion = await askRequired(rl, 'AWS region', 'us-east-1');
if (!isValidRegion(awsRegion)) output.write(' ↳ must look like "us-east-1".\n');
}
let hostedZone = '';
while (!isValidDomain(hostedZone)) {
hostedZone = await askRequired(rl, 'Route 53 hosted zone (e.g. example.com)');
if (!isValidDomain(hostedZone)) output.write(' ↳ must be a valid domain.\n');
}
const generated = randomBytes(32).toString('hex');
const apiTokenChoice = await ask(rl, 'API_TOKEN for the management app (press Enter to generate)', generated);
const apiToken = apiTokenChoice || generated;
const configureDiscord = await askBool(rl, 'Seed Discord credentials in tfvars now?', false);
let discordApplicationId: string | undefined;
let discordBotToken: string | undefined;
let discordPublicKey: string | undefined;
if (configureDiscord) {
discordApplicationId = await askRequired(rl, ' Discord Application ID');
discordBotToken = await askRequired(rl, ' Discord Bot Token');
discordPublicKey = await askRequired(rl, ' Discord Public Key');
}
const answers: Answers = {
parentDir,
submoduleDir,
submoduleName,
projectName,
awsRegion,
hostedZone,
apiToken,
configureDiscord,
discordApplicationId,
discordBotToken,
discordPublicKey,
};
output.write('\n Writing files…\n');
status(join(parentDir, 'Makefile'), writeIfSafe(join(parentDir, 'Makefile'), renderMakefile(answers)), parentDir);
status(join(parentDir, 'terraform.tfvars'), writeIfSafe(join(parentDir, 'terraform.tfvars'), renderTfvars(answers)), parentDir);
status(join(parentDir, '.env'), writeIfSafe(join(parentDir, '.env'), renderEnv(answers)), parentDir);
status(join(parentDir, '.gitignore'), writeIfSafe(join(parentDir, '.gitignore'), renderGitignore(answers)), parentDir);
output.write('\n ✓ Done.\n\n');
output.write(' Next steps:\n');
output.write(` 1. Review terraform.tfvars and add at least one entry under game_servers.\n`);
output.write(` 2. Run \`make setup\` to bootstrap the submodule and Terraform.\n`);
output.write(` 3. Run \`make plan\` then \`make apply\`.\n`);
output.write(` 4. \`make dev\` to launch the management app on :5173.\n\n`);
if (existsSync(join(parentDir, '.gitmodules'))) {
const gm = readFileSync(join(parentDir, '.gitmodules'), 'utf8');
if (!gm.includes(submoduleDir)) {
output.write(` Note: ${submoduleDir} is not in .gitmodules. Add it with:\n`);
output.write(` git submodule add https://github.com/CoderCoco/Hyveon.git ${submoduleDir}\n\n`);
}
} else {
output.write(` Note: no .gitmodules found. Add the submodule with:\n`);
output.write(` git submodule add https://github.com/CoderCoco/Hyveon.git ${submoduleDir}\n\n`);
}
} finally {
rl.close();
}
}
// Only run when this file is the entry point — keeps the renderers importable
// from tests without auto-launching the prompt loop. Compare normalized
// absolute paths so relative invocations (e.g. `tsx init-parent.ts`) still
// match.
const isEntrypoint =
argv[1] !== undefined && fileURLToPath(import.meta.url) === resolve(argv[1]);
if (isEntrypoint) {
main().catch((err) => {
process.stderr.write(`\n ✗ ${err instanceof Error ? err.message : String(err)}\n`);
exit(1);
});
}