def __init__(self, service_manager): # Client for all the services on a management node. self.service_manager = service_manager # Returns the service which provides support for generic functionality # which can be applied equally to all types of libraries self.library_service = Library(self.service_manager.stub_config) # Returns the service for managing local libraries self.local_library_service = LocalLibrary(self.service_manager.stub_config) # Returns the service for managing subscribed libraries self.subscribed_library_service = SubscribedLibrary(self.service_manager.stub_config) # Returns the service for managing library items self.library_item_service = Item(self.service_manager.stub_config) # Returns the service for managing sessions to update or delete content self.upload_service = UpdateSession(self.service_manager.stub_config) # Returns the service for managing files within an update session self.upload_file_service = UpdateSessionFile(self.service_manager.stub_config) # Returns the service for managing sessions to download content self.download_service = DownloadSession(self.service_manager.stub_config) # Returns the service for managing files within a download session self.download_file_service = DownloadSessionFile(self.service_manager.stub_config) # Returns the service for deploying virtual machines from OVF library items self.ovf_lib_item_service = LibraryItem(self.service_manager.stub_config) # Returns the service for mount and unmount of an iso file on a VM self.iso_service = Image(self.service_manager.stub_config) # Returns the service for managing subscribed library items self.subscribed_item_service = SubscribedItem(self.service_manager.stub_config) # Returns the service for managing library items containing virtual # machine templates self.vmtx_service = VmtxLibraryItem(self.service_manager.stub_config) # Returns the service for managing subscription information of # the subscribers of a published library. self.subscriptions = Subscriptions(self.service_manager.stub_config) # Creates the service that communicates with virtual machines self.vm_service = VM(self.service_manager.stub_config) # Returns the service for managing checkouts of a library item containing # a virtual machine template self.check_outs_service = CheckOuts(self.service_manager.stub_config) # Returns the service for managing the live versions of the virtual machine # templates contained in a library item self.versions_service = Versions(self.service_manager.stub_config) # Returns the service for managing the history of content changes made # to a library item self.changes_service = Changes(self.service_manager.stub_config)
def run(self): # Get a placement spec datacenter_name = testbed.config['VM_DATACENTER_NAME'] vm_folder_name = testbed.config['VM_FOLDER2_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] if not self.placement_spec: self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( self.stub_config, datacenter_name, vm_folder_name, datastore_name) """ Create a default VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Use all the guest and system provided defaults. """ guest_os = testbed.config['VM_GUESTOS'] vm_create_spec = VM.CreateSpec(name=self.vm_name, guest_os=guest_os, placement=self.placement_spec) print( '\n# Example: create_default_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(self.stub_config) vm = vm_svc.create(vm_create_spec) print("create_default_vm: Created VM '{}' ({})".format( self.vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def get_vms(stub_config, vm_names): """Return identifiers of a list of vms""" vm_svc = VM(stub_config) vms = vm_svc.list(VM.FilterSpec(names=vm_names)) if len(vms) == 0: print('No vm found') return None print("Found VMs '{}' ({})".format(vm_names, vms)) return vms
def run(self): """ List VMs present in server """ vm_svc = VM(self.service_manager.stub_config) list_of_vms = vm_svc.list() print("----------------------------") print("List Of VMs") print("----------------------------") pprint(list_of_vms) print("----------------------------")
def run(): """ List VMs present in server """ vm_svc = VM(stub_config) list_of_vms = vm_svc.list() print("----------------------------") print("List Of VMs") print("----------------------------") for vm in list_of_vms: print('{}'.format(vm)) print("----------------------------")
def run(self): # TODO add your sample code here # Using REST API service vm_service = VM(self.service_manager.stub_config) filter_spec = VM.FilterSpec(names=set([self.vm_name])) vms = vm_service.list(filter_spec) print(vms) # Using Vim API service (pyVmomi) current_time = self.service_manager.si.CurrentTime() print(current_time)
def create_basic_vm(stub_config, placement_spec, standard_network): """ Create a basic VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Create 2 disks and specify one of them on scsi0:0 since it's the boot disk * Specify 1 ethernet adapter using a Standard Portgroup backing * Setup for PXE install by selecting network as first boot device Use guest and system provided defaults for most configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] boot_disk = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec()) data_disk = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec()) nic = Ethernet.CreateSpec(start_connected=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) # TODO Should DISK be put before ETHERNET? Does the BIOS automatically try # the next device if the DISK is empty? boot_device_order = [ BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET), BootDevice.EntryCreateSpec(BootDevice.Type.DISK) ] vm_create_spec = VM.CreateSpec(name=vm_name, guest_os=guest_os, placement=placement_spec, disks=[boot_disk, data_disk], nics=[nic], boot_devices=boot_device_order) print('\n# Example: create_basic_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(stub_config) vm = vm_svc.create(vm_create_spec) print("create_basic_vm: Created VM '{}' ({})".format(vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def cleanup(): vm = get_vm(stub_config, vm_name) if vm: power_svc = Power(stub_config) vm_svc = VM(stub_config) state = power_svc.get(vm) if state == Power.Info(state=Power.State.POWERED_ON): power_svc.stop(vm) elif state == Power.Info(state=Power.State.SUSPENDED): power_svc.start(vm) power_svc.stop(vm) print("Deleting VM '{}' ({})".format(vm_name, vm)) vm_svc.delete(vm)
def __init__(self, service_manager): # Client for all the services on a management node. self.service_manager = service_manager # Returns the service which provides support for generic functionality # which can be applied equally to all types of libraries self.library_service = Library(self.service_manager.stub_config) # Returns the service for managing local libraries self.local_library_service = LocalLibrary( self.service_manager.stub_config) # Returns the service for managing subscribed libraries self.subscribed_library_service = SubscribedLibrary( self.service_manager.stub_config) # Returns the service for managing library items self.library_item_service = Item(self.service_manager.stub_config) # Returns the service for managing sessions to update or delete content self.upload_service = UpdateSession(self.service_manager.stub_config) # Returns the service for managing files within an update session self.upload_file_service = UpdateSessionFile( self.service_manager.stub_config) # Returns the service for managing sessions to download content self.download_service = DownloadSession( self.service_manager.stub_config) # Returns the service for managing files within a download session self.download_file_service = DownloadSessionFile( self.service_manager.stub_config) # Returns the service for deploying virtual machines from OVF library items self.ovf_lib_item_service = LibraryItem( self.service_manager.stub_config) # Returns the service for mount and unmount of an iso file on a VM self.iso_service = Image(self.service_manager.stub_config) # Returns the service for managing subscribed library items self.subscribed_item_service = SubscribedItem( self.service_manager.stub_config) # Returns the service for managing library items containing virtual # machine templates self.vmtx_service = VmtxLibraryItem(self.service_manager.stub_config) # Creates the service that communicates with virtual machines self.vm_service = VM(self.service_manager.stub_config)
def get_vm(stub_config, vm_name): """ Return the identifier of a vm Note: The method assumes that there is only one vm with the mentioned name. """ vm_svc = VM(stub_config) names = set([vm_name]) vms = vm_svc.list(VM.FilterSpec(names=names)) if len(vms) == 0: print("VM with name ({}) not found".format(vm_name)) return None vm = vms[0].vm print("Found VM '{}' ({})".format(vm_name, vm)) return vm
def run(self): """ Delete User specified VM from Server """ vm_svc = VM(self.service_manager.stub_config) power_svc = Power(self.service_manager.stub_config) vm = get_vm(self.service_manager.stub_config, self.vm_name) if not vm: raise Exception('Sample requires an existing vm with name ({}).' 'Please create the vm first.'.format(vm_name)) print("Deleting VM -- '{}-({})')".format(self.vm_name, vm)) state = power_svc.get(vm) if state == Power.Info(state=Power.State.POWERED_ON): power_svc.stop(vm) elif state == Power.Info(state=Power.State.SUSPENDED): power_svc.start(vm) power_svc.stop(vm) vm_svc.delete(vm) print("Deleted VM -- '{}-({})".format(self.vm_name, vm))
def create_default_vm(stub_config, placement_spec): """ Create a default VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Use all the guest and system provided defaults. """ guest_os = testbed.config['VM_GUESTOS'] vm_create_spec = VM.CreateSpec(name=vm_name, guest_os=guest_os, placement=placement_spec) print('\n# Example: create_default_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(stub_config) vm = vm_svc.create(vm_create_spec) print("create_default_vm: Created VM '{}' ({})".format(vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def run(self): # Get a placement spec datacenter_name = testbed.config['VM_DATACENTER_NAME'] vm_folder_name = testbed.config['VM_FOLDER2_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] std_portgroup_name = testbed.config['STDPORTGROUP_NAME'] dv_portgroup_name = testbed.config['VDPORTGROUP1_NAME'] if not self.placement_spec: self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( self.stub_config, datacenter_name, vm_folder_name, datastore_name) # Get a standard network backing if not self.standard_network: self.standard_network = network_helper.get_standard_network_backing( self.stub_config, std_portgroup_name, datacenter_name) # Get a distributed network backing if not self.distributed_network: self.distributed_network = network_helper.get_distributed_network_backing( self.stub_config, dv_portgroup_name, datacenter_name) """ Create an exhaustive VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Hardware Version = VMX_11 (for 6.0) * CPU (count = 2, coresPerSocket = 2, hotAddEnabled = false, hotRemoveEnabled = false) * Memory (size_mib = 2 GB, hotAddEnabled = false) * 3 Disks and specify each of the HBAs and the unit numbers * (capacity=40 GB, name=<some value>, spaceEfficient=true) * Specify 2 ethernet adapters, one using a Standard Portgroup backing and the other using a DISTRIBUTED_PORTGROUP networking backing. * nic1: Specify Ethernet (macType=MANUAL, macAddress=<some value>) * nic2: Specify Ethernet (macType=GENERATED) * 1 CDROM (type=ISO_FILE, file="os.iso", startConnected=true) * 1 Serial Port (type=NETWORK_SERVER, file="tcp://localhost/16000", startConnected=true) * 1 Parallel Port (type=HOST_DEVICE, startConnected=false) * 1 Floppy Drive (type=CLIENT_DEVICE) * Boot, type=BIOS * BootDevice order: CDROM, DISK, ETHERNET Use guest and system provided defaults for remaining configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] iso_datastore_path = testbed.config['ISO_DATASTORE_PATH'] serial_port_network_location = \ testbed.config['SERIAL_PORT_NETWORK_SERVER_LOCATION'] GiB = 1024 * 1024 * 1024 GiBMemory = 1024 vm_create_spec = VM.CreateSpec( guest_os=guest_os, name=self.vm_name, placement=self.placement_spec, hardware_version=Hardware.Version.VMX_11, cpu=Cpu.UpdateSpec(count=2, cores_per_socket=1, hot_add_enabled=False, hot_remove_enabled=False), memory=Memory.UpdateSpec(size_mib=2 * GiBMemory, hot_add_enabled=False), disks=[ Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec(name='boot', capacity=40 * GiB)), Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec(name='data1', capacity=10 * GiB)), Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec(name='data2', capacity=10 * GiB)) ], nics=[ Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.MANUAL, mac_address='11:23:58:13:21:34', backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=self.standard_network)), Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.GENERATED, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP, network=self.distributed_network)), ], cdroms=[ Cdrom.CreateSpec( start_connected=True, backing=Cdrom.BackingSpec(type=Cdrom.BackingType.ISO_FILE, iso_file=iso_datastore_path) ) ], serial_ports=[ Serial.CreateSpec( start_connected=False, backing=Serial.BackingSpec( type=Serial.BackingType.NETWORK_SERVER, network_location=serial_port_network_location) ) ], parallel_ports=[ Parallel.CreateSpec( start_connected=False, backing=Parallel.BackingSpec( type=Parallel.BackingType.HOST_DEVICE) ) ], floppies=[ Floppy.CreateSpec( backing=Floppy.BackingSpec( type=Floppy.BackingType.CLIENT_DEVICE) ) ], boot=Boot.CreateSpec(type=Boot.Type.BIOS, delay=0, enter_setup_mode=False ), # TODO Should DISK be put before CDROM and ETHERNET? Does the BIOS # automatically try the next device if the DISK is empty? boot_devices=[ BootDevice.EntryCreateSpec(BootDevice.Type.CDROM), BootDevice.EntryCreateSpec(BootDevice.Type.DISK), BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET) ] ) print( '# Example: create_exhaustive_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(self.stub_config) vm = vm_svc.create(vm_create_spec) print("create_exhaustive_vm: Created VM '{}' ({})".format(self.vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def run(self): # Get a placement spec datacenter_name = testbed.config['VM_DATACENTER_NAME'] vm_folder_name = testbed.config['VM_FOLDER2_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] std_portgroup_name = testbed.config['STDPORTGROUP_NAME'] if not self.placement_spec: self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( self.stub_config, datacenter_name, vm_folder_name, datastore_name) # Get a standard network backing standard_network = network_helper.get_standard_network_backing( self.stub_config, std_portgroup_name, datacenter_name) """ Create a basic VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Create 2 disks and specify one of them on scsi0:0 since it's the boot disk * Specify 1 ethernet adapter using a Standard Portgroup backing * Setup for PXE install by selecting network as first boot device Use guest and system provided defaults for most configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] boot_disk = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec()) data_disk = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec()) nic = Ethernet.CreateSpec( start_connected=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) boot_device_order = [ BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET), BootDevice.EntryCreateSpec(BootDevice.Type.DISK) ] vm_create_spec = VM.CreateSpec(name=self.vm_name, guest_os=guest_os, placement=self.placement_spec, disks=[boot_disk, data_disk], nics=[nic], boot_devices=boot_device_order) print('\n# Example: create_basic_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(self.stub_config) vm = vm_svc.create(vm_create_spec) print("create_basic_vm: Created VM '{}' ({})".format(self.vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm