Как включить запуск виртуальной карты и ее настройку при клонировании шаблона с помощью pyvmomi? - PullRequest
0 голосов
/ 06 января 2019

Для удобства я недавно написал набор инструментов о VMware с использованием pyvmomi. Я хочу клонировать шаблон, а затем настроить IP-адрес клонированной виртуальной машины. Однако было обнаружено, что startconnected не может быть включен. Вот сводный код для этого модуля:

# define adapter
adaptermaps = []
guest_map = vim.vm.customization.AdapterMapping()
guest_map.adapter = vim.vm.customization.IPSettings()
guest_map.adapter.ip = vim.vm.customization.FixedIp()
guest_map.adapter.ip.ipAddress = str(setip)
guest_map.adapter.subnetMask = "255.255.255.0"
guest_map.adapter.gateway = "10.10.10.254"
adaptermaps.append(guest_map)
# EOF define adapter

ident = vim.vm.customization.LinuxPrep()
ident.hostName = vim.vm.customization.FixedName()
ident.hostName.name = "testname"

globalip = vim.vm.customization.GlobalIPSettings()

relocate_spec = vim.vm.RelocateSpec()
relocate_spec.datastore = ondatastore
relocate_spec.host = onhost
relocate_spec.pool = oncluster.resourcePool

vmconf_spec = vim.vm.ConfigSpec()
vmconf_spec.cpuHotAddEnabled = True
vmconf_spec.memoryHotAddEnabled = True

devices_change = []
# customization
custom_spec = vim.vm.customization.Specification()
custom_spec.nicSettingMap = adaptermaps
custom_spec.identity = ident
custom_spec.globalIPSettings = globalip
# EOF customization

nic_spec = vim.vm.device.VirtualDeviceSpec()
nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
for dev in target.config.hardware.device:
    if isinstance(dev, vim.vm.device.VirtualEthernetCard):
        break
else:
    raise Exception()

nic_spec.device = dev
nic_spec.device.wakeOnLanEnabled = True
nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic_spec.device.connectable.allowGuestControl = True
# ========== This!!! ==========
nic_spec.device.connectable.startConnected = True

nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
target_network = get_network_by_name(service_instance, "10.10.10.x")
nic_spec.device.backing.network = target_network
nic_spec.device.backing.deviceName = target_network.name

devices_change.append(nic_spec)
vmconf_spec.deviceChange = devices_change

clone_spec = vim.vm.CloneSpec()

clone_spec.powerOn = False
clone_spec.location = relocate_spec
clone_spec.config = vmconf_spec

clone_spec.customization = custom_spec
task = target.Clone(folder=onfolder, name=setname, spec=clone_spec)

После тестирования можно установить состояние startconnected, чтобы включить его, когда я отменяю настройку. Как показано ниже.

# define adapter
# adaptermaps = []
# guest_map = vim.vm.customization.AdapterMapping()
# guest_map.adapter = vim.vm.customization.IPSettings()
# guest_map.adapter.ip = vim.vm.customization.FixedIp()
# guest_map.adapter.ip.ipAddress = str(setip)
# guest_map.adapter.subnetMask = "255.255.255.0"
# guest_map.adapter.gateway = "10.10.10.254"
# adaptermaps.append(guest_map)
# EOF define adapter

# ident = vim.vm.customization.LinuxPrep()
# ident.hostName = vim.vm.customization.FixedName()
# ident.hostName.name = "testname"

# globalip = vim.vm.customization.GlobalIPSettings()

relocate_spec = vim.vm.RelocateSpec()
relocate_spec.datastore = ondatastore
relocate_spec.host = onhost
relocate_spec.pool = oncluster.resourcePool

vmconf_spec = vim.vm.ConfigSpec()
vmconf_spec.cpuHotAddEnabled = True
vmconf_spec.memoryHotAddEnabled = True

devices_change = []
# customization
# custom_spec = vim.vm.customization.Specification()
# custom_spec.nicSettingMap = adaptermaps
# custom_spec.identity = ident
# custom_spec.globalIPSettings = globalip
# EOF customization

nic_spec = vim.vm.device.VirtualDeviceSpec()
nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
for dev in target.config.hardware.device:
    if isinstance(dev, vim.vm.device.VirtualEthernetCard):
        break
else:
    raise Exception()

nic_spec.device = dev
nic_spec.device.wakeOnLanEnabled = True
nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic_spec.device.connectable.allowGuestControl = True
# ========== This!!! ==========
nic_spec.device.connectable.startConnected = True

nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
target_network = get_network_by_name(service_instance, "10.10.10.x")
nic_spec.device.backing.network = target_network
nic_spec.device.backing.deviceName = target_network.name

devices_change.append(nic_spec)
vmconf_spec.deviceChange = devices_change

clone_spec = vim.vm.CloneSpec()

clone_spec.powerOn = False
clone_spec.location = relocate_spec
clone_spec.config = vmconf_spec

# clone_spec.customization = custom_spec
task = target.Clone(folder=onfolder, name=setname, spec=clone_spec)

Итак, как я могу включить startconnected во время процесса клонирования вместо выполнения дополнительных задач?

...