import paramiko

host = '8.140.221.49'
username = 'root'
private_key_path = r'C:\Users\FZX\.ssh\FzxBdsh-ECS.pem'

def run_command(ssh, cmd):
    stdin, stdout, stderr = ssh.exec_command(cmd)
    return stdout.read().decode().strip(), stderr.read().decode().strip()

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, key_filename=private_key_path, timeout=30)

nginx_conf = '''user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name api.fengzhongxing.com;

        location /health {
            return 200 "OK";
        }

        location / {
            return 503 "Service Unavailable";
        }
    }

    server {
        listen 80;
        server_name user.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/user/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name merchant.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/merchant/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name rider.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/rider/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name platform.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/platform/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name official.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name www.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}
'''

print('更新Nginx配置...')
sftp = ssh.open_sftp()
with sftp.file('/opt/Fzxpodman/nginx/conf/nginx.conf', 'w') as f:
    f.write(nginx_conf)
sftp.close()

print('重启Nginx容器...')
run_command(ssh, 'podman rm -f fzx-aliyun-nginx')
run_command(ssh, '''
podman run -d --name fzx-aliyun-nginx \
  --network fzx-aliyun-dev-network --network-alias nginx \
  -p 0.0.0.0:80:80 -p 0.0.0.0:443:443 \
  -v /opt/Fzxpodman/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /opt/Fzxpodman/nginx/html:/usr/share/nginx/html:ro \
  docker.m.daocloud.io/library/nginx:1.25.5
''')

import time
time.sleep(3)

print('\n测试各端访问:')
domains = [
    ('platform.fengzhongxing.com', '/'),
    ('merchant.fengzhongxing.com', '/'),
    ('official.fengzhongxing.com', '/'),
    ('user.fengzhongxing.com', '/'),
    ('rider.fengzhongxing.com', '/')
]

print('\n测试结果:')
print('-' * 50)
for domain, path in domains:
    output, error = run_command(ssh, f'curl -s -o /dev/null -w "%{{http_code}}" -H "Host: {domain}" http://localhost{path}')
    status = '✅' if output == '200' else '❌'
    print(f'{status} {domain}: {output}')

ssh.close()

print('\n配置完成!')