| 1 | import json |
| 2 | import time |
| 3 | import urllib.error |
| 4 | import urllib.request |
| 5 | from pathlib import Path |
| 6 | |
| 7 | |
| 8 | def wait(config_path='/etc/claude-cage/browser-control.json', endpoints=None, timeout=60): |
| 9 | token = json.loads(Path(config_path).read_text())['token'] |
| 10 | endpoints = endpoints or ( |
| 11 | ('http://127.0.0.1:18080/healthz', False), |
| 12 | ('http://127.0.0.1:16080/vnc.html', False), |
| 13 | ('http://127.0.0.1:18500/health', True), |
| 14 | ) |
| 15 | opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) |
| 16 | deadline = time.monotonic() + timeout |
| 17 | while time.monotonic() < deadline: |
| 18 | try: |
| 19 | for address, browser in endpoints: |
| 20 | headers = {'Authorization': 'Bearer ' + token} if browser else {} |
| 21 | request = urllib.request.Request(address, headers=headers) |
| 22 | with opener.open(request, timeout=2) as response: |
| 23 | if browser and json.loads(response.read(1024)).get('ready') is not True: |
| 24 | raise ValueError('Browser is starting') |
| 25 | return |
| 26 | except (OSError, ValueError, urllib.error.URLError): |
| 27 | time.sleep(1) |
| 28 | raise SystemExit('Cage browser, editor or display service did not become ready. Inspect the guest service logs and retry.') |
| 29 | |
| 30 | |
| 31 | if __name__ == '__main__': |
| 32 | wait() |