ESXi Server上的虚拟机分布于不同的Datastores,现有的服务器能够很好的按照如下的方式进行:每个Datastore运行一个Image,这个程序就是实现这样一个功能!因为是在现有框架增加一些功能,所以这个小程序并没有那么的合理,没法,为了加入到现有框架中,所有的方法,数据结构都得按照以前框架中的一套来处理。
''' 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
谢谢阅读!