示例#1
0
def rename(request, cluster_slug, instance, rest=False, extracted_params=None):
    """
    Rename an existing instance
    """
    vm, cluster = get_vm_and_cluster_or_404(cluster_slug, instance)

    user = request.user
    if not (user.is_superuser or user.has_any_perms(vm, ['admin','modify'])
        or user.has_perm('admin', cluster)):
        raise Http403(
            _('You do not have permissions to edit this virtual machine'))

    if request.method == 'POST':
        form = RenameForm(vm, request.POST)
        params_ok = False
        if rest and extracted_params is not None:
            if all(k in extracted_params for k in ("hostname", "ip_check", "name_check")):
                hostname = extracted_params['hostname']
                ip_check = extracted_params['ip_check']
                name_check = extracted_params['name_check']
                params_ok = True
            else:
                return HttpResponseBadRequest()

        if form.is_valid():
            data = form.cleaned_data
            hostname = data['hostname']
            ip_check = data['ip_check']
            name_check = data['name_check']

        if form.is_valid() or params_ok:
            try:
                # In order for rename to work correctly, the vm must first be
                #   shutdown.
                if vm.is_running:
                    job1 = vm.shutdown()
                    log_action('VM_STOP', user, vm, job1)

                job_id = vm.rapi.RenameInstance(vm.hostname, hostname,
                                                ip_check, name_check)
                job = Job.objects.create(job_id=job_id, obj=vm, cluster=cluster)
                VirtualMachine.objects.filter(pk=vm.pk) \
                    .update(hostname=hostname, last_job=job, ignore_cache=True)

                # log information about creating the machine
                log_action('VM_RENAME', user, vm, job)

                if not rest:
                    return HttpResponseRedirect(
                    reverse('instance-detail', args=[cluster.slug, hostname]))
                else:
                    return HttpAccepted()

            except GanetiApiError, e:
                msg = 'Error renaming virtual machine: %s' % e
                form._errors["cluster"] = form.error_class([msg])
                if rest:
                    return HttpResponse(400, content=msg)
示例#2
0
def rename(request, cluster_slug, instance, rest=False, extracted_params=None):
    """
    Rename an existing instance
    """
    vm, cluster = get_vm_and_cluster_or_404(cluster_slug, instance)

    user = request.user
    if not (user.is_superuser or user.has_any_perms(vm, ['admin','modify'])
        or user.has_perm('admin', cluster)):
        return render_403(request,
            _('You do not have permissions to edit this virtual machine'))

    if request.method == 'POST':
        form = RenameForm(vm, request.POST)
        params_ok = False
        if ((rest) & (extracted_params != None)):
            if ((extracted_params.has_key('hostname')) & (extracted_params.has_key('ip_check')) & (extracted_params.has_key('name_check'))):
                hostname = extracted_params.get('hostname')
                ip_check = extracted_params.get('ip_check')
                name_check = extracted_params.get('name_check')
                params_ok = True
            else:
                return HttpResponseBadRequest

        if (form.is_valid()):
            data = form.cleaned_data
            hostname = data['hostname']
            ip_check = data['ip_check']
            name_check = data['name_check']

        if (form.is_valid() | params_ok):
            try:
                job_id = vm.rapi.RenameInstance(vm.hostname, hostname,
                                                ip_check, name_check)
                job = Job.objects.create(job_id=job_id, obj=vm, cluster=cluster)
                VirtualMachine.objects.filter(pk=vm.pk) \
                    .update(hostname=hostname, last_job=job, ignore_cache=True)

                # log information about creating the machine
                log_action('VM_RENAME', user, vm, job)

                if not rest:
                    return HttpResponseRedirect(
                    reverse('instance-detail', args=[cluster.slug, hostname]))
                else:
                    return HttpAccepted

            except GanetiApiError, e:
                msg = 'Error renaming virtual machine: %s' % e
                form._errors["cluster"] = form.error_class([msg])
                if rest:
                    return HttpResponse(400, content=msg)
def rename(request, cluster_slug, instance, rest=False, extracted_params=None):
    """
    Rename an existing instance
    """
    vm, cluster = get_vm_and_cluster_or_404(cluster_slug, instance)

    user = request.user
    if not (user.is_superuser or user.has_any_perms(vm, ['admin','modify'])
        or user.has_perm('admin', cluster)):
        raise Http403(
            _('You do not have permissions to edit this virtual machine'))

    if request.method == 'POST':
        form = RenameForm(vm, request.POST)
        params_ok = False
        if rest and extracted_params is not None:
            if all(k in extracted_params for k in ("hostname", "ip_check", "name_check")):
                hostname = extracted_params['hostname']
                ip_check = extracted_params['ip_check']
                name_check = extracted_params['name_check']
                params_ok = True
            else:
                return HttpResponseBadRequest()

        if form.is_valid():
            data = form.cleaned_data
            hostname = data['hostname']
            ip_check = data['ip_check']
            name_check = data['name_check']

        if form.is_valid() or params_ok:
            try:
                # In order for rename to work correctly, the vm must first be
                #   shutdown.
                if vm.is_running:
                    job1 = vm.shutdown()
                    log_action('VM_STOP', user, vm, job1)

                job_id = vm.rapi.RenameInstance(vm.hostname, hostname,
                                                ip_check, name_check)
                job = Job.objects.create(job_id=job_id, obj=vm, cluster=cluster)
                VirtualMachine.objects.filter(pk=vm.pk) \
                    .update(hostname=hostname, last_job=job, ignore_cache=True)

                # slip the new hostname to the log action
                vm.newname = hostname

                # log information about creating the machine
                log_action('VM_RENAME', user, vm, job)

                if not rest:
                    return HttpResponseRedirect(
                    reverse('instance-detail', args=[cluster.slug, hostname]))
                else:
                    return HttpAccepted()

            except GanetiApiError, e:
                msg = 'Error renaming virtual machine: %s' % e
                form._errors["cluster"] = form.error_class([msg])
                if rest:
                    return HttpResponse(400, content=msg)
                log_action('VM_RENAME', user, vm, job)

                if not rest:
                    return HttpResponseRedirect(
                    reverse('instance-detail', args=[cluster.slug, hostname]))
                else:
                    return HttpAccepted()

            except GanetiApiError, e:
                msg = 'Error renaming virtual machine: %s' % e
                form._errors["cluster"] = form.error_class([msg])
                if rest:
                    return HttpResponse(400, content=msg)

    elif request.method == 'GET':
        form = RenameForm(vm)

    return render_to_response('ganeti/virtual_machine/rename.html', {
        'cluster': cluster,
        'vm': vm,
        'form': form
        },
        context_instance=RequestContext(request),
    )


@login_required
def reparent(request, cluster_slug, instance):
    """
    update a virtual machine to have a new owner
    """