chankhavu commited on
Commit
a48ecfa
·
verified ·
1 Parent(s): cf7f6db

Daemon: drop RELAY_SECRET; auth via HF token only

Browse files
daemon/__pycache__/client.cpython-311.pyc CHANGED
Binary files a/daemon/__pycache__/client.cpython-311.pyc and b/daemon/__pycache__/client.cpython-311.pyc differ
 
daemon/client.py CHANGED
@@ -6,8 +6,7 @@ executes shell commands in persistent named bash sessions.
6
  Run it (foreground, or under nohup / tmux / systemd):
7
 
8
  export RELAY_SPACE="your-username/nii-relay" # the Space repo id
9
- export RELAY_SECRET="<same secret as the Space>"
10
- export HF_TOKEN="hf_..." # read token; needed if Space is private
11
  export CLIENT_ID="$(hostname)" # how it shows up in the UI (optional)
12
  python client.py
13
 
@@ -70,7 +69,6 @@ def _default_client_id() -> str:
70
 
71
 
72
  SPACE = os.environ.get("RELAY_SPACE", "")
73
- SECRET = os.environ.get("RELAY_SECRET", "")
74
  HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
75
  CLIENT_ID = os.environ.get("CLIENT_ID") or _default_client_id()
76
  POLL_INTERVAL = float(os.environ.get("POLL_INTERVAL", "5"))
@@ -314,7 +312,7 @@ def connect() -> Client:
314
  try:
315
  log(f"connecting to Space {SPACE} …")
316
  c = _make_client()
317
- c.predict(CLIENT_ID, SECRET, _meta(), api_name="/register")
318
  CLIENT = c
319
  log(f"registered as '{CLIENT_ID}'")
320
  return c
@@ -333,7 +331,7 @@ def _post(*args, **kwargs):
333
 
334
  def _safe_result(cid: str, code: int, output: str, session: str):
335
  try:
336
- _post(cid, CLIENT_ID, SECRET, code, output, session, api_name="/result")
337
  except Exception as e:
338
  log(f"result post failed for {session}: {type(e).__name__}: {e}")
339
 
@@ -360,7 +358,7 @@ def do_put(cmd: dict):
360
  dest = _expand(cmd.get("dest") or cmd.get("filename") or "downloaded.bin")
361
  log(f"[file] put → {dest}")
362
  try:
363
- ret = _post(cmd["file_id"], SECRET, api_name="/fetch_file")
364
  src = _result_path(ret)
365
  if not src or not os.path.isfile(src):
366
  raise FileNotFoundError("relay returned no file")
@@ -391,13 +389,13 @@ def do_get(cmd: dict):
391
  raise ValueError(f"{size} bytes exceeds the {MAX_FILE_BYTES}-byte limit")
392
  except Exception as e:
393
  try:
394
- _post(cid, CLIENT_ID, SECRET, None, f"{e}", api_name="/upload_result")
395
  except Exception as e2:
396
  log(f"error report failed: {type(e2).__name__}: {e2}")
397
  log(f"[file] get → error: {e}")
398
  return
399
  try:
400
- _post(cid, CLIENT_ID, SECRET, handle_file(remote), "", api_name="/upload_result")
401
  log(f"[file] get → sent {size} bytes")
402
  except Exception as e:
403
  log(f"[file] get upload failed: {type(e).__name__}: {e}")
@@ -465,14 +463,12 @@ def _shutdown():
465
  def main():
466
  if not SPACE:
467
  sys.exit("set RELAY_SPACE to the Space repo id, e.g. your-username/nii-relay")
468
- if not SECRET:
469
- log("WARNING: RELAY_SECRET is empty — the relay will reject this daemon")
470
  log(f"daemon starting — client_id='{CLIENT_ID}', poll every {POLL_INTERVAL}s")
471
  threading.Thread(target=_shell_reaper, daemon=True).start()
472
  connect()
473
  while True:
474
  try:
475
- res = CLIENT.predict(CLIENT_ID, SECRET, api_name="/poll")
476
  for cmd in (res or {}).get("commands", []):
477
  get_worker(cmd.get("session") or "main").submit(cmd)
478
  except KeyboardInterrupt:
 
6
  Run it (foreground, or under nohup / tmux / systemd):
7
 
8
  export RELAY_SPACE="your-username/nii-relay" # the Space repo id
9
+ export HF_TOKEN="hf_..." # token with access to the private Space (this is the auth)
 
10
  export CLIENT_ID="$(hostname)" # how it shows up in the UI (optional)
11
  python client.py
12
 
 
69
 
70
 
71
  SPACE = os.environ.get("RELAY_SPACE", "")
 
72
  HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
73
  CLIENT_ID = os.environ.get("CLIENT_ID") or _default_client_id()
74
  POLL_INTERVAL = float(os.environ.get("POLL_INTERVAL", "5"))
 
312
  try:
313
  log(f"connecting to Space {SPACE} …")
314
  c = _make_client()
315
+ c.predict(CLIENT_ID, _meta(), api_name="/register")
316
  CLIENT = c
317
  log(f"registered as '{CLIENT_ID}'")
318
  return c
 
331
 
332
  def _safe_result(cid: str, code: int, output: str, session: str):
333
  try:
334
+ _post(cid, CLIENT_ID, code, output, session, api_name="/result")
335
  except Exception as e:
336
  log(f"result post failed for {session}: {type(e).__name__}: {e}")
337
 
 
358
  dest = _expand(cmd.get("dest") or cmd.get("filename") or "downloaded.bin")
359
  log(f"[file] put → {dest}")
360
  try:
361
+ ret = _post(cmd["file_id"], api_name="/fetch_file")
362
  src = _result_path(ret)
363
  if not src or not os.path.isfile(src):
364
  raise FileNotFoundError("relay returned no file")
 
389
  raise ValueError(f"{size} bytes exceeds the {MAX_FILE_BYTES}-byte limit")
390
  except Exception as e:
391
  try:
392
+ _post(cid, CLIENT_ID, None, f"{e}", api_name="/upload_result")
393
  except Exception as e2:
394
  log(f"error report failed: {type(e2).__name__}: {e2}")
395
  log(f"[file] get → error: {e}")
396
  return
397
  try:
398
+ _post(cid, CLIENT_ID, handle_file(remote), "", api_name="/upload_result")
399
  log(f"[file] get → sent {size} bytes")
400
  except Exception as e:
401
  log(f"[file] get upload failed: {type(e).__name__}: {e}")
 
463
  def main():
464
  if not SPACE:
465
  sys.exit("set RELAY_SPACE to the Space repo id, e.g. your-username/nii-relay")
 
 
466
  log(f"daemon starting — client_id='{CLIENT_ID}', poll every {POLL_INTERVAL}s")
467
  threading.Thread(target=_shell_reaper, daemon=True).start()
468
  connect()
469
  while True:
470
  try:
471
+ res = CLIENT.predict(CLIENT_ID, api_name="/poll")
472
  for cmd in (res or {}).get("commands", []):
473
  get_worker(cmd.get("session") or "main").submit(cmd)
474
  except KeyboardInterrupt: