def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] instances = [] compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) get_instances = conn.get_instances() except libvirtError as msg_error: errors.append(msg_error.message) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance)}) try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
def instance_form(request, instance_name): try: instance = Instance.objects.get(name=instance_name) except Instance.DoesNotExist: messages.error(request, f'Tokens for "{instance_name}" does not exist') return redirect('/emailhistoryexport') else: if instance.access_token is None: messages.error(request, 'No access token') return redirect('/emailhistoryexport') if instance.expire_time < datetime.datetime.now(datetime.timezone.utc): response = get_refresh_token_response(instance.refresh_token) Instance.create_or_update_instance(response) context = {'instance_name': instance_name} return render(request, 'emailhistoryexport/instance_form.html', context)
def update_instance_cache(): Instance.create_cache() InstanceLevel.create_cache() EliteInstanceLevel.create_cache() Raid.create_cache() RaidLevelConf.create_cache() InstanceReward.create_cache() Enemy.create_cache() Trigger.create_cache() TriggerInfo.create_cache() TriggerData.create_cache() Zone.create_cache() GuildInstanceLevel.create_cache() SmallGame.create_cache() SmallGameReward.create_cache() ElementTowerInstance.create_cache() ElementTowerLevel.create_cache() ElementTowerBuff.create_cache()
def form(request): code = request.GET.get('code') redirect_uri = urllib.parse.quote_plus(request.build_absolute_uri('?')) # if a code is in the response params, it has been redirected from # authorization. # # Get tokens and persist to database. if code: response = get_access_token_response(request, code) if response.json().get('error'): messages.error(request, response.json().get('error_description')) else: name = Instance.create_or_update_instance(response) return redirect(f'/emailhistoryexport/{name}') context = { 'client_id': 'nndwt7ass8w95kqfgh2utw9h', 'redirect_uri': redirect_uri, } return render(request, 'emailhistoryexport/form.html', context)
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))
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) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384] 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_range) 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() 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 'pmwakeup' in request.POST: conn.pMWakeup() return HttpResponseRedirect(request.get_full_path() + '#pmwakeup') 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() if request.POST.get('delete_disk', ''): conn.delete_disk() 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 'add_cdrom' in request.POST: conn.add_cdrom() 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', '') 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', '') if cur_memory_custom: cur_memory = cur_memory_custom conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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']])) conn.close() except libvirtError as err: errors.append(err) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))
def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) errors = [] instances = [] time_refresh = 8000 get_instances = [] cnt_max = settings.vf_cnt_max conn = None meta_prealloc = False compute = Compute.objects.get(id=host_id) memory_range = [2048, 4096, 6144, 8192, 16384] ifaces_all = util.get_free_vfs() ifaces_all = sorted(ifaces_all.iteritems(),key=lambda ax:ax[0]) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) conn_create = wvmCreate(compute.hostname, compute.login, compute.password, compute.type) conn_storage = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, 'Images') state = conn_storage.is_active() if state: conn_storage.refresh() volumes = conn_storage.update_volumes() conn_storage.close() else: volumes = None get_images = volumes get_instances = conn.get_instances() except libvirtError as err: pass # errors.append(err) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance), 'has_managed_save_image': conn.get_instance_managed_save_image(instance)}) if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'managedsave' in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) 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_create.get_volume_path(data['template']) clone_path = conn_create.clone_from_template(data['name'], templ_path, metadata=meta_prealloc) volumes[clone_path] = conn_create.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.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_create.delete_volume(volumes.keys()[0]) errors.append(err) else: print form.errors errors.append(form.errors) conn.close() conn_create.close() except libvirtError as err: errors.append(err) hd_resources = util.load_hd_resources() mem_left = hd_resources["mem"] vcpu_left = hd_resources["vcpu"] memory_range = [ memory for memory in memory_range if memory/1024 <= mem_left ] vcpu_range = xrange(1, int(vcpu_left) + 1) vcpu_memory_left_flag = True if len(list(vcpu_range)) == 0 or len(memory_range) == 0: vcpu_memory_left_flag = False return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
def create(request, host_id): """ Create new instance. """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) conn = None errors = [] storages = [] networks = [] meta_prealloc = False compute = Compute.objects.get(id=host_id) flavors = Flavor.objects.filter().order_by('id') try: conn = wvmCreate(compute.hostname, compute.login, compute.password, compute.type) storages = sorted(conn.get_storages()) networks = sorted(conn.get_networks()) instances = conn.get_instances() get_images = sorted(conn.get_storages_images()) cache_modes = sorted(conn.get_cache_modes().items()) mac_auto = util.randomMAC() except libvirtError as err: errors.append(err) if conn: if not storages: msg = _("You haven't defined have any storage pools") errors.append(msg) if not networks: msg = _("You haven't defined have any network 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 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 = {} form = NewVMForm(request.POST) if form.is_valid(): data = form.cleaned_data if data['meta_prealloc']: meta_prealloc = True 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']: if not data['mac']: msg = _("No Virtual Machine MAC has been entered") errors.append(msg) else: 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) 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 data['cache_mode'] not in conn.get_cache_modes(): msg = _("Invalid cache mode") errors.append(msg) if not errors: uuid = util.randomUUID() try: conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'], uuid, volumes, data['cache_mode'], data['networks'], data['virtio'], data['mac']) create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid) create_instance.save() 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) conn.close() return render_to_response('create.html', locals(), context_instance=RequestContext(request))
def create(request, host_id): """ Create new instance. """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse("login")) conn = None errors = [] storages = [] networks = [] meta_prealloc = False compute = Compute.objects.get(id=host_id) flavors = Flavor.objects.filter().order_by("id") try: conn = wvmCreate(compute.hostname, compute.login, compute.password, compute.type) storages = sorted(conn.get_storages()) networks = sorted(conn.get_networks()) instances = conn.get_instances() get_images = sorted(conn.get_storages_images()) cache_modes = sorted(conn.get_cache_modes().items()) mac_auto = util.randomMAC() except libvirtError as err: errors.append(err) if conn: if not storages: msg = _("You haven't defined have any storage pools") errors.append(msg) if not networks: msg = _("You haven't defined have any network 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 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 = {} form = NewVMForm(request.POST) if form.is_valid(): data = form.cleaned_data if data["meta_prealloc"]: meta_prealloc = True 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"]: if not data["mac"]: msg = _("No Virtual Machine MAC has been entered") errors.append(msg) else: 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) 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 data["cache_mode"] not in conn.get_cache_modes(): msg = _("Invalid cache mode") errors.append(msg) if not errors: uuid = util.randomUUID() try: conn.create_instance( data["name"], data["memory"], data["vcpu"], data["host_model"], uuid, volumes, data["cache_mode"], data["networks"], data["virtio"], data["mac"], ) create_instance = Instance(compute_id=host_id, name=data["name"], uuid=uuid) create_instance.save() 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) conn.close() return render_to_response("create.html", locals(), context_instance=RequestContext(request))
def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) get_instances = conn.get_instances() except libvirtError as err: errors.append(err) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance), 'has_managed_save_image': conn.get_instance_managed_save_image(instance)}) if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'init' in request.POST: # print(inst) inst=request.POST.get('name') vmip=str(inst).split('-')[2] varch=str(inst).split('-')[1] vos=str(inst).split('-')[0] arch1=varch.split('_')[1] kvmip=Compute.objects.get(id=host_id) result=Network.objects.filter(ipaddr=vmip).values('netmask','gateway') if len(result) >0: kwargs={ 'ip':'%s' % kvmip, 'port':22, 'user':'******', 'vmip':vmip, 'os':vos[:6], 'version':vos[6:], 'arch':arch1, 'vmnetmask':result[0]['netmask'], 'vmgateway':result[0]['gateway'], } remote.ServerConn(**kwargs).init_vm() remote.ServerConn(**kwargs).template_vm() remote.ServerConn(**kwargs).setting_vm() remote.ServerConn(**kwargs).start_vm() else: msg='数据库内容为空,请检查程序' return HttpResponse(msg) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') def show_clone_disk(disks): clone_disk = [] for disk in disks: 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}) return clone_disk errors = [] messages = [] time_refresh = TIME_JS_REFRESH compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384] memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) telnet_port = conn.get_telnet_port() vnc_port = conn.get_vnc() vnc_keymap = conn.get_vnc_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) except libvirtError as msg_error: errors.append(msg_error.message) 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() try: if request.method == 'POST': if 'start' in request.POST: conn.start() return HttpResponseRedirect(request.get_full_path()) 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() if request.POST.get('delete_disk', ''): conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/instances/%s/' % 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', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') cur_memory = request.POST.get('cur_memory', '') conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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_vnc_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('vnc_passwd', '') clear = request.POST.get('clear_pass', False) if not passwd and not clear: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path() + '#vnc_pass') if 'set_vnc_keymap' in request.POST: keymap = request.POST.get('vnc_keymap', '') clear = request.POST.get('clear_keymap', False) if clear: conn.set_vnc_keymap('') else: conn.set_vnc_keymap(keymap) return HttpResponseRedirect(request.get_full_path() + '#vnc_keymap') if 'migrate' in request.POST: compute_id = request.POST.get('compute_id', '') 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) conn_migrate.define_move(vname) conn_migrate.close() return HttpResponseRedirect('/instance/%s/%s' % (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' in post: clone_data[post] = request.POST.get(post, '') conn.clone_instance(clone_data) return HttpResponseRedirect('/instance/%s/%s' % (host_id, clone_data['name'])) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] messages = [] time_refresh = TIME_JS_REFRESH compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384] memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) vnc_port = conn.get_vnc() snapshots = sorted(conn.get_snapshot(), reverse=True) inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE) has_managed_save_image = conn.get_managed_save_image() except libvirtError as msg_error: errors.append(msg_error.message) 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() try: if request.method == 'POST': if 'start' in request.POST: conn.start() return HttpResponseRedirect(request.get_full_path()) if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): conn.shutdown() return HttpResponseRedirect(request.get_full_path()) if 'destroy' == request.POST.get('power', ''): conn.force_shutdown() return HttpResponseRedirect(request.get_full_path()) if 'managedsave' == request.POST.get('power', ''): conn.managedsave() return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove() return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend() return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume() return HttpResponseRedirect(request.get_full_path()) if 'delete' in request.POST: if conn.get_status() == 1: conn.force_shutdown() if request.POST.get('delete_disk', ''): conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/instances/%s/' % host_id) if 'snapshot' in request.POST: name = request.POST.get('name', '') conn.create_snapshot(name) msg = _("Snapshot '%s' has been created successful" % name) messages.append(msg) if 'umount_iso' in request.POST: image = request.POST.get('iso_media', '') conn.umount_iso(image) return HttpResponseRedirect(request.get_full_path()) if 'mount_iso' in request.POST: image = request.POST.get('iso_media', '') conn.mount_iso(image) return HttpResponseRedirect(request.get_full_path()) if 'set_autostart' in request.POST: conn.set_autostart(1) return HttpResponseRedirect(request.get_full_path()) if 'unset_autostart' in request.POST: conn.set_autostart(0) return HttpResponseRedirect(request.get_full_path()) if 'change_settings' in request.POST: description = request.POST.get('description', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') cur_memory = request.POST.get('cur_memory', '') conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) return HttpResponseRedirect(request.get_full_path()) if 'change_xml' in request.POST: xml = request.POST.get('inst_xml', '') if xml: conn._defineXML(xml) return HttpResponseRedirect(request.get_full_path()) if 'set_vnc_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('vnc_passwd', '') if not passwd: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path()) if 'migrate' in request.POST: compute_id = request.POST.get('compute_id', '') 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) conn_migrate.define_move(vname) conn_migrate.close() return HttpResponseRedirect('/instance/%s/%s' % (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()) 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) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))
def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type, compute.hypervisor) get_instances = conn.get_instances() except libvirtError as err: errors.append(err) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() acl = Instance.objects.get(compute_id=host_id, name=instance).acl if request.user in acl.all() or request.user.is_staff: if compute.hypervisor == 'qemu': instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance), 'has_managed_save_image': conn.get_instance_managed_save_image(instance)}) else: instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance)}) if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'managedsave' in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
def instance(request, host_id, vname): """ Instance block """ def show_clone_disk(disks): clone_disk = [] for disk in disks: 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 compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384] memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) telnet_port = conn.get_telnet_port() vnc_port = conn.get_vnc_port() vnc_keymap = conn.get_vnc_keymap() vnc_password = conn.get_vnc_passwd() 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) cpu_usage = conn.cpu_usage()['cpu'] except libvirtError as err: errors.append(err) 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() try: 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() if request.POST.get('delete_disk', ''): print 'yeah' conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/%s/instances' % host_id) if 'assign_volume' in request.POST: file = request.POST.get('file', '') device = request.POST.get('device', '') conn.assign_volume(file, device) return HttpResponseRedirect(request.get_full_path() + '#instancedevice') if 'unassign_volume' in request.POST: device = request.POST.get('device', '') conn.unassign_volume(device) return HttpResponseRedirect(request.get_full_path() + '#instancedevice') if 'snapshot' in request.POST: name = request.POST.get('name', '') conn.create_snapshot(name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'external_storage_snapshot' in request.POST: name = request.POST.get('name', '') conn.create_external_storage_snapshot(name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'umount_iso' in request.POST: image = request.POST.get('path', '') first_cd = media[0]['dev'] if len( media) > 0 else request.POST.get('umount_iso', '') dev = request.POST.get('device', first_cd) conn.umount_iso(dev, image) return HttpResponseRedirect(request.get_full_path() + '#instancemedia') if 'mount_iso' in request.POST: image = request.POST.get('media', '') first_cd = media[0]['dev'] if len(media) > 0 else '' print first_cd dev = request.POST.get('device', first_cd) 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', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') cur_memory = request.POST.get('cur_memory', '') conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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_vnc_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('vnc_passwd', '') clear = request.POST.get('clear_pass', False) if not passwd and not clear: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path() + '#vnc_pass') if 'set_vnc_keymap' in request.POST: keymap = request.POST.get('vnc_keymap', '') clear = request.POST.get('clear_keymap', False) if clear: conn.set_vnc_keymap('') else: conn.set_vnc_keymap(keymap) return HttpResponseRedirect(request.get_full_path() + '#vnc_keymap') 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('/%s/instance/%s' % (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('/%s/instance/%s' % (host_id, clone_data['name'])) conn.close() except libvirtError as err: errors.append(err) object = { 'errors': [str(error) for error in errors], 'response': { 'name': vname, 'status': status, 'autostart': autostart, 'vcpu': vcpu, 'cur_vcpu': cur_vcpu, 'uuid': uuid, 'memory': memory, 'cur_memory': cur_memory, 'description': description, 'disks': disks, 'media': media, 'networks': networks, 'media_iso': media_iso, 'vcpu_range': vcpu_range.__len__(), 'memory_host': memory_host, 'vcpu_host': vcpu_host, 'telnet_port': telnet_port, 'vnc_port': vnc_port, 'vnc_keymap': vnc_keymap, 'snapshots': snapshots, 'inst_xml': inst_xml.__str__(), 'has_managed_save_image': has_managed_save_image, 'clone_disks': clone_disks, 'cpu_usage': cpu_usage, 'vnc_password': vnc_password } } return render(object, 'instance.html', locals(), request)
def instances(request, host_id): """ Instances block """ errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) except libvirtError as err: errors.append(err) if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'managedsave' in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) get_instances = conn.get_instances() for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() conn2 = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, instance) instances.append({ 'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'description': conn2.get_description(), 'vcpu': conn.get_instance_vcpu(instance), 'storage': conn2.get_disk_device(), 'has_managed_save_image': conn.get_instance_managed_save_image(instance) }) conn2.close() object = { 'response': { 'instances': instances }, 'errors': [str(error) for error in errors] } return render(object, 'instances.html', locals(), request)
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect("/login") def show_clone_disk(disks): clone_disk = [] for disk in disks: 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 compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384] if not memory in memory_range: insort(memory_range, memory) if not cur_memory in memory_range: insort(memory_range, cur_memory) memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) telnet_port = conn.get_telnet_port() vnc_port = conn.get_vnc_port() vnc_keymap = conn.get_vnc_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) vnc_passwd = conn.get_vnc_passwd() except libvirtError as err: errors.append(err) 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() try: 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() if request.POST.get("delete_disk", ""): conn.delete_disk() finally: conn.delete() return HttpResponseRedirect("/instances/%s/" % 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", "") 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", "") if cur_memory_custom: cur_memory = cur_memory_custom conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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_vnc_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("vnc_passwd", "") clear = request.POST.get("clear_pass", False) if clear: passwd = "" if not passwd and not clear: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path() + "#vnc_pass") if "set_vnc_keymap" in request.POST: keymap = request.POST.get("vnc_keymap", "") clear = request.POST.get("clear_keymap", False) if clear: conn.set_vnc_keymap("") else: conn.set_vnc_keymap(keymap) return HttpResponseRedirect(request.get_full_path() + "#vnc_keymap") 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("/instance/%s/%s" % (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("/instance/%s/%s" % (host_id, clone_data["name"])) conn.close() except libvirtError as err: errors.append(err) return render_to_response("instance.html", locals(), context_instance=RequestContext(request))
def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect("/login") errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) get_instances = conn.get_instances() except libvirtError as err: errors.append(err) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() instances.append( { "name": instance, "status": conn.get_instance_status(instance), "uuid": uuid, "memory": conn.get_instance_memory(instance), "vcpu": conn.get_instance_vcpu(instance), "has_managed_save_image": conn.get_instance_managed_save_image(instance), } ) if conn: try: if request.method == "POST": name = request.POST.get("name", "") if "start" in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if "shutdown" in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if "destroy" in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if "managedsave" in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if "deletesaveimage" in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if "suspend" in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if "resume" in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) return render_to_response("instances.html", locals(), context_instance=RequestContext(request))
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, compute.hypervisor) 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() networks = conn.get_net_device() vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384] 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_range) telnet_port = conn.get_telnet_port() inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE) if compute.hypervisor == 'qemu': snapshots = sorted(conn.get_snapshot(), reverse=True) has_managed_save_image = conn.get_managed_save_image() console_type = conn.get_console_type() console_port = conn.get_console_port() console_keymap = conn.get_console_keymap() console_passwd = conn.get_console_passwd() media_iso = sorted(conn.get_iso_media()) media = conn.get_media_device() disks = conn.get_disk_device() clone_disks = show_clone_disk(disks) if compute.hypervisor == 'lxc': fslist=[] disks=[] vm = conn.get_instance(vname) root = ET.fromstring(vm.XMLDesc()) filesystem = root.find('devices').findall('filesystem') for fs in list(filesystem): for child in fs: fslist.append(child.attrib) rootimg = fslist[0]['dir'].split('/')[2]+'.img' disks.append({'mount':'/','image':rootimg}) fslist = fslist[2:] tmp = 0 while tmp<len(fslist): disks.append({'mount':fslist[tmp+1]['dir'],'image':fslist[tmp]['file'].split('/')[2]}) tmp+=2 hypervisor = compute.hypervisor except libvirtError as err: errors.append(err) 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() acl = Instance.objects.get(compute_id=host_id, name=vname).acl if request.user not in acl.all() and not request.user.is_staff: raise PermissionDenied try: 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() if request.POST.get('delete_disk', ''): conn.delete_disk() 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', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') if int(cur_vcpu) > int(vcpu): vcpu = 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', '') if cur_memory_custom: cur_memory = cur_memory_custom if int(cur_memory) > int(memory): memory = cur_memory conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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, new_compute.hypervisor) 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']])) conn.close() except libvirtError as err: errors.append(err) return render_to_response('instance.html', locals(), context_instance=RequestContext(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 get_instance(instance_id): """ 获取章节信息 """ return Instance.get(int(instance_id))
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] messages = [] time_refresh = TIME_JS_REFRESH compute = Compute.objects.get(id=host_id) try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) status = conn.get_status() autostart = conn.get_autostart() vcpu = conn.get_vcpu() uuid = conn.get_uuid() memory = conn.get_memory() description = conn.get_description() disks = conn.get_disk_device() media = conn.get_media_device() networks = conn.get_net_device() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 8192, 16384] vnc_port = conn.get_vnc() inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE) except libvirtError as msg_error: errors.append(msg_error.message) 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() try: if request.method == 'POST': if 'start' in request.POST: conn.start() return HttpResponseRedirect(request.get_full_path()) if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): conn.shutdown() return HttpResponseRedirect(request.get_full_path()) if 'destroy' == request.POST.get('power', ''): conn.force_shutdown() return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend() return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume() return HttpResponseRedirect(request.get_full_path()) if 'delete' in request.POST: if conn.get_status() == 1: conn.force_shutdown() if request.POST.get('delete_disk', ''): conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/instances/%s/' % host_id) if 'snapshot' in request.POST: conn.create_snapshot() msg = _("Create snapshot for instance successful") messages.append(msg) if 'umount_iso' in request.POST: image = request.POST.get('iso_media', '') conn.umount_iso(image) return HttpResponseRedirect(request.get_full_path()) if 'mount_iso' in request.POST: image = request.POST.get('iso_media', '') conn.mount_iso(image) return HttpResponseRedirect(request.get_full_path()) if 'set_autostart' in request.POST: conn.set_autostart(1) return HttpResponseRedirect(request.get_full_path()) if 'unset_autostart' in request.POST: conn.set_autostart(0) return HttpResponseRedirect(request.get_full_path()) if 'change_settings' in request.POST: description = request.POST.get('description', '') vcpu = request.POST.get('vcpu', '') memory = request.POST.get('memory', '') conn.change_settings(description, memory, vcpu) return HttpResponseRedirect(request.get_full_path()) if 'change_xml' in request.POST: xml = request.POST.get('inst_xml', '') if xml: conn._defineXML(xml) return HttpResponseRedirect(request.get_full_path()) if 'set_vnc_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('vnc_passwd', '') if not passwd: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))
def get_instances(): ''' 获取所有章节 ''' instances = Instance.get_all_list() return sorted(instances, key=lambda x: x.id)
def instances(request, host_id): """ Instances block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) get_instances = conn.get_instances() except libvirtError as msg_error: errors.append(msg_error.message) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance), 'has_managed_save_image': conn.get_instance_managed_save_image(instance)}) if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'managedsave' in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
def instances(request, host_id): """ Instances block """ errors = [] instances = [] time_refresh = 8000 get_instances = [] conn = None compute = Compute.objects.get(id=host_id) try: conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type) get_instances = conn.get_instances() except libvirtError as err: errors.append(err) for instance in get_instances: try: inst = Instance.objects.get(compute_id=host_id, name=instance) uuid = inst.uuid except Instance.DoesNotExist: uuid = conn.get_uuid(instance) inst = Instance(compute_id=host_id, name=instance, uuid=uuid) inst.save() conn2 = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, instance) instances.append({'name': instance, 'status': conn.get_instance_status(instance), 'uuid': uuid, 'memory': conn.get_instance_memory(instance), 'vcpu': conn.get_instance_vcpu(instance), 'storage': conn2.get_disk_device(), 'has_managed_save_image': conn.get_instance_managed_save_image(instance)}) conn2.close() if conn: try: if request.method == 'POST': name = request.POST.get('name', '') if 'start' in request.POST: conn.start(name) return HttpResponseRedirect(request.get_full_path()) if 'shutdown' in request.POST: conn.shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'destroy' in request.POST: conn.force_shutdown(name) return HttpResponseRedirect(request.get_full_path()) if 'managedsave' in request.POST: conn.managedsave(name) return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove(name) return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend(name) return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume(name) return HttpResponseRedirect(request.get_full_path()) conn.close() except libvirtError as err: errors.append(err) object = { 'response': { 'instances': instances }, 'errors': [str(error) for error in errors] } return render(object, 'instances.html', locals(), request)
def create(request, host_id): """ Create new instance. """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) conn = None errors = [] storages = [] networks = [] meta_prealloc = False compute = Compute.objects.get(id=host_id) flavors = Flavor.objects.filter().order_by('id') try: conn = wvmCreate(compute.hostname, compute.login, compute.password, compute.type) storages = sorted(conn.get_storages()) networks = sorted(conn.get_networks()) instances = conn.get_instances() get_images = sorted(conn.get_storages_images()) mac_auto = util.randomMAC() except libvirtError as err: errors.append(err) if conn: if not storages: msg = _("You haven't defined have any storage pools") errors.append(msg) if not networks: msg = _("You haven't defined have any network 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 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 = {} form = NewVMForm(request.POST) if form.is_valid(): data = form.cleaned_data if data['meta_prealloc']: meta_prealloc = True 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']: if not data['mac']: msg = _("No Virtual Machine MAC has been entered") errors.append(msg) else: 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) 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: conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'], uuid, volumes, data['networks'], data['virtio'], data['mac']) create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid) create_instance.save() 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) conn.close() return render_to_response('create.html', locals(), context_instance=RequestContext(request))
def instance(request, host_id, vname): """ Instance block """ def show_clone_disk(disks): clone_disk = [] for disk in disks: 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 compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384] memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) telnet_port = conn.get_telnet_port() vnc_port = conn.get_vnc_port() vnc_keymap = conn.get_vnc_keymap() vnc_password = conn.get_vnc_passwd() 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) cpu_usage = conn.cpu_usage()['cpu'] except libvirtError as err: errors.append(err) 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() try: 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() if request.POST.get('delete_disk', ''): print 'yeah' conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/%s/instances' % host_id) if 'assign_volume' in request.POST: file = request.POST.get('file', '') device = request.POST.get('device', '') conn.assign_volume(file, device) return HttpResponseRedirect(request.get_full_path() + '#instancedevice') if 'unassign_volume' in request.POST: device = request.POST.get('device', '') conn.unassign_volume(device) return HttpResponseRedirect(request.get_full_path() + '#instancedevice') if 'snapshot' in request.POST: name = request.POST.get('name', '') conn.create_snapshot(name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'external_storage_snapshot' in request.POST: name = request.POST.get('name', '') conn.create_external_storage_snapshot(name) return HttpResponseRedirect(request.get_full_path() + '#istaceshapshosts') if 'umount_iso' in request.POST: image = request.POST.get('path', '') first_cd = media[0]['dev'] if len(media) > 0 else request.POST.get('umount_iso', '') dev = request.POST.get('device', first_cd) conn.umount_iso(dev, image) return HttpResponseRedirect(request.get_full_path() + '#instancemedia') if 'mount_iso' in request.POST: image = request.POST.get('media', '') first_cd = media[0]['dev'] if len(media) > 0 else '' print first_cd dev = request.POST.get('device', first_cd) 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', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') cur_memory = request.POST.get('cur_memory', '') conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) 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_vnc_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('vnc_passwd', '') clear = request.POST.get('clear_pass', False) if not passwd and not clear: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path() + '#vnc_pass') if 'set_vnc_keymap' in request.POST: keymap = request.POST.get('vnc_keymap', '') clear = request.POST.get('clear_keymap', False) if clear: conn.set_vnc_keymap('') else: conn.set_vnc_keymap(keymap) return HttpResponseRedirect(request.get_full_path() + '#vnc_keymap') 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('/%s/instance/%s' % (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('/%s/instance/%s' % (host_id, clone_data['name'])) conn.close() except libvirtError as err: errors.append(err) object = { 'errors': [str(error) for error in errors], 'response': { 'name': vname, 'status': status, 'autostart': autostart, 'vcpu': vcpu, 'cur_vcpu': cur_vcpu, 'uuid': uuid, 'memory': memory, 'cur_memory': cur_memory, 'description': description, 'disks': disks, 'media': media, 'networks': networks, 'media_iso': media_iso, 'vcpu_range': vcpu_range.__len__(), 'memory_host': memory_host, 'vcpu_host': vcpu_host, 'telnet_port': telnet_port, 'vnc_port': vnc_port, 'vnc_keymap': vnc_keymap, 'snapshots': snapshots, 'inst_xml': inst_xml.__str__(), 'has_managed_save_image': has_managed_save_image, 'clone_disks': clone_disks, 'cpu_usage': cpu_usage, 'vnc_password': vnc_password } } return render(object, 'instance.html', locals(), request)
def instance(request, host_id, vname): """ Instance block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') def show_clone_disk(disks): clone_disk = [] for disk in disks: 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}) return clone_disk errors = [] messages = [] time_refresh = TIME_JS_REFRESH compute = Compute.objects.get(id=host_id) computes = Compute.objects.all() computes_count = len(computes) keymaps = QEMU_KEYMAPS try: conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname) 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() media_iso = sorted(conn.get_iso_media()) vcpu_range = conn.get_max_cpus() memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384] memory_host = conn.get_max_memory() vcpu_host = len(vcpu_range) telnet_port = conn.get_telnet_port() vnc_port = conn.get_vnc() vnc_keymap = conn.get_vnc_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) except libvirtError as msg_error: errors.append(msg_error.message) 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() try: if request.method == 'POST': if 'start' in request.POST: conn.start() return HttpResponseRedirect(request.get_full_path()) if 'power' in request.POST: if 'shutdown' == request.POST.get('power', ''): conn.shutdown() return HttpResponseRedirect(request.get_full_path()) if 'destroy' == request.POST.get('power', ''): conn.force_shutdown() return HttpResponseRedirect(request.get_full_path()) if 'managedsave' == request.POST.get('power', ''): conn.managedsave() return HttpResponseRedirect(request.get_full_path()) if 'deletesaveimage' in request.POST: conn.managed_save_remove() return HttpResponseRedirect(request.get_full_path()) if 'suspend' in request.POST: conn.suspend() return HttpResponseRedirect(request.get_full_path()) if 'resume' in request.POST: conn.resume() return HttpResponseRedirect(request.get_full_path()) if 'delete' in request.POST: if conn.get_status() == 1: conn.force_shutdown() if request.POST.get('delete_disk', ''): conn.delete_disk() try: instance = Instance.objects.get(compute_id=host_id, name=vname) instance.delete() finally: conn.delete() return HttpResponseRedirect('/instances/%s/' % host_id) if 'snapshot' in request.POST: name = request.POST.get('name', '') conn.create_snapshot(name) msg = _("Snapshot '%s' has been created successful" % name) messages.append(msg) 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()) 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()) if 'set_autostart' in request.POST: conn.set_autostart(1) return HttpResponseRedirect(request.get_full_path()) if 'unset_autostart' in request.POST: conn.set_autostart(0) return HttpResponseRedirect(request.get_full_path()) if 'change_settings' in request.POST: description = request.POST.get('description', '') vcpu = request.POST.get('vcpu', '') cur_vcpu = request.POST.get('cur_vcpu', '') memory = request.POST.get('memory', '') cur_memory = request.POST.get('cur_memory', '') conn.change_settings(description, cur_memory, memory, cur_vcpu, vcpu) return HttpResponseRedirect(request.get_full_path()) if 'change_xml' in request.POST: xml = request.POST.get('inst_xml', '') if xml: conn._defineXML(xml) return HttpResponseRedirect(request.get_full_path()) if 'set_vnc_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('vnc_passwd', '') clear = request.POST.get('clear_pass', False) if not passwd and not clear: msg = _("Enter the VNC password or select Generate") errors.append(msg) if not errors: conn.set_vnc_passwd(passwd) return HttpResponseRedirect(request.get_full_path()) if 'set_vnc_keymap' in request.POST: keymap = request.POST.get('vnc_keymap', '') clear = request.POST.get('clear_keymap', False) if clear: conn.set_vnc_keymap('') else: conn.set_vnc_keymap(keymap) return HttpResponseRedirect(request.get_full_path()) if 'migrate' in request.POST: compute_id = request.POST.get('compute_id', '') 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) conn_migrate.define_move(vname) conn_migrate.close() return HttpResponseRedirect('/instance/%s/%s' % (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()) 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' in post: clone_data[post] = request.POST.get(post, '') conn.clone_instance(clone_data) return HttpResponseRedirect('/instance/%s/%s' % (host_id, clone_data['name'])) conn.close() except libvirtError as msg_error: errors.append(msg_error.message) return render_to_response('instance.html', locals(), context_instance=RequestContext(request))
cte = cteutils.get_obj(cname) except ErrorException, e: raise ErrorException(e.code, e.value) except Exception, e: logger.error(str(e)) raise ErrorException(500, "") try: cte_ips = cteutils.get_all_ip_range(cname, 4) except ErrorException, e: raise ErrorException(e.code, e.value) except Exception, e: logger.error(str(e)) raise ErrorException(500, "") ins = Instance() ins.uid = uid ins.name = name ins.desc = desc ins.vcpu = flv['vcpu'] ins.disk = flv['disk'] ins.memory = flv['memory'] ins.compute = cte try: ips_used = get_all_ips() except: ips_used = []