def create(attr_vm): try: name = attr_vm['name'] instance_type = int(attr_vm['instance_type']) image_id = int(attr_vm['image_id']) machine_from = allocate1.imagelist[image_id-1][0] iso_file_path = allocate1.imagelist[image_id-1][1] # cpu_type = iso_file_path # cpu_type=cpu_type.split(".") # cpu_type=str(cpu_type[0]).split("_") typelist=get_instance_type(instance_type) pmid = pm_number(typelist) if int(pmid) is int(-1): return 0 pmachine = allocate1.machinelist[pmid-1] user = pmachine[0] ip = pmachine[1] uuid = pmachine[2] pmid = pmachine[3] string_uuid = str(uuid4()) try: conn = libvirt.open(allocate1.path(user,ip)) except: print 'connection not found' sys_info = conn.getCapabilities() emulator_path = sys_info.split("emulator>") emulator_path = emulator_path[1].split("<")[0] #location of xen/qemu domain = sys_info.split("<domain type=") domain = domain[1].split(">")[0] #type of emulator present on given machine xen/qemu arch_type = sys_info.split("<arch>") arch_type = arch_type[1].split("<")[0] #archituctue of machine print arch_type local_copy = '~/' + 'image'+str(image_id)+'.img' if os.path.exists(local_copy) is False: scp_command = "scp" + ' ' + machine_from+':'+iso_file_path + ' ' + local_copy + ' 2> /dev/null' os.system(scp_command) new_iso_path = '/home/'+ user + '/' + 'image'+str(image_id)+'.img' scp_command = "scp" + ' ' + local_copy + ' ' + user + '@' + ip + ':' + new_iso_path + ' 2> /dev/null' os.system(scp_command) try: req = conn.defineXML(create_xml(name, conn.getType().lower(),string_uuid,new_iso_path,int(typelist[0]),int(typelist[1]),emulator_path,domain,arch_type)) vmid = allocate1.get_vmid() #print req except: print 'Error: Virtual Machine not defined' try: req.create() print 'Request Completed' except: print 'Error: Virtual Machine not created' # return jsonify({"vmid":0}) allocate1.vmlist[vmid] = [name, instance_type, image_id, pmid] return vmid except: print 'Error: Virtual Machine not allocated!' return 0
def delete(vmid): pmid = allocate1.vmlist[int(vmid)][3] user = allocate1.machinelist[int(pmid)-1][0] ip = allocate1.machinelist[int(pmid)-1][1] conn = libvirt.open(allocate1.path(user,ip)) try: vm_id = conn.lookupByName(allocate1.vmlist[int(vmid)][0]) if vm_id.isActive(): vm_id.destroy() vm_id.undefine() del allocate1.vmlist[int(vmid)] return 1 except: return 0