Beispiel #1
0
def create(caller_id, name, description, filesystem, size, disk_controller):
    """
    Method creates new Image. It's only used by disk_volume type (only url view with create)
    @cmview_user

    @parameter{name,string}
    @parameter{description,string}
    @parameter{filesystem,int} id of the filesystem. Supported filesystems are listed in settings
    @parameter{size,int} size of the Image to create [MB]
    @parameter{disk_controller}

    @response{dict} Image's dictionary
    """
    if size < 1:
        raise CMException('image_invalid_size')

    user = User.get(caller_id)
    user.check_storage(size)
    image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name,
                                size=size)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #2
0
def download(caller_id, description, name, path, disk_dev, disk_controller):
    """
    Downloads specified StorateImage from remote path.

    @cmview_admin_cm
    @param_post{description,string}
    @param_post{name,string} how to name newly downloaded storage image
    @param_post{path,string} HTTP or FTP path to download StorageImage.
    @param_post{disk_dev}
    @param_post{disk_controller}
    """

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception('Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)

    image = StorageImage.create(name=name, description=description, user=user, disk_dev=disk_dev,  disk_controller=disk_controller)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #3
0
def create(caller_id, name, description, filesystem, size, disk_controller):
    """
    Creates new StorageImage.

    @cmview_user
    @param_post{name,string}
    @param_post{description,string}
    @param_post{filesystem,int} id of the filesystem. Supported filesystems are
    common.hardware.disk_filesystems
    @param_post{size,int} size of the SystemImage to create [MB]
    @param_post{disk_controller}

    @response{dict} StorageImage.dict property of newly created StorageImage
    """
    if size < 1:
        raise CMException('image_invalid_size')

    user = User.get(caller_id)
    user.check_storage(size)
    image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name,
                                size=size)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #4
0
def download(caller_id, description, name, path, disk_dev, disk_controller):
    """
    Downloads image depending on the \c data parameter.
    @cmview_admin_cm

    @parameter{description,string}
    @parameter{name,string}
    @parameter{path,string} HTTP or FTP path to image to download
    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}
    """

    # size value is taken
    try:
        connection = urllib.urlopen(path)
        size = int(connection.info()["Content-Length"])
    except IOError:
        log.exception('Cannot find image')
        raise CMException('image_not_found')
    except KeyError:
        log.exception(caller_id, 'Cannot calculate size')
        raise CMException('image_calculate_size')

    user = User.get(caller_id)

    image = StorageImage.create(name=name, description=description, user=user, disk_dev=disk_dev,  disk_controller=disk_controller)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #5
0
def copy(caller_id, src_image_id, dest_user_id):
    """
    Copy selected StorageImage to user's StorageImages

    @cmview_admin_cm
    @param_post{src_image_id,int}
    @param_post{dest_user_id,int}
    """
    src_image = StorageImage.admin_get(src_image_id)
    dest_user = User.get(dest_user_id)
    dest_image = StorageImage.create(name=src_image.name, description=src_image.description, user=dest_user,
                                    disk_controller=src_image.disk_controller, size=src_image.size)

    try:
        dest_image.save()
    except Exception, e:
        log.error(caller_id, "Unable to commit: %s" % str(e))
        raise CMException('image_create')
Beispiel #6
0
def download(caller_id, name, description, path, disk_controller):
    """
    Downloads image depending on the \c data parameter.
    @cmview_user

    @parameter{name,string}
    @parameter{description,string}
    @parameter{path,string} HTTP or FTP path to image to download
    @parameter{disk_controller}

    @response{None}
    """
    user = User.get(caller_id)

    if path.startswith('/'):
        size = os.path.getsize(path.strip())
    else:
        if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]):
            path = 'http://' + path.strip()

        # size value is taken
        try:
            connection = urllib.urlopen(path)
            size = int(connection.info()["Content-Length"])
        except IOError:
            log.exception(caller_id, 'Cannot find image')
            raise CMException('image_not_found')
        except KeyError:
            log.exception(caller_id, 'Cannot calculate size')
            raise CMException('image_calculate_size')

    user.check_storage(size / (1024 * 1024))

    image = StorageImage.create(name=name, description=description, user=user, disk_controller=disk_controller)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #7
0
def convert_to_storage_image(caller_id, system_image_id):
    """
    Changes type of the given Image.

    @cmview_admin_cm
    @param_post{system_image_id,int} ID of an Image to change type of

    @response{None}
    """
    image = SystemImage.admin_get(system_image_id)

    storage_image = StorageImage.create(name=image.name, description=image.description, user=image.user,
                                        disk_controller=image.disk_controller)
    storage_image.state = image_states['ok']
    storage_image.size = image.size

    try:
        storage_image.save()
        os.rename(image.path, storage_image.path)
        image.delete()
    except Exception:
        raise CMException('image_change_type')
Beispiel #8
0
def download(caller_id, name, description, path, disk_controller):
    """
    Downloads specified StorageImage.

    @cmview_user
    @param_post{name,string}
    @param_post{description,string}
    @param_post{path,string} HTTP or FTP path to StorageImage to download
    @param_post{disk_controller}
    """
    user = User.get(caller_id)

    if path.startswith('/'):
        size = os.path.getsize(path.strip())
    else:
        if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]):
            path = 'http://' + path.strip()

        # size value is taken
        try:
            connection = urllib.urlopen(path)
            size = int(connection.info()["Content-Length"])
        except IOError:
            log.exception(caller_id, 'Cannot find image')
            raise CMException('image_not_found')
        except KeyError:
            log.exception(caller_id, 'Cannot calculate size')
            raise CMException('image_calculate_size')

    user.check_storage(size / (1024 * 1024))

    image = StorageImage.create(name=name, description=description, user=user, disk_controller=disk_controller)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
Beispiel #9
0
def convert_to_storage_image(caller_id, system_image_id):
    """
    Changes type of the given Image.

    @cmview_admin_cm
    @param_post{system_image_id,int} ID of an Image to change type of

    @response{None}
    """
    image = SystemImage.admin_get(system_image_id)

    storage_image = StorageImage.create(name=image.name,
                                        description=image.description,
                                        user=image.user,
                                        disk_controller=image.disk_controller)
    storage_image.state = image_states['ok']
    storage_image.size = image.size

    try:
        storage_image.save()
        os.rename(image.path, storage_image.path)
        image.delete()
    except Exception:
        raise CMException('image_change_type')