def interfaces(request, compute_id): """ :param request: :param compute_id: :return: """ ifaces_all = [] compute = get_object_or_404(Compute, pk=compute_id) try: conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type) ifaces = conn.get_ifaces() try: netdevs = conn.get_net_devices() except: netdevs = ["eth0", "eth1"] for iface in ifaces: interf = wvmInterface(compute.hostname, compute.login, compute.password, compute.type, iface) ifaces_all.append(interf.get_details()) if request.method == "POST": if "create" in request.POST: form = AddInterface(request.POST) if form.is_valid(): data = form.cleaned_data conn.create_iface( data["name"], data["itype"], data["start_mode"], data["netdev"], data["ipv4_type"], data["ipv4_addr"], data["ipv4_gw"], data["ipv6_type"], data["ipv6_addr"], data["ipv6_gw"], data["stp"], data["delay"], ) return HttpResponseRedirect(request.get_full_path()) else: for msg_err in form.errors.values(): messages.error(request, msg_err.as_text()) conn.close() except libvirtError as lib_err: messages.error(request, lib_err) return render(request, "interfaces.html", locals())
def interfaces(request, compute_id): """ :param request: :return: """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('index')) if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) ifaces_all = [] error_messages = [] compute = get_object_or_404(Compute, pk=compute_id) try: conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type) ifaces = conn.get_ifaces() try: netdevs = conn.get_net_device() except: netdevs = ['eth0', 'eth1'] for iface in ifaces: ifaces_all.append(conn.get_iface_info(iface)) if request.method == 'POST': if 'create' in request.POST: form = AddInterface(request.POST) if form.is_valid(): data = form.cleaned_data conn.create_iface(data['name'], data['itype'], data['start_mode'], data['netdev'], data['ipv4_type'], data['ipv4_addr'], data['ipv4_gw'], data['ipv6_type'], data['ipv6_addr'], data['ipv6_gw'], data['stp'], data['delay']) return HttpResponseRedirect(request.get_full_path()) else: for msg_err in form.errors.values(): error_messages.append(msg_err.as_text()) conn.close() except libvirtError as lib_err: error_messages.append(lib_err) return render(request, 'interfaces.html', locals())
def interfaces(request, host_id): """ Interfaces block """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('index')) if not request.user.is_staff: raise PermissionDenied errors = [] ifaces_all = [] compute = Compute.objects.get(id=host_id) try: conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type, compute.hypervisor) ifaces = conn.get_ifaces() try: netdevs = conn.get_net_device() except: netdevs = ['eth0', 'eth1'] for iface in ifaces: ifaces_all.append(conn.get_iface_info(iface)) if request.method == 'POST': if 'create' in request.POST: form = AddInterface(request.POST) if form.is_valid(): data = form.cleaned_data conn.create_iface(data['name'], data['itype'], data['start_mode'], data['netdev'], data['ipv4_type'], data['ipv4_addr'], data['ipv4_gw'], data['ipv6_type'], data['ipv6_addr'], data['ipv6_gw'], data['stp'], data['delay']) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) return render_to_response('interfaces.html', locals(), context_instance=RequestContext(request))
def interfaces(request, host_id): """ Interfaces block """ errors = [] ifaces_all = [] compute = Compute.objects.get(id=host_id) try: conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type) ifaces = conn.get_ifaces() try: netdevs = conn.get_net_device() except: netdevs = ['eth0', 'eth1'] for iface in ifaces: ifaces_all.append(conn.get_iface_info(iface)) if request.method == 'POST': if 'create' in request.POST: form = AddInterface(request.POST) if form.is_valid(): data = form.cleaned_data conn.create_iface(data['name'], data['itype'], data['start_mode'], data['netdev'], data['ipv4_type'], data['ipv4_addr'], data['ipv4_gw'], data['ipv6_type'], data['ipv6_addr'], data['ipv6_gw'], data['stp'], data['delay']) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) return render(None, 'interfaces.html', locals(), request)
def create(request, host_id): """ Create new instance. """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) conn = None errors = [] ifaces_all = [] ifaces_all_name = "" storages = [] hd_resources = {} meta_prealloc = False compute = Compute.objects.get(id=host_id) flavors = Flavor.objects.filter().order_by('id') flavor_name_list = Flavor.objects.filter(label='label') memory_range = [2048, 4096, 6144, 8192, 16384] vcpu_range = None try: conn = wvmCreate(compute.hostname, compute.login, compute.password, compute.type) conn_interfaces = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type) ifaces = conn_interfaces.get_ifaces() storages = sorted(conn.get_storages()) instances = conn.get_instances() get_images = sorted(conn.get_storages_images()) if os.path.exists(util.get_hd_resources_conf()): hd_resources = util.load_hd_resources() mem_left = hd_resources["mem"] vcpu_left = hd_resources["vcpu"] hd_resources = util.filter_hd_resources(hd_resources) else: hd_resources = util.create_hd_resources() mem_left = hd_resources["mem"] vcpu_left = hd_resources["vcpu"] hd_resources = util.filter_hd_resources(hd_resources) memory_range = [ memory for memory in memory_range if memory/1024 <= mem_left ] vcpu_range = xrange(1, int(vcpu_left) + 1) except libvirtError as err: errors.append(err) if conn_interfaces: try: netdevs = conn_interfaces.get_net_device() except: netdevs = ['eth0', 'eth1'] if conn: if not storages: msg = _("You haven't defined have any storage pools") errors.append(msg) if request.method == 'POST': if 'create_flavor' in request.POST: form = FlavorAddForm(request.POST) if form.is_valid(): data = form.cleaned_data flavor_name_list = Flavor.objects.filter(label=data['label']) if flavor_name_list: msg = _("A virtual machine template with this name already exists") errors.append(msg) else: create_flavor = Flavor(label=data['label'], vcpu=data['vcpu'], memory=data['memory'], disk=data['disk']) create_flavor.save() return HttpResponseRedirect(request.get_full_path()) if 'delete_flavor' in request.POST: flavor_id = request.POST.get('flavor', '') delete_flavor = Flavor.objects.get(id=flavor_id) delete_flavor.delete() return HttpResponseRedirect(request.get_full_path()) if 'create_xml' in request.POST: xml = request.POST.get('from_xml', '') try: name = util.get_xml_path(xml, '/domain/name') except util.libxml2.parserError: name = None if name in instances: msg = _("A virtual machine with this name already exists") errors.append(msg) else: try: conn._defineXML(xml) return HttpResponseRedirect(reverse('instance', args=[host_id, name])) except libvirtError as err: errors.append(err.message) if 'create' in request.POST: volumes = {} interfaces = [] vm_vfs_info = {} form = NewVMForm(request.POST) if form.is_valid(): data = form.cleaned_data if instances: if data['name'] in instances: msg = _("A virtual machine with this name already exists") errors.append(msg) if not errors: if data['hdd_size']: try: path = conn.create_volume(data['storage'], data['name'], data['hdd_size'], metadata=meta_prealloc) volumes[path] = conn.get_volume_type(path) except libvirtError as msg_error: errors.append(msg_error.message) elif data['template']: templ_path = conn.get_volume_path(data['template']) clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc) volumes[clone_path] = conn.get_volume_type(clone_path) ifaces_all = request.POST.getlist('interfaces') else: if not data['images']: msg = _("First you need to create or select an image") errors.append(msg) else: for vol in data['images'].split(','): try: path = conn.get_volume_path(vol) volumes[path] = conn.get_volume_type(path) except libvirtError as msg_error: errors.append(msg_error.message) if not errors: uuid = util.randomUUID() try: template_ver3_flag = True if data['template'].find("WiseGrid_V3") == -1: template_ver3_flag = False conn.create_instance(data['name'], data['cur_memory'], data['cur_vcpu'], uuid, volumes, ifaces_all, False, True, template_ver3_flag) create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid) create_instance.save() vm_vfs_info[data['name']] = ifaces_all if not errors: util.update_vfs_fro_vm(vm_vfs_info) return HttpResponseRedirect(reverse('instance', args=[host_id, data['name']])) except libvirtError as err: if data['hdd_size']: conn.delete_volume(volumes.keys()[0]) errors.append(err) else: print form.errors errors.append(form.errors) conn.close() return HttpResponseRedirect(reverse('instances',args=[host_id,errors]))
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) def show_clone_disk(disks): clone_disk = [] for disk in disks: if disk['image'] is None: continue if disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7: name, suffix = disk['image'].rsplit(".", 1) image = name + "-clone" + "." + suffix else: image = disk['image'] + "-clone" clone_disk.append( {'dev': disk['dev'], 'storage': disk['storage'], 'image': image, 'format': disk['format']}) return clone_disk errors = [] messages = [] time_refresh = TIME_JS_REFRESH * 3 compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS console_types = QEMU_CONSOLE_TYPES try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) conn_interfaces = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type) ifaces = conn_interfaces.get_ifaces() i = 0 ifaces_all = util.get_free_vfs() ifaces_all = sorted(ifaces_all.iteritems(),key=lambda ax:ax[0]) print ifaces_all temp_ifaces = [] hd_resources = [] hd_resources_checked = conn.get_hd_resources_device() if os.path.exists(util.get_hd_resources_conf()): hd_resources = util.load_hd_resources() for vf_filter in hd_resources.keys(): if vf_filter == "mem" or vf_filter == "vcpu": continue if hd_resources[vf_filter]['used'] == 1: del hd_resources[vf_filter] else: hd_resources = util.create_hd_resources() vcpu_left = hd_resources["vcpu"] mem_left = hd_resources["mem"] del hd_resources["vcpu"] del hd_resources["mem"] is_vf = False status = conn.get_status() autostart = conn.get_autostart() vcpu = conn.get_vcpu() cur_vcpu = conn.get_cur_vcpu() uuid = conn.get_uuid() memory = conn.get_memory() cur_memory = conn.get_cur_memory() description = conn.get_description() disks = conn.get_disk_device() media = conn.get_media_device() networks = conn.get_net_device() hd_resources_checked = conn.get_hd_resources_device() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() vcpu_max = vcpu_range vcpu_range = xrange(1, int(vcpu_left) + 1) memory_range = [2048, 4096, 6144, 8192, 16384] memory_range = [ memory for memory in memory_range if memory/1024 <= mem_left ] if memory not in memory_range: insort(memory_range, memory) if cur_memory not in memory_range: insort(memory_range, cur_memory) memory_host = conn.get_max_memory() vcpu_host = len(vcpu_max) telnet_port = conn.get_telnet_port() console_type = conn.get_console_type() console_port = conn.get_console_port() console_keymap = conn.get_console_keymap() snapshots = sorted(conn.get_snapshot(), reverse=True) inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE) has_managed_save_image = conn.get_managed_save_image() clone_disks = show_clone_disk(disks) console_passwd = conn.get_console_passwd() vf_infos = util.get_pfvf_map(vname) if not vf_infos: vf_infos = { "test_pf1":[("vf1",'1.0.21'),('vf2','1.0.22')], "test_pf2":[("vf2",'1.0.31'),("vf3",'1.0.32')], "test_pf3":[("vf3",'1.0.41'),("vf5",'1.0.42')], } try: instance = Instance.objects.get(compute_id=host_id, name=vname) if instance.uuid != uuid: instance.uuid = uuid instance.save() except Instance.DoesNotExist: instance = Instance(compute_id=host_id, name=vname, uuid=uuid) instance.save() if request.method == 'POST': if 'start' in request.POST: conn.start() return HttpResponseRedirect(request.get_full_path() + '#shutdown') if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): conn.shutdown() return HttpResponseRedirect(request.get_full_path() + '#shutdown') if 'destroy' == request.POST.get('power', ''): conn.force_shutdown() return HttpResponseRedirect(request.get_full_path() + '#forceshutdown') if 'managedsave' == request.POST.get('power', ''): conn.managedsave() return HttpResponseRedirect(request.get_full_path() + '#managedsave') if 'deletesaveimage' in request.POST: conn.managed_save_remove() return HttpResponseRedirect(request.get_full_path() + '#managedsave') if 'suspend' in request.POST: conn.suspend() return HttpResponseRedirect(request.get_full_path() + '#suspend') if 'resume' in request.POST: conn.resume() return HttpResponseRedirect(request.get_full_path() + '#suspend') if 'delete' in request.POST: if conn.get_status() == 1: conn.force_shutdown() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() conn.delete_disk() hd_resources_all = util.load_hd_resources() for vf in hd_resources_checked: hd_resources_all[vf]['used'] = 0 hd_resources_all["vcpu"] = int(hd_resources_all["vcpu"]) + vcpu hd_resources_all["mem"] = int(hd_resources_all["mem"]) + cur_memory / 1024 util.save_hd_resources(hd_resources_all) finally: conn.delete() return HttpResponseRedirect(reverse('instances', args=[host_id])) if 'snapshot' in request.POST: name = request.POST.get('name', '') conn.create_snapshot(name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'umount_iso' in request.POST: image = request.POST.get('path', '') dev = request.POST.get('umount_iso', '') conn.umount_iso(dev, image) return HttpResponseRedirect(request.get_full_path() + '#instancemedia') if 'mount_iso' in request.POST: image = request.POST.get('media', '') dev = request.POST.get('mount_iso', '') conn.mount_iso(dev, image) return HttpResponseRedirect(request.get_full_path() + '#instancemedia') if 'set_autostart' in request.POST: conn.set_autostart(1) return HttpResponseRedirect(request.get_full_path() + '#instancesettings') if 'unset_autostart' in request.POST: conn.set_autostart(0) return HttpResponseRedirect(request.get_full_path() + '#instancesettings') if 'change_settings' in request.POST: description = request.POST.get('description', '') cur_vcpu_original = vcpu cur_mem_original = cur_memory vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') memory_custom = request.POST.get('memory_custom', '') if memory_custom: memory = memory_custom cur_memory = request.POST.get('cur_memory', '') cur_memory_custom = request.POST.get('cur_memory_custom', '') hd_resources_set = request.POST.getlist("ethx") if cur_memory_custom: cur_memory = cur_memory_custom conn.change_settings(description, cur_memory, cur_memory, cur_vcpu, cur_vcpu, hd_resources_set) hd_resources_all = util.load_hd_resources() for vf in hd_resources_checked: if vf not in hd_resources_set: hd_resources_all[vf]['used'] = 0 for vf in hd_resources_set: hd_resources_all[vf]['used'] = 1 hd_resources_all["vcpu"] = int(hd_resources_all["vcpu"]) - int(cur_vcpu) + cur_vcpu_original hd_resources_all["mem"] = (int(hd_resources_all["mem"]) * 1024 - int(cur_memory)) / 1024 + int(cur_mem_original)/1024 util.save_hd_resources(hd_resources_all) return HttpResponseRedirect(request.get_full_path() + '#instancesettings') if 'change_xml' in request.POST: xml = request.POST.get('inst_xml', '') if xml: conn._defineXML(xml) return HttpResponseRedirect(request.get_full_path() + '#instancexml') if 'set_console_passwd' in request.POST: if request.POST.get('auto_pass', ''): passwd = ''.join([choice(letters + digits) for i in xrange(12)]) else: passwd = request.POST.get('console_passwd', '') clear = request.POST.get('clear_pass', False) if clear: passwd = '' if not passwd and not clear: msg = _("Enter the console password or select Generate") errors.append(msg) if not errors: if not conn.set_console_passwd(passwd): msg = _("Error setting console password. You should check that your instance have an graphic device.") errors.append(msg) else: return HttpResponseRedirect(request.get_full_path() + '#console_pass') if 'set_console_keymap' in request.POST: keymap = request.POST.get('console_keymap', '') clear = request.POST.get('clear_keymap', False) if clear: conn.set_console_keymap('') else: conn.set_console_keymap(keymap) return HttpResponseRedirect(request.get_full_path() + '#console_keymap') if 'set_console_type' in request.POST: console_type = request.POST.get('console_type', '') conn.set_console_type(console_type) return HttpResponseRedirect(request.get_full_path() + '#console_type') if 'migrate' in request.POST: compute_id = request.POST.get('compute_id', '') live = request.POST.get('live_migrate', False) unsafe = request.POST.get('unsafe_migrate', False) xml_del = request.POST.get('xml_delete', False) new_compute = Compute.objects.get(id=compute_id) conn_migrate = wvmInstances(new_compute.hostname, new_compute.login, new_compute.password, new_compute.type) conn_migrate.moveto(conn, vname, live, unsafe, xml_del) conn_migrate.define_move(vname) conn_migrate.close() return HttpResponseRedirect(reverse('instance', args=[compute_id, vname])) if 'delete_snapshot' in request.POST: snap_name = request.POST.get('name', '') conn.snapshot_delete(snap_name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'revert_snapshot' in request.POST: snap_name = request.POST.get('name', '') conn.snapshot_revert(snap_name) msg = _("Successful revert snapshot: ") msg += snap_name messages.append(msg) if 'clone' in request.POST: clone_data = {} clone_data['name'] = request.POST.get('name', '') for post in request.POST: if 'disk' or 'meta' in post: clone_data[post] = request.POST.get(post, '') conn.clone_instance(clone_data) return HttpResponseRedirect(reverse('instance', args=[host_id, clone_data['name']])) if 'add_vf' in request.POST: from django.utils.translation import ugettext as _ count_vf = 0 for pf,vfs in vf_infos.items(): for vf in vfs: count_vf = count_vf+1 cnt_max = settings.vf_cnt_max if count_vf>cnt_max: errors.append(_("One instance has amount max VF is:")+str(cnt_max)) return render_to_response('instance.html', locals(), context_instance=RequestContext(request)) vf_data = request.POST.getlist('interfaces') if (len(vf_data)+count_vf)>cnt_max: errors.append(_("One instance has amount max VF is:")+str(cnt_max)) return render_to_response('instance.html', locals(), context_instance=RequestContext(request)) inst_xml = conn.change_nics_settings(True, vf_data) vf_infos = util.get_pfvf_map(vname) if not vf_infos: vf_infos = { "test_pf1":[("vf1",'1.0.21'),('vf2','1.0.22')], "test_pf2":[("vf2",'1.0.31'),("vf3",'1.0.32')], "test_pf3":[("vf3",'1.0.41'),("vf5",'1.0.42')], } return HttpResponseRedirect('/instance/%s/%s'%(host_id,vname)) if 'del_vf' in request.POST: del_vf_id = request.POST.get('vf_id', '') count_vf = 0 for pf,vfs in vf_infos.items(): for vf in vfs: count_vf = count_vf+1 if count_vf == 1: errors.append(_("It is the last Vf. Can't delete !")) return render_to_response('instance.html', locals(), context_instance=RequestContext(request)) inst_xml = conn.change_nics_settings(False, [del_vf_id]) vf_infos = util.get_pfvf_map(vname) is_vf = True if not vf_infos: vf_infos = { "test_pf1":[("vf1",'1.0.21'),('vf2','1.0.22')], "test_pf2":[("vf2",'1.0.31'),("vf3",'1.0.32')], "test_pf3":[("vf3",'1.0.41'),("vf5",'1.0.42')], } return HttpResponseRedirect('/instance/%s/%s'%(host_id,vname)) conn.close() except libvirtError as err: errors.append(err) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))