def vm_migrate_dependency(source_host,dest_host,vmid): obj=NodeFinder() #Remove entry from host_vm_dict.pkl for the source_host #Add Entry to the dest_host in node_dict.pkl #Remove the configuration XML host_vm_dict = getHostVMDict() guest = host_vm_dict[source_host][vmid] #update guest dictionay on source_host addOrUpdateDictionaryOfVM(source_host, vmid, None) #Update host dictionary for source host resume_resources_from_guest(source_host, vmid, guest) #update guest dictionay on dest_host addOrUpdateDictionaryOfVM(dest_host, vmid, guest) #Update host dictionary for dest host host = obj.place_job (dest_host, guest.current_cpu,guest.current_memory,guest.io)
def vm_migrate_dependency(source_host, dest_host, vmid): obj = NodeFinder() #Remove entry from host_vm_dict.pkl for the source_host #Add Entry to the dest_host in node_dict.pkl #Remove the configuration XML host_vm_dict = getHostVMDict() guest = host_vm_dict[source_host][vmid] #update guest dictionay on source_host addOrUpdateDictionaryOfVM(source_host, vmid, None) #Update host dictionary for source host resume_resources_from_guest(source_host, vmid, guest) #update guest dictionay on dest_host addOrUpdateDictionaryOfVM(dest_host, vmid, guest) #Update host dictionary for dest host host = obj.place_job(dest_host, guest.current_cpu, guest.current_memory, guest.io)
def vm_submitjob(vmid, cpu, memory, max_memory, io): try: #Activity Log vmsubmission_log = open( '/var/lib/virtdc/logs/activity_logs/vmsubmission.log', 'a+') obj = NodeFinder() #memory=float(_base_memory_size) + float(memory) #print "Memory "+str(memory) #Identify is there a space for the VM host = obj.is_space_available_for_vm(cpu, memory, io) print 'VMID "', vmid print 'CPU "', cpu print 'memory"', memory print 'max_memory"', max_memory print 'io"', io if host is None: print "Cant create new domain. Space not available." #print subprocess.call("date") #print 'VMID "', vmid #print 'CPU "', cpu #print 'memory"', memory #print 'max_memory"', max_memory #print 'io"', io return False prefix_host = host print host #Code to check whether the VM can be placed if (host == _master): prefix_host = '' _imageCopyCmd = "cp " _cloneCmd = "virsh create " else: _cloneCmd = "virsh --connect qemu+ssh://" + host + "/system create " prefix_host = host + ":" _imageCopyCmd = "scp " #print "The node is %s",host #To get the guest os configuration xml for the first time #cmd = "virsh dumpxml Test_clone > /root/Desktop/PYTHON/guestconfig.xml" #p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) #command to get the xml into python string for further updates. guestconfig.xml needs to be copied(physically present) int the same folder of this script nodeInfo = "cat " + guest_config_path xmlstring = subprocess.check_output(nodeInfo, shell=True, stderr=subprocess.PIPE) #command to copy the iso image to the destination. Every VM will have an individual iso image. (I think this copy can be cleared later on). Make the nodes passwordless image_path = guest_image + vmid + ".img" image_dest = prefix_host + guest_image + vmid + ".img" cp_cmd = _imageCopyCmd + guest_image + "base_image.img " + image_dest copy_image = subprocess.check_output(cp_cmd, shell=True, stderr=subprocess.PIPE) vmsubmission_log.write( str(datetime.datetime.now()) + '::Copy Image ::' + host + ' :: ' + vmid + ' :: Successfully copied the image\n') #uuid = subprocess.check_output("uuidgen", shell=True, stderr=subprocess.PIPE) #config update based on the new VM requiement #image_path max_memory current_memory current_cpu max_cpu xmlstring = xmlstring.replace("vm_name", vmid) #xmlstring=xmlstring.replace("vm_uuid", uuid); xmlstring = xmlstring.replace("max_memory", str(int(max_memory))) xmlstring = xmlstring.replace("current_memory", str(int(memory))) xmlstring = xmlstring.replace("current_cpu", str(int(cpu))) xmlstring = xmlstring.replace("max_cpu", "8") xmlstring = xmlstring.replace("image_path", image_path) #command to write the xml string to file guest_info_file = guest_config_file_storage_path + vmid + ".xml" config_temp_file = open(guest_info_file, "w") config_temp_file.write(xmlstring) config_temp_file.close() #command to clone the image clone = _cloneCmd + guest_info_file clone_out = subprocess.check_output(clone, shell=True, stderr=subprocess.PIPE) vmsubmission_log.write( str(datetime.datetime.now()) + '::Create VM ::' + host + ' :: ' + vmid + ' :: Successfully created the VM\n') #VM Successfully created. Update the node dictionary pickle host = obj.place_job(host, cpu, max_memory, io) if host is None: print 'Issue in updating node dictionary' vmsubmission_log.write( str(datetime.datetime.now()) + '::Update Node Dictionary ::' + host + ' :: ' + vmid + ' :: Issue in updating node dictionary\n') return False # Wait for VM to boot up time.sleep(60) # Get the IP address of Virtual Machine and update in VM_Info_Updater guest_ip = getGuestIP(host.strip(), vmid.strip(), "root", "Teamb@123") # If get ip failed, terminate this VM if guest_ip == "": print 'Issue in retrieving public IP of domain : Terminating the domain!' vmsubmission_log.write( str(datetime.datetime.now()) + '::Issue in retrieving public IP of domain : Terminating the domain - ' + vmid + '\n') vm_terminate_guest(host.strip(), vmid.strip()) return False # Making the max cpu as 8 for all VM's addOrUpdateDictionaryOfVM( host, vmid, Guest(guest_ip, vmid, float(cpu), float(8), float(memory), float(max_memory), float(1), str(datetime.datetime.now()))) vmsubmission_log.write( str(datetime.datetime.now()) + '::Update IP::' + host + ' :: ' + vmid + ' :: Successfully updated the IP\n') # copy host_config.txt file to guest # this will start monitor_agent on guest do_prereq_start_workload(host, vmid) #Run Job in Guest runJobOnVM(host, vmid) vmsubmission_log.write( str(datetime.datetime.now()) + '::Run Job::' + host + ' :: ' + vmid + ' :: Successfully ran the job\n') return True except Exception, e: print 'Error occured during domain creation/run job' + str(vmid) #exc_type, exc_obj, exc_tb = sys.exc_info() #fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] #print exc_type, fname, exc_tb.tb_lineno vmsubmission_log.write( str(datetime.datetime.now()) + ' :: Create Guest :: ' + vmid + ' :: Cannot create the guest\n') #print "error>",e.output,'<' vmsubmission_log.write(str(e) + '\n') mail_subject = 'Guest creation error - ' + str(vmid) mail_content = 'Error occured during guest creation ' + vmid + ' . The error as follows, \n\n' + str( e) + '\n' send_support_mail(mail_subject, mail_content.strip()) return False
def vm_submitjob(vmid, cpu, memory, max_memory, io): try: # Activity Log vmsubmission_log = open("/var/lib/virtdc/logs/activity_logs/vmsubmission.log", "a+") obj = NodeFinder() # memory=float(_base_memory_size) + float(memory) # print "Memory "+str(memory) # Identify is there a space for the VM host = obj.is_space_available_for_vm(cpu, memory, io) print 'VMID "', vmid print 'CPU "', cpu print 'memory"', memory print 'max_memory"', max_memory print 'io"', io if host is None: print "Cant create new domain. Space not available." # print subprocess.call("date") # print 'VMID "', vmid # print 'CPU "', cpu # print 'memory"', memory # print 'max_memory"', max_memory # print 'io"', io return False prefix_host = host print host # Code to check whether the VM can be placed if host == _master: prefix_host = "" _imageCopyCmd = "cp " _cloneCmd = "virsh create " else: _cloneCmd = "virsh --connect qemu+ssh://" + host + "/system create " prefix_host = host + ":" _imageCopyCmd = "scp " # print "The node is %s",host # To get the guest os configuration xml for the first time # cmd = "virsh dumpxml Test_clone > /root/Desktop/PYTHON/guestconfig.xml" # p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) # command to get the xml into python string for further updates. guestconfig.xml needs to be copied(physically present) int the same folder of this script nodeInfo = "cat " + guest_config_path xmlstring = subprocess.check_output(nodeInfo, shell=True, stderr=subprocess.PIPE) # command to copy the iso image to the destination. Every VM will have an individual iso image. (I think this copy can be cleared later on). Make the nodes passwordless image_path = guest_image + vmid + ".img" image_dest = prefix_host + guest_image + vmid + ".img" cp_cmd = _imageCopyCmd + guest_image + "base_image.img " + image_dest copy_image = subprocess.check_output(cp_cmd, shell=True, stderr=subprocess.PIPE) vmsubmission_log.write( str(datetime.datetime.now()) + "::Copy Image ::" + host + " :: " + vmid + " :: Successfully copied the image\n" ) # uuid = subprocess.check_output("uuidgen", shell=True, stderr=subprocess.PIPE) # config update based on the new VM requiement #image_path max_memory current_memory current_cpu max_cpu xmlstring = xmlstring.replace("vm_name", vmid) # xmlstring=xmlstring.replace("vm_uuid", uuid); xmlstring = xmlstring.replace("max_memory", str(int(max_memory))) xmlstring = xmlstring.replace("current_memory", str(int(memory))) xmlstring = xmlstring.replace("current_cpu", str(int(cpu))) xmlstring = xmlstring.replace("max_cpu", "8") xmlstring = xmlstring.replace("image_path", image_path) # command to write the xml string to file guest_info_file = guest_config_file_storage_path + vmid + ".xml" config_temp_file = open(guest_info_file, "w") config_temp_file.write(xmlstring) config_temp_file.close() # command to clone the image clone = _cloneCmd + guest_info_file clone_out = subprocess.check_output(clone, shell=True, stderr=subprocess.PIPE) vmsubmission_log.write( str(datetime.datetime.now()) + "::Create VM ::" + host + " :: " + vmid + " :: Successfully created the VM\n" ) # VM Successfully created. Update the node dictionary pickle host = obj.place_job(host, cpu, max_memory, io) if host is None: print "Issue in updating node dictionary" vmsubmission_log.write( str(datetime.datetime.now()) + "::Update Node Dictionary ::" + host + " :: " + vmid + " :: Issue in updating node dictionary\n" ) return False # Wait for VM to boot up time.sleep(60) # Get the IP address of Virtual Machine and update in VM_Info_Updater guest_ip = getGuestIP(host.strip(), vmid.strip(), "root", "Teamb@123") # If get ip failed, terminate this VM if guest_ip == "": print "Issue in retrieving public IP of domain : Terminating the domain!" vmsubmission_log.write( str(datetime.datetime.now()) + "::Issue in retrieving public IP of domain : Terminating the domain - " + vmid + "\n" ) vm_terminate_guest(host.strip(), vmid.strip()) return False # Making the max cpu as 8 for all VM's addOrUpdateDictionaryOfVM( host, vmid, Guest( guest_ip, vmid, float(cpu), float(8), float(memory), float(max_memory), float(1), str(datetime.datetime.now()), ), ) vmsubmission_log.write( str(datetime.datetime.now()) + "::Update IP::" + host + " :: " + vmid + " :: Successfully updated the IP\n" ) # copy host_config.txt file to guest # this will start monitor_agent on guest do_prereq_start_workload(host, vmid) # Run Job in Guest runJobOnVM(host, vmid) vmsubmission_log.write( str(datetime.datetime.now()) + "::Run Job::" + host + " :: " + vmid + " :: Successfully ran the job\n" ) return True except Exception, e: print "Error occured during domain creation/run job" + str(vmid) # exc_type, exc_obj, exc_tb = sys.exc_info() # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] # print exc_type, fname, exc_tb.tb_lineno vmsubmission_log.write( str(datetime.datetime.now()) + " :: Create Guest :: " + vmid + " :: Cannot create the guest\n" ) # print "error>",e.output,'<' vmsubmission_log.write(str(e) + "\n") mail_subject = "Guest creation error - " + str(vmid) mail_content = "Error occured during guest creation " + vmid + " . The error as follows, \n\n" + str(e) + "\n" send_support_mail(mail_subject, mail_content.strip()) return False
def process_action_on_current_usage(host, vmid, value, cpu_usage, mem_usage, io_usage): node_dict = GetNodeDict() #for key, value in node_dict.iteritems() : #print key, value.hostname, value.ip_address, value.max_cpu, value.max_memory, value.max_io, value.avail_cpu, value.avail_memory, value.avail_io #Log activity manager_activity_log = open('/var/lib/virtdc/logs/activity_logs/manager.log', 'a+') manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::MEMORY::'+host+' :: '+vmid+' :: Alotted Memory '+str(value.current_memory)+' :: Current Memory '+str(mem_usage)+'\n') manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::CPU::'+host+' :: '+vmid+' :: Alotted CPU '+str(value.current_cpu)+' :: Current CPU '+str(cpu_usage)+'\n') obj=NodeFinder() max_cpu = value.max_cpu #print 'Max CPU '+str(max_cpu) allotted_cpu = float(value.current_cpu) #print 'Allocate CPU : '+str(allotted_cpu) #Base OS should not go below the minimum memory mem_usage = float(mem_usage) + float(_base_mem_size) allotted_memory = float(value.current_memory) max_memory = float(value.max_memory) #Check CPU usage, regarding a 0.1 margin as eligible to scale up if((cpu_usage+0.1 > allotted_cpu) and (cpu_usage < max_cpu)): if ( obj.is_cpu_available_on_host(host, 1) ): #print 'Test 2' new_cpu_value = value.current_cpu + 1 print "New CPU: %s" % new_cpu_value vm_cpu_scaling(host, vmid, value.vmip, new_cpu_value) manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::CPU::Scaling ::'+host+' :: '+vmid+' :: Memory scaled from '+str(value.current_cpu)+' to '+str(cpu_usage)+'\n') else: print 'Test 3' new_host = obj.is_space_available_for_vm(cpu_usage, mem_usage , io_usage) if new_host is None: print "Cant migrate guest" else: print 'Dest Node : '+new_host #Initiate vm migration migrate_flag = vm_migrate_guest(host, new_host, vmid) if (migrate_flag): manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::CPU::Migration ::'+host+' :: '+vmid+' :: Domain migrated from '+str(host)+' to '+str(new_host)+' for CPU Scaling from'+str(value.current_cpu)+' to '+str(cpu_usage)+'\n') else: manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::CPU::Migration ::'+host+' :: '+vmid+' :: Cannot migrate from '+str(host)+' to '+str(new_host)+' for CPU Scaling from'+str(value.current_cpu)+' to '+str(cpu_usage)+'\n') #if( (cpu_usage>current_cpu) and (cpu_usage<max_cpu) ): -- CPU scaling down is not implemented #Check Memory Usage - Memory scale up will be initiated when usage is greater than usage+scaleup_threshold #print "MEM USAGE: " + str(mem_usage) #print "Alloc mem: " + str(allotted_memory) #print "threadhold: " + str(float(mem_scale_up_threshold)) #print "Max mem: " + str(max_memory) if(((mem_usage > (allotted_memory + float(mem_scale_up_threshold))))and (mem_usage<max_memory)): required_extra_memory = mem_usage - allotted_memory print "Required Mem: " + str(required_extra_memory) if( obj.is_mem_available_on_host(host, required_extra_memory) ): vm_memory_scaling(host, vmid, float(mem_usage)) manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::MEMORY::Scaling ::'+str(host)+' :: '+str(vmid)+' :: Memory scaled from '+str(allotted_memory)+' to '+str(mem_usage)+'\n') else: new_host = obj.is_space_available_for_vm(cpu_usage, mem_usage , io_usage) if new_host is None: print "Cant migrate Guest" else: #Initiate vm migration migrate_flag = vm_migrate_guest(host, new_host, vmid) if (migrate_flag): manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::MEMORY::Migration ::'+host+' :: '+vmid+' :: Domain migrated from '+str(host)+' to '+str(new_host)+' for Memory Scaling from'+str(value.current_memory)+' to '+str(mem_usage)+'\n') else: manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::MEMORY::Migration ::'+host+' :: '+vmid+' :: Cannot migrate from '+str(host)+' to '+str(new_host)+' for Memory Scaling from'+str(value.current_memory)+' to '+str(mem_usage)+'\n') #To scale down memory - Memory scale down will be initiated when usage is lower than usage-scaledown_threshold or elif((mem_usage<(allotted_memory - float(mem_scale_down_threshold)))and (mem_usage<max_memory) ): vm_memory_scaling(host, vmid, float(mem_usage)) manager_activity_log.write(str(datetime.datetime.now())+'::PLACEMENT MANAGER::MEMORY::Scaling ::'+str(host)+' :: '+str(vmid)+' :: Memory scaled from '+str(allotted_memory)+' to '+str(mem_usage)+'\n')
def process_action_on_current_usage(host, vmid, value, cpu_usage, mem_usage, io_usage): node_dict = GetNodeDict() #for key, value in node_dict.iteritems() : #print key, value.hostname, value.ip_address, value.max_cpu, value.max_memory, value.max_io, value.avail_cpu, value.avail_memory, value.avail_io #Log activity manager_activity_log = open( '/var/lib/virtdc/logs/activity_logs/manager.log', 'a+') manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::MEMORY::' + host + ' :: ' + vmid + ' :: Alotted Memory ' + str(value.current_memory) + ' :: Current Memory ' + str(mem_usage) + '\n') manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::CPU::' + host + ' :: ' + vmid + ' :: Alotted CPU ' + str(value.current_cpu) + ' :: Current CPU ' + str(cpu_usage) + '\n') obj = NodeFinder() max_cpu = value.max_cpu #print 'Max CPU '+str(max_cpu) allotted_cpu = float(value.current_cpu) #print 'Allocate CPU : '+str(allotted_cpu) #Base OS should not go below the minimum memory mem_usage = float(mem_usage) + float(_base_mem_size) allotted_memory = float(value.current_memory) max_memory = float(value.max_memory) #Check CPU usage, regarding a 0.1 margin as eligible to scale up if ((cpu_usage + 0.1 > allotted_cpu) and (cpu_usage < max_cpu)): if (obj.is_cpu_available_on_host(host, 1)): #print 'Test 2' new_cpu_value = value.current_cpu + 1 print "New CPU: %s" % new_cpu_value vm_cpu_scaling(host, vmid, value.vmip, new_cpu_value) manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::CPU::Scaling ::' + host + ' :: ' + vmid + ' :: Memory scaled from ' + str(value.current_cpu) + ' to ' + str(cpu_usage) + '\n') else: print 'Test 3' new_host = obj.is_space_available_for_vm(cpu_usage, mem_usage, io_usage) if new_host is None: print "Cant migrate guest" else: print 'Dest Node : ' + new_host #Initiate vm migration migrate_flag = vm_migrate_guest(host, new_host, vmid) if (migrate_flag): manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::CPU::Migration ::' + host + ' :: ' + vmid + ' :: Domain migrated from ' + str(host) + ' to ' + str(new_host) + ' for CPU Scaling from' + str(value.current_cpu) + ' to ' + str(cpu_usage) + '\n') else: manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::CPU::Migration ::' + host + ' :: ' + vmid + ' :: Cannot migrate from ' + str(host) + ' to ' + str(new_host) + ' for CPU Scaling from' + str(value.current_cpu) + ' to ' + str(cpu_usage) + '\n') #if( (cpu_usage>current_cpu) and (cpu_usage<max_cpu) ): -- CPU scaling down is not implemented #Check Memory Usage - Memory scale up will be initiated when usage is greater than usage+scaleup_threshold #print "MEM USAGE: " + str(mem_usage) #print "Alloc mem: " + str(allotted_memory) #print "threadhold: " + str(float(mem_scale_up_threshold)) #print "Max mem: " + str(max_memory) if (((mem_usage > (allotted_memory + float(mem_scale_up_threshold)))) and (mem_usage < max_memory)): required_extra_memory = mem_usage - allotted_memory print "Required Mem: " + str(required_extra_memory) if (obj.is_mem_available_on_host(host, required_extra_memory)): vm_memory_scaling(host, vmid, float(mem_usage)) manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::MEMORY::Scaling ::' + str(host) + ' :: ' + str(vmid) + ' :: Memory scaled from ' + str(allotted_memory) + ' to ' + str(mem_usage) + '\n') else: new_host = obj.is_space_available_for_vm(cpu_usage, mem_usage, io_usage) if new_host is None: print "Cant migrate Guest" else: #Initiate vm migration migrate_flag = vm_migrate_guest(host, new_host, vmid) if (migrate_flag): manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::MEMORY::Migration ::' + host + ' :: ' + vmid + ' :: Domain migrated from ' + str(host) + ' to ' + str(new_host) + ' for Memory Scaling from' + str(value.current_memory) + ' to ' + str(mem_usage) + '\n') else: manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::MEMORY::Migration ::' + host + ' :: ' + vmid + ' :: Cannot migrate from ' + str(host) + ' to ' + str(new_host) + ' for Memory Scaling from' + str(value.current_memory) + ' to ' + str(mem_usage) + '\n') #To scale down memory - Memory scale down will be initiated when usage is lower than usage-scaledown_threshold or elif ((mem_usage < (allotted_memory - float(mem_scale_down_threshold))) and (mem_usage < max_memory)): vm_memory_scaling(host, vmid, float(mem_usage)) manager_activity_log.write( str(datetime.datetime.now()) + '::PLACEMENT MANAGER::MEMORY::Scaling ::' + str(host) + ' :: ' + str(vmid) + ' :: Memory scaled from ' + str(allotted_memory) + ' to ' + str(mem_usage) + '\n')