Teach Application Module 3 of Fishing-Operations and Maintenance Platform (Vmware Customization Specification Paper)

###A K8S-like orchestration function enables Vmware to be automatically scaled up and resilient (by triggering resilient expansion through monitoring callbacks, the online environment is connected to hardware load balancing, and the automatically cloned host can automatically add load).The Enterprise Enhanced Version of vSphere 6.7 provides Instant Clone technology, referring to the address https://www.virtuallyghetto.com/2018/04/new-instant-clone-architecture-in-vsphere-6-7-part-1.html, which can create VMs in seconds.Today's introduction is VMWARE modification hardware (network card), virtual machine cloning, preset host name (LINUX HOSTNAME), preset IP address, etc.Start with the previous image:

#1. Environmental preparation

  • Pyvmomi corresponds to your python version, download at https://github.com/vmware/pyvmomi/releases
  • Ensure python environment is greater than 2.7.11

#2. Preset IP and hostname after cloning

Vm clones of Linux need to change these items: operating system physical address (/etc/udev/rules.d/70-persistent-net.rules), IP address, host name, and then delete the UUID and physical address HWADDR in the network card (these two lines are automatically generated by the operating system after Linux restarts)

The Customization Spec (Custom Specification Definition) of Vmware can be used to directly modify the IP address and Hostname of the Vm machine without turning it on after cloning the Vm, eliminating some tedious manual steps:
The code is as follows:

 def ip_assign(self, vm, vm_ip, vm_gateway, vm_name):
        """Set Static IP """
        adaptermap = vim.vm.customization.AdapterMapping()
        globalip = vim.vm.customization.GlobalIPSettings()
        adaptermap.adapter = vim.vm.customization.IPSettings()
        adaptermap.adapter.ip = vim.vm.customization.FixedIp()
        adaptermap.adapter.ip.ipAddress = vm_ip
        adaptermap.adapter.subnetMask = self.subnet
        adaptermap.adapter.gateway = vm_gateway
        adaptermap.adapter.dnsDomain = self.domain
        """Set Host Name"""
        ident = vim.vm.customization.LinuxPrep()
        ident.domain = self.domain
        ident.hostName = vim.vm.customization.FixedName()
        ident.hostName.name = vm_name
        customspec = vim.vm.customization.Specification()
        customspec.nicSettingMap = [adaptermap]
        customspec.globalIPSettings = globalip
        customspec.identity = ident
        print "Reconfiguring VM Networks . . ."
        task = self.get_obj([vim.VirtualMachine],vm).Customize(spec=customspec)
        self.wait_for_task(task, "config task")

 def vm_from_clone(self,src_vm_name,datastore_name,cluster_name,vm_name):
        """Clone Virtual Machine"""
        src_vm_name = src_vm_name
        datacenter_name = 'GQTZY'
        vm_folder = None
        datastore_name = datastore_name
        cluster_name = cluster_name
        resource_pool = None
        power_on = False
        datastorecluster_name = None
        vm_name = vm_name
        src_vm = self.get_obj( [vim.VirtualMachine], src_vm_name)
        datacenter = self.get_obj( [vim.Datacenter], datacenter_name)
        if vm_folder:
            destfolder = self.get_obj( [vim.Folder], vm_folder)
        else:
            destfolder = datacenter.vmFolder
        if datastore_name:
            datastore = self.get_obj( [vim.Datastore], datastore_name)
        else:
            datastore = self.get_obj( [vim.Datastore], src_vm.datastore[0].info.name)
        cluster = self.get_obj( [vim.ClusterComputeResource], cluster_name)
        if resource_pool:
            resource_pool = self.get_obj( [vim.ResourcePool], resource_pool)
        else:
            resource_pool = cluster.resourcePool
        vmconf = vim.vm.ConfigSpec()
        if datastorecluster_name:
            podsel = vim.storageDrs.PodSelectionSpec()
            pod = self.get_obj( [vim.StoragePod], datastorecluster_name)
            podsel.storagePod = pod
            storagespec = vim.storageDrs.StoragePlacementSpec()
            storagespec.podSelectionSpec = podsel
            storagespec.type = 'create'
            storagespec.folder = destfolder
            storagespec.resourcePool = resource_pool
            storagespec.configSpec = vmconf
            try:
                rec = self.content.storageResourceManager.RecommendDatastores(
                    storageSpec=storagespec)
                rec_action = rec.recommendations[0].action[0]
                real_datastore_name = rec_action.destination.name
            except:
                real_datastore_name = src_vm.datastore[0].info.name
            datastore = self.get_obj( [vim.Datastore], real_datastore_name)
        relospec = vim.vm.RelocateSpec()
        relospec.datastore = datastore
        relospec.pool = resource_pool
        clonespec = vim.vm.CloneSpec()
        clonespec.location = relospec
        clonespec.powerOn = power_on
        task = src_vm.Clone(folder=destfolder, name=vm_name, spec=clonespec)
        self.wait_for_task(task,actionName="cloning VM("+vm_name+") from VM("+src_vm_name+")...")

    def calculate_clusters_to_datasote(self, clusters_name):
        """Calculate the storage volume with the largest remaining space in the selected cluster"""
        clusters_list = []
        if clusters_name == '':
            return
        else:
            listOBJ = self.get_obj([vim.ClusterComputeResource],clusters_name)
        for each in listOBJ.datastore:
            clusters_dict = {}
            clusters_dict[int(each.summary.freeSpace)] = each
            clusters_list.append(clusters_dict)
        return max(clusters_list)

    def get_clusters_resourcepool_dict(self):
        """Get Dictionary of Cluster and Resource Pool Correspondence"""
        clusters_resourcepool_dict = {}
        for i in self.list_obj([vim.ClusterComputeResource]):
            clusters_resourcepool_dict[i.resourcePool] = i.name
        return clusters_resourcepool_dict

The backend uses Celery for asynchronous task processing because IP and Hostname actions tend to work with this Vm clone and because Vm clones take a long time to clone.Cloning requires passing in the source Vm name, storage space name (automatically calculating the maximum storage space in the cluster or manually selecting), cluster name, and target Vm name.

@task(base=MyTask)
def vm_clone(src_vm_name,vm_name,vm_get_datastore_type):
    ss = VCenter()
    ss.connect_to_vcenter()
    if vm_get_datastore_type == "1":
        """Get Cluster and Resource Pool Dictionary"""
        dict = ss.get_clusters_resourcepool_dict()
        """Get cluster name from resource pool"""
        vm_cluster_name = dict[ss.get_hosts(src_vm_name).resourcePool]
        """Maximum space storage in computing clusters"""
        vm_datastore_name = str(ss.calculate_clusters_to_datasote(vm_cluster_name).values()[0]).strip("''")
    elif vm_get_datastore_type == "2":
        vm_datastore_name = ss.get_hosts(src_vm_name).datastore[0]
    ss.vm_from_clone(src_vm_name, vm_datastore_name, vm_cluster_name, vm_name)
Fifteen original articles were published. Approved 0. Visits 102
Private letter follow

Tags: vim Vmware Linux network

Posted on Wed, 15 Jan 2020 20:32:14 -0500 by slands10