Wednesday, June 22, 2016

Today we review some the logic to automate commonly recurring tasks with vCenter.
1)  to list all Virtual Machines:
     we create a container view with a view Type as vim.VirtualMachine. Then we list the container which is the root folder.
        content = service_instance.RetrieveContent()
        container = content.rootFolder
        viewType = [vim.VirtualMachine]
        recursive = True
        containerView = content.viewManager.CreateContainerView(
            container, viewType, recursive)
Each item in the container view is now a virtual machine instance.

2) to find a specific VM:
we create a search index. All VMs have their own uuid so we can use that to search the index:

search_index = si.content.searchIndex
vm = search_index.FindByUuid(None, args.uuid, True, True)

3) Display vm information
From the above, the virtual machine instance has the following:

details = {'name': vm.summary.config.name,
           'instance UUID': vm.summary.config.instanceUuid,
           'bios UUID': vm.summary.config.uuid,
           'path to VM': vm.summary.config.vmPathName,
           'guest OS id': vm.summary.config.guestId,
           'guest OS name': vm.summary.config.guestFullName,
           'host name': vm.runtime.host.name,
           'last booted timestamp': vm.runtime.bootTime,
           }

4) Export a VM so that it can be backed up:
The main task here is to create the ovf descriptor for the VM.  This is a portable format. We do this only if the VM is powered off.
To create the ovf, we first get an NFCLease on the VM. Then for each device Url in the lease, we download the device to a temporary target directory For each downloaded file, we create an ovf file and set its attribute Then we ask the OvfManager to create a descriptor with ovf files we specify and write this descriptor which is an xml file to the same temporary target directory. This temporary target directory with all the files is the backup.
            http_nfc_lease = vm_obj.ExportVm()
            if http_nfc_lease.state == vim.HttpNfcLease.State.ready:
               for deviceUrl in http_nfc_lease.info.deviceUrl:
                    temp_target_disk = os.path.join(target_directory,
                                                    deviceUrl.targetId)
                    current_bytes_written = download_device(
                        headers=headers, cookies=cookies,
                        temp_target_disk=temp_target_disk,
                        device_url=deviceUrl.url,
                        lease_updater=lease_updater,
                        total_bytes_written=total_bytes_written,
                        total_bytes_to_write=total_bytes_to_write)
                    total_bytes_written += current_bytes_written
                    ovf_file = vim.OvfManager.OvfFile()
                    ovf_file.deviceId = deviceUrl.key
                    ovf_file.path = deviceUrl.targetId
                    ovf_file.size = current_bytes_written
                    ovf_files.append(ovf_file)
           ovf_manager = si.content.ovfManager
           ovf_parameters = vim.OvfManager.CreateDescriptorParams()
           ovf_parameters.name = vm_descriptor_name
           ovf_parameters.ovfFiles = ovf_files
           vm_descriptor_result = ovf_manager.CreateDescriptor(obj=vm_obj,
                                                            cdp=ovf_parameters)



No comments:

Post a Comment