本文整理匯總了Python中pysphere.vi_task.VITask.get_state方法的典型用法代碼示例。如果您正苦於以下問題:Python VITask.get_state方法的具體用法?Python VITask.get_state怎麽用?Python VITask.get_state使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pysphere.vi_task.VITask
的用法示例。
在下文中一共展示了VITask.get_state方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_vm
# 需要導入模塊: from pysphere.vi_task import VITask [as 別名]
# 或者: from pysphere.vi_task.VITask import get_state [as 別名]
#.........這裏部分代碼省略.........
if not ds:
raise CreatorException("Datastore is not available")
volume_name = "[%s]" % ds_name
# add parameters to the create vm task
create_vm_request = VI.CreateVM_TaskRequestMsg()
config = create_vm_request.new_config()
vmfiles = config.new_files()
vmfiles.set_element_vmPathName(volume_name)
config.set_element_files(vmfiles)
config.set_element_name(vm_name)
config.set_element_annotation(description)
config.set_element_memoryMB(memory_size)
config.set_element_numCPUs(cpu_count)
config.set_element_guestId(guestosid)
devices = []
# add a scsi controller
disk_ctrl_key = 1
scsi_ctrl_spec = config.new_deviceChange()
scsi_ctrl_spec.set_element_operation("add")
scsi_ctrl = VI.ns0.VirtualLsiLogicController_Def("scsi_ctrl").pyclass()
scsi_ctrl.set_element_busNumber(0)
scsi_ctrl.set_element_key(disk_ctrl_key)
scsi_ctrl.set_element_sharedBus("noSharing")
scsi_ctrl_spec.set_element_device(scsi_ctrl)
devices.append(scsi_ctrl_spec)
# find ide controller
if iso:
ide_ctlr = None
for dev in defaul_devs:
if dev.typecode.type[1] == "VirtualIDEController":
ide_ctlr = dev
# add a cdrom based on a physical device
if ide_ctlr:
cd_spec = config.new_deviceChange()
cd_spec.set_element_operation("add")
cd_ctrl = VI.ns0.VirtualCdrom_Def("cd_ctrl").pyclass()
cd_device_backing = VI.ns0.VirtualCdromIsoBackingInfo_Def("cd_device_backing").pyclass()
ds_ref = cd_device_backing.new_datastore(ds)
ds_ref.set_attribute_type(ds.get_attribute_type())
cd_device_backing.set_element_datastore(ds_ref)
cd_device_backing.set_element_fileName("%s %s" % (volume_name, iso))
cd_ctrl.set_element_backing(cd_device_backing)
cd_ctrl.set_element_key(20)
cd_ctrl.set_element_controllerKey(ide_ctlr.get_element_key())
cd_ctrl.set_element_unitNumber(0)
cd_spec.set_element_device(cd_ctrl)
devices.append(cd_spec)
# create a new disk - file based - for the vm
if disk_size != 0:
disk_spec = config.new_deviceChange()
disk_spec.set_element_fileOperation("create")
disk_spec.set_element_operation("add")
disk_ctlr = VI.ns0.VirtualDisk_Def("disk_ctlr").pyclass()
disk_backing = VI.ns0.VirtualDiskFlatVer2BackingInfo_Def("disk_backing").pyclass()
disk_backing.set_element_fileName(volume_name)
disk_backing.set_element_diskMode("persistent")
disk_ctlr.set_element_key(0)
disk_ctlr.set_element_controllerKey(disk_ctrl_key)
disk_ctlr.set_element_unitNumber(0)
disk_ctlr.set_element_backing(disk_backing)
disk_ctlr.set_element_capacityInKB(disk_size)
disk_spec.set_element_device(disk_ctlr)
devices.append(disk_spec)
# add a NIC. the network Name must be set as the device name to create the NIC.
for network_name in networks:
nic_spec = config.new_deviceChange()
nic_spec.set_element_operation("add")
nic_ctlr = VI.ns0.VirtualPCNet32_Def("nic_ctlr").pyclass()
nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
nic_backing.set_element_deviceName(network_name)
nic_ctlr.set_element_addressType("generated")
nic_ctlr.set_element_backing(nic_backing)
nic_ctlr.set_element_key(4)
nic_spec.set_element_device(nic_ctlr)
devices.append(nic_spec)
config.set_element_deviceChange(devices)
create_vm_request.set_element_config(config)
folder_mor = create_vm_request.new__this(vmfmor)
folder_mor.set_attribute_type(vmfmor.get_attribute_type())
create_vm_request.set_element__this(folder_mor)
rp_mor = create_vm_request.new_pool(rpmor)
rp_mor.set_attribute_type(rpmor.get_attribute_type())
create_vm_request.set_element_pool(rp_mor)
host_mor = create_vm_request.new_host(hostmor)
host_mor.set_attribute_type(hostmor.get_attribute_type())
create_vm_request.set_element_host(host_mor)
# CREATE THE VM - add option "wait"
taskmor = self.esx_server._proxy.CreateVM_Task(create_vm_request)._returnval
task = VITask(taskmor, self.esx_server)
task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if task.get_state() == task.STATE_ERROR:
self._disconnect_from_esx()
raise CreatorException("Error creating vm: %s" % task.get_error_message())
示例2: VITask
# 需要導入模塊: from pysphere.vi_task import VITask [as 別名]
# 或者: from pysphere.vi_task.VITask import get_state [as 別名]
create_vm_request.set_element_config(config)
folder_mor = create_vm_request.new__this(vmfmor)
folder_mor.set_attribute_type(vmfmor.get_attribute_type())
create_vm_request.set_element__this(folder_mor)
rp_mor = create_vm_request.new_pool(rpmor)
rp_mor.set_attribute_type(rpmor.get_attribute_type())
create_vm_request.set_element_pool(rp_mor)
host_mor = create_vm_request.new_host(hostmor)
host_mor.set_attribute_type(hostmor.get_attribute_type())
create_vm_request.set_element_host(host_mor)
# CREATE THE VM
taskmor = vsphere_client._proxy.CreateVM_Task(create_vm_request)._returnval
task = VITask(taskmor, vsphere_client)
task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if task.get_state() == task.STATE_ERROR:
vsphere_client.disconnect()
module.fail_json(msg="Error creating vm: %s" %
task.get_error_message())
else:
# We always need to get the vm because we are going to gather facts
vm = vsphere_client.get_vm_by_name(guest)
# VM was created. If there is any extra config options specified, set
# them here , disconnect from vcenter, then exit.
if vm_extra_config:
vm.set_extra_config(vm_extra_config)
# Power on the VM if it was requested
power_state(vm, state, True)
示例3: create_vm
# 需要導入模塊: from pysphere.vi_task import VITask [as 別名]
# 或者: from pysphere.vi_task.VITask import get_state [as 別名]
#.........這裏部分代碼省略.........
scsi_ctrl_spec = config.new_deviceChange()
scsi_ctrl_spec.set_element_operation('add')
scsi_ctrl = VI.ns0.VirtualLsiLogicController_Def("scsi_ctrl").pyclass()
scsi_ctrl.set_element_busNumber(0)
scsi_ctrl.set_element_key(disk_ctrl_key)
scsi_ctrl.set_element_sharedBus("noSharing")
scsi_ctrl_spec.set_element_device(scsi_ctrl)
devices.append(scsi_ctrl_spec)
# find ide controller
ide_ctlr = None
for dev in defaul_devs:
if dev.typecode.type[1] == "VirtualIDEController":
ide_ctlr = dev
# add a cdrom based on a physical device
if ide_ctlr:
cd_spec = config.new_deviceChange()
cd_spec.set_element_operation('add')
cd_ctrl = VI.ns0.VirtualCdrom_Def("cd_ctrl").pyclass()
cd_device_backing = VI.ns0.VirtualCdromIsoBackingInfo_Def("cd_device_backing").pyclass()
ds_ref = cd_device_backing.new_datastore(ds)
ds_ref.set_attribute_type(ds.get_attribute_type())
cd_device_backing.set_element_datastore(ds_ref)
cd_device_backing.set_element_fileName("%s %s" % (volume_name,
cd_iso_location))
cd_ctrl.set_element_backing(cd_device_backing)
cd_ctrl.set_element_key(20)
cd_ctrl.set_element_controllerKey(ide_ctlr.get_element_key())
cd_ctrl.set_element_unitNumber(0)
cd_spec.set_element_device(cd_ctrl)
devices.append(cd_spec)
# create a new disk - file based - for the vm
disk_spec = config.new_deviceChange()
disk_spec.set_element_fileOperation("create")
disk_spec.set_element_operation("add")
disk_ctlr = VI.ns0.VirtualDisk_Def("disk_ctlr").pyclass()
disk_backing = VI.ns0.VirtualDiskFlatVer2BackingInfo_Def("disk_backing").pyclass()
disk_backing.set_element_fileName(volume_name)
disk_backing.set_element_diskMode("persistent")
disk_backing.ThinProvisioned = True
disk_ctlr.set_element_key(0)
disk_ctlr.set_element_controllerKey(disk_ctrl_key)
disk_ctlr.set_element_unitNumber(0)
disk_ctlr.set_element_backing(disk_backing)
disk_ctlr.set_element_capacityInKB(disksize)
disk_spec.set_element_device(disk_ctlr)
devices.append(disk_spec)
# add a NIC. the network Name must be set as the device name to create the NIC.
nic_spec = config.new_deviceChange()
if network_name:
nic_spec.set_element_operation("add")
nic_ctlr = VI.ns0.VirtualPCNet32_Def("nic_ctlr").pyclass()
nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
nic_backing.set_element_deviceName(network_name)
nic_ctlr.set_element_addressType("generated")
nic_ctlr.set_element_backing(nic_backing)
nic_ctlr.set_element_key(4)
nic_spec.set_element_device(nic_ctlr)
devices.append(nic_spec)
config.set_element_deviceChange(devices)
create_vm_request.set_element_config(config)
folder_mor = create_vm_request.new__this(vmfmor)
folder_mor.set_attribute_type(vmfmor.get_attribute_type())
create_vm_request.set_element__this(folder_mor)
rp_mor = create_vm_request.new_pool(rpmor)
rp_mor.set_attribute_type(rpmor.get_attribute_type())
create_vm_request.set_element_pool(rp_mor)
host_mor = create_vm_request.new_host(hostmor)
host_mor.set_attribute_type(hostmor.get_attribute_type())
create_vm_request.set_element_host(host_mor)
# CREATE THE VM
taskmor = s._proxy.CreateVM_Task(create_vm_request)._returnval
task = VITask(taskmor, s)
task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if task.get_state() == task.STATE_ERROR:
raise Exception("Error creating vm: %s" %
task.get_error_message())
# Here you should power your VM (refer to the pysphere documentation)
# So it boots from the specified ISO location
try:
new_vm = s.get_vm_by_name(opts.name)
connect_vm_cdroms(new_vm, s)
try:
new_vm.power_on()
except Exception as e:
print "Failed to power-on the new VM using:", opts.name
print "Exception:", str(e)
except Exception as e:
print "Failed to locate the new VM using:", opts.name
print "Exception:", str(e)
# disconnect from the server
s.disconnect()