cloud19 commited on
Commit
0d2889a
·
verified ·
1 Parent(s): 8568992

обновление agent.py

Browse files
Files changed (1) hide show
  1. agent.py +25 -15
agent.py CHANGED
@@ -255,21 +255,31 @@ class PublicAddress:
255
  label = _read("/root/.vast_containerlabel")
256
  instance_id = label.replace("C.", "").strip()
257
  if key and instance_id:
258
- try:
259
- data = http_json("GET", f"{VAST_API}/instances/{instance_id}/",
260
- headers={"Authorization": f"Bearer {key}"}, timeout=30)[1]
261
- inst = data["instances"]
262
- self.host = self.host or inst.get("public_ipaddr", "").strip()
263
- for spec, binds in (inst.get("ports") or {}).items():
264
- inner = int(spec.split("/")[0])
265
- for b in binds:
266
- if b.get("HostIp") == "0.0.0.0":
267
- self.ports[inner] = int(b["HostPort"])
268
- log(f"публичный адрес: {self.host}, проброшено портов: {len(self.ports)}")
269
- self._loaded = True
270
- return
271
- except Exception as e:
272
- log(f"Vast API не ответил ({e}), падаю на определение адреса снаружи")
 
 
 
 
 
 
 
 
 
 
273
 
274
  if not self.host:
275
  try:
 
255
  label = _read("/root/.vast_containerlabel")
256
  instance_id = label.replace("C.", "").strip()
257
  if key and instance_id:
258
+ # Здесь нельзя откатываться на «внешний ip + тот же порт»: снаружи
259
+ # порт другой, и нода зарегистрируется по адресу, куда менеджер не
260
+ # достучится. Лучше долбиться в API, пока не ответит.
261
+ for attempt in range(1, 11):
262
+ try:
263
+ status, data = http_json("GET", f"{VAST_API}/instances/{instance_id}/",
264
+ headers={"Authorization": f"Bearer {key}"}, timeout=30)
265
+ if status != 200:
266
+ raise RuntimeError(f"HTTP {status}: {str(data)[:200]}")
267
+ inst = data["instances"]
268
+ self.host = self.host or inst.get("public_ipaddr", "").strip()
269
+ for spec, binds in (inst.get("ports") or {}).items():
270
+ inner = int(spec.split("/")[0])
271
+ for b in binds:
272
+ if b.get("HostIp") == "0.0.0.0":
273
+ self.ports[inner] = int(b["HostPort"])
274
+ if not self.host or not self.ports:
275
+ raise RuntimeError("Vast ещё не отдал адрес и проброс портов")
276
+ log(f"публичный адрес: {self.host}, проброшено портов: {len(self.ports)}")
277
+ self._loaded = True
278
+ return
279
+ except Exception as e:
280
+ log(f"Vast API не ответил (попытка {attempt} из 10): {e}")
281
+ time.sleep(15)
282
+ raise RuntimeError("Vast API так и не отдал проброс портов")
283
 
284
  if not self.host:
285
  try: