#!/usr/bin/env python3
"""
修复主机名问题的脚本
"""

import sys
import os
from pathlib import Path

# 添加项目路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(project_root / 'python'))

def fix_hostname_issues():
    """修复主机名问题"""
    try:
        from config_manager.main import EnhancedConfigManager
        
        print("🔧 修复主机名问题...")
        
        # 创建配置管理器
        config_manager = EnhancedConfigManager()
        
        # 获取所有服务器
        servers = config_manager.get_existing_servers()
        
        # 修复 cpu_221 的主机名
        if 'cpu_221' in servers:
            config = servers['cpu_221']
            old_host = config.get('host')
            
            # 修正主机名 (添加缺失的0)
            if old_host == 'bjhw-sys-rpm221.bjhw':
                new_host = 'bjhw-sys-rpm0221.bjhw'
                config['host'] = new_host
                
                # 同时更新 specs.connection.target.host
                if 'specs' in config and 'connection' in config['specs']:
                    if 'target' in config['specs']['connection']:
                        config['specs']['connection']['target']['host'] = new_host
                
                print(f"✅ 修复 cpu_221 主机名: {old_host} -> {new_host}")
                
                # 保存配置
                config_manager._save_server_config('cpu_221', config)
                print("✅ 配置已保存")
            else:
                print(f"ℹ️ cpu_221 主机名已正确: {old_host}")
        
        # 检查其他服务器的主机名是否正确
        hostname_fixes = {
            'hg222': {
                'current': 'szzj-isa-ai-peking-poc06.szzj',
                'should_be': '10.129.130.222'  # 使用IP地址可能更稳定
            },
            'hg223': {
                'current': 'szzj-isa-ai-peking-poc06.szzj', 
                'should_be': '10.129.130.223'  # 使用IP地址可能更稳定
            }
        }
        
        for server_name, fix_info in hostname_fixes.items():
            if server_name in servers:
                config = servers[server_name]
                current_host = config.get('host')
                
                print(f"\n🔍 检查 {server_name}:")
                print(f"   当前主机: {current_host}")
                print(f"   建议主机: {fix_info['should_be']}")
                
                # 这里先不自动修改，让用户确认
                # 因为可能需要确认正确的主机名
        
        print("\n✅ 主机名检查完成")
        
    except Exception as e:
        print(f"❌ 修复失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    fix_hostname_issues() 