Beispiel #1
0
def new(request):
    image_form = ImageForm()
    context = {
        'image_form': image_form,
        "vm_types": configuration.vm_image_types
    }
    return render(request, 'images/new.html', context)
Beispiel #2
0
def edit(request, image_id):
    image = get_object_or_404(Image, pk=image_id)

    image_form = ImageForm(instance=image)
    return render(request, 'images/edit.html', {
        'image': image,
        'image_form': image_form
    })
Beispiel #3
0
def edit(request, image_id):
    image = get_object_or_404(Image, pk=image_id)

    #     template = get_object_or_404(ConfigTemplate, pk=template_id)
    # template_form = ConfigTemplateForm(instance=template)
    image_form = ImageForm(instance=image)
    return render(request, 'images/edit.html', {
        'image': image,
        'image_form': image_form
    })
Beispiel #4
0
def add_image(request):
    if request.is_ajax():
        image_form = ImageForm()
        if "locations" in request.GET:
            return render_to_response('tags/image_form_locations.html',
                                      {'image_form': image_form})
        else:
            return render_to_response('tags/image_form_tags.html',
                                      {'image_form': image_form})
    else:
        if request.POST:
            image_form = ImageForm(request.POST, request.FILES)
            if image_form.is_valid():
                img = image_form.save(commit=False)
                img.user = request.user
                img.save()
                image_form.save_m2m()
                return HttpResponseRedirect("/")
        else:
            image_form = ImageForm()

    return render_to_response('tags/add_image.html',
                              {'image_form': image_form},
                              context_instance=RequestContext(request))
Beispiel #5
0
def add_image(request):
    if request.is_ajax():
        image_form = ImageForm()
        if "locations" in request.GET:
            return render_to_response('tags/image_form_locations.html', {'image_form': image_form})
        else:
            return render_to_response('tags/image_form_tags.html', {'image_form': image_form})
    else:
        if request.POST:
            image_form = ImageForm(request.POST, request.FILES)
            if image_form.is_valid():
                img = image_form.save(commit=False)
                img.user = request.user
                img.save()
                image_form.save_m2m()
                return HttpResponseRedirect("/")
        else:
            image_form = ImageForm()

    return render_to_response('tags/add_image.html', {'image_form': image_form}, context_instance=RequestContext(request))
Beispiel #6
0
def create(request):
    try:
        logger.debug('---- Create Image ----')
        image_form = ImageForm(request.POST, request.FILES)
        if not image_form.is_valid():
            logger.error("Could not save image for some reason!")
            context = {'image_form': image_form}
            return render(request, 'images/new.html', context)

        # if not osUtils.checkPath(image_form.cleaned_data['path']):
        # logger.debug("PATH DOESN'T EXIST")
        # context = {'error' : "PATH DOESNT EXIST"}
        #    return render(request, 'error.html', context)

        logger.debug("Saving form")
        orig_image = image_form.save()
        messages.info(request, "Image uploaded successfully")

        image_type = request.POST["type"]
        image_name = request.POST["name"]
        full_path = orig_image.filePath.path

        if re.match(".*\.vmdk$", full_path):
            # we need to convert this for KVM based deployments!
            converted_image_path = re.sub("\.vmdk$", ".qcow2", full_path)
            converted_image_file_name = converted_image_path.split('/')[-1]
            if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path):
                logger.info("Converted vmdk image to qcow2!")
                orig_image.filePath = "user_images/%s" % converted_image_file_name
                orig_image.save()

                logger.debug("Removing original vmdk")
                osUtils.remove_instance(full_path)
            else:
                logger.error("Could not convert vmdk!")

        if image_type == "junos_vre_15" and "jinstall64-vmx-15.1" in full_path:
            logger.debug("Creating RIOT image for Junos vMX 15.1")
            # lets replace the last "." with "_riot."
            if '.' in full_path:
                new_image_path = re.sub(r"(.*)\.(.*)$", r"\1_riot.\2",
                                        full_path)
            else:
                # if there is no '.', let's just add one
                new_image_path = full_path + "_riot.img"

            new_image_file_name = new_image_path.split('/')[-1]
            new_image_name = image_name + ' Riot PFE'
            if osUtils.copy_image_to_clone(full_path, new_image_path):
                logger.debug("Copied from %s" % full_path)
                logger.debug("Copied to %s" % new_image_path)
                image = Image()
                image.name = new_image_name
                image.type = "junos_riot"
                image.description = orig_image.description + "\nRiot PFE"
                image.filePath = "user_images/" + new_image_file_name
                image.save()

        return HttpResponseRedirect('/images')

    except Exception as e:
        logger.error(e)
        messages.info(request, "Could not create image!")
        return HttpResponseRedirect('/images/')