def create_vm(self, client, name, compute_resource, nics, memory, num_cpus, guest_id, host=None, disks=None): print 'Creating VM %s' % name print '########compute_resource#########',compute_resource if host is None: try: target = client.find_entity_view('ComputeResource', filter={'name': compute_resource}) except ObjectNotFoundError as ex: raise ObjectNotFoundError('Could not find ComputeResource:%s. %s' % (compute_resource, to_str(ex))) resource_pool = target.resourcePool print '=====resource_pool========', print resource_pool else: try: target = client.find_entity_view('HostSystem', filter={'name': host}) except ObjectNotFoundError as ex: raise ObjectNotFoundError('Could not find HostSystem:%s. %s' % (host, to_str(ex))) resource_pool = target.parent.resourcePool print '======memory======', print memory vm_devices = [] virtual_controller = VirtualController() lsi_logic_controller = self.create_lsi_logic_controller(client, virtual_controller.get_key()) vm_devices.append(lsi_logic_controller) lsi_logic_controller_index = lsi_logic_controller.device.key print '=======lsi_logic_controller_index========', print lsi_logic_controller_index target_datastore_dict = self.get_datastores(target) default_datastore = None for disk in disks: #716 if disk.type in ('file',): #625 ds_name = parse_datastore_name_from_diskpath(disk.filename) ds = self.get_datastore_by_name(ds_name, target_datastore_dict) if not default_datastore: #405 default_datastore = ds if disk.type in ('file',) and not disk.is_iso(): disk_spec = self.create_disk(client, ds, disk.get_size(), virtual_controller.get_key(), virtual_controller.get_virtual_disk_unit_number_count(), controller_index=lsi_logic_controller_index) vm_devices.append(disk_spec) #713 elif disk.type in ('file',) and disk.is_iso(): #621 iso_path = self.parse_filename_get_path(disk.filename) controller_key, unit_number = virtual_controller.get_virtual_ide_controller_key() iso_disk_spec = self.add_iso_disk(client, ds, iso_path, virtual_controller.get_key(), unit_number, controller_key) vm_devices.append(iso_disk_spec) elif disk.type in ('phy',): #712 unit_number,controller_key = virtual_controller.get_virtual_ide_controller_key() cdrom_spec = self.add_virtual_cd_rom(client, disk.filename, virtual_controller.get_key(), unit_number, controller_key) vm_devices.append(cdrom_spec) for nic in nics: #820 nic_spec = self.create_nic(client, target, nic, virtual_controller.get_key(), virtual_controller.get_virtual_network_unit_number_count()) if nic_spec is None: msg = 'Could not create spec for NIC' raise Exception(msg) vm_devices.append(nic_spec) vm_path_name = default_datastore.name print vm_path_name,'##########vm_path_name########' vmfi = self.create_virtual_machine_file_info(client, vm_path_name) print '######vmfi########',vmfi print '******************************************' vm_config_spec = self.create_virtual_machine_config_spec(client, name, num_cpus, memory, guest_id, vmfi, vm_devices) print vm_config_spec,'#######vm_config_spec######' datacenter = self.get_datacenter(target) try: task = datacenter.vmFolder.CreateVM_Task(config=vm_config_spec, pool=resource_pool) except VimFault as e: msg = 'Failed to create %s: ' % e raise Exception(msg) task_result = self.wait_for_task(task) print '====task_result====', print task_result return task_result
def clone_to(self, client, v_config, to_template=False, power_on=False, vm_config_spec_dict=None, config_context=None): if not vm_config_spec_dict: vm_config_spec_dict = {} if not config_context: config_context = {} print '======config_context==========', print config_context name = config_context.get('name') mob_name = config_context.get('mob_name') host = config_context.get('dest_host') if not host: raise Exception('Esxi Host should not be None') try: vm = client.find_entity_view('VirtualMachine', filter={'name': mob_name}) except ObjectNotFoundError as ex: raise ObjectNotFoundError('Could not find VirtualMachine:%s. %s' % (mob_name, to_str(ex))) vmfolder_mob = None vmfolder = config_context.get('vmfolder') if not vmfolder: raise Exception('Location/vmFolder should not be None') try: vmfolder_mob = client.find_entity_view('Folder', filter={'name': vmfolder}) except ObjectNotFoundError as ex: raise ObjectNotFoundError('Could not find Folder:%s. %s' % (vmfolder, to_str(ex))) try: host_system = client.find_entity_view('HostSystem', filter={'name': host}) except ObjectNotFoundError as ex: raise ObjectNotFoundError('Could not find HostSystem:%s. %s' % (host, to_str(ex))) resource_pool = host_system.parent.resourcePool ds_to_use = None disks = config_context.get('disks') for disk in disks: if disk.type in ('file',) and not disk.is_iso(): ds_name = parse_datastore_name_from_diskpath(disk.filename) target_datastore_dict = self.get_datastores(host_system) ds_to_use = self.get_datastore_by_name(ds_name, target_datastore_dict) break if not ds_to_use: raise Exception('Could not find datastore') vm_clone_spec = client.create('VirtualMachineCloneSpec') vm_reloc_spec = client.create('VirtualMachineRelocateSpec') vm_reloc_spec.datastore = ds_to_use vm_reloc_spec.diskMoveType = None vm_reloc_spec.pool = resource_pool vm_reloc_spec.host = host_system vm_reloc_spec.transform = 'sparse' vm_reloc_spec_disk_locr = client.create('VirtualMachineRelocateSpecDiskLocator') vm_reloc_spec_disk_locr.datastore = ds_to_use vm_reloc_spec_disk_locr.diskMoveType = None vm_reloc_spec.disk = None vm_clone_spec.location = vm_reloc_spec vm_clone_spec.template = to_template vm_clone_spec.powerOn = power_on vm_config_spec = client.create('VirtualMachineConfigSpec') vm_config_spec.alternateGuestName = None vm_config_spec.annotation = None vm_config_spec.bootOptions = None vm_config_spec.changeTrackingEnabled = None vm_config_spec.changeVersion = None vm_config_spec.consolePreferences = None vm_config_spec.cpuAffinity = None vm_config_spec.cpuAllocation = None vm_config_spec.cpuFeatureMask = None vm_config_spec.cpuHotAddEnabled = None vm_config_spec.cpuHotRemoveEnabled = None vm_config_spec.deviceChange = None vm_config_spec.extraConfig = None vm_config_spec.files = None vm_config_spec.firmware = None vm_config_spec.flags = None vm_config_spec.ftInfo = None vm_config_spec.guestAutoLockEnabled = None vm_config_spec.guestId = None vm_config_spec.instanceUuid = None vm_config_spec.locationId = None vm_config_spec.managedBy = None vm_config_spec.maxMksConnections = None vm_config_spec.memoryAffinity = None vm_config_spec.memoryAllocation = None vm_config_spec.memoryHotAddEnabled = None vm_config_spec.memoryMB = None vm_config_spec.memoryReservationLockedToMax = None vm_config_spec.name = None vm_config_spec.networkShaper = None vm_config_spec.npivDesiredNodeWwns = None vm_config_spec.npivDesiredPortWwns = None vm_config_spec.npivNodeWorldWideName = None vm_config_spec.npivOnNonRdmDisks = None vm_config_spec.npivPortWorldWideName = None vm_config_spec.npivTemporaryDisabled = None vm_config_spec.npivWorldWideNameOp = None vm_config_spec.npivWorldWideNameType = None vm_config_spec.numCoresPerSocket = None vm_config_spec.numCPUs = None vm_config_spec.powerOpInfo = None vm_config_spec.swapPlacement = None vm_config_spec.tools = None vm_config_spec.uuid = None vm_config_spec.vAppConfig = None vm_config_spec.vAppConfigRemoved = None vm_config_spec.vAssertsEnabled = None vm_config_spec.version = None vm_config_spec.virtualICH7MPresent = None vm_config_spec.virtualSMCPresent = None vm_clone_spec.config = vm_config_spec vm_clone_spec.snapshot = None vm_clone_spec.customization = None try: task = vm.CloneVM_Task(folder=vmfolder_mob, name=name, spec=vm_clone_spec) print '===successfully Created====', print name, print task, print vars(task) except Exception as ex: msg = 'Failed to clone %s: ' % ex raise Exception(msg) task_result = self.wait_for_task(task) print '====task_result====', print task_result return task_result