def test_has_cdrom2(self): cluster = make_mock_cluster("2.5.0") self.assertTrue(has_cdrom2(cluster))
def test_lacks_cdrom2(self): cluster = make_mock_cluster("2.2.0") self.assertFalse(has_cdrom2(cluster))
def create(request, cluster_slug=None): """ Create a new instance Store in DB and Create on given cluster """ user = request.user if not(user.is_superuser or user.has_any_perms(Cluster, ['admin', 'create_vm'])): raise Http403( _('You do not have permission to create virtual machines')) if cluster_slug is not None: cluster = get_object_or_404(Cluster, slug=cluster_slug) else: cluster = None if request.method == 'POST': form = NewVirtualMachineForm(user, request.POST) if form.is_valid(): data = form.cleaned_data start = data.get('start') no_install = data.get('no_install') owner = data.get('owner') grantee = data.get('grantee') cluster = data.get('cluster') hostname = data.get('hostname') disk_template = data.get('disk_template') # Default to not pass in pnode and snode # since these will be set if the form is correct pnode = None snode = None os = data.get('os') name_check = data.get('name_check') iallocator = data.get('iallocator') # Hidden fields iallocator_hostname = None if 'iallocator_hostname' in data: iallocator_hostname = data.get('iallocator_hostname') # BEPARAMS vcpus = data.get('vcpus') disks = data.get('disks') disk_size = data.get('disk_size') nics = data.get('nics') memory = data.get('memory') # If iallocator was not checked do not pass in the iallocator # name. If iallocator was checked don't pass snode,pnode. if not iallocator: iallocator_hostname = None pnode = data.get('pnode') # If drbd is being used assign the secondary node if disk_template == 'drbd' and pnode is not None: snode = data.get('snode') # Create dictionary of only parameters supposed to be in hvparams hv = data.get('hypervisor') hvparams = {} hvparam_fields = () if hv == 'xen-pvm': hvparam_fields = ('kernel_path', 'root_path') elif hv == 'xen-hvm': hvparam_fields = ( 'boot_order', 'disk_type', 'nic_type', 'cdrom_image_path', ) elif hv == 'kvm': hvparam_fields = [ 'kernel_path', 'root_path', 'serial_console', 'boot_order', 'disk_type', 'cdrom_image_path', 'nic_type', ] # Check before adding cdrom2; see #11655. if has_cdrom2(cluster): hvparam_fields.append('cdrom2_image_path') # Force cdrom disk type to IDE; see #9297. hvparams['cdrom_disk_type'] = 'ide' for field in hvparam_fields: hvparams[field] = data[field] # XXX attempt to load the virtual machine. This ensure that if # there was a previous vm with the same hostname, but had not # successfully been deleted, then it will be deleted now try: VirtualMachine.objects.get(cluster=cluster, hostname=hostname) except VirtualMachine.DoesNotExist: pass try: job_id = cluster.rapi.CreateInstance('create', hostname, disk_template, disks,nics, no_install=no_install, start=start, os=os, pnode=pnode, snode=snode, name_check=name_check, ip_check=name_check, iallocator=iallocator_hostname, hypervisor=hv, hvparams=hvparams, beparams={"memory": memory, "vcpus":vcpus}) except GanetiApiError, e: msg = '%s: %s' % (_('Error creating virtual machine on this cluster'),e) form._errors["cluster"] = form.error_class([msg]) else: # Check for a vm recovery, If it is not found then if 'vm_recovery' in data: vm = data['vm_recovery'] vm_template = vm.template else: vm_template = VirtualMachineTemplate() vm = VirtualMachine(owner=owner) vm.cluster = cluster vm.hostname = hostname vm.ram = memory vm.virtual_cpus = vcpus vm.disk_size = disk_size # save temporary template # XXX copy each property in data. Avoids errors from properties # that don't exist on the model for k,v in data.items(): setattr(vm_template, k, v) vm_template.save() vm.template = vm_template vm.ignore_cache = True # Do a dance to get the VM and the job referencing each other. vm.save() job = Job.objects.create(job_id=job_id, obj=vm, cluster=cluster) job.save() vm.last_job = job vm.save() # grant admin permissions to the owner. Only do this for new # VMs. otherwise we run the risk of granting perms to a # different owner. We should be preventing that elsewhere, but # lets be extra careful since this check is cheap. if 'vm_recovery' in data: log_action('VM_RECOVER', user, vm, job) else: grantee.grant('admin', vm) log_action('CREATE', user, vm) return HttpResponseRedirect( reverse('instance-detail', args=[cluster.slug, vm.hostname])) cluster_defaults = {} if 'cluster' in request.POST and request.POST['cluster'] != '': try: cluster = Cluster.objects.get(pk=request.POST['cluster']) if cluster.info: cluster_defaults = cluster_default_info(cluster) except Cluster.DoesNotExist: pass