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. 创建本地配置文件...')
config_content = '''server:
  port: 8080

spring:
  main:
    web-application-type: reactive
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        enabled: false
      config:
        enabled: false
    gateway:
      routes:
        - id: auth-service
          uri: http://auth-service:8082
          predicates:
            - Path=/api/auth/**,/api/users/**
          filters:
            - StripPrefix=2
        - id: user-service
          uri: http://user-service:8091
          predicates:
            - Path=/api/user/**,/api/address/**,/api/favorite/**,/api/history/**,/api/review/**,/api/member/**,/api/feedback/**,/api/settings/**,/api/privacy/**,/api/transaction/**,/api/v/**,/api/invite/**,/api/legal/**,/api/version/**
          filters:
            - StripPrefix=2
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins:
              - http://localhost:80
              - http://localhost
              - http://8.140.221.49
            allowedMethods:
              - GET
              - POST
              - PUT
              - DELETE
              - OPTIONS
            allowedHeaders: "*"
            allowCredentials: true
  data:
    redis:
      host: ${SPRING_REDIS_HOST:redis}
      port: ${SPRING_REDIS_PORT:6379}
      password: ${SPRING_REDIS_PASSWORD:redis_aliyun_2026}

jwt:
  enabled: true
  secret-key: ${JWT_SECRET_KEY:aliyun_very_secure_jwt_secret_2026}

management:
  endpoints:
    web:
      exposure:
        include: health,info,routes,prometheus,metrics
  endpoint:
    health:
      show-details: always

logging:
  level:
    root: INFO
    org.springframework.cloud.gateway: DEBUG
'''

cmd = 'cat > /opt/Fzxpodman/config/application-local.yml << "EOF"\n' + config_content + '\nEOF'
stdin, stdout, stderr = ssh.exec_command(cmd)
print('配置文件创建成功!')

# 2. 启动网关使用local配置
print('\n2. 启动网关...')
cmd = 'podman rm -f fzx-aliyun-gateway; podman run -d --name fzx-aliyun-gateway --net fzx-dev-network -e SPRING_PROFILES_ACTIVE=local -e SPRING_REDIS_HOST=redis -e SPRING_REDIS_PASSWORD=redis_aliyun_2026 -e JWT_SECRET_KEY=aliyun_very_secure_jwt_secret_2026 -p 8080:8080 -v /opt/Fzxpodman/config/application-local.yml:/app/config/application-local.yml localhost/fzx-aliyun-dev_api-gateway:latest'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode().strip()
print(f'启动结果: {result}')

# 3. 等待启动
print('\n3. 等待网关启动...')
import time
time.sleep(90)

# 4. 检查状态
print('\n4. 检查状态...')
cmd = 'podman ps | grep gateway'
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
print(f'网关状态: {result}')

# 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)