def request(server, attrs): get.intialize(server,200) try: pmid = attrs["pmid"] machine = get.find_machine_pmid(pmid) user = machine[0] ip = machine[1] connect = libv.open(get.make_path(user,ip)) #int numDomains; #numDomains = NumOfDomains(connect) #server.write(json_out({"pmid":pmid,"vms":numDomains})) #int *activeDomains #activeDomains = malloc(sizeof(int) * numDomains) domains = connect.listDefinedDomains() out = {"pmid": pmid} for d in domains: out.append(find_vmid_name(d)) server.write(json_out({"vmids":out})) except: server.write(json_out({"vmids":[]}))
def destroy(vid): try: cnt = 0 for i in create_vm.vmList: if i[0] == vid: break cnt = cnt + 1 machine = get.machine_list[i[3]] user = machine[0] ip = machine[1] print user connect = libvirt.open(get.make_path(user, ip)) req = connect.lookupByName(i[1]) if req.isActive(): req.destroy() req.undefine() del create_vm.vmList[cnt] return {"status": "1"} except: return {"status": "0"}
def destroy(vid): try: cnt=0 for i in create_vm.vm_list: if i[0]==vid: break cnt=cnt+1 machine=get.machine_list[i[3]] user=machine[0] ip=machine[1] print user connect = libvirt.open(get.make_path(user,ip)) req = connect.lookupByName(i[1]) if req.isActive(): req.destroy() req.undefine() del create_vm.vm_list[cnt] return {"status":"1"} except: return {"status":"0"}
def request(server, attrs): get.intialize(server, 200) try: name = attrs["name"] instance_type = int(attrs["instance_type"]) #addr = "[email protected]/" machine = get.machine_list[random()*len(get.machines_list)] user = machine[0] ip = machine[1] connect = libv.open(get.make_path(user, ip)) req = connect.defineXML(create_xml(name, connect.getType().lower())) try: req.create() except: server.write(json_out({"vmid":0})) vmid = get.next_vmid() get.req[vmid] = [name, instance_type, machine[2]] server.write(json_out({"vmid":vmid})) except: server.write(json_out({"vmid":0}))
def create(attrs): global pmID, vmID, vmList #Attributes to XML name = attrs["name"] instance_type = int(attrs["instance_type"]) image_id = attrs["image_id"] Image_name = image_id + ".img" Ram = get.Desc['types'][instance_type - 1]['ram'] Ram = Ram * 1024 vcpu = int(get.Desc['types'][instance_type - 1]['cpu']) counter = 1 machine = get.machine_list[pmID] # Get path for user machine user = machine[0] ip = machine[1] #Check free space in cpu free_cpu = int( subprocess.check_output("ssh " + user + "@" + ip + " nproc", shell=True)) #Free space in machine avail_space = (subprocess.check_output("ssh " + user + "@" + ip + " free -m", shell=True)) avail_space = avail_space.split("\n") avail_space = avail_space[1].split() avail_ram = int(avail_space[3]) avail_ram = avail_ram * 1024 try: temp = (subprocess.check_output("ssh " + user + "@" + ip + " cat /proc/cpuinfo | grep lm ", shell=True)) bits = '64' except: bits = '32' os_arch = (((get.img_list[int(image_id) - 1])[-1]).split('amd')[-1]).split('.')[0] while (free_cpu < vcpu or avail_ram < Ram or int(bits) < int(os_arch)): pmID = (pmID + 1) % (len(get.machine_list)) counter = counter + 1 if (counter > len(get.machine_list)): return { "Error": " Specifications could not be satisfied, Virtual Machine cannot be created" } machine = get.machine_list[pmID] user = machine[0] ip = machine[1] free_cpu = int( subprocess.check_output("ssh " + user + "@" + ip + " nproc", shell=True)) avail_space = (subprocess.check_output("ssh " + user + "@" + ip + " free -m", shell=True)) avail_space = avail_space.split("\n") avail_space = avail_space[1].split() avail_ram = int(avail_space[3]) avail_ram = avail_ram * 1024 os_arch = (((get.img_list[int(image_id) - 1])[-1]).split('amd')[-1]).split('.')[0] vmID = vmID + 1 vmList.append([vmID, name, instance_type, pmID]) pmID = (pmID + 1) % (len(get.machine_list)) uid = str(uuid4()) #try: # os.path.exists("~/"+Image_name+"/") # except: get.scp_img_path(int(image_id)) # print Image_name Image_path = "/home/" + user + "/" + Image_name os.system("scp ~/" + Image_name + " " + user + "@" + ip + ":" + Image_path + " 2> /dev/null") connect = libvirt.open(get.make_path(user, ip)) system_info = connect.getCapabilities() emulator_path = system_info.split("emulator>") emulator_path = emulator_path[1].split("<")[0] #location of xen/qemu # print emulator_path emulator1 = system_info.split("<domain type=") emulator1 = emulator1[1].split(">")[ 0] #type of emulator present on given machine xen/qemu # print emulator1 arch_type = system_info.split("<arch>") arch_type = arch_type[1].split("<")[ 0] #archituctue of machine print arch_type req = connect.defineXML( create_xml(name, connect.getType().lower(), uid, Image_path, str(Ram), str(vcpu), emulator_path, emulator1, arch_type)) try: req.create() return {"vmID": vmID} except: return {"vmID": 0}
def create(attrs): name=attrs["name"] instance_type = int(attrs["instance_type"]) image_id=attrs["image_id"] num=1 Image_name = image_id + ".img" Ram=get.Desc['types'][instance_type-1]['ram'] Ram = Ram * 1024 vcpu=int(get.Desc['types'][instance_type-1]['cpu']) global pmid,vmid,vm_list tot = 1 machine = get.machine_list[pmid] user = machine[0] ip = machine[1] avail_cpu=int(subprocess.check_output("ssh " + user + "@" + ip + " nproc" ,shell=True)) free_space=(subprocess.check_output("ssh " + user + "@" + ip + " free -m" ,shell=True)) free_space=free_space.split("\n") free_space=free_space[1].split() avail_ram=int(free_space[3]) avail_ram = avail_ram * 1024 try: anystr=(subprocess.check_output("ssh " + user + "@" + ip + " cat /proc/cpuinfo | grep lm " ,shell=True)) avail_bit = '64' except: avail_bit = '32' check_arch = (((get.img_list[int(image_id)-1])[-1]).split('/')[-1]).split('.')[0].split('_')[1] while(avail_cpu < vcpu or avail_ram < Ram or int(avail_bit) < int(check_arch)): pmid=(pmid+1)%(len(get.machine_list)) tot=tot+1 if(tot > len(get.machine_list)): return {"Error" : " Specifications could not be satisfied, Virtual Machine cannot be created" } machine = get.machine_list[pmid] user = machine[0] ip = machine[1] avail_cpu=int(subprocess.check_output("ssh " + user + "@" + ip + " nproc" ,shell=True)) free_space=(subprocess.check_output("ssh " + user + "@" + ip + " free -m" ,shell=True)) free_space=free_space.split("\n") free_space=free_space[1].split() avail_ram=int(free_space[3]) avail_ram = avail_ram * 1024 check_arch = (((get.img_list[int(image_id)-1])[-1]).split('/')[-1]).split('.')[0].split('_')[1] vmid=vmid+1 vm_list.append([vmid,name,instance_type,pmid]) pmid=(pmid+1)%(len(get.machine_list)) uid = str(uuid4()) #try: # os.path.exists("~/"+Image_name+"/") # except: get.scp_img_path(int(image_id)) # print Image_name Image_path="/home/" + user + "/" + Image_name os.system("scp ~/" + Image_name + " " + user + "@" + ip + ":" + Image_path + " 2> /dev/null")#[email protected]:~/replica11.img") connect = libvirt.open(get.make_path(user, ip)) system_info = connect.getCapabilities() emulator_path = system_info.split("emulator>") emulator_path = emulator_path[1].split("<")[0] #location of xen/qemu # print emulator_path emulator1 = system_info.split("<domain type=") emulator1 = emulator1[1].split(">")[0] #type of emulator present on given machine xen/qemu # print emulator1 arch_type = system_info.split("<arch>") arch_type = arch_type[1].split("<")[0] #archituctue of machine print arch_type req = connect.defineXML(create_xml(name, connect.getType().lower(),uid,Image_path,str(Ram),str(vcpu),emulator_path,emulator1,arch_type)) try: req.create() return {"vmid": vmid} except: return {"vmid" : 0 }
def create(attrs): global pmID,vmID,vmList #Attributes to XML name=attrs["name"] instance_type = int(attrs["instance_type"]) image_id=attrs["image_id"] Image_name = image_id + ".img" Ram=get.Desc['types'][instance_type-1]['ram'] Ram = Ram * 1024 vcpu=int(get.Desc['types'][instance_type-1]['cpu']) counter = 1 machine = get.machine_list[pmID] # Get path for user machine user = machine[0] ip = machine[1] #Check free space in cpu free_cpu=int(subprocess.check_output("ssh " + user + "@" + ip + " nproc" ,shell=True)) #Free space in machine avail_space=(subprocess.check_output("ssh " + user + "@" + ip + " free -m" ,shell=True)) avail_space=avail_space.split("\n") avail_space=avail_space[1].split() avail_ram=int(avail_space[3]) avail_ram = avail_ram * 1024 try: temp = (subprocess.check_output("ssh " + user + "@" + ip + " cat /proc/cpuinfo | grep lm " ,shell=True)) bits = '64' except: bits = '32' os_arch = (((get.img_list[int(image_id)-1])[-1]).split('amd')[-1]).split('.')[0] while(free_cpu < vcpu or avail_ram < Ram or int(bits) < int(os_arch)): pmID=(pmID+1)%(len(get.machine_list)) counter=counter+1 if(counter > len(get.machine_list)): return {"Error" : " Specifications could not be satisfied, Virtual Machine cannot be created" } machine = get.machine_list[pmID] user = machine[0] ip = machine[1] free_cpu = int(subprocess.check_output("ssh " + user + "@" + ip + " nproc" ,shell=True)) avail_space = (subprocess.check_output("ssh " + user + "@" + ip + " free -m" ,shell=True)) avail_space = avail_space.split("\n") avail_space = avail_space[1].split() avail_ram = int(avail_space[3]) avail_ram = avail_ram * 1024 os_arch = (((get.img_list[int(image_id)-1])[-1]).split('amd')[-1]).split('.')[0] vmID = vmID+1 vmList.append([vmID,name,instance_type,pmID]) pmID = (pmID+1)%(len(get.machine_list)) uid = str(uuid4()) #try: # os.path.exists("~/"+Image_name+"/") # except: get.scp_img_path(int(image_id)) # print Image_name Image_path = "/home/" + user + "/" + Image_name os.system("scp ~/" + Image_name + " " + user + "@" + ip + ":" + Image_path + " 2> /dev/null") connect = libvirt.open(get.make_path(user, ip)) system_info = connect.getCapabilities() emulator_path = system_info.split("emulator>") emulator_path = emulator_path[1].split("<")[0] #location of xen/qemu # print emulator_path emulator1 = system_info.split("<domain type=") emulator1 = emulator1[1].split(">")[0] #type of emulator present on given machine xen/qemu # print emulator1 arch_type = system_info.split("<arch>") arch_type = arch_type[1].split("<")[0] #archituctue of machine print arch_type req = connect.defineXML(create_xml(name, connect.getType().lower(),uid,Image_path,str(Ram),str(vcpu),emulator_path,emulator1,arch_type)) try: req.create() return {"vmID": vmID} except: return {"vmID" : 0 }