-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·1285 lines (1053 loc) · 41.6 KB
/
setup.py
File metadata and controls
executable file
·1285 lines (1053 loc) · 41.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""TOD Production Setup Wizard.
Interactive setup script that configures .env.prod, bootstraps OpenBao,
builds Docker images, starts the stack, and verifies service health.
Usage:
python3 setup.py
"""
import base64
import datetime
import getpass
import os
import pathlib
import re
import secrets
import shutil
import signal
import socket
import subprocess
import sys
import time
# ── Constants ────────────────────────────────────────────────────────────────
PROJECT_ROOT = pathlib.Path(__file__).resolve().parent
ENV_PROD = PROJECT_ROOT / ".env.prod"
INIT_SQL_TEMPLATE = PROJECT_ROOT / "scripts" / "init-postgres.sql"
INIT_SQL_PROD = PROJECT_ROOT / "scripts" / "init-postgres-prod.sql"
COMPOSE_BASE = "docker-compose.yml"
COMPOSE_PROD = "docker-compose.prod.yml"
COMPOSE_CMD = [
"docker", "compose",
"-f", COMPOSE_BASE,
"-f", COMPOSE_PROD,
]
REQUIRED_PORTS = {
5432: "PostgreSQL",
6379: "Redis",
4222: "NATS",
8001: "API",
3000: "Frontend",
51820: "WireGuard (UDP)",
}
# ── Color helpers ────────────────────────────────────────────────────────────
def _supports_color() -> bool:
return hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
_COLOR = _supports_color()
def _c(code: str, text: str) -> str:
return f"\033[{code}m{text}\033[0m" if _COLOR else text
def green(t: str) -> str: return _c("32", t)
def yellow(t: str) -> str: return _c("33", t)
def red(t: str) -> str: return _c("31", t)
def cyan(t: str) -> str: return _c("36", t)
def bold(t: str) -> str: return _c("1", t)
def dim(t: str) -> str: return _c("2", t)
def banner(text: str) -> None:
width = 62
print()
print(cyan("=" * width))
print(cyan(f" {text}"))
print(cyan("=" * width))
print()
def section(text: str) -> None:
print()
print(bold(f"--- {text} ---"))
print()
def ok(text: str) -> None:
print(f" {green('✓')} {text}")
def warn(text: str) -> None:
print(f" {yellow('!')} {text}")
def fail(text: str) -> None:
print(f" {red('✗')} {text}")
def info(text: str) -> None:
print(f" {dim('·')} {text}")
# ── Input helpers ────────────────────────────────────────────────────────────
def ask(prompt: str, default: str = "", required: bool = False,
secret: bool = False, validate=None) -> str:
"""Prompt the user for input with optional default, validation, and secret mode."""
suffix = f" [{default}]" if default else ""
full_prompt = f" {prompt}{suffix}: "
while True:
if secret:
value = getpass.getpass(full_prompt)
else:
value = input(full_prompt)
value = value.strip()
if not value and default:
value = default
if required and not value:
warn("This field is required.")
continue
if validate:
error = validate(value)
if error:
warn(error)
continue
return value
def ask_yes_no(prompt: str, default: bool = False) -> bool:
"""Ask a yes/no question."""
hint = "Y/n" if default else "y/N"
while True:
answer = input(f" {prompt} [{hint}]: ").strip().lower()
if not answer:
return default
if answer in ("y", "yes"):
return True
if answer in ("n", "no"):
return False
warn("Please enter y or n.")
def mask_secret(value: str) -> str:
"""Show first 8 chars of a secret, mask the rest."""
if len(value) <= 12:
return "*" * len(value)
return value[:8] + "..."
# ── Validators ───────────────────────────────────────────────────────────────
def validate_password_strength(value: str) -> str | None:
if len(value) < 12:
return "Password must be at least 12 characters."
return None
def validate_email(value: str) -> str | None:
if not re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", value):
return "Please enter a valid email address."
return None
def validate_domain(value: str) -> str | None:
# Strip protocol if provided
cleaned = re.sub(r"^https?://", "", value).rstrip("/")
if not re.match(r"^[a-zA-Z0-9]([a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$", cleaned):
return "Please enter a valid domain (e.g. tod.example.com)."
return None
# ── System checks ────────────────────────────────────────────────────────────
def check_python_version() -> bool:
if sys.version_info < (3, 10):
fail(f"Python 3.10+ required, found {sys.version}")
return False
ok(f"Python {sys.version_info.major}.{sys.version_info.minor}")
return True
def check_docker() -> bool:
try:
result = subprocess.run(
["docker", "info"],
capture_output=True, text=True, timeout=10,
)
if result.returncode != 0:
fail("Docker is not running. Start Docker and try again.")
return False
ok("Docker Engine")
except FileNotFoundError:
fail("Docker is not installed.")
return False
except subprocess.TimeoutExpired:
fail("Docker is not responding.")
return False
try:
result = subprocess.run(
["docker", "compose", "version"],
capture_output=True, text=True, timeout=10,
)
if result.returncode != 0:
fail("Docker Compose v2 is not available.")
return False
version_match = re.search(r"v?(\d+\.\d+)", result.stdout)
version_str = version_match.group(1) if version_match else "unknown"
ok(f"Docker Compose v{version_str}")
except FileNotFoundError:
fail("Docker Compose is not installed.")
return False
return True
def check_ram() -> None:
try:
if sys.platform == "darwin":
result = subprocess.run(
["sysctl", "-n", "hw.memsize"],
capture_output=True, text=True, timeout=5,
)
if result.returncode != 0:
return
ram_bytes = int(result.stdout.strip())
else:
with open("/proc/meminfo") as f:
for line in f:
if line.startswith("MemTotal:"):
ram_bytes = int(line.split()[1]) * 1024
break
else:
return
ram_gb = ram_bytes / (1024 ** 3)
if ram_gb < 4:
warn(f"Only {ram_gb:.1f} GB RAM detected. 4 GB+ recommended for builds.")
else:
ok(f"{ram_gb:.1f} GB RAM")
except Exception:
info("Could not detect RAM — skipping check")
def check_ports() -> None:
for port, service in REQUIRED_PORTS.items():
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
result = s.connect_ex(("127.0.0.1", port))
if result == 0:
warn(f"Port {port} ({service}) is already in use")
else:
ok(f"Port {port} ({service}) is free")
except Exception:
info(f"Could not check port {port} ({service})")
def check_existing_env() -> str:
"""Check for existing .env.prod. Returns 'overwrite', 'backup', or 'abort'."""
if not ENV_PROD.exists():
return "overwrite"
print()
warn(f"Existing .env.prod found at {ENV_PROD}")
print()
print(" What would you like to do?")
print(f" {bold('1)')} Overwrite it")
print(f" {bold('2)')} Back it up and create a new one")
print(f" {bold('3)')} Abort")
print()
while True:
choice = input(" Choice [1/2/3]: ").strip()
if choice == "1":
return "overwrite"
elif choice == "2":
ts = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
backup = ENV_PROD.with_name(f".env.prod.backup.{ts}")
shutil.copy2(ENV_PROD, backup)
ok(f"Backed up to {backup.name}")
return "overwrite"
elif choice == "3":
return "abort"
else:
warn("Please enter 1, 2, or 3.")
def preflight() -> bool:
"""Run all pre-flight checks. Returns True if OK to proceed."""
banner("TOD Production Setup")
print(" This wizard will configure your production environment,")
print(" generate secrets, bootstrap OpenBao, build images, and")
print(" start the stack.")
print()
section("Pre-flight Checks")
if not check_python_version():
return False
if not check_docker():
return False
check_ram()
check_ports()
action = check_existing_env()
if action == "abort":
print()
info("Setup aborted.")
return False
return True
# ── Secret generation ────────────────────────────────────────────────────────
def generate_jwt_secret() -> str:
return secrets.token_urlsafe(64)
def generate_encryption_key() -> str:
return base64.b64encode(secrets.token_bytes(32)).decode()
def generate_db_password() -> str:
return secrets.token_urlsafe(24)
def generate_admin_password() -> str:
return secrets.token_urlsafe(18)
# ── Wizard sections ─────────────────────────────────────────────────────────
def wizard_database(config: dict) -> None:
section("Database")
info("PostgreSQL superuser password — used for migrations and admin operations.")
info("The app and poller service passwords will be auto-generated.")
print()
config["postgres_password"] = ask(
"PostgreSQL superuser password",
required=True,
secret=True,
validate=validate_password_strength,
)
config["app_user_password"] = generate_db_password()
config["poller_user_password"] = generate_db_password()
config["postgres_db"] = "tod"
ok("Database passwords configured")
info(f"app_user password: {mask_secret(config['app_user_password'])}")
info(f"poller_user password: {mask_secret(config['poller_user_password'])}")
def wizard_security(config: dict) -> None:
section("Security")
info("Auto-generating cryptographic keys...")
print()
config["jwt_secret"] = generate_jwt_secret()
config["encryption_key"] = generate_encryption_key()
ok("JWT signing key generated")
ok("Credential encryption key generated")
print()
warn("Save these somewhere safe — they cannot be recovered if lost:")
info(f"JWT_SECRET_KEY={mask_secret(config['jwt_secret'])}")
info(f"CREDENTIAL_ENCRYPTION_KEY={mask_secret(config['encryption_key'])}")
def wizard_admin(config: dict) -> None:
section("Admin Account")
info("The first admin account is created on initial startup.")
print()
config["admin_email"] = ask(
"Admin email",
default="admin@the-other-dude.dev",
required=True,
validate=validate_email,
)
print()
info("Enter a password or press Enter to auto-generate one.")
password = ask("Admin password", secret=True)
if password:
error = validate_password_strength(password)
while error:
warn(error)
password = ask("Admin password", secret=True, required=True,
validate=validate_password_strength)
error = None # ask() already validated
config["admin_password"] = password
config["admin_password_generated"] = False
else:
config["admin_password"] = generate_admin_password()
config["admin_password_generated"] = True
ok(f"Generated password: {bold(config['admin_password'])}")
warn("Save this now — it will not be shown again after setup.")
def wizard_email(config: dict) -> None:
section("Email (SMTP)")
info("Email is used for password reset links.")
print()
if not ask_yes_no("Configure SMTP now?", default=False):
config["smtp_configured"] = False
info("Skipped — you can re-run setup.py later to configure email.")
return
config["smtp_configured"] = True
config["smtp_host"] = ask("SMTP host", required=True)
config["smtp_port"] = ask("SMTP port", default="587")
config["smtp_user"] = ask("SMTP username (optional)")
config["smtp_password"] = ask("SMTP password (optional)", secret=True) if config["smtp_user"] else ""
config["smtp_from"] = ask("From address", required=True, validate=validate_email)
config["smtp_tls"] = ask_yes_no("Use TLS?", default=True)
def wizard_domain(config: dict) -> None:
section("Web / Domain")
info("Your production domain, used for CORS and email links.")
print()
raw = ask("Production domain (e.g. tod.example.com)", required=True, validate=validate_domain)
domain = re.sub(r"^https?://", "", raw).rstrip("/")
config["domain"] = domain
config["app_base_url"] = f"https://{domain}"
config["cors_origins"] = f"https://{domain}"
ok(f"APP_BASE_URL=https://{domain}")
ok(f"CORS_ORIGINS=https://{domain}")
# ── Reverse proxy ───────────────────────────────────────────────────────────
PROXY_EXAMPLES = PROJECT_ROOT / "infrastructure" / "reverse-proxy-examples"
PROXY_CONFIGS = {
"caddy": {
"label": "Caddy",
"binary": "caddy",
"example": PROXY_EXAMPLES / "caddy" / "Caddyfile.example",
"targets": [
pathlib.Path("/etc/caddy/Caddyfile.d"),
pathlib.Path("/etc/caddy"),
],
"filename": None, # derived from domain
"placeholders": {
"tod.example.com": None, # replaced with domain
"YOUR_TOD_HOST": None, # replaced with host IP
},
},
"nginx": {
"label": "nginx",
"binary": "nginx",
"example": PROXY_EXAMPLES / "nginx" / "tod.conf.example",
"targets": [
pathlib.Path("/etc/nginx/sites-available"),
pathlib.Path("/etc/nginx/conf.d"),
],
"filename": None,
"placeholders": {
"tod.example.com": None,
"YOUR_TOD_HOST": None,
},
},
"apache": {
"label": "Apache",
"binary": "apache2",
"alt_binary": "httpd",
"example": PROXY_EXAMPLES / "apache" / "tod.conf.example",
"targets": [
pathlib.Path("/etc/apache2/sites-available"),
pathlib.Path("/etc/httpd/conf.d"),
],
"filename": None,
"placeholders": {
"tod.example.com": None,
"YOUR_TOD_HOST": None,
},
},
"haproxy": {
"label": "HAProxy",
"binary": "haproxy",
"example": PROXY_EXAMPLES / "haproxy" / "haproxy.cfg.example",
"targets": [
pathlib.Path("/etc/haproxy"),
],
"filename": "haproxy.cfg",
"placeholders": {
"tod.example.com": None,
"YOUR_TOD_HOST": None,
},
},
"traefik": {
"label": "Traefik",
"binary": "traefik",
"example": PROXY_EXAMPLES / "traefik" / "traefik-dynamic.yaml.example",
"targets": [
pathlib.Path("/etc/traefik/dynamic"),
pathlib.Path("/etc/traefik"),
],
"filename": None,
"placeholders": {
"tod.example.com": None,
"YOUR_TOD_HOST": None,
},
},
}
def _detect_proxy(name: str, cfg: dict) -> bool:
"""Check if a reverse proxy binary is installed."""
binary = cfg["binary"]
if shutil.which(binary):
return True
alt = cfg.get("alt_binary")
if alt and shutil.which(alt):
return True
return False
def _get_host_ip() -> str:
"""Best-effort detection of the host's LAN IP."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
def _write_system_file(path: pathlib.Path, content: str) -> bool:
"""Write a file, using sudo tee if direct write fails with permission error."""
# Try direct write first
try:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
return True
except PermissionError:
pass
# Fall back to sudo
info(f"Need elevated permissions to write to {path.parent}")
if not ask_yes_no("Use sudo?", default=True):
warn("Skipped. You can copy the config manually later.")
return False
try:
# Ensure parent directory exists
subprocess.run(
["sudo", "mkdir", "-p", str(path.parent)],
check=True, timeout=30,
)
# Write via sudo tee
result = subprocess.run(
["sudo", "tee", str(path)],
input=content, text=True,
capture_output=True, timeout=30,
)
if result.returncode != 0:
fail(f"sudo tee failed: {result.stderr.strip()}")
return False
return True
except subprocess.CalledProcessError as e:
fail(f"sudo failed: {e}")
return False
except Exception as e:
fail(f"Failed to write config: {e}")
return False
def wizard_reverse_proxy(config: dict) -> None:
section("Reverse Proxy")
info("TOD needs a reverse proxy for HTTPS termination.")
info("Example configs are included for Caddy, nginx, Apache, HAProxy, and Traefik.")
print()
if not ask_yes_no("Configure a reverse proxy now?", default=True):
config["proxy_configured"] = False
info("Skipped. Example configs are in infrastructure/reverse-proxy-examples/")
return
# Detect installed proxies
detected = []
for name, cfg in PROXY_CONFIGS.items():
if _detect_proxy(name, cfg):
detected.append(name)
if detected:
print()
info(f"Detected: {', '.join(PROXY_CONFIGS[n]['label'] for n in detected)}")
else:
print()
info("No reverse proxy detected on this system.")
# Show menu
print()
print(" Which reverse proxy are you using?")
choices = list(PROXY_CONFIGS.keys())
for i, name in enumerate(choices, 1):
label = PROXY_CONFIGS[name]["label"]
tag = f" {green('(detected)')}" if name in detected else ""
print(f" {bold(f'{i})')} {label}{tag}")
print(f" {bold(f'{len(choices) + 1})')} Skip — I'll configure it myself")
print()
while True:
choice = input(f" Choice [1-{len(choices) + 1}]: ").strip()
if not choice.isdigit():
warn("Please enter a number.")
continue
idx = int(choice) - 1
if idx == len(choices):
config["proxy_configured"] = False
info("Skipped. Example configs are in infrastructure/reverse-proxy-examples/")
return
if 0 <= idx < len(choices):
break
warn(f"Please enter 1-{len(choices) + 1}.")
selected = choices[idx]
cfg = PROXY_CONFIGS[selected]
domain = config["domain"]
host_ip = _get_host_ip()
# Read and customize the example config
if not cfg["example"].exists():
fail(f"Example config not found: {cfg['example']}")
config["proxy_configured"] = False
return
template = cfg["example"].read_text()
# Replace placeholders
output = template.replace("tod.example.com", domain)
output = output.replace("YOUR_TOD_HOST", host_ip)
# Determine output filename
if cfg["filename"]:
out_name = cfg["filename"]
else:
safe_domain = domain.replace(".", "-")
ext = cfg["example"].suffix.replace(".example", "") or ".conf"
if cfg["example"].name == "Caddyfile.example":
out_name = f"{safe_domain}.caddy"
else:
out_name = f"{safe_domain}{ext}"
# Find a writable target directory
target_dir = None
for candidate in cfg["targets"]:
if candidate.is_dir():
target_dir = candidate
break
print()
if target_dir:
out_path = target_dir / out_name
info(f"Will write: {out_path}")
else:
# Fall back to project directory
out_path = PROJECT_ROOT / out_name
info(f"No standard config directory found for {cfg['label']}.")
info(f"Will write to: {out_path}")
print()
info("Preview (first 20 lines):")
for line in output.splitlines()[:20]:
print(f" {dim(line)}")
print(f" {dim('...')}")
print()
custom_path = ask(f"Write config to", default=str(out_path))
out_path = pathlib.Path(custom_path)
if out_path.exists():
if not ask_yes_no(f"{out_path} already exists. Overwrite?", default=False):
info("Skipped writing proxy config.")
config["proxy_configured"] = False
return
written = _write_system_file(out_path, output)
if not written:
config["proxy_configured"] = False
return
ok(f"Wrote {cfg['label']} config to {out_path}")
config["proxy_configured"] = True
config["proxy_type"] = cfg["label"]
config["proxy_path"] = str(out_path)
# Post-install hints
print()
if selected == "caddy":
info("Reload Caddy: sudo systemctl reload caddy")
elif selected == "nginx":
if "/sites-available/" in str(out_path):
sites_enabled = out_path.parent.parent / "sites-enabled" / out_path.name
info(f"Enable site: sudo ln -s {out_path} {sites_enabled}")
info("Test config: sudo nginx -t")
info("Reload nginx: sudo systemctl reload nginx")
elif selected == "apache":
if "/sites-available/" in str(out_path):
info(f"Enable site: sudo a2ensite {out_path.stem}")
info("Test config: sudo apachectl configtest")
info("Reload Apache: sudo systemctl reload apache2")
elif selected == "haproxy":
info("Test config: sudo haproxy -c -f /etc/haproxy/haproxy.cfg")
info("Reload: sudo systemctl reload haproxy")
elif selected == "traefik":
info("Traefik watches for file changes — no reload needed.")
# ── Summary ──────────────────────────────────────────────────────────────────
def show_summary(config: dict) -> bool:
banner("Configuration Summary")
print(f" {bold('Database')}")
print(f" POSTGRES_DB = {config['postgres_db']}")
print(f" POSTGRES_PASSWORD = {mask_secret(config['postgres_password'])}")
print(f" app_user password = {mask_secret(config['app_user_password'])}")
print(f" poller_user password = {mask_secret(config['poller_user_password'])}")
print()
print(f" {bold('Security')}")
print(f" JWT_SECRET_KEY = {mask_secret(config['jwt_secret'])}")
print(f" ENCRYPTION_KEY = {mask_secret(config['encryption_key'])}")
print()
print(f" {bold('Admin Account')}")
print(f" Email = {config['admin_email']}")
print(f" Password = {'(auto-generated)' if config.get('admin_password_generated') else mask_secret(config['admin_password'])}")
print()
print(f" {bold('Email')}")
if config.get("smtp_configured"):
print(f" SMTP_HOST = {config['smtp_host']}")
print(f" SMTP_PORT = {config['smtp_port']}")
print(f" SMTP_FROM = {config['smtp_from']}")
print(f" SMTP_TLS = {config['smtp_tls']}")
else:
print(f" {dim('(not configured)')}")
print()
print(f" {bold('Web')}")
print(f" Domain = {config['domain']}")
print(f" APP_BASE_URL = {config['app_base_url']}")
print()
print(f" {bold('Reverse Proxy')}")
if config.get("proxy_configured"):
print(f" Type = {config['proxy_type']}")
print(f" Config = {config['proxy_path']}")
else:
print(f" {dim('(not configured)')}")
print()
print(f" {bold('OpenBao')}")
print(f" {dim('(will be captured automatically during bootstrap)')}")
print()
return ask_yes_no("Write .env.prod with these settings?", default=True)
# ── File writers ─────────────────────────────────────────────────────────────
def write_env_prod(config: dict) -> None:
"""Write the .env.prod file."""
db = config["postgres_db"]
pg_pw = config["postgres_password"]
app_pw = config["app_user_password"]
poll_pw = config["poller_user_password"]
ts = datetime.datetime.now().isoformat(timespec="seconds")
smtp_block = ""
if config.get("smtp_configured"):
smtp_block = f"""\
SMTP_HOST={config['smtp_host']}
SMTP_PORT={config['smtp_port']}
SMTP_USER={config.get('smtp_user', '')}
SMTP_PASSWORD={config.get('smtp_password', '')}
SMTP_USE_TLS={'true' if config.get('smtp_tls') else 'false'}
SMTP_FROM_ADDRESS={config['smtp_from']}"""
else:
smtp_block = """\
# Email not configured — re-run setup.py to add SMTP
SMTP_HOST=
SMTP_PORT=587
SMTP_USER=
SMTP_PASSWORD=
SMTP_USE_TLS=true
SMTP_FROM_ADDRESS=noreply@example.com"""
content = f"""\
# ============================================================
# TOD Production Environment — generated by setup.py
# Generated: {ts}
# ============================================================
# --- Database ---
POSTGRES_DB={db}
POSTGRES_USER=postgres
POSTGRES_PASSWORD={pg_pw}
DATABASE_URL=postgresql+asyncpg://postgres:{pg_pw}@postgres:5432/{db}
SYNC_DATABASE_URL=postgresql+psycopg2://postgres:{pg_pw}@postgres:5432/{db}
APP_USER_DATABASE_URL=postgresql+asyncpg://app_user:{app_pw}@postgres:5432/{db}
POLLER_DATABASE_URL=postgres://poller_user:{poll_pw}@postgres:5432/{db}
# --- Security ---
JWT_SECRET_KEY={config['jwt_secret']}
CREDENTIAL_ENCRYPTION_KEY={config['encryption_key']}
# --- OpenBao (KMS) ---
OPENBAO_ADDR=http://openbao:8200
OPENBAO_TOKEN=PLACEHOLDER_RUN_SETUP
BAO_UNSEAL_KEY=PLACEHOLDER_RUN_SETUP
# --- Admin Bootstrap ---
FIRST_ADMIN_EMAIL={config['admin_email']}
FIRST_ADMIN_PASSWORD={config['admin_password']}
# --- Email ---
{smtp_block}
# --- Web ---
APP_BASE_URL={config['app_base_url']}
CORS_ORIGINS={config['cors_origins']}
# --- Application ---
ENVIRONMENT=production
LOG_LEVEL=info
DEBUG=false
APP_NAME=TOD - The Other Dude
# --- Storage ---
GIT_STORE_PATH=/data/git-store
FIRMWARE_CACHE_DIR=/data/firmware-cache
WIREGUARD_CONFIG_PATH=/data/wireguard
WIREGUARD_GATEWAY=wireguard
CONFIG_RETENTION_DAYS=90
# --- Redis & NATS ---
REDIS_URL=redis://redis:6379/0
NATS_URL=nats://nats:4222
# --- Poller ---
POLL_INTERVAL_SECONDS=60
CONNECTION_TIMEOUT_SECONDS=10
COMMAND_TIMEOUT_SECONDS=30
# --- Remote Access ---
TUNNEL_PORT_MIN=49000
TUNNEL_PORT_MAX=49100
TUNNEL_IDLE_TIMEOUT=300
SSH_RELAY_PORT=8080
SSH_IDLE_TIMEOUT=900
# --- Config Backup ---
CONFIG_BACKUP_INTERVAL=21600
CONFIG_BACKUP_MAX_CONCURRENT=10
"""
ENV_PROD.write_text(content)
ENV_PROD.chmod(0o600)
ok(f"Wrote {ENV_PROD.name}")
def write_init_sql_prod(config: dict) -> None:
"""Generate init-postgres-prod.sql with production passwords."""
app_pw = config["app_user_password"]
poll_pw = config["poller_user_password"]
db = config["postgres_db"]
# Use dollar-quoting ($pw$...$pw$) to avoid SQL injection from passwords
content = f"""\
-- Production database init — generated by setup.py
-- Passwords match those in .env.prod
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'app_user') THEN
CREATE ROLE app_user WITH LOGIN PASSWORD $pw${app_pw}$pw$ NOSUPERUSER NOCREATEDB NOCREATEROLE;
END IF;
END
$$;
GRANT CONNECT ON DATABASE {db} TO app_user;
GRANT USAGE ON SCHEMA public TO app_user;
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'poller_user') THEN
CREATE ROLE poller_user WITH LOGIN PASSWORD $pw${poll_pw}$pw$ NOSUPERUSER NOCREATEDB NOCREATEROLE BYPASSRLS;
END IF;
END
$$;
GRANT CONNECT ON DATABASE {db} TO poller_user;
GRANT USAGE ON SCHEMA public TO poller_user;
"""
INIT_SQL_PROD.write_text(content)
INIT_SQL_PROD.chmod(0o644) # postgres container needs to read this
ok(f"Wrote {INIT_SQL_PROD.name}")
# ── Data directory setup ─────────────────────────────────────────────────────
# UID 1001 = appuser inside the API container
APPUSER_UID = 1001
# Directories the API container writes to (as appuser)
API_WRITABLE_DIRS = [
"docker-data/git-store",
"docker-data/firmware-cache",
]
# Directories that need broad write access (shared between containers)
SHARED_WRITABLE_DIRS = [
"docker-data/wireguard/wg_confs",
]
# Directories that just need to exist (owned by their respective containers)
DATA_DIRS = [
"docker-data/postgres",
"docker-data/redis",
"docker-data/nats",
"docker-data/wireguard",
"docker-data/wireguard/custom-cont-init.d",
]
def prepare_data_dirs() -> None:
"""Create data directories with correct ownership and permissions."""
section("Preparing Data Directories")
# Create all directories
for d in DATA_DIRS + API_WRITABLE_DIRS + SHARED_WRITABLE_DIRS:
path = PROJECT_ROOT / d
path.mkdir(parents=True, exist_ok=True)
# Set ownership for API-writable dirs (appuser uid 1001)
for d in API_WRITABLE_DIRS:
path = PROJECT_ROOT / d
try:
os.chown(path, APPUSER_UID, APPUSER_UID)
ok(f"{d} (owned by appuser)")
except PermissionError:
# Try with sudo
try:
subprocess.run(
["sudo", "chown", "-R", f"{APPUSER_UID}:{APPUSER_UID}", str(path)],
check=True, timeout=10,
)
ok(f"{d} (owned by appuser via sudo)")
except Exception:
warn(f"{d} — could not set ownership, backups/firmware may fail")
# Set permissions for shared dirs (API + WireGuard container both write)
for d in SHARED_WRITABLE_DIRS:
path = PROJECT_ROOT / d
try:
path.chmod(0o777)
ok(f"{d} (world-writable for container sharing)")
except PermissionError:
try:
subprocess.run(
["sudo", "chmod", "-R", "777", str(path)],
check=True, timeout=10,
)
ok(f"{d} (world-writable via sudo)")
except Exception:
warn(f"{d} — could not set permissions, VPN config sync may fail")
# Create/update WireGuard forwarding init script (always overwrite for isolation rules)
fwd_script = PROJECT_ROOT / "docker-data/wireguard/custom-cont-init.d/10-forwarding.sh"
fwd_script.write_text("""\
#!/bin/sh
# Enable forwarding between Docker network and WireGuard tunnel
# Idempotent: check before adding to prevent duplicates on restart
# Allow Docker→VPN (poller/API reaching devices)
iptables -C FORWARD -i eth0 -o wg0 -j ACCEPT 2>/dev/null || iptables -A FORWARD -i eth0 -o wg0 -j ACCEPT
# Allow VPN→Docker ONLY (devices reaching poller/API, NOT the public internet)
iptables -C FORWARD -i wg0 -o eth0 -d 172.16.0.0/12 -j ACCEPT 2>/dev/null || iptables -A FORWARD -i wg0 -o eth0 -d 172.16.0.0/12 -j ACCEPT
# Block VPN→anywhere else (prevents using server as exit node)
iptables -C FORWARD -i wg0 -o eth0 -j DROP 2>/dev/null || iptables -A FORWARD -i wg0 -o eth0 -j DROP
# Block cross-subnet traffic on wg0 (tenant isolation)
# Peers in 10.10.1.0/24 cannot reach peers in 10.10.2.0/24
iptables -C FORWARD -i wg0 -o wg0 -j DROP 2>/dev/null || iptables -A FORWARD -i wg0 -o wg0 -j DROP