def checkurl(server,url,typefile): host_name = url if '?' in url: host_name, arguments = url.split('?') components = host_name.split('/') if 'attach' in components: global VOLUME_LIST final_argument_list = [] arg_split = arguments.split('=') final_argument_list.append(arg_split[1]) args=final_argument_list vid=int(args[0]) volid=int(args[1])-1 machid=data.vmdetails[vid][2] m_addr=data.pm[machid][0] path = 'remote+ssh://'+m_addr+'/system' vm_na=data.vmdetails[vid][0] if int(VOLUME_LIST[volid][2])==0: #that volume is not already attached to something try: connect = libvirt.open(path) dom=connect.lookupByName(vm_na) f=open("/etc/ceph/ceph.conf",'r') l=f.readlines() l1=f.split("\n") host=l1[2].split('=')[1] f.close() xml="<disk type='network' device='disk'> \ <source protocol='rbd' name='rbd/"+VOLUME_LIST[volid][0]+"'> \ <host name="+str(host)+" port='6789'/> \ </source> \ <target dev='hdb' bus='virtio'/> \ </disk>" dom.attachDevice(xml) server.wfile.write(json_out({"status":1})) VOLUME_LIST[volid][2]=1 VOLUME_LIST[volid][3]=int(vid) except: server.wfile.write(json_out({"status":0})) else: server.wfile.write(json_out({"Storage Already attached to some machine so! status":0})) if 'detach' in components: global VOLUME_LIST final_argument_list = [] arg_split = arguments.split('=') final_argument_list.append(arg_split[1]) args=final_argument_list #vid=int(args[0]) volid=int(args[0])-1 if volid in VOLUME_LIST: if VOLUME_LIST[volid][2]==1: vid=VOLUME_LIST[volid][3] machid=data.vmdetails[vid][2] m_addr=data.pm[machid][0] path = 'remote+ssh://'+m_addr+'/system' vm_na=data.vmdetails[volid][0] try: connect = libvirt.open(path) dom=connect.lookupByName(vm_na) f=open("/etc/ceph/ceph.conf",'r') l=f.readlines() l1=f.split("\n") host=l1[2].split('=')[1] f.close() xml="<disk type='network' device='disk'> \ <source protocol='rbd' name='rbd/"+VOLUME_LIST[volid][0]+"'> \ <host name="+str(host)+" port='6789'/> \ </source> \ <target dev='hdb' bus='virtio'/> \ </disk>" dom.detachDevice(xml) server.wfile.write(json_out({"status":1})) VOLUME_LIST[vid][2]=0 VOLUME_LIST[vid][3]=int(vid) except: server.wfile.write(json_out({"status":0})) else: server.wfile.write(json_out({"status":0})) else: server.wfile.write(json_out({"status":0})) if 'create' in components: final_argument_list = [] arg_split = arguments.split('&') if 'volume' in components: for i in xrange(0,2): final_argument_list.append((arg_split[i].split('='))[1]) print final_argument_list data.server_header(server,final_argument_list) global VOLUME_LIST args=final_argument_list volume_name = str(args[0]) volume_size = args[1] actual_size = str(int(float(volume_size)*(1024**3))) #as size was mentioned in GB's so conversion into bytes #os.system('sudo rbd create %s --size %s'%(str(volume_name),str(actual_size))) try: global MID rbdInstance.create(ioctx,volume_name,int(actual_size)) os.system('sudo rbd map %s --pool %s --name client.admin'%(volume_name,str(POOL_NAME))); #os.system('sudo rbd create ' + volume_name + ' --size ' + volume_size +' -k /etc/ceph/ceph.client.admin.keyring') #rbdInstance.create(ioctx,str(volume_name),actual_size) #os.system('sudo modprobe rbd') #os.system('sudo rbd map ' + volume_name + ' --pool rbd --name client.admin -k /etc/ceph/ceph.client.admin.keyring') #os.system('sudo mkfs.ext4 -m0 /dev/rbd/rbd/' + volume_name) #os.system('sudo rbd map %s --pool %s --name client.admin'%(str(volume_name),str(POOL_NAME))); global VOLUME_LIST MID=MID+1 volume_id=MID VOLUME_LIST[int(volume_id)]=[volume_name,volume_size,0,0] print "list is",VOLUME_LIST server.wfile.write(json_out({"volumeid":volume_id+1})) except: server.wfile.write(json_out({"volumeid":'0'})) else: for i in xrange(0,3): final_argument_list.append((arg_split[i].split('='))[1]) print final_argument_list create.create(server,final_argument_list) #return jsonify(volumeid=volume_id) #else: # create.create(server,final_argument_list) if 'destroy' in components: final_argument_list = [] arg_split = arguments.split('=') if 'volume' in components: final_argument_list.append(arg_split[1]) global VOLUME_LIST args=final_argument_list volume_id = int(args[0])-1 if volume_id in VOLUME_LIST: volume_name=str(VOLUME_LIST[int(volume_id)][0]) else: server.wfile.write(json_out({"status":'0'})) try: os.system('sudo rbd unmap /dev/rbd/%s/%s'%(POOL_NAME,volume_name)) rbdInstance.remove(ioctx,volume_name) #os.system("sudo rbd rm %s"%(volume_name)) server.wfile.write(json_out({"status":'1'})) del VOLUME_LIST[volume_id] except: server.wfile.write(json_out({"status":'0'})) #rbdInstance.remove(ioctx,volume_name) else: final_argument_list.append(arg_split[1]) delete.destroy(server,final_argument_list) pass if 'types' in components: data.server_header(server,200) try: f = typefile fopen = open(f) server.wfile.write(create.json_out(json.load(fopen))) except Exception,e: print str(e) server.wfile.write(create.json_out({"status":0}))
def checkurl(server,url,typefile): host_name = url if '?' in url: host_name, arguments = url.split('?') components = host_name.split('/') if 'attach' in components: #if 'detach' in components: if 'create' in components: final_argument_list = [] arg_split = arguments.split('&') if 'volume' in components: for i in xrange(0,2): final_argument_list.append((arg_split[i].split('='))[1]) print final_argument_list data.server_header(server,final_argument_list) global VOLUME_LIST args=final_argument_list volume_name = str(args[0]) volume_size = args[1] actual_size = str(int(float(volume_size)*(1024**3))) #os.system('sudo rbd create %s --size %s'%(str(volume_name),str(actual_size))) try: os.system('sudo rbd create ' + volume_name + ' --size ' + volume_size +' -k /etc/ceph/ceph.client.admin.keyring') #rbdInstance.create(ioctx,str(volume_name),actual_size) os.system('sudo modprobe rbd') os.system('sudo rbd map ' + volume_name + ' --pool rbd --name client.admin -k /etc/ceph/ceph.client.admin.keyring') #os.system('sudo mkfs.ext4 -m0 /dev/rbd/rbd/' + volume_name) #os.system('sudo rbd map %s --pool %s --name client.admin'%(str(volume_name),str(POOL_NAME))); global VOLUME_LIST volume_id=len(VOLUME_LIST) VOLUME_LIST[int(volume_id)]=volume_name print VOLUME_LIST server.wfile.write(json_out({"volumeid":volume_id+1})) except: server.wfile.write(json_out({"volumeid":'0'})) else: for i in xrange(0,3): final_argument_list.append((arg_split[i].split('='))[1]) print final_argument_list create.create(server,final_argument_list) #return jsonify(volumeid=volume_id) #else: # create.create(server,final_argument_list) if 'destroy' in components: final_argument_list = [] arg_split = arguments.split('=') if 'volume' in components: final_argument_list.append(arg_split[1]) global VOLUME_LIST args=final_argument_list volume_id = int(args[0])-1 if volume_id in VOLUME_LIST: volume_name=str(VOLUME_LIST[int(volume_id)]) else: print "here\n" try: os.system('sudo rbd unmap /dev/rbd/%s/%s'%(POOL_NAME,volume_name)) os.system("sudo rbd rm %s"%(volume_name)) server.wfile.write(json_out({"status":'1'})) except: server.wfile.write(json_out({"status":'0'})) #rbdInstance.remove(ioctx,volume_name) else: final_argument_list.append(arg_split[1]) delete.destroy(server,final_argument_list) pass if 'types' in components: data.server_header(server,200) try: f = typefile fopen = open(f) server.wfile.write(create.json_out(json.load(fopen))) except Exception,e: print str(e) server.wfile.write(create.json_out({"status":0})) if 'query' in components: if 'volume' in components: print 'volume' else: print "entered!!" final_argument_list = int(arguments.split('=')[1]) query(server,final_argument_list) if 'list' in components: listimage(server)
def checkurl(server, url, typefile): host_name = url if "?" in url: host_name, arguments = url.split("?") components = host_name.split("/") # if 'attach' in components: # if 'detach' in components: if "create" in components: final_argument_list = [] arg_split = arguments.split("&") if "volume" in components: for i in xrange(0, 2): final_argument_list.append((arg_split[i].split("="))[1]) print final_argument_list data.server_header(server, final_argument_list) global VOLUME_LIST args = final_argument_list volume_name = str(args[0]) volume_size = args[1] actual_size = str(int(float(volume_size) * (1024 ** 3))) # os.system('sudo rbd create %s --size %s'%(str(volume_name),str(actual_size))) try: global MID rbdInstance.create(ioctx, volume_name, int(volume_size)) os.system("sudo rbd map %s --pool %s --name client.admin" % (volume_name, str(POOL_NAME))) # os.system('sudo rbd create ' + volume_name + ' --size ' + volume_size +' -k /etc/ceph/ceph.client.admin.keyring') # rbdInstance.create(ioctx,str(volume_name),actual_size) # os.system('sudo modprobe rbd') # os.system('sudo rbd map ' + volume_name + ' --pool rbd --name client.admin -k /etc/ceph/ceph.client.admin.keyring') # os.system('sudo mkfs.ext4 -m0 /dev/rbd/rbd/' + volume_name) # os.system('sudo rbd map %s --pool %s --name client.admin'%(str(volume_name),str(POOL_NAME))); global VOLUME_LIST MID = MID + 1 volume_id = MID VOLUME_LIST[int(volume_id)] = [volume_name, volume_size, 0, 0] print "list is", VOLUME_LIST server.wfile.write(json_out({"volumeid": volume_id + 1})) except: server.wfile.write(json_out({"volumeid": "0"})) else: for i in xrange(0, 3): final_argument_list.append((arg_split[i].split("="))[1]) print final_argument_list create.create(server, final_argument_list) # return jsonify(volumeid=volume_id) # else: # create.create(server,final_argument_list) if "destroy" in components: final_argument_list = [] arg_split = arguments.split("=") if "volume" in components: final_argument_list.append(arg_split[1]) global VOLUME_LIST args = final_argument_list volume_id = int(args[0]) - 1 if volume_id in VOLUME_LIST: volume_name = str(VOLUME_LIST[int(volume_id)][0]) else: print "here\n" try: os.system("sudo rbd unmap /dev/rbd/%s/%s" % (POOL_NAME, volume_name)) rbdInstance.remove(ioctx, volume_name) # os.system("sudo rbd rm %s"%(volume_name)) server.wfile.write(json_out({"status": "1"})) del VOLUME_LIST[volume_id] except: server.wfile.write(json_out({"status": "0"})) # rbdInstance.remove(ioctx,volume_name) else: final_argument_list.append(arg_split[1]) delete.destroy(server, final_argument_list) pass if "types" in components: data.server_header(server, 200) try: f = typefile fopen = open(f) server.wfile.write(create.json_out(json.load(fopen))) except Exception, e: print str(e) server.wfile.write(create.json_out({"status": 0}))