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 memusage(request, host_id): """ Return Memory Usage in % """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except: conn = None if conn: mem_usage = conn.memory_get_usage() return HttpResponse(mem_usage[2])
def cpuusage(request, host_id): """ Return CPU Usage in % """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except: conn = None if conn: cpu_usage = conn.cpu_get_usage() return HttpResponse(cpu_usage)
def dom_snapshot(request, host_id, vname): """ Snapshot block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] snapshot_page = True 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() vm_snapshot = conn.snapshots_get_vds(vname) if request.method == 'POST': if 'delete' in request.POST: snap_name = request.POST.get('name', '') try: conn.snapshot_delete(vname, snap_name) return HttpResponseRedirect('/snapshot/%s/%s/' % (host_id, vname)) except libvirtError as error_msg: errors.append(error_msg.message) if 'revert' in request.POST: snap_name = request.POST.get('name', '') try: conn.snapshot_revert(vname, snap_name) message = _("Successful revert snapshot: ") message = message + snap_name except libvirtError as error_msg: errors.append(error_msg.message) return render_to_response('snapshot.html', locals(), context_instance=RequestContext(request))
def clusters(request): """ Infrastructure page. """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') hosts = Host.objects.filter().order_by('id') hosts_vms = {} for host in hosts: try: import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(1) if host.conn_type == 'ssh': s.connect((host.ipaddr, host.ssh_port)) else: s.connect((host.ipaddr, 16509)) s.close() status = 1 except Exception as err: status = 2 if status == 1: conn = ConnServer(host) host_info = conn.node_get_info() host_mem = conn.memory_get_usage() hosts_vms[host.id, host.hostname, status, host_info[2], host_mem[0], host_mem[2]] = conn.vds_on_cluster() else: hosts_vms[host.id, host.hostname, status, None, None, None] = None for host in hosts_vms: hosts_vms[host] = SortHosts(hosts_vms[host]) hosts_vms = SortHosts(hosts_vms) return render_to_response('clusters.html', locals(), context_instance=RequestContext(request))
def memusage(request, host_id, vname): """ VM Memory Usage """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except: conn = None if conn: memory_usage = conn.vds_memory_usage(vname)[1] return HttpResponse(memory_usage)
def cpuusage(request, host_id, vname): """ VM Cpu Usage """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except: conn = None if conn: cpu_usage = conn.vds_cpu_usage(vname) return HttpResponse(cpu_usage)
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 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 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))