import sys, traceback
sys.path.insert(0, "/opt/terraform-web/backend")
import vsphere_vm_manager as m
from pyVmomi import vim
from pyVim.task import WaitForTask
VM = "dcn03srv628"
print("power_off_vm_wait present:", hasattr(m, "power_off_vm_wait"))
print("module file:", m.__file__)
vm = m.find_vm_by_name(VM)
print("power:", m.get_vm_power_state(vm))
cd = next((d for d in vm.config.hardware.device
if isinstance(d, vim.vm.device.VirtualCdrom)), None)
print("cdrom key:", getattr(cd, "key", None), "ctrlKey:", getattr(cd, "controllerKey", None))
print("backing:", type(cd.backing).__name__, getattr(cd.backing, "fileName", None))
print("connected:", cd.connectable.connected, "startConnected:", cd.connectable.startConnected)
# Тот же reconfig, что и attach, но с ТЕМ ЖЕ файлом (исключаем проблему пути) —
# проверяем именно, разрешает ли сфера правку CD в текущем состоянии питания.
try:
cd.connectable = vim.vm.device.VirtualDevice.ConnectInfo(
startConnected=True, connected=False, allowGuestControl=True)
spec = vim.vm.device.VirtualDeviceConfigSpec(
operation=vim.vm.device.VirtualDeviceConfigSpec.Operation.edit, device=cd)
WaitForTask(vm.ReconfigVM_Task(spec=vim.vm.ConfigSpec(deviceChange=[spec])),
raiseOnError=True)
print("RECONFIG OK")
except Exception as e:
print("RECONFIG FAILED")
traceback.print_exc()
print("type:", type(e).__name__, "| repr:", repr(e))
for a in ("msg", "faultMessage", "property", "reason", "faultCause"):
print(" ", a, "=", getattr(e, a, "<none>"))