-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
executable file
·469 lines (396 loc) · 15.3 KB
/
Copy pathstart.py
File metadata and controls
executable file
·469 lines (396 loc) · 15.3 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
#!/usr/bin/env python3
"""Start the local AgentCore Launchpad stack in the background."""
from __future__ import annotations
import argparse
import ipaddress
import json
import os
import shutil
import signal
import socket
import subprocess
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parent
RUN_DIR = ROOT / ".run"
LOG_DIR = RUN_DIR / "logs"
STATE_FILE = RUN_DIR / "launchpad.json"
CHECK_HOST = "127.0.0.1"
class LaunchError(RuntimeError):
"""A user-actionable local launch failure."""
@dataclass(frozen=True)
class Service:
name: str
cwd: Path
command: tuple[str, ...]
port: int
health_path: str
@property
def log_file(self) -> Path:
return LOG_DIR / f"{self.name}.log"
@property
def health_url(self) -> str:
return f"http://{CHECK_HOST}:{self.port}{self.health_path}"
def _yaml_setting(key: str) -> Any:
"""Read one key from config/launchpad.yaml, or None when unavailable.
Best-effort on purpose: this only feeds the pre-flight message below. The
binding control is the per-request guard in `app.routers.auth`, so a wrong
guess here changes when the operator learns about it, not whether the console
is protected.
"""
config_file = ROOT / "config" / "launchpad.yaml"
if not config_file.is_file():
return None
try:
import yaml
data = yaml.safe_load(config_file.read_text(encoding="utf-8"))
except Exception: # noqa: BLE001 — pre-flight must never be the thing that breaks a launch
return None
return data.get(key) if isinstance(data, dict) else None
def _truthy(raw: str | None) -> bool:
return (raw or "").strip().lower() in {"1", "true", "yes", "on"}
def _is_loopback_host(host: str) -> bool:
if host == "localhost":
return True
try:
return ipaddress.ip_address(host).is_loopback
except ValueError:
# A hostname we cannot classify, or an empty host (which servers read as
# "all interfaces") — treat it as reachable so the check fails closed.
return False
def _check_console_is_protected(hosts: list[str]) -> None:
"""Refuse to launch an unauthenticated console on a reachable interface.
Mirrors the request-time guard so the operator gets one clear message up
front instead of a console that refuses every call.
"""
if _truthy(os.environ.get("LAUNCHPAD_ALLOW_OPEN_CONSOLE")) or _yaml_setting(
"allow_open_console"
):
return
if os.environ.get("LAUNCHPAD_AUTH_PASSWORD") or _yaml_setting("auth_password"):
return
reachable = sorted({host for host in hosts if not _is_loopback_host(host)})
if not reachable:
return
raise LaunchError(
"refusing to serve an unauthenticated console on "
+ ", ".join(reachable)
+ ".\n"
" Set auth_password in config/launchpad.yaml (or LAUNCHPAD_AUTH_PASSWORD)\n"
" to enable the login gate, or LAUNCHPAD_ALLOW_OPEN_CONSOLE=true to accept\n"
" an open console on a reachable interface."
)
def _env_port(name: str, default: int) -> int:
raw = os.environ.get(name, str(default))
try:
port = int(raw)
except ValueError as exc:
raise LaunchError(f"{name} must be an integer, got {raw!r}") from exc
if not 1 <= port <= 65535:
raise LaunchError(f"{name} must be between 1 and 65535, got {port}")
return port
def _process_start_time(pid: int) -> str | None:
try:
stat = (Path("/proc") / str(pid) / "stat").read_text(encoding="utf-8")
fields = stat.rsplit(") ", 1)[1].split()
return fields[19]
except (IndexError, OSError):
return None
def _record_is_alive(record: dict[str, Any]) -> bool:
try:
pid = int(record["pid"])
expected_start = str(record["start_time"])
except (KeyError, TypeError, ValueError):
return False
return _process_start_time(pid) == expected_start
def _load_state() -> dict[str, Any] | None:
if not STATE_FILE.is_file():
return None
try:
state = json.loads(STATE_FILE.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as exc:
raise LaunchError(f"cannot read lifecycle state {STATE_FILE}: {exc}") from exc
if state.get("root") != str(ROOT) or not isinstance(state.get("services"), list):
raise LaunchError(f"invalid lifecycle state in {STATE_FILE}")
return state
def _write_state(mode: str, records: list[dict[str, Any]]) -> None:
RUN_DIR.mkdir(parents=True, exist_ok=True)
state = {
"version": 1,
"root": str(ROOT),
"mode": mode,
"started_at": datetime.now(UTC).isoformat(),
"services": records,
}
temporary = STATE_FILE.with_suffix(".tmp")
temporary.write_text(json.dumps(state, indent=2) + "\n", encoding="utf-8")
temporary.replace(STATE_FILE)
def _signal_record(record: dict[str, Any], sig: signal.Signals) -> None:
if not _record_is_alive(record):
return
try:
os.killpg(int(record["pgid"]), sig)
except ProcessLookupError:
pass
def _wait_for_exit(records: list[dict[str, Any]], timeout: float) -> list[dict[str, Any]]:
deadline = time.monotonic() + timeout
alive = [record for record in records if _record_is_alive(record)]
while alive and time.monotonic() < deadline:
# Deliberate shutdown polling interval; timeout_s bounds the wait.
time.sleep(0.2) # nosemgrep: arbitrary-sleep
alive = [record for record in alive if _record_is_alive(record)]
return alive
def _terminate_records(records: list[dict[str, Any]]) -> list[dict[str, Any]]:
owned = [record for record in records if _record_is_alive(record)]
for record in reversed(owned):
_signal_record(record, signal.SIGTERM)
remaining = _wait_for_exit(owned, 10)
for record in reversed(remaining):
_signal_record(record, signal.SIGKILL)
return _wait_for_exit(remaining, 2)
def stop() -> int:
state = _load_state()
if state is None:
print("AgentCore Launchpad is not running under start.py.")
return 0
records = state["services"]
owned = [record for record in records if _record_is_alive(record)]
if not owned:
STATE_FILE.unlink(missing_ok=True)
print("AgentCore Launchpad is not running (removed stale lifecycle state).")
return 0
print("Stopping AgentCore Launchpad...")
remaining = _terminate_records(owned)
STATE_FILE.unlink(missing_ok=True)
if remaining:
names = ", ".join(str(record.get("name", record.get("pid"))) for record in remaining)
raise LaunchError(f"could not stop: {names}")
print("AgentCore Launchpad stopped.")
return 0
def _port_is_listening(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.2)
return sock.connect_ex((CHECK_HOST, port)) == 0
def _ensure_command(name: str) -> None:
if shutil.which(name) is None:
raise LaunchError(f"required command not found: {name}")
def _ensure_frontend_dependencies(directory: Path) -> None:
if (directory / "node_modules" / ".bin" / "vite").is_file():
return
print(f"Installing frontend dependencies in {directory.relative_to(ROOT)}...")
subprocess.run(
["npm", "install", "--no-audit", "--no-fund"],
cwd=directory,
check=True,
)
def _build_frontend(directory: Path) -> None:
print(f"Building {directory.relative_to(ROOT)}...")
subprocess.run(["npm", "run", "build"], cwd=directory, check=True)
def _service_definitions(prod: bool) -> tuple[list[Service], dict[str, str]]:
mode = "prod" if prod else "dev"
public_host = os.environ.get(
"LAUNCHPAD_HOST",
"0.0.0.0" if prod else CHECK_HOST,
)
api_host = os.environ.get(
"LAUNCHPAD_API_HOST",
"0.0.0.0" if prod else CHECK_HOST,
)
_check_console_is_protected([public_host, api_host])
ports = {
"platform_api": _env_port("PLATFORM_API_PORT", 8000),
"platform_ui": _env_port("PLATFORM_UI_PORT", 5173),
}
if len(set(ports.values())) != len(ports):
raise LaunchError("PLATFORM_API_PORT and PLATFORM_UI_PORT must be unique")
platform_backend = [
"uv",
"run",
"uvicorn",
"app.main:app",
"--host",
api_host,
"--port",
str(ports["platform_api"]),
]
if not prod:
platform_backend.append("--reload")
frontend_command = "preview" if prod else "dev"
services = [
Service(
name="platform-backend",
cwd=ROOT / "backend",
command=tuple(platform_backend),
port=ports["platform_api"],
health_path="/api/health",
),
Service(
name="platform-frontend",
cwd=ROOT / "frontend",
command=(
"npm",
"run",
frontend_command,
"--",
"--host",
public_host,
"--port",
str(ports["platform_ui"]),
"--strictPort",
),
port=ports["platform_ui"],
health_path="/",
),
]
child_env = os.environ.copy()
child_env.update(
{
"LAUNCHPAD_RUN_MODE": mode,
"LAUNCHPAD_API": f"http://{CHECK_HOST}:{ports['platform_api']}",
"PLATFORM_API_PORT": str(ports["platform_api"]),
"PLATFORM_UI_PORT": str(ports["platform_ui"]),
}
)
return services, child_env
def _http_ready(url: str) -> bool:
try:
with urllib.request.urlopen(url, timeout=1) as response:
return response.status < 400
except (OSError, urllib.error.URLError):
return False
def _tail_log(path: Path, line_count: int = 20) -> str:
try:
lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
except OSError:
return "(log unavailable)"
return "\n".join(lines[-line_count:])
def _wait_until_ready(service: Service, process: subprocess.Popen[bytes]) -> None:
deadline = time.monotonic() + 45
while time.monotonic() < deadline:
return_code = process.poll()
if return_code is not None:
raise LaunchError(
f"{service.name} exited with code {return_code}\n"
f"{_tail_log(service.log_file)}"
)
if _http_ready(service.health_url):
return
# Deliberate readiness polling interval; service.timeout_s bounds the wait.
time.sleep(0.5) # nosemgrep: arbitrary-sleep
raise LaunchError(
f"{service.name} did not become healthy at {service.health_url}\n"
f"{_tail_log(service.log_file)}"
)
def _print_running(mode: str, records: list[dict[str, Any]]) -> None:
ports = {record["name"]: record["port"] for record in records}
print(f"AgentCore Launchpad running ({mode}).")
print(f" Console: http://localhost:{ports['platform-frontend']}")
print(f" API docs: http://localhost:{ports['platform-frontend']}/api/docs")
print(f" Logs: {LOG_DIR.relative_to(ROOT)}/")
print(" Stop: ./stop.sh")
def start(prod: bool) -> int:
_ensure_command("uv")
_ensure_command("npm")
services, child_env = _service_definitions(prod)
mode = "prod" if prod else "dev"
existing = _load_state()
if existing is not None:
records = existing["services"]
if records and all(_record_is_alive(record) for record in records):
current_names = [record.get("name") for record in records]
requested_names = [service.name for service in services]
if existing.get("mode") == mode and current_names == requested_names:
_print_running(mode, records)
return 0
raise LaunchError(
"a different launch mode or service set is already running; "
"run ./stop.sh before starting the requested stack"
)
print("Cleaning up a partial or stale previous launch...")
_terminate_records(records)
STATE_FILE.unlink(missing_ok=True)
for service in services:
if _port_is_listening(service.port):
raise LaunchError(
f"port {service.port} for {service.name} is already in use; "
"override it with the corresponding *_PORT environment variable"
)
frontend_directories = [ROOT / "frontend"]
for directory in frontend_directories:
_ensure_frontend_dependencies(directory)
if prod:
for directory in frontend_directories:
_build_frontend(directory)
LOG_DIR.mkdir(parents=True, exist_ok=True)
records: list[dict[str, Any]] = []
processes: dict[str, subprocess.Popen[bytes]] = {}
try:
for service in services:
with service.log_file.open("ab") as log:
log.write(
f"\n[{datetime.now(UTC).isoformat()}] starting {mode}\n".encode()
)
# Service commands are internal argv tuples; no shell is involved.
process = subprocess.Popen( # nosemgrep: dangerous-subprocess-use-audit
service.command,
cwd=service.cwd,
env=child_env,
stdin=subprocess.DEVNULL,
stdout=log,
stderr=subprocess.STDOUT,
start_new_session=True,
)
start_time = _process_start_time(process.pid)
if start_time is None:
raise LaunchError(f"could not inspect started process {process.pid}")
record = {
"name": service.name,
"pid": process.pid,
"pgid": process.pid,
"start_time": start_time,
"port": service.port,
"log_file": str(service.log_file.relative_to(ROOT)),
"command": list(service.command),
}
records.append(record)
processes[service.name] = process
_write_state(mode, records)
for service in services:
_wait_until_ready(service, processes[service.name])
except (Exception, KeyboardInterrupt):
_terminate_records(records)
STATE_FILE.unlink(missing_ok=True)
raise
_print_running(mode, records)
return 0
def main() -> int:
parser = argparse.ArgumentParser(
description="Start the local AgentCore Launchpad stack in the background."
)
action = parser.add_mutually_exclusive_group()
action.add_argument(
"--prod",
action="store_true",
help="build the platform frontend and run without backend auto-reload",
)
action.add_argument("--stop", action="store_true", help=argparse.SUPPRESS)
args = parser.parse_args()
try:
return stop() if args.stop else start(args.prod)
except subprocess.CalledProcessError as exc:
print(f"error: command failed with exit code {exc.returncode}", file=sys.stderr)
return exc.returncode or 1
except LaunchError as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
except KeyboardInterrupt:
print("Interrupted.", file=sys.stderr)
return 130
if __name__ == "__main__":
raise SystemExit(main())