def snapshot(request, host_id): """ Snapshot block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: all_vm = SortHosts(conn.vds_get_node()) all_vm_snap = conn.snapshots_get_node() conn.close() if all_vm_snap: return HttpResponseRedirect('/snapshot/%s/%s/' % (host_id, all_vm_snap.keys()[0])) return render_to_response('snapshot.html', locals(), context_instance=RequestContext(request))
def snapshot(request, host_id): """ Snapshot block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: all_vm = conn.vds_get_node() all_vm_snap = conn.snapshots_get_node() conn.close() if all_vm_snap: return HttpResponseRedirect('/snapshot/%s/%s/' % (host_id, all_vm_snap.keys()[0])) return render_to_response('snapshot.html', locals(), context_instance=RequestContext(request))
def vnc(request, host_id, vname): """ VNC vm's block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: vnc_port = conn.vnc_get_port(vname) try: vm = Vm.objects.get(host=host_id, vname=vname) socket_port = 6080 socket_host = request.META['HTTP_HOST'] if ':' in socket_host: socket_host = re.sub('\:[0-9]+', '', socket_host) except: vm = None conn.close() response = render_to_response('vnc.html', locals(), context_instance=RequestContext(request)) response.set_cookie('token', vname) return response
def newvm(request, host_id): """ Page add new VM. """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: try: flavors = Flavor.objects.filter().order_by('id') except: flavors = 'error' all_vm = SortHosts(conn.vds_get_node()) all_networks = conn.networks_get_node() all_storages = conn.storages_get_node() all_img = conn.images_get_storages(all_storages) vcpu_range = [x for x in range(1, 9)] hdd_digits_size = [a for a in range(1, 601)] memory_range = ['128', '256', '512', '768', '1024', '2048', '4096', '8192', '16384'] if not all_networks: msg = _("You haven't defined any virtual networks") errors.append(msg) if not all_storages: msg = _("You haven't defined have any storage pools") errors.append(msg) if request.method == 'POST': if 'add_flavor' in request.POST: name = request.POST.get('name', '') vcpu = request.POST.get('vcpu', '') ram = request.POST.get('ram', '') hdd = request.POST.get('hdd', '') for flavor in flavors: if name == flavor.name: msg = _("Name already use") errors.append(msg) if not errors: flavor_add = Flavor(name=name, vcpu=vcpu, ram=ram, hdd=hdd) flavor_add.save() return HttpResponseRedirect(request.get_full_path()) if 'del_flavor' in request.POST: flavor_id = request.POST.get('flavor', '') flavor_del = Flavor.objects.get(id=flavor_id) flavor_del.delete() return HttpResponseRedirect(request.get_full_path()) if 'newvm' in request.POST: net = request.POST.get('network', '') storage = request.POST.get('storage', '') vname = request.POST.get('name', '') hdd_size = request.POST.get('hdd_size', '') img = request.POST.get('img', '') ram = request.POST.get('ram', '') vcpu = request.POST.get('vcpu', '') virtio = request.POST.get('virtio', '') symbol = re.search('[^a-zA-Z0-9\_\-\.]+', vname) if all_vm: if vname in all_vm: msg = _("A virtual machine with this name already exists") errors.append(msg) if len(vname) > 14: msg = _("The name of the virtual machine must not exceed 14 characters") errors.append(msg) if symbol: msg = _("The name of the virtual machine must not contain any special characters") errors.append(msg) if not vname: msg = _("Enter the name of the virtual machine") errors.append(msg) if not errors: if not hdd_size: if not img: msg = _("First you need to create an image") errors.append(msg) else: image = conn.image_get_path(img, all_storages) else: try: conn.new_volume(storage, vname, hdd_size) except libvirtError as msg_error: errors.append(msg_error.message) if not errors: if not img: vl = conn.storageVol(vname, storage) else: vl = conn.storageVolPath(image) image = vl.path() try: conn.add_vm(vname, ram, vcpu, image, net, virtio, all_storages) return HttpResponseRedirect('/vds/%s/%s/' % (host_id, vname)) except libvirtError as msg_error: if hdd_size: vl.delete(0) errors.append(msg_error.message) conn.close() return render_to_response('newvm.html', locals(), context_instance=RequestContext(request))
def overview(request, host_id): """ Overview page. """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] time_refresh = TIME_JS_REFRESH host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: have_kvm = conn.hard_accel_node() if not have_kvm: msg = _('Your CPU doesn\'t support hardware virtualization') errors.append(msg) all_vm = SortHosts(conn.vds_get_node()) hostname, arch, cpus, cpu_model, type_conn, libvirt_ver = conn.node_get_info( ) all_mem, mem_usage, mem_perc = conn.memory_get_usage() cpu_usage = conn.cpu_get_usage() if request.method == 'POST': vname = request.POST.get('vname', '') dom = conn.lookupVM(vname) if 'start' in request.POST: try: dom.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'shutdown' in request.POST: try: dom.shutdown() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'destroy' in request.POST: try: dom.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'suspend' in request.POST: try: dom.suspend() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'resume' in request.POST: try: dom.resume() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) conn.close() return render_to_response('overview.html', locals(), context_instance=RequestContext(request))
def storage(request, host_id, pool): """ Storage pool block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') def handle_uploaded_file(path, f_name): target = path + '/' + str(f_name) destination = open(target, 'wb+') for chunk in f_name.chunks(): destination.write(chunk) destination.close() errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: storages = conn.storages_get_node() if pool is None: if len(storages) == 0: return HttpResponseRedirect('/storage/%s/add/' % (host_id)) else: return HttpResponseRedirect('/storage/%s/%s/' % (host_id, storages.keys()[0])) if pool == 'add': if request.method == 'POST': if 'addpool' in request.POST: pool_name = request.POST.get('name', '') pool_type = request.POST.get('type', '') pool_target = request.POST.get('target', '') pool_source = request.POST.get('source', '') name_have_symbol = re.search('[^a-zA-Z0-9\_\-]+', pool_name) path_have_symbol = re.search('[^a-zA-Z0-9\/]+', pool_source) if name_have_symbol or path_have_symbol: msg = _("The host name must not contain any special characters") errors.append(msg) if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The host name must not exceed 12") errors.append(msg) if pool_type == 'logical': if not pool_source: msg = _("No device has been entered") errors.append(msg) if pool_type == 'dir': if not pool_target: msg = _("No path has been entered") errors.append(msg) if pool_name in storages.keys(): msg = _("Pool name already use") errors.append(msg) if not errors: try: conn.new_storage_pool(pool_type, pool_name, pool_source, pool_target) return HttpResponseRedirect('/storage/%s/%s/' % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = SortHosts(conn.vds_get_node()) form_hdd_size = [10, 20, 40, 80, 160, 320, 640] stg = conn.storagePool(pool) size, free, usage, percent, state, s_type, path = conn.storage_get_info(pool) if state: stg.refresh(0) volumes_info = conn.volumes_get_info(pool) if request.method == 'POST': if 'start' in request.POST: try: stg.create(0) return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'stop' in request.POST: try: stg.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'delete' in request.POST: try: stg.undefine() return HttpResponseRedirect('/storage/%s/' % host_id) except libvirtError as error_msg: errors.append(error_msg.message) if 'addimg' in request.POST: name = request.POST.get('name', '') size = request.POST.get('size', '') img_name = name + '.img' name_have_symbol = re.search('[^a-zA-Z0-9\_\-\.]+', name) if img_name in stg.listVolumes(): msg = _("Volume name already use") errors.append(msg) if not name: msg = _("No name has been entered") errors.append(msg) elif len(name) > 20: msg = _("The host name must not exceed 20") errors.append(msg) else: if name_have_symbol: msg = _("The host name must not contain any special characters") errors.append(msg) if not errors: conn.new_volume(pool, name, size) return HttpResponseRedirect(request.get_full_path()) if 'delimg' in request.POST: img = request.POST.get('img', '') try: vol = stg.storageVolLookupByName(img) vol.delete(0) return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'upload' in request.POST: if str(request.FILES['file']) in stg.listVolumes(): msg = _("ISO image already exist") errors.append(msg) else: handle_uploaded_file(path, request.FILES['file']) return HttpResponseRedirect(request.get_full_path()) if 'clone' in request.POST: img = request.POST.get('img', '') clone_name = request.POST.get('new_img', '') full_img_name = clone_name + '.img' name_have_symbol = re.search('[^a-zA-Z0-9\_\-\.]+', clone_name) if full_img_name in stg.listVolumes(): msg = _("Volume name already use") errors.append(msg) if not clone_name: msg = _("No name has been entered") errors.append(msg) elif len(clone_name) > 20: msg = _("The host name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _("The host name must not contain any special characters") errors.append(msg) if not errors: try: conn.clone_volume(pool, img, clone_name) return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) conn.close() return render_to_response('storage.html', locals(), context_instance=RequestContext(request))
def network(request, host_id, pool): """ Networks block """ if not request.user.is_authenticated(): return HttpResponseRedirect("/login") errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: networks = conn.networks_get_node() if pool is None: if len(networks) == 0: return HttpResponseRedirect("/network/%s/add/" % (host_id)) else: return HttpResponseRedirect("/network/%s/%s/" % (host_id, networks.keys()[0])) if pool == "add": if request.method == "POST": if "addpool" in request.POST: dhcp = [] pool_name = request.POST.get("name", "") net_addr = request.POST.get("net_addr", "") forward = request.POST.get("forward", "") dhcp.append(request.POST.get("dhcp", "")) name_have_symbol = re.search("[^a-zA-Z0-9\_\-]+", pool_name) ip_have_symbol = re.search("[^0-9\.\/]+", net_addr) if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The pool name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _("The pool name must not contain any special characters") errors.append(msg) if not net_addr: msg = _("No subnet has been entered") errors.append(msg) elif ip_have_symbol: msg = _("The pool name must not contain any special characters") errors.append(msg) if pool_name in networks.keys(): msg = _("Pool name already in use") errors.append(msg) try: netmask = IP(net_addr).strNetmask() ipaddr = IP(net_addr) gateway = ipaddr[0].strNormal()[-1] if gateway == "0": gw = ipaddr[1].strNormal() dhcp_start = ipaddr[2].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() else: gw = ipaddr[0].strNormal() dhcp_start = ipaddr[1].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() dhcp.append(dhcp_start) dhcp.append(dhcp_end) except: msg = _("Input subnet pool error") errors.append(msg) if not errors: try: conn.new_network_pool(pool_name, forward, gw, netmask, dhcp) return HttpResponseRedirect("/network/%s/%s/" % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = conn.vds_get_node() info = conn.network_get_info(pool) if info[0] == True: ipv4_net = conn.network_get_subnet(pool) if request.method == "POST": net = conn.networkPool(pool) if "start" in request.POST: try: net.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if "stop" in request.POST: try: net.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if "delete" in request.POST: try: net.undefine() return HttpResponseRedirect("/network/%s/" % host_id) except libvirtError as error_msg: errors.append(error_msg.message) conn.close() return render_to_response("network.html", locals(), context_instance=RequestContext(request))
def overview(request, host_id): """ Overview page. """ if not request.user.is_authenticated(): return HttpResponseRedirect("/login") errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: have_kvm = conn.hard_accel_node() if not have_kvm: msg = _("Your CPU doesn't support hardware virtualization") errors.append(msg) all_vm = conn.vds_get_node() host_info = conn.node_get_info() mem_usage = conn.memory_get_usage() cpu_usage = conn.cpu_get_usage() if request.method == "POST": vname = request.POST.get("vname", "") dom = conn.lookupVM(vname) if "start" in request.POST: try: dom.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if "shutdown" in request.POST: try: dom.shutdown() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if "destroy" in request.POST: try: dom.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if "suspend" in request.POST: try: dom.suspend() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if "resume" in request.POST: try: dom.resume() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) conn.close() return render_to_response("overview.html", locals(), context_instance=RequestContext(request))
def vds(request, host_id, vname): """ VDS block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] time_refresh = TIME_JS_REFRESH messages = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: all_vm = SortHosts(conn.vds_get_node()) vcpu, memory, mac, network, description = conn.vds_get_info(vname) cpu_usage = conn.vds_cpu_usage(vname) memory_usage = conn.vds_memory_usage(vname)[1] hdd_image = conn.vds_get_hdd(vname) iso_images = sorted(conn.get_all_media()) media = conn.vds_get_media(vname) dom = conn.lookupVM(vname) vcpu_range = [str(x) for x in range(1, 9)] memory_range = [128, 256, 512, 768, 1024, 2048, 4096, 8192, 16384] vnc_port = conn.vnc_get_port(vname) try: vm = Vm.objects.get(vname=vname) except: vm = None if request.method == 'POST': if 'start' in request.POST: try: dom.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): try: dom.shutdown() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'destroy' == request.POST.get('power', ''): try: dom.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'suspend' in request.POST: try: dom.suspend() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'resume' in request.POST: try: dom.resume() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'delete' in request.POST: try: if dom.info()[0] == 1: dom.destroy() if request.POST.get('image', ''): conn.vds_remove_hdd(vname) try: vm = Vm.objects.get(host=host_id, vname=vname) vm.delete() except: pass dom.undefine() return HttpResponseRedirect('/overview/%s/' % host_id) except libvirtError as msg_error: errors.append(msg_error.message) if 'snapshot' in request.POST: try: conn.vds_create_snapshot(vname) msg = _("Create snapshot for instance successful") messages.append(msg) except libvirtError as msg_error: errors.append(msg_error.message) if 'remove_iso' in request.POST: image = request.POST.get('iso_img', '') try: conn.vds_umount_iso(vname, image) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'add_iso' in request.POST: image = request.POST.get('iso_img', '') try: conn.vds_mount_iso(vname, image) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'edit' in request.POST: description = request.POST.get('description', '') vcpu = request.POST.get('vcpu', '') ram = request.POST.get('ram', '') try: conn.vds_edit(vname, description, ram, vcpu) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'vnc_pass' in request.POST: if request.POST.get('auto_pass', ''): from string import letters, digits from random import choice passwd = ''.join([choice(letters + digits) for i in range(12)]) else: passwd = request.POST.get('vnc_passwd', '') if not passwd: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: try: conn.vds_set_vnc_passwd(vname, passwd) vnc_pass = Vm(host_id=host_id, vname=vname, vnc_passwd=passwd) vnc_pass.save() except libvirtError as msg_error: errors.append(msg_error.message) return HttpResponseRedirect(request.get_full_path()) conn.close() return render_to_response('vds.html', locals(), context_instance=RequestContext(request))
def storage(request, host_id, pool): """ Storage pool block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') def handle_uploaded_file(path, f_name): target = path + '/' + str(f_name) destination = open(target, 'wb+') for chunk in f_name.chunks(): destination.write(chunk) destination.close() errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: storages = conn.storages_get_node() if pool is None: if len(storages) == 0: return HttpResponseRedirect('/storage/%s/add/' % (host_id)) else: return HttpResponseRedirect('/storage/%s/%s/' % (host_id, storages.keys()[0])) if pool == 'add': if request.method == 'POST': if 'addpool' in request.POST: pool_name = request.POST.get('name', '') pool_type = request.POST.get('type', '') pool_target = request.POST.get('target', '') pool_source = request.POST.get('source', '') name_have_symbol = re.search('[^a-zA-Z0-9\_\-]+', pool_name) path_have_symbol = re.search('[^a-zA-Z0-9\/]+', pool_source) if name_have_symbol or path_have_symbol: msg = _( "The host name must not contain any special characters" ) errors.append(msg) if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The host name must not exceed 12") errors.append(msg) if pool_type == 'logical': if not pool_source: msg = _("No device has been entered") errors.append(msg) if pool_type == 'dir': if not pool_target: msg = _("No path has been entered") errors.append(msg) if pool_name in storages.keys(): msg = _("Pool name already use") errors.append(msg) if not errors: try: conn.new_storage_pool(pool_type, pool_name, pool_source, pool_target) return HttpResponseRedirect('/storage/%s/%s/' % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = SortHosts(conn.vds_get_node()) form_hdd_size = [10, 20, 40, 80, 160, 320, 640] stg = conn.storagePool(pool) size, free, usage, percent, state, s_type, path = conn.storage_get_info( pool) if state: stg.refresh(0) volumes_info = conn.volumes_get_info(pool) if request.method == 'POST': if 'start' in request.POST: try: stg.create(0) return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'stop' in request.POST: try: stg.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'delete' in request.POST: try: stg.undefine() return HttpResponseRedirect('/storage/%s/' % host_id) except libvirtError as error_msg: errors.append(error_msg.message) if 'addimg' in request.POST: name = request.POST.get('name', '') size = request.POST.get('size', '') img_name = name + '.img' name_have_symbol = re.search('[^a-zA-Z0-9\_\-\.]+', name) if img_name in stg.listVolumes(): msg = _("Volume name already use") errors.append(msg) if not name: msg = _("No name has been entered") errors.append(msg) elif len(name) > 20: msg = _("The host name must not exceed 20") errors.append(msg) else: if name_have_symbol: msg = _( "The host name must not contain any special characters" ) errors.append(msg) if not errors: conn.new_volume(pool, name, size) return HttpResponseRedirect(request.get_full_path()) if 'delimg' in request.POST: img = request.POST.get('img', '') try: vol = stg.storageVolLookupByName(img) vol.delete(0) return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'upload' in request.POST: if str(request.FILES['file']) in stg.listVolumes(): msg = _("ISO image already exist") errors.append(msg) else: handle_uploaded_file(path, request.FILES['file']) return HttpResponseRedirect(request.get_full_path()) if 'clone' in request.POST: img = request.POST.get('img', '') clone_name = request.POST.get('new_img', '') full_img_name = clone_name + '.img' name_have_symbol = re.search('[^a-zA-Z0-9\_\-\.]+', clone_name) if full_img_name in stg.listVolumes(): msg = _("Volume name already use") errors.append(msg) if not clone_name: msg = _("No name has been entered") errors.append(msg) elif len(clone_name) > 20: msg = _("The host name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _( "The host name must not contain any special characters" ) errors.append(msg) if not errors: try: conn.clone_volume(pool, img, clone_name) return HttpResponseRedirect( request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) conn.close() return render_to_response('storage.html', locals(), context_instance=RequestContext(request))
def vds(request, host_id, vname): """ VDS block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] time_refresh = TIME_JS_REFRESH messages = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: all_vm = SortHosts(conn.vds_get_node()) vcpu, memory, mac, network, description = conn.vds_get_info(vname) cpu_usage = conn.vds_cpu_usage(vname) memory_usage = conn.vds_memory_usage(vname)[1] hdd_image = conn.vds_get_hdd(vname) iso_images = sorted(conn.get_all_media()) media = conn.vds_get_media(vname) dom = conn.lookupVM(vname) vcpu_range = [str(x) for x in range(1, 9)] memory_range = [128, 256, 512, 768, 1024, 2048, 4096, 8192, 16384] vnc_port = conn.vnc_get_port(vname) try: vm = Vm.objects.get(vname=vname) except: vm = None if request.method == 'POST': if 'start' in request.POST: try: dom.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): try: dom.shutdown() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'destroy' == request.POST.get('power', ''): try: dom.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'suspend' in request.POST: try: dom.suspend() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'resume' in request.POST: try: dom.resume() return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'delete' in request.POST: try: if dom.info()[0] == 1: dom.destroy() if request.POST.get('image', ''): conn.vds_remove_hdd(vname) try: vm = Vm.objects.get(host=host_id, vname=vname) vm.delete() except: pass dom.undefine() return HttpResponseRedirect('/overview/%s/' % host_id) except libvirtError as msg_error: errors.append(msg_error.message) if 'snapshot' in request.POST: try: conn.vds_create_snapshot(vname) msg = _("Create snapshot for instance successful") messages.append(msg) except libvirtError as msg_error: errors.append(msg_error.message) if 'remove_iso' in request.POST: image = request.POST.get('iso_img', '') try: conn.vds_umount_iso(vname, image) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'add_iso' in request.POST: image = request.POST.get('iso_img', '') try: conn.vds_mount_iso(vname, image) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'edit' in request.POST: description = request.POST.get('description', '') vcpu = request.POST.get('vcpu', '') ram = request.POST.get('ram', '') try: conn.vds_edit(vname, description, ram, vcpu) if vm: conn.vds_set_vnc_passwd(vname, vm.vnc_passwd) return HttpResponseRedirect(request.get_full_path()) except libvirtError as msg_error: errors.append(msg_error.message) if 'vnc_pass' in request.POST: if request.POST.get('auto_pass', ''): from string import letters, digits from random import choice passwd = ''.join( [choice(letters + digits) for i in range(12)]) else: passwd = request.POST.get('vnc_passwd', '') if not passwd: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: try: conn.vds_set_vnc_passwd(vname, passwd) vnc_pass = Vm(host_id=host_id, vname=vname, vnc_passwd=passwd) vnc_pass.save() except libvirtError as msg_error: errors.append(msg_error.message) return HttpResponseRedirect(request.get_full_path()) conn.close() return render_to_response('vds.html', locals(), context_instance=RequestContext(request))
def network(request, host_id, pool): """ Networks block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: networks = conn.networks_get_node() if pool is None: if len(networks) == 0: return HttpResponseRedirect('/network/%s/add/' % (host_id)) else: return HttpResponseRedirect('/network/%s/%s/' % (host_id, networks.keys()[0])) if pool == 'add': if request.method == 'POST': if 'addpool' in request.POST: dhcp = [] pool_name = request.POST.get('name', '') net_addr = request.POST.get('net_addr', '') forward = request.POST.get('forward', '') dhcp.append(request.POST.get('dhcp', '')) name_have_symbol = re.search('[^a-zA-Z0-9\_\-]+', pool_name) ip_have_symbol = re.search('[^0-9\.\/]+', net_addr) if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The pool name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _( "The pool name must not contain any special characters" ) errors.append(msg) if not net_addr: msg = _("No subnet has been entered") errors.append(msg) elif ip_have_symbol: msg = _( "The pool name must not contain any special characters" ) errors.append(msg) if pool_name in networks.keys(): msg = _("Pool name already in use") errors.append(msg) try: netmask = IP(net_addr).strNetmask() ipaddr = IP(net_addr) gateway = ipaddr[0].strNormal()[-1] if gateway == '0': gw = ipaddr[1].strNormal() dhcp_start = ipaddr[2].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() else: gw = ipaddr[0].strNormal() dhcp_start = ipaddr[1].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() dhcp.append(dhcp_start) dhcp.append(dhcp_end) except: msg = _("Input subnet pool error") errors.append(msg) if not errors: try: conn.new_network_pool(pool_name, forward, gw, netmask, dhcp) return HttpResponseRedirect('/network/%s/%s/' % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = SortHosts(conn.vds_get_node()) info = conn.network_get_info(pool) if info[0]: ipv4_net = conn.network_get_subnet(pool) if request.method == 'POST': net = conn.networkPool(pool) if 'start' in request.POST: try: net.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'stop' in request.POST: try: net.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if 'delete' in request.POST: try: net.undefine() return HttpResponseRedirect('/network/%s/' % host_id) except libvirtError as error_msg: errors.append(error_msg.message) conn.close() return render_to_response('network.html', locals(), context_instance=RequestContext(request))