add SETUP.md setup guide and setup_check.py, correct free-unit economics
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python
|
||||
"""Проверка окружения перед запуском авторега Runtime.
|
||||
|
||||
python setup_check.py # окружение и пути
|
||||
python setup_check.py --usage # плюс остатки юнитов по ключам из реестра
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
OK, BAD = "[ok] ", "[fail]"
|
||||
fails = []
|
||||
|
||||
|
||||
def check(name, fn):
|
||||
try:
|
||||
detail = fn()
|
||||
print(OK + name + ((" " + str(detail)) if detail else ""))
|
||||
except Exception as exc:
|
||||
fails.append(name)
|
||||
print(BAD + " " + name + " " + str(exc)[:160])
|
||||
|
||||
|
||||
def const(file, name):
|
||||
path = os.path.join(HERE, file)
|
||||
m = re.search(r'^%s\s*=\s*(?:os\.environ\.get\([^)]*\)\s*or\s*)?r?["\'](.+?)["\']' % name,
|
||||
open(path, encoding="utf-8").read(), re.M)
|
||||
if not m:
|
||||
raise RuntimeError("%s not found in %s" % (name, file))
|
||||
return m.group(1)
|
||||
|
||||
|
||||
def main():
|
||||
check("python >= 3.11", lambda: sys.version.split()[0]
|
||||
if sys.version_info >= (3, 11) else (_ for _ in ()).throw(RuntimeError("too old")))
|
||||
|
||||
def pw():
|
||||
import playwright
|
||||
from playwright.sync_api import sync_playwright # noqa: F401
|
||||
return getattr(playwright, "__version__", "installed")
|
||||
check("playwright", pw)
|
||||
|
||||
def chrome():
|
||||
p = const("wr_reg.py", "CHROME")
|
||||
if not os.path.exists(p):
|
||||
raise RuntimeError("нет файла " + p)
|
||||
return p
|
||||
check("chrome", chrome)
|
||||
|
||||
def port():
|
||||
s = socket.socket()
|
||||
try:
|
||||
s.bind(("127.0.0.1", 9223))
|
||||
return "9223 свободен"
|
||||
except OSError:
|
||||
return "9223 занят (браузер уже поднят)"
|
||||
finally:
|
||||
s.close()
|
||||
check("cdp port", port)
|
||||
|
||||
def web():
|
||||
req = urllib.request.Request("https://withruntime.com/api/analytics",
|
||||
headers={"User-Agent": "Mozilla/5.0"})
|
||||
with urllib.request.urlopen(req, timeout=20) as r:
|
||||
return "withruntime.com %s" % r.status
|
||||
check("site", web)
|
||||
|
||||
def apidoc():
|
||||
with urllib.request.urlopen("https://api.withruntime.com/v1/openapi.json", timeout=20) as r:
|
||||
return "openapi %s" % r.status
|
||||
check("api", apidoc)
|
||||
|
||||
def pool():
|
||||
p = const("wr_gfarm.py", "GPW")
|
||||
if not os.path.exists(p):
|
||||
raise RuntimeError("нет пула " + p)
|
||||
n = sum(1 for line in open(p, encoding="utf-8") if line.strip())
|
||||
return "%s (%d строк)" % (p, n)
|
||||
check("account pool", pool)
|
||||
|
||||
def registry():
|
||||
p = const("wr_api.py", "REG")
|
||||
if not os.path.exists(p):
|
||||
return "%s ещё не создан" % p
|
||||
rows = [json.loads(l) for l in open(p, encoding="utf-8") if l.strip()]
|
||||
return "%d ключей" % len(rows)
|
||||
check("key registry", registry)
|
||||
|
||||
if "--usage" in sys.argv:
|
||||
def usage():
|
||||
root = HERE
|
||||
sys.path.insert(0, root)
|
||||
rows = [json.loads(l) for l in open(const("wr_api.py", "REG"), encoding="utf-8") if l.strip()]
|
||||
live = 0
|
||||
for r in rows:
|
||||
req = urllib.request.Request("https://api.withruntime.com/v1/usage",
|
||||
headers={"Authorization": "Bearer " + r["key"]})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=25) as resp:
|
||||
d = json.loads(resp.read())
|
||||
except Exception:
|
||||
continue
|
||||
for a in d.get("allowances", []):
|
||||
if a["pool"] == "machine-cpu" and a["left"] > 0:
|
||||
live += 1
|
||||
break
|
||||
return "%d из %d с остатком CPU" % (live, len(rows))
|
||||
check("units", usage)
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print("ПРОВАЛЕНО: " + ", ".join(fails))
|
||||
return 1
|
||||
print("окружение готово")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user