def predict_vm_all(ecs_lines, test_lines, input_lines): # Do your work from here# # training data processing result = [] if ecs_lines is None: print('ecs information is none') return result if input_lines is None: print('input file information is none') return result data = data_processing.train_data_process(ecs_lines) test = data_processing.train_data_process(test_lines) target, memory, cpu, flavor_list, start_time, end_time = data_processing.read_input_file(input_lines) # data = localmidian.data_clean(data, flavor_list) data = localmidian.mix_clean(data, flavor_list) # 指数平滑的方法 last_time = data[len(data) - 1][0] data = data_processing.data_sum(data, flavor_list) test = data_processing.test_sum(test,data,flavor_list) # numbers = exponential_smoothing.predict_draw(data,test,last_time, start_time, end_time) # numbers = exponential_smoothing.predict(data, last_time, start_time, end_time) numbers = exponential_smoothing.predict_all(data, last_time, start_time, end_time) predict_list=[] for i in range(len(numbers)): flavor = flavor_list[i] for k in range(len(numbers[i])): f = Flavor(flavor.name+'_'+str(k), flavor.cpu, flavor.memory) f.predict_num = numbers[i][k] predict_list.append(f) # 线性归回的方法 # thetas, remains = wlr.weight_linear_regression(data, flavor_list) # numbers = wlr.predic(thetas, remains, data[len(data) - 1][0], start_time, end_time) # 神经网络的方法 # numbers = neural_network.predict(data, flavor_list, 5, start_time, end_time) machine_list = mffd(target, predict_list, memory, cpu) sum_vm = 0 flavor_list.sort(key=lambda Flavor: Flavor.name) for index in range(len(predict_list)): result.append(predict_list[index].name + ' ' + str(predict_list[index].predict_num)) sum_vm = sum_vm + predict_list[index].predict_num result.insert(0, sum_vm) result = write_result(machine_list, result) return result
class StackData: def __init__(self): self.hwData = Hardware() self.flData = Flavor() self.imgData = Image() def config(self, configuration, fileName): if (configuration == "hardware"): self.hwData.config(fileName) elif (configuration == "flavor"): self.flData.config(fileName) elif (configuration == "image"): self.imgData.config(fileName) def show(self, configuration): if (configuration == "hardware"): self.hwData.show() elif (configuration == "flavor"): self.flData.show() elif (configuration == "images"): self.imgData.show() def showAll(self): if self.hwData.getHardwareDataSize() != 0: self.hwData.show() else: print('\n No hardware data found\n') if self.flData.getFlavorsDataSize() != 0: self.flData.show() else: print('\n No flavors data found\n') if self.imgData.getImageDataSize() != 0: self.imgData.show() else: print('\n No images data found\n') logging.info('SUCCESS') def adminShowAvailableHardware(self): self.hwData.showAvailableHardware() def canHost(self, hardwareName, flavorName): hardware = self.hwData.getHardwareInfo(hardwareName) flavor = self.flData.getFlavorInfo(flavorName) if hardware == None or flavor == None: print("\n Please verify hardware name and flavor name") logging.error('FAILURE') return False if hardware.mem >= flavor.mem and hardware.numDisks >= flavor.numDisks and hardware.numVCpus >= flavor.numVCpus: return True else: return False
def predict_vm(ecsContent, inputContent): # Do your work from here# result = [] if ecsContent is None: print 'ecs information is none' return result if inputContent is None: print 'input file information is none' return result flavorList = Flavor() input = Common.parseInput(inputContent, flavorList) return result
def read_input_file(input_lines): i = 0 flavor_list = [] cpu = int(input_lines[i].split()[0]) memory = int(input_lines[i].split(' ')[1]) * 1024 # print 'cpu: ', cpu, ' memory: ', memory i = i + 2 flavor_num = int(input_lines[i].split(' ')[0]) i = i + 1 for index in range(i, i + flavor_num): line = input_lines[index].split() # print line flavor_list.append(Flavor(line[0], line[1], line[2])) i = i + flavor_num + 1 target = input_lines[i].split()[0] # print 'target: ', target i = i + 2 start_time = input_lines[i].split(' ')[0].strip() end_time = input_lines[i + 1].split(' ')[0].strip() return target, memory, cpu, flavor_list, start_time, end_time
def getServer(self): server = None if self.instance: instance_data = self.instance["nova_object.data"] flavor_data = instance_data["flavor"]["nova_object.data"] flavor = Flavor() flavor.setId(flavor_data["flavorid"]) flavor.setName(flavor_data["name"]) flavor.setMemory(flavor_data["memory_mb"]) flavor.setVCPUs(flavor_data["vcpus"]) flavor.setStorage(flavor_data["root_gb"]) server = Server() server.setFlavor(flavor) server.setId(instance_data["uuid"]) server.setUserId(instance_data["user_id"]) server.setProjectId(instance_data["project_id"]) server.setCreatedAt(instance_data["created_at"]) server.setMetadata(instance_data["metadata"]) server.setKeyName(instance_data["key_name"]) if "user_data" in instance_data: user_data = instance_data["user_data"] if user_data: server.setUserData(utils.decodeBase64(user_data)) return server
def fromDict(cls, request_dict): request = Request() request.data = request_dict["data"] request.action = request_dict["action"] request.context = request_dict["context"] if "instances" in request.data: instance = request.data["instances"][0] else: build_request = request.data["build_requests"][0] instance = build_request["nova_object.data"]["instance"] instance_data = instance["nova_object.data"] request.id = instance_data["uuid"] request.user_id = instance_data["user_id"] request.project_id = instance_data["project_id"] created_at = instance_data["created_at"] request.created_at = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ") flavor_data = instance_data["flavor"]["nova_object.data"] flavor = Flavor() flavor.setId(flavor_data["flavorid"]) flavor.setName(flavor_data["name"]) flavor.setMemory(flavor_data["memory_mb"]) flavor.setVCPUs(flavor_data["vcpus"]) flavor.setStorage(flavor_data["root_gb"]) server = Server() server.setFlavor(flavor) server.setId(instance_data["uuid"]) server.setUserId(instance_data["user_id"]) server.setProjectId(instance_data["project_id"]) server.setCreatedAt(instance_data["created_at"]) server.setMetadata(instance_data["metadata"]) server.setUserData(instance_data["user_data"]) server.setKeyName(instance_data["key_name"]) server.setType() request.server = server if "filter_properties" in request.data: filter_properties = request.data["filter_properties"] request.retry = filter_properties.get("retry", {}) else: request_spec = request.data["request_specs"][0] nova_object = request_spec["nova_object.data"] request.retry = nova_object.get("retry", {}) if not request.retry: request.retry = {} return request
def handle_config(cmd_parts): global hardware, hardware_configs global images, image_configs global flavors, flavor_configs global curr_command if (len(cmd_parts) != 4): sys.stderr.write("ERROR: Invalid command." + "\n") logger.info(curr_command + ": Failure") return if (cmd_parts[2] == "--hardware"): file = cmd_parts[3] my_file = "../p0_config/" + file if (os.path.exists(my_file)): hardware = True with open(my_file) as f: lines = f.readlines() num_configs = int(lines[0]) for x in range(1, num_configs + 1): cfg = lines[x].split(" ") h = Hardware(cfg[0], cfg[1], cfg[2], cfg[3], cfg[4]) hardware_configs[cfg[0]] = h sys.stdout.write( str(len(hardware_configs)) + ' physical servers now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") elif (cmd_parts[2] == "--images"): file = cmd_parts[3] my_file = "../p0_config/" + file if (os.path.exists(my_file)): images = True with open(my_file) as f: lines = f.readlines() num_configs = int(lines[0]) for x in range(1, num_configs + 1): cfg = lines[x].split(" ") img = Image(cfg[0], cfg[1]) image_configs[cfg[0]] = img sys.stdout.write( str(len(image_configs)) + ' images now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") elif (cmd_parts[2] == "--flavors"): file = cmd_parts[3] my_file = "../p0_config/" + file if (os.path.exists(my_file)): flavors = True with open(my_file) as f: lines = f.readlines() num_configs = int(lines[0]) for x in range(1, num_configs + 1): cfg = lines[x].split(' ') flv = Flavor(cfg[0], cfg[1], cfg[2], cfg[3]) flavor_configs[cfg[0]] = flv sys.stdout.write( str(len(flavor_configs)) + ' VM flavors now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") else: sys.stderr.write("ERROR: Invalid command." + "\n") logger.info(curr_command + ": Failure")
def handle_config(cmd_parts): global hardware, hardware_configs global images, image_configs global flavors, flavor_configs global curr_command, racks, instances, instance_on_server, server_instances, machines_on_racks global hardware_configs_fixed if (len(cmd_parts) != 4): sys.stderr.write("ERROR: Invalid command.") logger.info(curr_command + ": Failure") return if (cmd_parts[2] == "--hardware"): file = cmd_parts[3] my_file = "../p1_config/" + file if (os.path.exists(my_file)): if hardware: hardware_configs = {} racks = {} instances = {} instance_on_server = {} server_instances = {} machines_on_racks = {} racks = {} instances = {} instance_on_server = {} server_instances = {} machines_on_racks = {} hardware_configs_fixed = {} hardware = True with open(my_file) as f: lines = f.readlines() num_racks = int(lines[0]) for x in range(1, num_racks + 1): rack = lines[x].split(" ") r = Rack(rack[0], rack[1]) racks[rack[0]] = r num_configs = int(lines[num_racks + 1]) for x in range(num_racks + 2, num_racks + num_configs + 2): cfg = lines[x].split(" ") h = NewHardware(cfg[0], cfg[1], cfg[2], cfg[3], cfg[4], cfg[5]) hf = NewHardware(cfg[0], cfg[1], cfg[2], cfg[3], cfg[4], cfg[5]) hardware_configs[cfg[0]] = h hardware_configs_fixed[cfg[0]] = hf if machines_on_racks.get(cfg[1]) == None: machines_on_racks[cfg[1]] = [] machines_on_racks[cfg[1]].append(cfg[0]) sys.stdout.write( str(len(hardware_configs)) + ' physical servers now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") elif (cmd_parts[2] == "--images"): file = cmd_parts[3] my_file = "../p1_config/" + file if (os.path.exists(my_file)): images = True with open(my_file) as f: lines = f.readlines() num_configs = int(lines[0]) for x in range(1, num_configs + 1): cfg = lines[x].split(" ") img = NewImage(cfg[0], cfg[1], cfg[2]) image_configs[cfg[0]] = img sys.stdout.write( str(len(image_configs)) + ' images now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") elif (cmd_parts[2] == "--flavors"): file = cmd_parts[3] my_file = "../p1_config/" + file if (os.path.exists(my_file)): flavors = True with open(my_file) as f: lines = f.readlines() num_configs = int(lines[0]) for x in range(1, num_configs + 1): cfg = lines[x].split(' ') flv = Flavor(cfg[0], cfg[1], cfg[2], cfg[3]) flavor_configs[cfg[0]] = flv sys.stdout.write( str(len(flavor_configs)) + ' VM flavors now available.' + "\n") logger.info(curr_command + ": Success") else: sys.stderr.write("ERROR: File you specified does not exist." + "\n") logger.info(curr_command + ": Failure") else: sys.stderr.write("ERROR: Invalid command." + "\n") logger.info(curr_command + ": Failure")
def __init__(self, OS_AUTH_URL, OS_PROJECT_NAME, OS_USERNAME, OS_PASSWORD): self.auth = Authenticate(OS_AUTH_URL, OS_PROJECT_NAME, OS_USERNAME, OS_PASSWORD) self.flavor = Flavor(self.auth.get_token_id(), self.auth.get_compute_admin_url()) self.image = Image(self.auth.get_token_id(), self.auth.get_image_admin_url()) self.heat = Heat(self.auth.get_token_id(), self.auth.get_orchestration_admin_url())
def __init__(self): self.hwData = Hardware() self.flData = Flavor() self.imgData = Image()
class StackData: def __init__(self): self.hwData = Hardware() self.flData = Flavor() self.imgData = Image() self.serverData = Server() def config(self, configuration, fileName): if configuration == "hardware": self.hwData.config(fileName) elif configuration == "flavor": self.flData.config(fileName) elif configuration == "image": self.imgData.config(fileName) def show(self, configuration): if configuration == "hardware": self.hwData.show() elif configuration == "flavor": self.flData.show() elif configuration == "images": self.imgData.show() elif configuration == "server list": self.serverData.showServer() elif configuration == "server instance": self.serverData.showInstance() def showAll(self): if self.hwData.getHardwareDataSize() != 0: self.hwData.show() else: print('\n No hardware data found\n') if self.flData.getFlavorsDataSize() != 0: self.flData.show() else: print('\n No flavors data found\n') if self.imgData.getImageDataSize() != 0: self.imgData.show() else: print('\n No images data found\n') logging.info('SUCCESS') def deleteInstance(self, name): if name in self.serverData.serverDataDict: server = self.serverData.serverDataDict[name] flavor = self.flData.getFlavorInfo(server.flName) allocated = self.hwData.allocatedHardware[server.mcName] allocated.mem = allocated.mem - flavor.mem allocated.numVCpus = allocated.numVCpus - flavor.numVCpus allocated.numDisks = allocated.numDisks - flavor.numDisks if allocated.mem > 0: self.hwData.allocatedHardware[name] = allocated else: self.hwData.allocatedHardware.pop(server.mcName, None) self.serverData.serverDataDict.pop(name, None) else: print("ERROR!! Instance Not Found..") def validateImageAndFlavor(self, imageName, flavorName): if ((imageName in self.imgData.imageDict.keys()) and (flavorName in self.flData.flDataDict.keys())): return True else: return False def createInstance(self, serverName, imageName, flavorName): if (self.validateImageAndFlavor(imageName, flavorName)): mcName = self.getMachine(flavorName, imageName) if len(mcName) > 0: print(mcName) self.serverData.config(serverName, imageName, flavorName, mcName) logging.info('SUCCESS') else: logging.error("No available hardware found") else: print("ERROR! Image name or file name not found") def getMachine(self, flavorName, imageName): #aggiestack server create --image linux-ubuntu-16 --flavor small ins1 flavor = self.flData.getFlavorInfo(flavorName) availableMachines = [] # Find available machines for name, hwData in self.hwData.hwDataDict.items(): availMem = hwData.mem availNoDisks = hwData.numDisks availVcpus = hwData.numVCpus if name in self.hwData.allocatedHardware: allocated = self.hwData.allocatedHardware[name] availMem = availMem - allocated.mem availNoDisks = availNoDisks - allocated.numDisks availVcpus = availVcpus - allocated.numVCpus if availMem >= flavor.mem and availNoDisks >= flavor.numDisks and availVcpus >= flavor.numVCpus: availableMachines.append(name) # Find the machine whose rack has the image for name in availableMachines: rackName = self.hwData.hwDataDict[name].rack storedImagesList = self.hwData.rackDataDict[rackName].stored_images if imageName in storedImagesList: print("Image already found") allocated = self.hwData.allocatedHardware.get( name, HardwareData()) allocated.name = name allocated.mem = allocated.mem + flavor.mem allocated.numDisks = allocated.numDisks + flavor.numDisks allocated.numVCpus = allocated.numVCpus + flavor.numVCpus self.hwData.allocatedHardware[name] = allocated return name # Find any rack where this image can be inserted for name in availableMachines: rackName = self.hwData.hwDataDict[name].rack rackStorage = self.hwData.rackDataDict[rackName].capacity if rackStorage >= int( self.imgData.imageDict[imageName].noOfAvailable): # Copy image to rack storage self.hwData.rackDataDict[rackName].stored_images.append( imageName) self.hwData.rackDataDict[rackName].capacity -= int( self.imgData.imageDict[imageName].noOfAvailable) print("Image added to rack storage") # Return Machine name by updating the parameters allocated = self.hwData.allocatedHardware.get( name, HardwareData()) allocated.name = name allocated.mem = allocated.mem + flavor.mem allocated.numDisks = allocated.numDisks + flavor.numDisks allocated.numVCpus = allocated.numVCpus + flavor.numVCpus self.hwData.allocatedHardware[name] = allocated return name if len(availableMachines) == 0: return '' else: # Simply add an instance to any machine name = availableMachines[0] allocated = self.hwData.allocatedHardware.get(name, HardwareData()) allocated.name = name allocated.mem = allocated.mem + flavor.mem allocated.numDisks = allocated.numDisks + flavor.numDisks allocated.numVCpus = allocated.numVCpus + flavor.numVCpus self.hwData.allocatedHardware[name] = allocated return name def adminShowAvailableHardware(self): self.hwData.showAvailableHardware() def canHost(self, hardwareName, flavorName): hardware = self.hwData.getHardwareInfo(hardwareName) flavor = self.flData.getFlavorInfo(flavorName) if hardware is None or flavor is None: print("\n Please verify hardware name and flavor name") logging.error('FAILURE') return False if hardware.mem >= flavor.mem and hardware.numDisks >= flavor.numDisks and hardware.numVCpus >= flavor.numVCpus: return True else: return False def remove(self, machineName): #removes the machine from the available list #first remove all the linked instances tempInst = [] for name, svData in self.serverData.serverDataDict.items(): if (svData.mcName == machineName): tempInst.append(svData.name) # for name in tempInst: # self.deleteInstance(name) if (len(tempInst) > 0): logging.error( "Can not remove. Migrate the running instance from this machine" ) print( "Can not remove. Migrate the running instance from this machine" ) return try: del self.hwData.hwDataDict[machineName] self.hwData.numOfMachines -= 1 logging.info("Machine removed") except KeyError as ex: logging.error("This machine does not exist") def addMachine(self, mem, disk, vcpu, ip, r_name, m_name): hwData = HardwareData() if (r_name not in self.hwData.rackDataDict.keys()): print("This rack does not exist, can not add machine") logging.error("This rack does not exist, can not add machine") return hwData.name = m_name hwData.rack = r_name hwData.ip = ip hwData.mem = mem hwData.numDisks = disk hwData.numVCpus = vcpu self.hwData.hwDataDict[m_name] = hwData def findRack(self, rackName): for name, rackData in self.hwData.rackDataDict.items(): if (rackData.name != rackName): subsRack = rackData.name self.hwData.rackDataDict.pop(rackName) return subsRack def evacuateRack(self, rackName): if (rackName in self.hwData.rackDataDict.keys()): substituteRack = self.findRack(rackName) if (substituteRack == None): print("Rack Not evacuated, No other rack found") else: for name, hwData in self.hwData.hwDataDict.items(): if (hwData.rack == rackName): hwData.rack = substituteRack self.hwData.hwDataDict[name] = hwData else: print(" Error!! RackName Not Found ") def adminShowCachedImagesOnRack(self, rackName): if rackName in self.hwData.rackDataDict: storedImagesList = self.hwData.rackDataDict[rackName].stored_images data = [["List of Images"]] for image in storedImagesList: data.append([image]) rackStorage = self.hwData.rackDataDict[rackName].capacity T = Texttable() T.add_rows(data) print(T.draw()) print("The amount of storage available in the given rack is ", rackStorage) else: print("Please enter valid rack-name")