|
24 | 24 |
|
25 | 25 | LOG_FILE = "/tmp/nemoclaw-sandbox-create.log" |
26 | 26 | BREV_ENV_ID = os.environ.get("BREV_ENV_ID", "") |
| 27 | +_detected_brev_id = "" |
27 | 28 |
|
28 | 29 | _sandbox_lock = threading.Lock() |
29 | 30 | _sandbox_state = { |
|
34 | 35 | } |
35 | 36 |
|
36 | 37 |
|
| 38 | +def _extract_brev_id(host: str) -> str: |
| 39 | + """Extract the Brev environment ID from a Host header like '80810-xxx.brevlab.com'.""" |
| 40 | + match = re.match(r"\d+-(.+?)\.brevlab\.com", host) |
| 41 | + return match.group(1) if match else "" |
| 42 | + |
| 43 | + |
| 44 | +def _maybe_detect_brev_id(host: str) -> None: |
| 45 | + """Cache the Brev environment ID from the request Host header (idempotent).""" |
| 46 | + global _detected_brev_id |
| 47 | + if not _detected_brev_id: |
| 48 | + brev_id = _extract_brev_id(host) |
| 49 | + if brev_id: |
| 50 | + _detected_brev_id = brev_id |
| 51 | + |
| 52 | + |
37 | 53 | def _build_openclaw_url(token: str | None) -> str: |
38 | 54 | """Build the externally reachable OpenClaw URL. |
39 | 55 |
|
40 | 56 | Uses the Cloudflare tunnel pattern from nemoclaw-start.sh when |
41 | | - BREV_ENV_ID is available, otherwise falls back to localhost. |
| 57 | + BREV_ENV_ID is available (or detected from the request Host header), |
| 58 | + otherwise falls back to localhost. |
42 | 59 | """ |
43 | | - if BREV_ENV_ID: |
44 | | - url = f"https://187890-{BREV_ENV_ID}.brevlab.com/" |
| 60 | + brev_id = BREV_ENV_ID or _detected_brev_id |
| 61 | + if brev_id: |
| 62 | + url = f"https://187890-{brev_id}.brevlab.com/" |
45 | 63 | else: |
46 | 64 | url = "http://127.0.0.1:18789/" |
47 | 65 | if token: |
@@ -70,6 +88,21 @@ def _read_openclaw_token() -> str | None: |
70 | 88 | return None |
71 | 89 |
|
72 | 90 |
|
| 91 | +def _gateway_log_ready() -> bool: |
| 92 | + """True once nemoclaw-start.sh has launched the OpenClaw gateway. |
| 93 | +
|
| 94 | + The startup script prints this sentinel *after* ``openclaw gateway`` |
| 95 | + has been backgrounded and the auth token extracted, so its presence |
| 96 | + in the log is a reliable readiness signal — unlike a bare port check |
| 97 | + which fires as soon as the forwarding tunnel opens. |
| 98 | + """ |
| 99 | + try: |
| 100 | + with open(LOG_FILE) as f: |
| 101 | + return "OpenClaw gateway starting in background" in f.read() |
| 102 | + except FileNotFoundError: |
| 103 | + return False |
| 104 | + |
| 105 | + |
73 | 106 | def _cleanup_existing_sandbox(): |
74 | 107 | """Delete any leftover sandbox named 'nemoclaw' from a previous attempt.""" |
75 | 108 | try: |
@@ -153,8 +186,14 @@ def _stream_output(): |
153 | 186 |
|
154 | 187 | deadline = time.time() + 120 |
155 | 188 | while time.time() < deadline: |
156 | | - if _port_open("127.0.0.1", 18789): |
| 189 | + if _gateway_log_ready() and _port_open("127.0.0.1", 18789): |
157 | 190 | token = _read_openclaw_token() |
| 191 | + if token is None: |
| 192 | + for _ in range(5): |
| 193 | + time.sleep(1) |
| 194 | + token = _read_openclaw_token() |
| 195 | + if token is not None: |
| 196 | + break |
158 | 197 | url = _build_openclaw_url(token) |
159 | 198 | with _sandbox_lock: |
160 | 199 | _sandbox_state["status"] = "running" |
@@ -197,11 +236,13 @@ def end_headers(self): |
197 | 236 | # -- Routing -------------------------------------------------------- |
198 | 237 |
|
199 | 238 | def do_POST(self): |
| 239 | + _maybe_detect_brev_id(self.headers.get("Host", "")) |
200 | 240 | if self.path == "/api/install-openclaw": |
201 | 241 | return self._handle_install_openclaw() |
202 | 242 | self.send_error(404) |
203 | 243 |
|
204 | 244 | def do_GET(self): |
| 245 | + _maybe_detect_brev_id(self.headers.get("Host", "")) |
205 | 246 | if self.path == "/api/sandbox-status": |
206 | 247 | return self._handle_sandbox_status() |
207 | 248 | if self.path == "/api/connection-details": |
@@ -240,7 +281,9 @@ def _handle_sandbox_status(self): |
240 | 281 | with _sandbox_lock: |
241 | 282 | state = dict(_sandbox_state) |
242 | 283 |
|
243 | | - if state["status"] == "creating" and _port_open("127.0.0.1", 18789): |
| 284 | + if (state["status"] == "creating" |
| 285 | + and _gateway_log_ready() |
| 286 | + and _port_open("127.0.0.1", 18789)): |
244 | 287 | token = _read_openclaw_token() |
245 | 288 | url = _build_openclaw_url(token) |
246 | 289 | with _sandbox_lock: |
|
0 commit comments