-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonboarding.py
More file actions
654 lines (547 loc) · 23.5 KB
/
onboarding.py
File metadata and controls
654 lines (547 loc) · 23.5 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
"""ZineCore2 Server Onboarding TUI — interactive setup wizard powered by Textual."""
from __future__ import annotations
import asyncio
import os
import re
import secrets
import shutil
import string
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Awaitable, Callable
from textual import on, work
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Horizontal, Vertical
from textual.widgets import Input, ProgressBar, RichLog, Static
# ---------------------------------------------------------------------------
# Project root (directory containing this script)
# ---------------------------------------------------------------------------
PROJECT_ROOT = Path(__file__).parent.resolve()
BACKEND_DIR = PROJECT_ROOT / "backend"
MANAGE_PY = ".venv/bin/python backend/manage.py"
DJANGO_ENV = {"DJANGO_SETTINGS_MODULE": "zinecore.settings.development"}
def _load_dotenv() -> dict[str, str]:
"""Read key=value pairs from .env (ignoring comments and blank lines)."""
env_path = PROJECT_ROOT / ".env"
if not env_path.exists():
return {}
result = {}
for line in env_path.read_text().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
key, _, value = line.partition("=")
result[key.strip()] = value.strip()
return result
# ---------------------------------------------------------------------------
# Step model
# ---------------------------------------------------------------------------
class StepStatus(Enum):
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
SKIPPED = "skipped"
FAILED = "failed"
@dataclass
class OnboardingStep:
name: str
run: Callable[..., Awaitable[None]]
needs_prompt: Callable[..., Awaitable[bool]] | None = None
prompt_text: str = ""
status: StepStatus = field(default=StepStatus.PENDING)
# ---------------------------------------------------------------------------
# Custom widgets
# ---------------------------------------------------------------------------
class StepList(Vertical):
"""Sidebar list of onboarding steps with status indicators."""
def __init__(self, steps: list[OnboardingStep], **kwargs) -> None:
super().__init__(**kwargs)
self.steps = steps
def compose(self) -> ComposeResult:
for i, step in enumerate(self.steps):
yield Static(
self._format(i, step),
id=f"step-{i}",
classes="step-item --pending",
markup=True,
)
def update_step(self, index: int, step: OnboardingStep) -> None:
widget = self.query_one(f"#step-{index}", Static)
widget.update(self._format(index, step))
for cls in ("--pending", "--running", "--completed", "--failed", "--skipped"):
widget.remove_class(cls)
widget.add_class(f"--{step.status.value}")
@staticmethod
def _format(index: int, step: OnboardingStep) -> str:
icons = {
StepStatus.PENDING: " ",
StepStatus.RUNNING: "> ",
StepStatus.COMPLETED: "[green]\u2713[/green] ",
StepStatus.SKIPPED: "[dim]--[/dim]",
StepStatus.FAILED: "[red]!![/red]",
}
icon = icons.get(step.status, " ")
return f"{icon} {step.name}"
# ---------------------------------------------------------------------------
# Main application
# ---------------------------------------------------------------------------
class OnboardingApp(App):
"""ZineCore2 Server Onboarding TUI."""
CSS_PATH = "onboarding.tcss"
TITLE = "ZineCore2 Server Onboarding"
BINDINGS = [
Binding("ctrl+c", "quit", "Quit", show=True, priority=True),
]
def __init__(self) -> None:
super().__init__()
self.steps: list[OnboardingStep] = self._define_steps()
self._prompt_event: asyncio.Event | None = None
self._prompt_answer: bool | None = None
self._current_proc: asyncio.subprocess.Process | None = None
# -- compose -------------------------------------------------------------
def compose(self) -> ComposeResult:
yield Static(" ZineCore2 Server Onboarding", id="masthead")
with Horizontal(id="main-content"):
with Vertical(id="sidebar"):
yield Static(self._progress_text(0), id="progress-info")
yield ProgressBar(
total=len(self.steps),
show_eta=False,
show_percentage=False,
id="progress-bar",
)
yield StepList(self.steps, id="step-list")
yield RichLog(
highlight=True,
markup=True,
auto_scroll=True,
id="log-panel",
)
with Vertical(id="prompt-bar"):
yield Static("", id="prompt-text", markup=True)
with Horizontal():
yield Input(placeholder="y/n", id="prompt-input", max_length=1)
def on_mount(self) -> None:
self._run_all_steps()
# -- step runner ---------------------------------------------------------
@work(exclusive=True)
async def _run_all_steps(self) -> None:
log = self.query_one("#log-panel", RichLog)
step_list = self.query_one("#step-list", StepList)
progress_bar = self.query_one("#progress-bar", ProgressBar)
total = len(self.steps)
for i, step in enumerate(self.steps):
step.status = StepStatus.RUNNING
step_list.update_step(i, step)
self._update_progress(i, total)
log.write(f"\n[bold]--- Step {i + 1}/{total}: {step.name} ---[/bold]")
user_said_yes: bool | None = None
if step.needs_prompt is not None:
should_prompt = await step.needs_prompt(self)
if should_prompt:
user_said_yes = await self._ask_user(step.prompt_text)
try:
await step.run(self, user_said_yes)
if step.status == StepStatus.RUNNING:
step.status = StepStatus.COMPLETED
except Exception as exc:
step.status = StepStatus.FAILED
log.write(f"[red]Error: {exc}[/red]")
step_list.update_step(i, step)
progress_bar.advance(1)
self._update_progress(i + 1, total)
self._show_summary(log)
# -- prompt mechanism ----------------------------------------------------
async def _ask_user(self, question: str) -> bool:
self._prompt_event = asyncio.Event()
self._prompt_answer = None
prompt_bar = self.query_one("#prompt-bar")
prompt_bar.add_class("--visible")
self.query_one("#prompt-text", Static).update(question)
prompt_input = self.query_one("#prompt-input", Input)
prompt_input.value = ""
prompt_input.focus()
await self._prompt_event.wait()
prompt_bar.remove_class("--visible")
return self._prompt_answer # type: ignore[return-value]
@on(Input.Submitted, "#prompt-input")
def _on_prompt_submitted(self, event: Input.Submitted) -> None:
value = event.value.strip().lower()
if value in ("y", "n"):
self._prompt_answer = value == "y"
if self._prompt_event:
self._prompt_event.set()
event.input.value = ""
else:
event.input.value = ""
self.query_one("#log-panel", RichLog).write(
"[yellow]Please enter 'y' or 'n'[/yellow]"
)
# -- subprocess helper ---------------------------------------------------
async def run_command(
self,
cmd: str,
*,
cwd: Path | None = None,
env: dict[str, str] | None = None,
check: bool = True,
) -> int:
merged_env = {**os.environ, **_load_dotenv(), **DJANGO_ENV, **(env or {})}
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
cwd=str(cwd or PROJECT_ROOT),
env=merged_env,
)
self._current_proc = proc
log = self.query_one("#log-panel", RichLog)
assert proc.stdout is not None
while True:
line = await proc.stdout.readline()
if not line:
break
log.write(line.decode("utf-8", errors="replace").rstrip("\n"))
returncode = await proc.wait()
self._current_proc = None
if check and returncode != 0:
raise RuntimeError(f"Command exited with code {returncode}")
return returncode
def log_message(self, text: str) -> None:
self.query_one("#log-panel", RichLog).write(text)
# -- progress helpers ----------------------------------------------------
def _progress_text(self, completed: int) -> str:
total = len(self.steps) if self.steps else 9
pct = int((completed / total) * 100) if total else 0
return f"Progress: {completed}/{total} {pct}%"
def _update_progress(self, completed: int, total: int) -> None:
pct = int((completed / total) * 100) if total else 0
self.query_one("#progress-info", Static).update(
f"Progress: {completed}/{total} {pct}%"
)
# -- completion summary --------------------------------------------------
def _show_summary(self, log: RichLog) -> None:
failed = [s for s in self.steps if s.status == StepStatus.FAILED]
skipped = [s for s in self.steps if s.status == StepStatus.SKIPPED]
log.write("")
log.write("[bold green]============================[/bold green]")
log.write("[bold green] Onboarding Complete! [/bold green]")
log.write("[bold green]============================[/bold green]")
log.write("")
if failed:
log.write(
f"[red]Failed steps: {', '.join(s.name for s in failed)}[/red]"
)
if skipped:
log.write(
f"[yellow]Skipped steps: {', '.join(s.name for s in skipped)}[/yellow]"
)
log.write("")
log.write("You can now run:")
log.write(" ./start.sh - Start development server")
log.write("")
log.write("Admin credentials:")
log.write(" Username: admin")
log.write(" Password: admin")
log.write("")
log.write("Services:")
log.write(" PostgreSQL: localhost:5433")
log.write(" Django: http://localhost:8000")
log.write(" Admin: http://localhost:8000/admin/")
log.write(" API docs: http://localhost:8000/api/docs/")
log.write("")
log.write("[dim]Press Ctrl+C to exit[/dim]")
# -- graceful quit -------------------------------------------------------
def action_quit(self) -> None:
if self._current_proc is not None:
try:
self._current_proc.terminate()
except ProcessLookupError:
pass
self.exit()
# -----------------------------------------------------------------------
# Step definitions
# -----------------------------------------------------------------------
def _define_steps(self) -> list[OnboardingStep]:
return [
OnboardingStep(
name="Docker Volumes",
run=step_docker_volumes,
needs_prompt=check_docker_volumes,
prompt_text="Delete and recreate docker volumes? (WARNING: deletes all data) (y/n):",
),
OnboardingStep(
name="Set up ENV",
run=step_env,
needs_prompt=check_env,
prompt_text="Regenerate .env with new secrets? (y/n):",
),
OnboardingStep(
name="Spec Submodule",
run=step_spec_submodule,
),
OnboardingStep(
name="Docker Compose",
run=step_docker_compose,
),
OnboardingStep(
name="Python Venv",
run=step_python_venv,
needs_prompt=check_python_venv,
prompt_text="Delete and recreate virtual environment? (y/n):",
),
OnboardingStep(
name="Database Setup",
run=step_database,
),
OnboardingStep(
name="Load GeoNames Places",
run=step_load_geonames,
),
OnboardingStep(
name="Load Vocabularies",
run=step_load_vocabularies,
),
OnboardingStep(
name="Load ISO 639-1 Languages",
run=step_load_languages,
),
OnboardingStep(
name="Import Fixtures",
run=step_import_fixture_data,
),
OnboardingStep(
name="Import Custom Fixtures",
run=step_import_custom_data,
),
OnboardingStep(
name="Create Superuser",
run=step_create_superuser,
),
]
# ---------------------------------------------------------------------------
# Prompt-condition checkers (return True if a prompt is needed)
# ---------------------------------------------------------------------------
async def check_docker_volumes(app: OnboardingApp) -> bool:
# Check for any containers (running or stopped) from this compose project
proc_ps = await asyncio.create_subprocess_shell(
"docker compose ps -a -q",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL,
cwd=str(PROJECT_ROOT),
)
stdout_ps, _ = await proc_ps.communicate()
has_containers = bool(stdout_ps.decode().strip())
# Check for volumes matching this compose project
proc_vol = await asyncio.create_subprocess_shell(
"docker volume ls --format '{{.Name}}'",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL,
cwd=str(PROJECT_ROOT),
)
stdout_vol, _ = await proc_vol.communicate()
names = stdout_vol.decode().strip().splitlines()
found = [n for n in names if n.startswith("zinecore2_")]
if has_containers or found:
if found:
app.log_message("Found existing data volumes:")
for name in found:
app.log_message(f" {name}")
if has_containers:
app.log_message("Found existing compose containers.")
return True
app.log_message("No existing docker volumes or containers found.")
return False
async def check_env(app: OnboardingApp) -> bool:
env_path = PROJECT_ROOT / ".env"
if env_path.exists():
app.log_message("Found existing .env file")
return True
return False
async def check_python_venv(app: OnboardingApp) -> bool:
venv_path = PROJECT_ROOT / ".venv"
if venv_path.is_dir():
app.log_message("Found existing .venv directory")
return True
return False
# ---------------------------------------------------------------------------
# Step implementations
# ---------------------------------------------------------------------------
async def step_docker_volumes(app: OnboardingApp, user_said_yes: bool | None) -> None:
if user_said_yes is None:
return
if user_said_yes:
app.log_message("Stopping containers and removing volumes...")
await app.run_command("docker compose down -v")
app.log_message("[green]\u2713 Volumes deleted[/green]")
else:
app.log_message("Keeping existing volumes, stopping containers...")
await app.run_command("docker compose down")
async def step_env(app: OnboardingApp, user_said_yes: bool | None) -> None:
env_path = PROJECT_ROOT / ".env"
example_path = PROJECT_ROOT / ".env.example"
if user_said_yes is False:
app.log_message("Keeping existing .env")
return
if user_said_yes is None:
app.log_message("Creating .env from .env.example...")
else:
app.log_message("Regenerating .env with new secrets...")
secret_key = secrets.token_urlsafe(50)
alphanumeric = string.ascii_letters + string.digits
postgres_password = "".join(secrets.choice(alphanumeric) for _ in range(16))
template = example_path.read_text()
content = template
content = re.sub(
r"^DJANGO_SECRET_KEY=.*$",
f"DJANGO_SECRET_KEY={secret_key}",
content,
flags=re.MULTILINE,
)
content = re.sub(
r"^DATABASE_PASSWORD=.*$",
f"DATABASE_PASSWORD={postgres_password}",
content,
flags=re.MULTILINE,
)
env_path.write_text(content)
app.log_message("[green]\u2713 .env written with new secrets[/green]")
app.log_message(f" DJANGO_SECRET_KEY={secret_key[:12]}...")
app.log_message(f" DATABASE_PASSWORD={postgres_password[:12]}...")
async def step_spec_submodule(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Initializing and updating spec submodule...")
await app.run_command("git submodule update --init --remote spec")
app.log_message("[green]\u2713 Spec submodule is up to date[/green]")
async def step_docker_compose(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Bringing up PostgreSQL...")
await app.run_command("docker compose up -d --wait")
app.log_message("[green]\u2713 PostgreSQL is ready[/green]")
async def step_python_venv(app: OnboardingApp, user_said_yes: bool | None) -> None:
if user_said_yes is True:
app.log_message("Removing .venv...")
shutil.rmtree(PROJECT_ROOT / ".venv", ignore_errors=True)
app.log_message("[green]\u2713 .venv removed[/green]")
elif user_said_yes is False:
app.log_message("Keeping existing .venv...")
app.log_message("Running uv sync...")
await app.run_command("uv sync")
app.log_message("[green]\u2713 Python dependencies installed[/green]")
async def step_database(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Waiting for PostgreSQL to accept connections...")
for attempt in range(10):
ret = await app.run_command(
"docker compose exec db pg_isready -U zinecore",
check=False,
)
if ret == 0:
break
app.log_message(f" Attempt {attempt + 1}/10 — waiting 3s...")
await asyncio.sleep(3)
else:
raise RuntimeError("PostgreSQL did not become ready in time")
app.log_message("Running migrations...")
await app.run_command(f"{MANAGE_PY} migrate")
app.log_message("[green]\u2713 Database migrations complete[/green]")
async def step_load_vocabularies(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Fetching controlled vocabularies from zinecore.org API...")
try:
await app.run_command(f"{MANAGE_PY} load_vocabularies")
except RuntimeError:
app.log_message(
"[yellow]Warning: Some vocabularies failed to fetch from API[/yellow]"
)
# Load external_id_systems and external_uri_types from data/ directory
app.log_message("Loading external identifier and URI vocabularies from data/ directory...")
try:
await app.run_command(
f"{MANAGE_PY} load_vocabularies --local --vocab external_id_systems --vocab-dir data"
)
await app.run_command(
f"{MANAGE_PY} load_vocabularies --local --vocab external_uri_types --vocab-dir data"
)
app.log_message("[green]\u2713 Vocabularies loaded[/green]")
except RuntimeError:
app.log_message(
"[yellow]Warning: Failed to load local vocabularies from data/. "
"You can retry later with: ./manage.py load_vocabularies --local --vocab-dir data[/yellow]"
)
for s in app.steps:
if s.name == "Load Vocabularies":
s.status = StepStatus.SKIPPED
break
async def step_load_languages(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Fetching ISO 639-1 language codes from Library of Congress...")
try:
await app.run_command(f"{MANAGE_PY} load_languages")
app.log_message("[green]\u2713 ISO 639-1 languages loaded[/green]")
except RuntimeError:
app.log_message(
"[yellow]Warning: Failed to fetch languages from LOC. "
"You can retry later with: ./manage.py load_languages[/yellow]"
)
for s in app.steps:
if s.name == "Load ISO 639-1 Languages":
s.status = StepStatus.SKIPPED
break
async def step_load_geonames(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Loading geographic places from GeoNames (countries, admin1, cities)...")
try:
await app.run_command(f"{MANAGE_PY} load_geonames")
app.log_message("[green]\u2713 GeoNames places loaded[/green]")
except RuntimeError:
app.log_message(
"[yellow]Warning: Failed to load GeoNames data. "
"You can retry later with: ./manage.py load_geonames[/yellow]"
)
for s in app.steps:
if s.name == "Load GeoNames Places":
s.status = StepStatus.SKIPPED
break
async def step_import_fixture_data(app: OnboardingApp, _user_said_yes: bool | None) -> None:
data_file = PROJECT_ROOT / "data" / "fixtures.json"
if not data_file.exists():
app.log_message("[yellow]data/fixtures.json not found, skipping[/yellow]")
for s in app.steps:
if s.name == "Import Fixtures":
s.status = StepStatus.SKIPPED
break
return
app.log_message("Loading sample data (agents, repos, zines, holdings)...")
await app.run_command(f"{MANAGE_PY} loaddata data/fixtures.json")
app.log_message("[green]\u2713 Sample data loaded[/green]")
async def step_import_custom_data(app: OnboardingApp, _user_said_yes: bool | None) -> None:
data_file = PROJECT_ROOT / "data" / "custom.json"
if not data_file.exists():
app.log_message("[yellow]data/custom.json not found, skipping[/yellow]")
for s in app.steps:
if s.name == "Import Custom Fixtures":
s.status = StepStatus.SKIPPED
break
return
app.log_message("Loading custom data (agents, repos, zines, holdings)...")
await app.run_command(f"{MANAGE_PY} loaddata data/custom.json")
app.log_message("[green]\u2713 Custom data loaded[/green]")
async def step_create_superuser(app: OnboardingApp, _user_said_yes: bool | None) -> None:
app.log_message("Ensuring admin superuser (username: admin, password: admin)...")
await app.run_command(
f'{MANAGE_PY} shell -c "'
"from django.contrib.auth import get_user_model;"
"User = get_user_model();"
"u, created = User.objects.get_or_create(username='admin', defaults={'email': 'admin@example.com', 'is_staff': True, 'is_superuser': True});"
"u.set_password('admin');"
"u.is_staff = True;"
"u.is_superuser = True;"
"u.save();"
"print('Created' if created else 'Reset password for', 'admin superuser')"
'"',
)
app.log_message("[green]\u2713 Superuser ready[/green]")
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
if __name__ == "__main__":
app = OnboardingApp()
app.run()