First Commit
This commit is contained in:
40
vcenter_connector.py
Normal file
40
vcenter_connector.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from pyVim.connect import SmartConnect, Disconnect
|
||||
from pyVmomi import vim
|
||||
from config.settings import VCENTER_USER, VCENTER_PASSWORD
|
||||
from utils.logger import logger
|
||||
|
||||
|
||||
def connect_to_vcenter(vcenter_host):
|
||||
"""
|
||||
连接到指定的vCenter服务器
|
||||
:param vcenter_host: vCenter主机名/IP
|
||||
:return: 成功返回ServiceInstance,失败返回None
|
||||
"""
|
||||
try:
|
||||
# 禁用SSL证书验证(生产环境建议启用证书验证)
|
||||
si = SmartConnect(
|
||||
host=vcenter_host,
|
||||
user=VCENTER_USER,
|
||||
pwd=VCENTER_PASSWORD,
|
||||
disableSslCertValidation=True
|
||||
)
|
||||
if not si:
|
||||
logger.error(f"❌ 无法连接到vCenter: {vcenter_host}(无返回实例)")
|
||||
return None
|
||||
|
||||
logger.info(f"✅ 成功连接到vCenter: {vcenter_host}")
|
||||
return si
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ 连接vCenter {vcenter_host} 失败: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
def disconnect_from_vcenter(si):
|
||||
"""
|
||||
关闭vCenter连接
|
||||
:param si: ServiceInstance实例
|
||||
"""
|
||||
if si:
|
||||
Disconnect(si)
|
||||
logger.debug("已关闭vCenter连接")
|
||||
Reference in New Issue
Block a user