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('创建Nacos命名空间')
print('='*80)

# 1. 创建aliyun-dev命名空间
print('\n1. 创建aliyun-dev命名空间...')
cmd = 'curl -X POST "http://localhost:8848/nacos/v1/console/namespaces" -d "customNamespaceId=aliyun-dev&namespaceName=aliyun-dev&namespaceDesc=阿里云开发环境命名空间" 2>&1'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'创建结果: {result}')

# 2. 检查命名空间是否创建成功
print('\n2. 检查命名空间...')
cmd = 'curl -s "http://localhost:8848/nacos/v1/ns/instance/list?serviceName=user-service&namespaceId=aliyun-dev" 2>&1'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'用户服务注册信息: {result}')

# 3. 重启网关服务
print('\n3. 重启网关服务...')
cmd = 'cd /opt/Fzxpodman/infrastructure/compose && podman-compose -f podman-compose.aliyun-dev.yml up -d api-gateway'
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120)
result = stdout.read().decode()[-500:]
print(f'重启输出: {result}')

# 4. 等待网关重启
print('\n4. 等待网关重启...')
import time
time.sleep(90)

# 5. 测试通过网关访问
print('\n5. 测试通过网关访问...')
cmd = 'curl -s http://localhost:8080/api/user/health'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'网关响应: {result}')

ssh.close()

print('\n' + '='*80)
print('检查完成!')
print('='*80)