# -*- coding: utf-8 -*- """Install Netdata + deploy host metrics UI to dy.nomadro.live (session-gated /netdata/).""" from __future__ import annotations import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "108.174.50.68" ROOT = Path(__file__).resolve().parents[1] APP = "/opt/douyinWEB" API_PORT = 8014 FILES = [ "backend/app/host_metrics.py", "backend/app/main.py", "backend/app/ops_panel.py", "frontend/src/components/HostResourceBar.tsx", "frontend/src/components/Shell.tsx", "frontend/src/app/ops/page.tsx", ] REMOTE = """#!/usr/bin/env bash set -euo pipefail API_PORT=__API_PORT__ echo "=== install netdata if missing ===" if ! command -v netdata >/dev/null 2>&1 && [ ! -x /usr/sbin/netdata ]; then curl -fsSL https://get.netdata.cloud/kickstart.sh -o /tmp/netdata-kickstart.sh sh /tmp/netdata-kickstart.sh --non-interactive --stable-channel --disable-telemetry --dont-wait || true fi systemctl enable netdata 2>/dev/null || true echo "=== netdata: localhost + subpath /netdata + disable cloud ===" mkdir -p /etc/systemd/system/netdata.service.d cat >/etc/systemd/system/netdata.service.d/web-path.conf <<'EOF' [Service] Environment=NETDATA_WEB_PATH=/netdata EOF if [ -f /etc/netdata/netdata.conf ]; then if grep -q '^[[:space:]]*bind to' /etc/netdata/netdata.conf; then sed -i 's/^[[:space:]]*bind to = .*/ bind to = 127.0.0.1/' /etc/netdata/netdata.conf || true else printf '\\n[web]\\n bind to = 127.0.0.1\\n' >>/etc/netdata/netdata.conf fi fi mkdir -p /etc/netdata touch /etc/netdata/netdata.conf if grep -q '^\\[cloud\\]' /etc/netdata/netdata.conf; then sed -i '/^\\[cloud\\]/,/^\\[/{{ /^[[:space:]]*enabled =/s/.*/ enabled = no/; }}' /etc/netdata/netdata.conf || true else printf '\\n[cloud]\\n enabled = no\\n' >>/etc/netdata/netdata.conf fi systemctl daemon-reload systemctl restart netdata sleep 3 systemctl is-active netdata curl -sS -o /dev/null -w "netdata_subpath=%{{http_code}}\\n" http://127.0.0.1:19999/netdata/api/v1/info echo "=== nginx /netdata/ (auth_request + websocket) ===" CONF=/etc/nginx/conf.d/dy-nomadro-live.conf python3 <<'PY' import re from pathlib import Path API_PORT = "__API_PORT__" conf = Path("/etc/nginx/conf.d/dy-nomadro-live.conf") text = conf.read_text(encoding="utf-8") bak = conf.with_suffix(".conf.bak.netdata-fix") if not bak.exists(): bak.write_text(text, encoding="utf-8") for pat in ( r"\\n location = /_douyinweb_netdata_auth \\{.*?\\n \\}\\n?", r"\\n location @douyinweb_netdata_login \\{.*?\\n \\}\\n?", r"\\n location \\^~ /netdata/ \\{.*?\\n \\}\\n?", ): text = re.sub(pat, "\\n", text, flags=re.DOTALL) block = f''' location = /_douyinweb_netdata_auth {{ internal; proxy_pass http://127.0.0.1:{API_PORT}/api/auth/netdata-gate; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Cookie $http_cookie; proxy_set_header Host $host; }} location @douyinweb_netdata_login {{ return 302 /login?next=/ops; }} location ^~ /netdata/ {{ auth_request /_douyinweb_netdata_auth; error_page 401 = @douyinweb_netdata_login; proxy_pass http://127.0.0.1:19999/netdata/; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 600s; proxy_buffering off; proxy_hide_header X-Frame-Options; add_header X-Frame-Options SAMEORIGIN always; }} ''' marker = " location ^~ /douyin-covers/" if marker not in text: marker = " location / {" if "_douyinweb_netdata_auth" not in text: text = text.replace(marker, block + marker, 1) conf.write_text(text, encoding="utf-8") print("nginx patched", conf) PY nginx -t nginx -s reload curl -sS -o /dev/null -w "netdata_nginx_no_cookie=%{{http_code}}\\n" -k https://127.0.0.1/netdata/ -H "Host: dy.nomadro.live" || true echo "=== build web ===" cd /opt/douyinWEB/frontend npm run build 2>&1 | tail -8 systemctl restart douyinweb-web sleep 3 systemctl is-active douyinweb-web echo "=== restart api ===" systemctl restart douyinweb-api sleep 4 systemctl is-active douyinweb-api curl -sS http://127.0.0.1:$API_PORT/api/ops/host-metrics | head -c 200 echo echo DONE """.replace("__API_PORT__", str(API_PORT)) def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, "root", "Xiao4669805", timeout=60, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: print("upload", rel) sftp.put(str(ROOT / rel), f"{APP}/{rel.replace(chr(92), '/')}") with sftp.file("/tmp/deploy_netdata.sh", "w") as f: f.write(REMOTE) sftp.chmod("/tmp/deploy_netdata.sh", 0o755) sftp.close() chan = c.get_transport().open_session() chan.settimeout(600) chan.exec_command("bash /tmp/deploy_netdata.sh") out = b"" deadline = time.time() + 580 while time.time() < deadline: if chan.recv_ready(): out += chan.recv(8192) elif chan.exit_status_ready(): while chan.recv_ready(): out += chan.recv(8192) break else: time.sleep(0.5) print(out.decode("utf-8", "replace")[-12000:]) if chan.exit_status_ready(): print("exit", chan.recv_exit_status()) c.close() if __name__ == "__main__": main()