#!/usr/bin/env python3
"""
部署语料采集系统到广州服务器
通过 Gatekeeper API 远程操作
"""
import json
import urllib.request

GATEKEEPER = "http://43.139.217.141:3910"
TOKEN = "zy_gtw_4a155446b7acc09fe46a2a29972c0ca5d9954da19c35dc23"
HEADERS = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/json"
}

DEPLOY_DIR = "/data/guanghulab/server/corpus-agent"

def gatekeeper(method, path, data=None):
    url = f"{GATEKEEPER}/{path}"
    body = json.dumps(data).encode() if data else None
    req = urllib.request.Request(url, data=body, headers=HEADERS, method="POST")
    with urllib.request.urlopen(req, timeout=30) as resp:
        return json.loads(resp.read().decode())

def exec_cmd(cmd, timeout=30):
    print(f"  → {cmd[:80]}...")
    result = gatekeeper("exec", "exec", {"cmd": cmd, "timeout": timeout * 1000})
    stdout = result.get("stdout", "")
    stderr = result.get("stderr", "")
    if stdout.strip():
        for line in stdout.strip().split("\n"):
            print(f"    {line}")
    if stderr.strip():
        for line in stderr.strip().split("\n"):
            print(f"    [ERR] {line}")
    return result.get("exitCode", 0) == 0

def write_file(remote_path, content):
    print(f"  → 写入 {remote_path}")
    gatekeeper("write", "file/write", {"path": remote_path, "content": content})

def read_local(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

print("=" * 60)
print("部署语料采集系统到广州服务器")
print("=" * 60)

# Step 1: Create directory
print("\n[1/6] 创建目录...")
exec_cmd(f"mkdir -p {DEPLOY_DIR}")
exec_cmd(f"mkdir -p {DEPLOY_DIR}/static")
exec_cmd(f"mkdir -p {DEPLOY_DIR}/data")

# Step 2: Upload files
print("\n[2/6] 上传代码文件...")
LOCAL_DIR = "/workspace/guanghulab/server/corpus-agent"

for fname in ["server.py", "engine.py", "requirements.txt"]:
    content = read_local(f"{LOCAL_DIR}/{fname}")
    write_file(f"{DEPLOY_DIR}/{fname}", content)
    print(f"    ✓ {fname}")

# Upload static files
for fname in ["index.html"]:
    content = read_local(f"{LOCAL_DIR}/static/{fname}")
    write_file(f"{DEPLOY_DIR}/static/{fname}", content)
    print(f"    ✓ static/{fname}")

# Step 3: Install Python dependencies
print("\n[3/6] 安装 Python 依赖...")
exec_cmd(f"pip3 install httpx 2>&1 | tail -5", timeout=60)
exec_cmd(f"cd {DEPLOY_DIR} && pip3 install -r requirements.txt 2>&1 | tail -5", timeout=120)

# Step 4: Stop any existing service
print("\n[4/6] 停止旧服务...")
exec_cmd(f"pkill -f 'uvicorn.*corpus-agent' 2>/dev/null; sleep 1; echo 'done'")

# Step 5: Update nginx config
print("\n[5/6] 更新 Nginx 配置...")
nginx_conf = read_local(f"{LOCAL_DIR}/deploy-nginx.conf")
write_file("/etc/nginx/sites-enabled/guanghulab", nginx_conf)
exec_cmd("nginx -t 2>&1")
exec_cmd("systemctl reload nginx 2>&1 || nginx -s reload 2>&1")
print("    ✓ Nginx 配置已更新并重载")

# Step 6: Start the service
print("\n[6/6] 启动语料采集服务...")
start_cmd = (
    f"cd {DEPLOY_DIR} && "
    f"nohup python3 server.py > ../logs/corpus-agent.log 2>&1 & "
    f"echo 'PID: $!'"
)
exec_cmd(start_cmd)
exec_cmd("sleep 2")

# Verify
print("\n" + "=" * 60)
print("验证部署...")

result = gatekeeper("exec", "exec", {"cmd": "ps aux | grep corpus-agent/server.py | grep -v grep", "timeout": 5000})
stdout = result.get("stdout", "")
if stdout.strip():
    print(f"✓ 进程已启动: {stdout.strip()}")
else:
    print("✗ 进程未启动!")

result = gatekeeper("exec", "exec", {"cmd": "curl -s http://127.0.0.1:8084/api/health 2>/dev/null || echo 'FAIL'", "timeout": 5000})
stdout = result.get("stdout", "")
print(f"✓ 健康检查: {stdout[:200]}")

result = gatekeeper("exec", "exec", {"cmd": "curl -s -o /dev/null -w '%{http_code}' https://guanghulab.com/corpus/ 2>/dev/null || echo 'FAIL'", "timeout": 10000})
stdout = result.get("stdout", "")
print(f"✓ 公网访问: HTTP {stdout}")

print("\n" + "=" * 60)
print("部署完成!")
print(f"公网地址: https://guanghulab.com/corpus/")
print(f"本地端口: 127.0.0.1:8084")
print("=" * 60)
