61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Configure ntfy ACL: public read on nomadcna-* topics, backend publish token."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import secrets
|
|
import sys
|
|
|
|
import paramiko
|
|
|
|
from load_dev_env import ssh_config
|
|
|
|
HOST, USER, SSH_PASSWORD = ssh_config()
|
|
|
|
|
|
|
|
|
|
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 120) -> str:
|
|
print(f"\n$ {cmd.replace(PASSWORD, '***')}")
|
|
_, o, e = c.exec_command(f"echo '{PASSWORD}' | sudo -S bash -lc \"{cmd}\"", timeout=timeout, get_pty=True)
|
|
out = o.read().decode("utf-8", errors="replace")
|
|
err = e.read().decode("utf-8", errors="replace")
|
|
sys.stdout.buffer.write(out.encode("utf-8", errors="replace"))
|
|
sys.stdout.buffer.write(err.encode("utf-8", errors="replace"))
|
|
return out + err
|
|
|
|
|
|
def main() -> None:
|
|
api_pass = secrets.token_urlsafe(16)
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, username=USER, password=PASSWORD, timeout=30)
|
|
try:
|
|
run(c, "docker exec nomadro-ntfy ntfy user add --role=admin nomadcna-api --pass='{}' 2>/dev/null || true".format(api_pass))
|
|
run(c, "docker exec nomadro-ntfy ntfy access nomadcna-* read everyone")
|
|
run(c, "docker exec nomadro-ntfy ntfy access nomadcna-* write nomadcna-api")
|
|
token_out = run(c, "docker exec nomadro-ntfy ntfy token add --user=nomadcna-api nomadcna-backend 2>&1")
|
|
token_match = re.search(r"tk_[a-zA-Z0-9_-]+", token_out)
|
|
token = token_match.group(0) if token_match else ""
|
|
|
|
if token:
|
|
run(c, f"grep -q NTFY_AUTH_TOKEN /opt/nomadro-services/.env || echo 'NTFY_AUTH_TOKEN={token}' >> /opt/nomadro-services/.env")
|
|
run(c, f"grep -q NTFY_AUTH_TOKEN /home/ubuntu/gitlab-instance-0a899031_cnomadcna/.env.local 2>/dev/null || echo 'NTFY_AUTH_TOKEN={token}' >> /home/ubuntu/gitlab-instance-0a899031_cnomadcna/.env.local")
|
|
|
|
test = run(
|
|
c,
|
|
f"curl -s -X POST https://ntfy.nomadro.cn/nomadcna-healthcheck "
|
|
f"-H 'Authorization: Bearer {token}' -H 'Title: NomadCNA' -d 'push ok'",
|
|
)
|
|
print("\n=== Token (save to FastAPI env) ===")
|
|
print(token or "(check token add output above)")
|
|
print("=== Test response ===")
|
|
print(test.strip()[:200])
|
|
finally:
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|