def reinstall(request, cluster_slug, instance): """ Reinstall a VM. """ user = request.user instance, cluster = get_vm_and_cluster_or_404(cluster_slug, instance) # Check permissions. # XXX Reinstalling is somewhat similar to # deleting in that you destroy data, # so use that for now. if not (user.is_superuser or user.has_any_perms( instance, ["remove", "admin"]) or user.has_perm("admin", cluster)): raise PermissionDenied(NO_PRIVS) if request.method == 'GET': return render_to_response( "ganeti/virtual_machine/reinstall.html", { 'vm': instance, 'oschoices': cluster_os_list(cluster), 'current_os': instance.operating_system, 'cluster': cluster }, context_instance=RequestContext(request), ) elif request.method == 'POST': # Reinstall instance if "os" in request.POST: os = request.POST["os"] else: os = instance.operating_system # XXX no_startup=True prevents quota circumventions. # possible future solution would be a checkbox # asking whether they want to start up, and check # quota here if they do (would also involve # checking whether this VM is already running and subtracting that) job_id = instance.rapi \ .ReinstallInstance(instance.hostname, os=os, no_startup=True) job = Job.objects.create(job_id=job_id, obj=instance, cluster=cluster) VirtualMachine.objects \ .filter(id=instance.id).update(last_job=job, ignore_cache=True) # log information log_action('VM_REINSTALL', user, instance, job) return HttpResponseRedirect( reverse('instance-detail', args=[cluster.slug, instance.hostname]))
def reinstall(request, cluster_slug, instance): """ Reinstall a VM. """ user = request.user instance, cluster = get_vm_and_cluster_or_404(cluster_slug, instance) # Check permissions. # XXX Reinstalling is somewhat similar to # deleting in that you destroy data, # so use that for now. if not ( user.is_superuser or user.has_any_perms(instance, ["remove", "admin"]) or user.has_perm("admin", cluster)): raise PermissionDenied(NO_PRIVS) if request.method == 'GET': return render_to_response( "ganeti/virtual_machine/reinstall.html", {'vm': instance, 'oschoices': cluster_os_list(cluster), 'current_os': instance.operating_system, 'cluster': cluster}, context_instance=RequestContext(request), ) elif request.method == 'POST': # Reinstall instance if "os" in request.POST: os = request.POST["os"] else: os = instance.operating_system # XXX no_startup=True prevents quota circumventions. # possible future solution would be a checkbox # asking whether they want to start up, and check # quota here if they do (would also involve # checking whether this VM is already running and subtracting that) job_id = instance.rapi \ .ReinstallInstance(instance.hostname, os=os, no_startup=True) job = Job.objects.create(job_id=job_id, obj=instance, cluster=cluster) VirtualMachine.objects \ .filter(id=instance.id).update(last_job=job, ignore_cache=True) # log information log_action('VM_REINSTALL', user, instance, job) return HttpResponseRedirect( reverse('instance-detail', args=[cluster.slug, instance.hostname]))
def test_view_modify_confirm_quota_over(self): args = (self.cluster.slug, self.vm.hostname) url = '/cluster/%s/%s/edit/confirm' % args user = User(id=52, username='******') user.set_password('secret2') user.save() user.grant('modify', self.vm) profile = user.get_profile() self.vm.owner = profile self.vm.save() self.cluster.set_quota(profile, dict(ram=1000, disk=2000, virtual_cpus=10)) os_list = cluster_os_list(self.cluster) edit_form = dict(vcpus=2000, acpi=True, disk_cache='default', initrd_path='', kernel_args='ro', kvm_flag='', mem_path='', migration_downtime=30, security_domain='', security_model='none', usb_mouse='', use_chroot=False, use_localtime=False, vnc_bind_address='0.0.0.0', vnc_tls=False, vnc_x509_path='', vnc_x509_verify=False, memory=512, os='image+debian-osgeo', disk_type='paravirtual', boot_order='disk', nic_type='paravirtual', nic_count=1, nic_count_original=1, nic_link_0='br0', nic_mac_0='aa:bb:00:00:33:d2', root_path='/dev/vda1', kernel_path='/boot/vmlinuz-2.32.6-27-generic', serial_console=True, cdrom_image_path='') data = {'rapi_dict': json.dumps(edit_form), 'save': True} # regular user self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() response = self.c.post(url, data) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_confirm.html') self.c.logout()
def test_view_modify_confirm(self): """ Test confirm page for modifying an instance """ args = (self.cluster.slug, self.vm.hostname) url = '/cluster/%s/%s/edit/confirm' % args user = User(id=52, username='******') user.set_password('secret2') user.save() self.vm.owner = user.get_profile() self.vm.save() os_list = cluster_os_list(self.cluster) edit_form = dict(vcpus=2, acpi=True, disk_cache='default', initrd_path='', kernel_args='ro', kvm_flag='', mem_path='', migration_downtime=30, security_domain='', security_model='none', usb_mouse='', use_chroot=False, use_localtime=False, vnc_bind_address='0.0.0.0', vnc_tls=False, vnc_x509_path='', vnc_x509_verify=False, memory=512, os='image+debian-osgeo', disk_type='paravirtual', boot_order='disk', nic_type='paravirtual', nic_count=1, nic_count_original=1, nic_link_0='br0', nic_mac_0='aa:bb:00:00:33:d2', root_path='/dev/vda1', kernel_path='/boot/vmlinuz-2.32.6-27-generic', serial_console=True, cdrom_image_path='', notes="foo") # -- SESSION VARIABLES # Make sure session variables are set user.is_superuser = True user.save() self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session # edit_form response = self.c.get(url) self.assertEqual(400, response.status_code) session['edit_form'] = edit_form session.save() response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_confirm.html') # session['os_list'] = os_list # session.save() user.revoke_all(self.vm) user.is_superuser = False user.save() self.c.logout() # -- GET # Anonymous User response = self.c.get(url) self.assertEqual(302, response.status_code) # User with Modify Permissions user.grant('modify', self.vm) self.assertFalse(user.is_superuser) self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_confirm.html') user.revoke_all(self.vm) self.c.logout() # User with Admin Permissions user.grant('admin', self.vm) self.assertFalse(user.is_superuser) self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_confirm.html') user.revoke_all(self.vm) self.c.logout() # Superuser user.is_superuser = True user.save() self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_confirm.html') self.c.logout() user.is_superuser = False user.save() # -- POST data = {'rapi_dict': json.dumps(edit_form)} # Anonymous User response = self.c.post(url, data) self.assertEqual(302, response.status_code) for i in ('cancel', 'edit', 'save', 'reboot'): data[i] = True # Superuser user.is_superuser = True user.save() self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() self.assertTrue(user.is_superuser) response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.c.logout() user.is_superuser = False user.save() # User without Permissions self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) response = self.c.post(url, data) self.assertEqual(403, response.status_code) self.assertTrue(response.context['message']) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, '403.html') self.c.logout() # User with Modify Permissions user.grant('modify', self.vm) user.grant('power', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() self.assertFalse(user.is_superuser) response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) user.revoke_all(self.vm) self.c.logout() # User with Admin Permissions user.grant('admin', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['edit_form'] = edit_form session['os_list'] = os_list session.save() self.assertFalse(user.is_superuser) response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) user.revoke_all(self.vm) self.c.logout() del data[i]
def test_view_modify(self): """ Test modifying an instance """ args = (self.cluster.slug, self.vm.hostname) url = '/cluster/%s/%s/edit' % args user = User(id=52, username='******') user.set_password('secret2') user.save() # -- GET # Anonymous User response = self.c.get(url) self.assertEqual(302, response.status_code) # User with Modify Permissions user.grant('modify', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) self.assertTrue(user.has_perm('modify', self.vm)) self.assertFalse(user.has_perm('admin', self.vm)) response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_kvm.html') user.revoke_all(self.vm) self.c.logout() # User with Admin Permissions user.grant('admin', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_kvm.html') user.revoke_all(self.vm) self.c.logout() # Superuser user.is_superuser = True user.save() self.assertTrue(self.c.login(username=user.username, password='******')) response = self.c.get(url) self.assertEqual(200, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_kvm.html') self.c.logout() user.is_superuser = False user.save() # -- POST os_list = cluster_os_list(self.cluster) data = dict(vcpus=2, acpi=True, disk_cache='default', initrd_path='', kernel_args='ro', kvm_flag='', mem_path='', migration_downtime=30, security_domain='', security_model='none', usb_mouse='', use_chroot=False, use_localtime=False, vnc_bind_address='0.0.0.0', vnc_tls=False, vnc_x509_path='', vnc_x509_verify=False, memory=512, os='image+debian-osgeo', disk_type='paravirtual', boot_order='disk', nic_type='paravirtual', nic_count=1, nic_count_original=1, nic_link_0='br0', nic_mac_0='aa:bb:00:00:33:d2', root_path='/dev/vda1', kernel_path='/boot/vmlinuz-2.32.6-27-generic', serial_console=True, cdrom_image_path='', cdrom2_image_path='') # Required Values user.grant('modify', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) session = self.c.session session['os_list'] = os_list session.save() for property in ['vcpus', 'memory', 'disk_type', 'boot_order', 'nic_type']: data_ = data.copy() del data_[property] self.assertFalse(user.is_superuser) response = self.c.post(url, data_) # If failure then a field that is not required by the model, but # should be required by the form, is not being required by # the form. See the ModifyVirtualMachineForm.required field. self.assertNotEqual(response.context['form'][property].errors, [], msg=property) self.assertEqual(200, response.status_code) # 302 if success (BAD) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, 'ganeti/virtual_machine/edit_kvm.html') self.c.logout() user.revoke_all(self.vm) # Anonymous User response = self.c.post(url, data) self.assertEqual(302, response.status_code) # Superuser user.is_superuser = True user.save() self.assertTrue(self.c.login(username=user.username, password='******')) self.assertTrue(user.is_superuser) session = self.c.session session['os_list'] = os_list session.save() response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.c.logout() user.is_superuser = False user.save() # User without Permissions self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) session = self.c.session session['os_list'] = os_list session.save() response = self.c.post(url, data) self.assertEqual(403, response.status_code) self.assertTrue(response.context['message']) self.assertEqual('text/html; charset=utf-8', response['content-type']) self.assertTemplateUsed(response, '403.html') self.c.logout() # User with Modify Permissions user.grant('modify', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) session = self.c.session session['os_list'] = os_list session.save() response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) user.revoke_all(self.vm) self.c.logout() # User with Admin Permissions user.grant('admin', self.vm) self.assertTrue(self.c.login(username=user.username, password='******')) self.assertFalse(user.is_superuser) session = self.c.session session['os_list'] = os_list session.save() response = self.c.post(url, data) self.assertEqual(302, response.status_code) self.assertEqual('text/html; charset=utf-8', response['content-type']) user.revoke_all(self.vm) self.c.logout()