def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualSIOController) if not controller: return unit_number = self.next_unit_number(controller=controller) backing_info = pyVmomi.vim.VirtualFloppyRemoteDeviceBackingInfo( deviceName='', useAutoDetect=False) connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=False, startConnected=False) device = pyVmomi.vim.VirtualFloppy(backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change]) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge(dialog=self.dialog, task=task, title=self.title, text='Adding floppy drive ...') gauge.display()
def remove(obj, dialog): """ Remove a Managed Entity NOTE: This operation also removes any child objects of the managed entity. Args: obj (vim.ManagedEntity): A Managed Entity dialog (dialog.Dialog): A Dialog instance """ title = '{} ({})'.format(obj.name, obj.__class__.__name__) code = dialog.yesno(title=title, text='Remove {} and all child objects?'.format( obj.name)) if code in (dialog.CANCEL, dialog.ESC): return task = obj.Destroy() gauge = pvc.widget.gauge.TaskGauge( dialog=dialog, task=task, title=title, text='Removing {} and any child objects ...'.format(obj.name)) gauge.display()
def display(self): if not self.datacenter: self.datacenter = self.select_datacenter() if not self.datacenter: return if not self.cluster: self.cluster = self.select_cluster(folder=self.datacenter) if not self.cluster: return if not self.select_host(cluster=self.cluster): return if self.host: datastore = self.select_datastore(obj=self.host) else: datastore = self.select_datastore(obj=self.cluster) if not datastore: return vmx_version = self.select_vmx_version(obj=self.cluster) if not vmx_version: return # TODO: Add guest operation system # TODO: Add virtual disks # TODO: Add networking specs = self.get_vm_specs() if not specs: return folder = self.datacenter.vmFolder pool = self.cluster.resourcePool vmx_file = pyVmomi.vim.VirtualMachineFileInfo( vmPathName='[{}] {}'.format(datastore.name, specs['Name']) ) config_spec = pyVmomi.vim.VirtualMachineConfigSpec( name=specs['Name'], numCPUs=int(specs['vCPU(s)']), memoryMB=int(specs['Memory Size (MB)']), files=vmx_file, version=vmx_version ) task = folder.CreateVM_Task( config=config_spec, pool=pool, host=self.host ) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title='Creating New Virtual Machine', text='Creating virtual machine {}'.format(specs['Name']) ) gauge.display()
def _configure_vnc_options(self, enabled, port, password): """ Configure VNC console options Args: enabled (bool): If True enable the VNC console port (int): VNC port to be configured password (str): VNC password to be configured """ options = [ pyVmomi.vim.OptionValue(key='RemoteDisplay.vnc.enabled', value=str(enabled)), pyVmomi.vim.OptionValue(key='RemoteDisplay.vnc.port', value=str(port)), pyVmomi.vim.OptionValue(key='RemoteDisplay.vnc.password', value=str(password)), ] spec = pyVmomi.vim.VirtualMachineConfigSpec(extraConfig=options) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( title=self.title, text='Configuring VNC Settings', dialog=self.dialog, task=task ) gauge.display()
def remove(obj, dialog): """ Remove a Managed Entity NOTE: This operation also removes any child objects of the managed entity. Args: obj (vim.ManagedEntity): A Managed Entity dialog (dialog.Dialog): A Dialog instance """ title = '{} ({})'.format(obj.name, obj.__class__.__name__) code = dialog.yesno( title=title, text='Remove {} and all child objects?'.format(obj.name) ) if code in (dialog.CANCEL, dialog.ESC): return task = obj.Destroy() gauge = pvc.widget.gauge.TaskGauge( dialog=dialog, task=task, title=title, text='Removing {} and any child objects ...'.format(obj.name) ) gauge.display()
def rename(obj, dialog): """ Rename a Managed Entity Args: obj (vim.ManagedEntity): A Managed Entity dialog (dialog.Dialog): A Dialog instance """ title = '{} ({})'.format(obj.name, obj.__class__.__name__) code, new_name = dialog.inputbox( title=title, text='New name for {}?'.format(obj.name), init=obj.name ) if code in (dialog.CANCEL, dialog.ESC): return task = obj.Rename(newName=new_name) gauge = pvc.widget.gauge.TaskGauge( dialog=dialog, task=task, title=title, text='Renaming {} to {} ...'.format(obj.name, new_name) ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualPCIController) if not controller: return unit_number = self.next_unit_number(controller=controller) bus_number = self.next_bus_number(controller=self.scsi_controller) device = self.scsi_controller( controllerKey=controller.key, key=-1, unitNumber=unit_number, busNumber=bus_number, hotAddRemove=True, sharedBus=pyVmomi.vim.VirtualSCSISharing.noSharing) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change]) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge(dialog=self.dialog, task=task, title=self.title, text='Adding SCSI controller ...') gauge.display()
def rename(obj, dialog): """ Rename a Managed Entity Args: obj (vim.ManagedEntity): A Managed Entity dialog (dialog.Dialog): A Dialog instance """ title = '{} ({})'.format(obj.name, obj.__class__.__name__) code, new_name = dialog.inputbox(title=title, text='New name for {}?'.format(obj.name), init=obj.name) if code in (dialog.CANCEL, dialog.ESC): return task = obj.Rename(newName=new_name) gauge = pvc.widget.gauge.TaskGauge(dialog=dialog, task=task, title=title, text='Renaming {} to {} ...'.format( obj.name, new_name)) gauge.display()
def disconnect_host(self): """ Disconnect host(s) from the cluster """ self.dialog.infobox( title=self.title, text='Retrieving information ...' ) items = [ pvc.widget.checklist.CheckListItem(tag=h.name, description=h.runtime.connectionState) for h in self.obj.host if h.runtime.connectionState == pyVmomi.vim.HostSystemConnectionState.connected ] if not items: self.dialog.msgbox( title=self.title, text='There are no hosts connected to cluster' ) return checklist = pvc.widget.checklist.CheckList( items=items, dialog=self.dialog, title=self.title, text='Select host(s) to be disconnected from the cluster' ) checklist.display() selected = checklist.selected() if not selected: return text = ( 'The following host(s) will be disconnected from cluster.' '\n\n{}\n\n' 'Disconnect host(s) from cluster?\n' ) code = self.dialog.yesno( title=self.title, text=text.format('\n'.join(selected)) ) if code in (self.dialog.ESC, self.dialog.CANCEL): return host_objects = [h for h in self.obj.host if h.name in selected] for host_obj in host_objects: task = host_obj.Disconnect() gauge = pvc.widget.gauge.TaskGauge( title=self.title, text='Disconnecting {} from cluster ...'.format(host_obj.name), dialog=self.dialog, task=task ) gauge.display()
def connect_host(self): """ Connect a host to the cluster and add it to the inventory """ text = ( 'Enter hostname or IP address of the ' 'host to be connected to cluster {}\n' ) elements = [ pvc.widget.form.FormElement(label='Hostname'), pvc.widget.form.FormElement(label='SSL Thumbprint'), pvc.widget.form.FormElement(label='Username'), pvc.widget.form.FormElement(label='Password', attributes=0x1), ] form = pvc.widget.form.Form( dialog=self.dialog, form_elements=elements, mixed_form=True, title=self.title, text=text.format(self.obj.name) ) code, fields = form.display() if code in (self.dialog.CANCEL, self.dialog.ESC): return if not all(fields.values()): self.dialog.msgbox( title=self.title, text='Invalid input provided' ) return connect_spec = pyVmomi.vim.HostConnectSpec( hostName=fields['Hostname'], sslThumbprint=fields['SSL Thumbprint'], userName=fields['Username'], password=fields['Password'] ) task = self.obj.AddHost( spec=connect_spec, asConnected=True ) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Connecting {} to cluster ...'.format(fields['Hostname']) ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualSIOController ) if not controller: return unit_number = self.next_unit_number( controller=controller ) backing_info = pyVmomi.vim.VirtualFloppyRemoteDeviceBackingInfo( deviceName='', useAutoDetect=False ) connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=False, startConnected=False ) device = pyVmomi.vim.VirtualFloppy( backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number ) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add ) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change] ) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Adding floppy drive ...' ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualIDEController ) if not controller: return unit_number = self.next_unit_number( controller=controller ) backing_info = self.select_backing() if not backing_info: return connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=False, startConnected=False ) device = pyVmomi.vim.VirtualCdrom( backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number ) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add ) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change] ) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Adding CD/DVD drive ...' ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualPCIController) if not controller: return unit_number = self.next_unit_number(controller=controller) adapter = self.select_ethernet_adapter() if not adapter: return network = pvc.widget.common.choose_network(agent=self.agent, dialog=self.dialog, obj=self.obj.runtime.host) if not network: return connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=True, startConnected=True) backing_info = pyVmomi.vim.VirtualEthernetCardNetworkBackingInfo( deviceName=network.name, useAutoDetect=False, network=network) device = adapter(backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change]) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge(dialog=self.dialog, task=task, title=self.title, text='Adding ethernet adapter ...') gauge.display()
def reconnect_host(self): """ Reconnect disconnected hosts to cluster """ self.dialog.infobox( title=self.title, text='Retrieving information ...' ) items = [ pvc.widget.checklist.CheckListItem(tag=h.name, description=h.runtime.connectionState) for h in self.obj.host if h.runtime.connectionState == pyVmomi.vim.HostSystemConnectionState.disconnected ] if not items: self.dialog.msgbox( title=self.title, text='There are no disconnected hosts in the cluster' ) return checklist = pvc.widget.checklist.CheckList( items=items, dialog=self.dialog, title=self.title, text='Select host(s) to be reconnected to the cluster' ) checklist.display() selected_hosts = checklist.selected() if not selected_hosts: return host_objects = [h for sh in selected_hosts for h in self.obj.host if sh == h.name] for host_obj in host_objects: task = host_obj.Reconnect() gauge = pvc.widget.gauge.TaskGauge( title=self.title, text='Reconnecting {} to cluster ...'.format(host_obj.name), dialog=self.dialog, task=task ) gauge.display()
def disable_vnc(self): """ Disable VNC console for the Virtual Machine """ options = [ pyVmomi.vim.OptionValue(key='RemoteDisplay.vnc.enabled', value='false'), ] spec = pyVmomi.vim.VirtualMachineConfigSpec(extraConfig=options) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Disabling VNC Console', ) gauge.display()
def reset(self): """ Reset the virtual machine """ if self.obj.runtime.powerState != pyVmomi.vim.VirtualMachinePowerState.poweredOn: self.dialog.msgbox( title=self.title, text='Virtual Machine is not powered on, cannot reset.' ) return task = self.obj.Reset() gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Resetting Virtual Machine' ) gauge.display()
def power_off(self): """ Power off the virtual machine """ if self.obj.runtime.powerState == pyVmomi.vim.VirtualMachinePowerState.poweredOff: self.dialog.msgbox( title=self.title, text='Virtual Machine is already powered off.' ) return task = self.obj.PowerOff() gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Powering Off Virtual Machine' ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualPCIController ) if not controller: return unit_number = self.next_unit_number(controller=controller) bus_number = self.next_bus_number(controller=self.scsi_controller) device = self.scsi_controller( controllerKey=controller.key, key=-1, unitNumber=unit_number, busNumber=bus_number, hotAddRemove=True, sharedBus=pyVmomi.vim.VirtualSCSISharing.noSharing ) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add ) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change] ) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Adding SCSI controller ...' ) gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualIDEController) if not controller: return unit_number = self.next_unit_number(controller=controller) backing_info = self.select_backing() if not backing_info: return connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=False, startConnected=False) device = pyVmomi.vim.VirtualCdrom(backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change]) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge(dialog=self.dialog, task=task, title=self.title, text='Adding CD/DVD drive ...') gauge.display()
def display(self): controller = self.choose_controller( controller=pyVmomi.vim.VirtualPCIController ) if not controller: return unit_number = self.next_unit_number( controller=controller ) adapter = self.select_ethernet_adapter() if not adapter: return network = pvc.widget.common.choose_network( agent=self.agent, dialog=self.dialog, obj=self.obj.runtime.host ) if not network: return connect_info = pyVmomi.vim.VirtualDeviceConnectInfo( allowGuestControl=True, connected=True, startConnected=True ) backing_info = pyVmomi.vim.VirtualEthernetCardNetworkBackingInfo( deviceName=network.name, useAutoDetect=False, network=network ) device = adapter( backing=backing_info, connectable=connect_info, controllerKey=controller.key, key=-1, unitNumber=unit_number ) device_change = pyVmomi.vim.VirtualDeviceConfigSpec( device=device, operation=pyVmomi.vim.VirtualDeviceConfigSpecOperation.add ) spec = pyVmomi.vim.VirtualMachineConfigSpec( deviceChange=[device_change] ) task = self.obj.ReconfigVM_Task(spec=spec) gauge = pvc.widget.gauge.TaskGauge( dialog=self.dialog, task=task, title=self.title, text='Adding ethernet adapter ...' ) gauge.display()