Python ESXi virtual machine allocation

ESXi Server virtual machine on a distributed in different Datastores, the server can be well carried out according to the following manner: each Datastore runs a Image, this program is to achieve such a function! Because is to increase some functions in the existing framework, so this small program is not so reasonable, no law, in order to join the existing framework, all the methods, data structures are in accordance with the previously in the framework of a deal.

'''
Created on May 27, 2013

@author: Berlin
'''
class VMDispatcher():
    vms = []
    datastores_dics = {}
    vms_mapping = {}
    
    def __init__(self, vms_to_run, datastores_mapping):
        self.vms = vms_to_run
        self.datastores_dics = datastores_mapping
        
    def __ParseVmsToRun(self):
        for datastore in self.datastores_dics.keys():
            vms_list = []
            for vm_datastore in self.datastores_dics[datastore]:
                for vm_path in self.vms:
                    if vm_path == vm_datastore:
                        vms_list.append(vm_path)
                    self.vms_mapping[datastore] = vms_list
        #print self.vms_mapping
    
    def __FirstRunVMs(self):
        self.__ParseVmsToRun()
        vms_to_run = []
        for datastore, vms_path in self.vms_mapping.iteritems():
            if len(vms_path):
                vms_to_run.append(vms_path.pop())
                self.vms_mapping[datastore] = vms_path
        return vms_to_run
    
    def __IsAnyVMsOfSameDatastore(self, datastore):
        if len(self.vms_mapping[datastore]) == 0:
            return False
        else:
            return True
    
    #get datastore of vm according to the vm_path
    def __GetDatastore(self, vm):
        for datastore in self.datastores_dics.keys():
            if vm in self.datastores_dics[datastore]:
                return datastore
    
    def AllocateVMsToRun(self, vm_path):
        datastore = self.__GetDatastore(vm_path)
        vm_to_run = []
        if self.__IsAnyVMsOfSameDatastore(datastore):
            vms_path = self.vms_mapping[datastore]
            vm_to_run = vms_path.pop()
            self.vms_mapping[datastore] = vms_path
        
        return vm_to_run
    
    def DispatchToVMs(self):
        vms_path = self.__FirstRunVMs()
        return vms_path

Thank you for readingļ¼

Posted by Marcy at November 22, 2013 - 12:26 AM