#!/usr/bin/env python3
"""
上传新的 Dockerfile 并重新构建
"""
import requests
import time
import hashlib
import os
import base64

# 禁用 SSL 警告
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 宝塔配置
BT_PANEL = "http://47.107.172.23:8888"
BT_API_KEY = "PKdfnaInQL0P5ghB8SvwbrGcIpXWaEvq"

DEPLOY_PATH = "/www/wwwroot/ai-interview"


def bt_api(action, data=None, timeout=60):
    """调用宝塔 API"""
    if data is None:
        data = {}
    
    request_time = int(time.time())
    request_token = hashlib.md5(
        f"{request_time}{hashlib.md5(BT_API_KEY.encode()).hexdigest()}".encode()
    ).hexdigest()
    
    data['request_time'] = request_time
    data['request_token'] = request_token
    
    url = f"{BT_PANEL}/{action}"
    
    try:
        response = requests.post(url, data=data, timeout=timeout, verify=False)
        try:
            return response.json()
        except:
            return {"status": True, "msg": response.text[:1000]}
    except Exception as e:
        return {"status": False, "msg": str(e)}


def run_task(shell_body, task_name, wait_time=30):
    """创建计划任务并执行"""
    result = bt_api("crontab?action=AddCrontab", {
        "name": task_name,
        "type": "minute-n",
        "where1": "1",
        "hour": "",
        "minute": "",
        "week": "",
        "sType": "toShell",
        "sBody": shell_body,
        "sName": "",
        "backupTo": "",
        "save": "",
        "urladdress": ""
    })
    
    if not result.get("status") or not result.get("id"):
        print(f"   创建任务失败: {result}")
        return None
    
    cron_id = result["id"]
    bt_api("crontab?action=StartTask", {"id": cron_id}, timeout=600)
    print(f"   任务 {cron_id} 已启动，等待 {wait_time} 秒...")
    time.sleep(wait_time)
    
    log_result = bt_api("crontab?action=GetLogs", {"id": cron_id})
    bt_api("crontab?action=DelCrontab", {"id": cron_id})
    
    return log_result


def main():
    print("=" * 60)
    print("🐳 更新 Dockerfile 并重新构建")
    print("=" * 60)
    print()
    
    # 1. 读取新的 Dockerfile
    dockerfile_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Dockerfile.backend")
    with open(dockerfile_path, 'r') as f:
        dockerfile_content = f.read()
    
    # 2. 通过 shell 命令更新文件
    encoded = base64.b64encode(dockerfile_content.encode('utf-8')).decode('utf-8')
    
    update_script = f"""#!/bin/bash
echo "更新 Dockerfile.backend..."
echo '{encoded}' | base64 -d > {DEPLOY_PATH}/deploy/Dockerfile.backend
cat {DEPLOY_PATH}/deploy/Dockerfile.backend
"""
    
    print("📝 步骤 1: 更新 Dockerfile...")
    result = run_task(update_script, f"update_dockerfile_{int(time.time())}", wait_time=10)
    if result and result.get("msg"):
        print(result["msg"][:1000])
    
    # 3. 重新构建
    print("\n🐳 步骤 2: 重新构建 Docker 容器 (使用阿里云镜像)...")
    rebuild_script = f"""#!/bin/bash
cd {DEPLOY_PATH}/deploy

echo "停止旧容器..."
docker-compose down 2>/dev/null || true
sleep 2

echo "删除旧镜像..."
docker rmi deploy-backend 2>/dev/null || true

echo "重新构建 (这可能需要几分钟)..."
docker-compose build --no-cache backend 2>&1

echo ""
echo "启动容器..."
docker-compose up -d 2>&1

sleep 10

echo ""
echo "========== 容器状态 =========="
docker ps --format 'table {{{{.Names}}}}\\t{{{{.Status}}}}\\t{{{{.Ports}}}}'

echo ""
echo "========== 后端日志 =========="
docker logs --tail 30 ai-interview-backend 2>&1

echo ""
echo "========== 测试服务 =========="
curl -s http://127.0.0.1:8000/health 2>&1 || echo "后端未响应"
echo ""
curl -s -o /dev/null -w "前端: HTTP %{{http_code}}" http://127.0.0.1:3000 2>&1
"""
    
    result = run_task(rebuild_script, f"rebuild_{int(time.time())}", wait_time=180)
    
    if result and result.get("msg"):
        print("\n📋 构建结果:")
        print("-" * 60)
        print(result["msg"])
    
    print()
    print("=" * 60)
    print("✅ 完成!")
    print("=" * 60)
    print()
    print("🌐 访问地址:")
    print("   http://interview.test.ai.ireborn.com.cn")
    print("   http://interview.test.ai.ireborn.com.cn/admin")


if __name__ == "__main__":
    main()
