import paramiko

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

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

print('='*80)
print('重新创建网络')
print('='*80)

# 1. 停止所有相关容器
print('\n1. 停止网关容器...')
cmd = 'podman stop fzx-aliyun-gateway'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'停止结果: {result.strip()}')

# 2. 删除旧网络
print('\n2. 删除旧网络...')
cmd = 'podman network rm fzx-dev-network'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'删除结果: {result.strip()}')

# 3. 创建新网络
print('\n3. 创建新网络...')
cmd = 'podman network create fzx-dev-network --subnet=10.89.0.0/24 --gateway=10.89.0.1'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'创建结果: {result.strip()}')

# 4. 重启Nacos容器加入新网络
print('\n4. 重启Nacos容器...')
cmd = 'podman stop fzx-aliyun-nacos && podman rm fzx-aliyun-nacos'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'删除Nacos容器: {result.strip()}')

cmd = '''podman run -d --name fzx-aliyun-nacos --network fzx-dev-network --network-alias fzx-aliyun-nacos -p 8080:8080 -p 9848:9848 \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=fzx-aliyun-mariadb \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER=fzx_aliyun \
-e MYSQL_SERVICE_PASSWORD=fzx_aliyun_password_2026 \
-e MYSQL_SERVICE_PORT=3306 \
-e NACOS_AUTH_ENABLE=false \
-e NACOS_JVM_ARGS="-Xms512M -Xmx1G" \
-e TZ=Asia/Shanghai \
-v fzx-aliyun-nacos-data:/home/nacos/data \
docker.m.daocloud.io/nacos/nacos-server:v2.4.3'''
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120)
result = stdout.read().decode()
print(f'启动Nacos: {result.strip()}')

# 5. 等待Nacos启动
print('\n5. 等待Nacos启动...')
import time
time.sleep(120)

# 6. 获取Nacos IP
print('\n6. 获取Nacos IP...')
cmd = 'podman inspect fzx-aliyun-nacos | grep -A5 "IPAddress" | grep -v "SecondaryIPAddresses" | head -1 | awk -F\\" "{print $4}"'
stdin, stdout, stderr = ssh.exec_command(cmd)
nacos_ip = stdout.read().decode().strip()
print(f'Nacos IP: {nacos_ip}')

# 7. 更新compose文件中的Nacos IP
print('\n7. 更新compose文件...')
cmd = f'sed -i "s/NACOS_SERVER_ADDR: .*/NACOS_SERVER_ADDR: {nacos_ip}:8848/g" /opt/Fzxpodman/infrastructure/compose/podman-compose.aliyun-dev.yml'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'更新结果: {result.strip()}')

# 8. 启动网关
print('\n8. 启动网关...')
cmd = f'cd /opt/Fzxpodman && NACOS_SERVER_ADDR={nacos_ip}:8848 podman-compose -f infrastructure/compose/podman-compose.aliyun-dev.yml up -d api-gateway'
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120)
result = stdout.read().decode()
print(f'启动结果: {result.strip()[-500:]}')

# 9. 等待启动
print('\n9. 等待网关启动...')
time.sleep(60)

# 10. 测试网关
print('\n10. 测试网关...')
cmd = 'curl -v http://localhost:8080/api/user/health 2>&1'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'网关响应:\n{result}')

ssh.close()

print('\n' + '='*80)
print('操作完成!')
print('='*80)