示例#1
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/')
示例#2
0
def create_local_image(name, description, file_path, image_type):
    if osUtils.check_path(file_path):
        logger.debug("path exists")
        if settings.MEDIA_ROOT not in file_path:
            raise Exception("Image must in in path: %s" % settings.MEDIA_ROOT)

        else:
            logger.debug("removing media_root")
            file_path = file_path.replace(settings.MEDIA_ROOT + '/', '')
            logger.debug(file_path)
    else:
        raise Exception("Invalid image path")

    try:
        logger.debug(file_path)
        image = Image()
        image.description = description
        image.name = name
        image.filePath = file_path
        image.type = image_type
        image.save()

        full_path = 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!")
                image.filePath = "user_images/%s" % converted_image_file_name
                image.save()

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

        if image_type == "junos_vre" 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 file_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 = 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)
                n_image = Image()
                n_image.name = new_image_name
                n_image.type = "junos_riot"
                n_image.description = image.description + "\nRiot PFE"
                n_image.filePath = "user_images/" + new_image_file_name
                n_image.save()

        return image.id

    except Exception as e:
        logger.error("Caught Error in create_local_image")
        logger.error(str(e))
        raise
示例#3
0
def create_local_image(name, description, file_path, image_type):
    """
    Register a local (server-side) image with Wistar
    :param name: name of the image
    :param description: Description of the image
    :param file_path: full path to the where the file exists on the server
    :param image_type: The wistar type of the image, this type must already be registered in Wistar
    :return: The local id of the image
    """
    if image_exists(name):
        logger.info('Image with this name already exists!')
        try:
            existing_image = Image.objects.get(name=name)
            return existing_image.id
        except Image.DoesNotExist:
            logger.error('Image already exists but we cannot get it locally!')
            pass

    if osUtils.check_path(file_path):
        logger.debug("path exists")
        if settings.MEDIA_ROOT not in file_path:
            raise Exception("Image must in in path: %s" % settings.MEDIA_ROOT)

        else:
            logger.debug("removing media_root")
            file_path = file_path.replace(settings.MEDIA_ROOT + '/', '')
            logger.debug(file_path)
    else:
        raise Exception("Invalid image path")

    try:
        logger.debug(file_path)
        image = Image()
        image.description = description
        image.name = name
        image.filePath = file_path
        image.type = image_type
        image.save()

        full_path = 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!")
                image.filePath = "user_images/%s" % converted_image_file_name
                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 file_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 = 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)
                n_image = Image()
                n_image.name = new_image_name
                n_image.type = "junos_riot"
                n_image.description = image.description + "\nRiot PFE"
                n_image.filePath = "user_images/" + new_image_file_name
                n_image.save()

        return image.id

    except Exception as e:
        logger.error("Caught Error in create_local_image")
        logger.error(str(e))
        raise