Files
buryan-infra/ops/creds/immich_key2.py
T

64 lines
2.3 KiB
Python

"""Задаёт пароль Immich и создаёт API-ключ по API."""
import json
import subprocess
import urllib.error
import urllib.request
import bcrypt
PW_DB = open("/root/.pg-superuser-pass").read().strip()
NEW_PASS = "HubImmich2026!"
EMAIL = "maloyakapg@gmail.com"
h = bcrypt.hashpw(NEW_PASS.encode(), bcrypt.gensalt(rounds=10)).decode()
def sh(c, timeout=60):
r = subprocess.run(["pct", "exec", "151", "--", "bash", "-c", c],
capture_output=True, text=True, timeout=timeout)
return (r.stdout or r.stderr or "").strip()
sql = (f"update public.\"user\" set password = '{h}', \"shouldChangePassword\" = false "
f"where email = '{EMAIL}';")
open("/tmp/immich_pw.sql", "w").write(sql)
subprocess.run(["pct", "push", "151", "/tmp/immich_pw.sql", "/tmp/immich_pw.sql", "--perms", "600"],
capture_output=True, text=True)
out = sh(f"PGPASSWORD={PW_DB} psql -h 127.0.0.1 -U postgres -d immich_db < /tmp/immich_pw.sql")
print("обновление пароля:", out or "ок")
def api(path, method="GET", body=None, token=None):
headers = {"Content-Type": "application/json"}
if token:
headers["Authorization"] = "Bearer " + token
data = json.dumps(body).encode() if body is not None else None
r = urllib.request.Request("http://192.168.1.104:2283" + path, data=data,
method=method, headers=headers)
try:
with urllib.request.urlopen(r, timeout=25) as x:
raw = x.read()
return (json.loads(raw) if raw else {}), None
except urllib.error.HTTPError as e:
return None, f"HTTP {e.code}: {e.read()[:200].decode('utf-8', 'replace')}"
except Exception as e:
return None, str(e)[:120]
res, err = api("/api/auth/login", "POST", {"email": EMAIL, "password": NEW_PASS})
print("логин:", err or "ок")
token = (res or {}).get("accessToken")
if not token:
print(" ответ:", str(res)[:200])
raise SystemExit(1)
print(" токен получен")
key_res, err2 = api("/api/api-keys", "POST", {"name": "buryans-hub", "permissions": ["all"]}, token=token)
print("создание ключа:", err2 or "ок")
key = (key_res or {}).get("secret")
if key:
open("/root/.immich-key-new", "w").write(key)
print(" ключ сохранён:", key[:12], "…")
else:
print(" ответ:", str(key_res)[:200])