示例#1
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    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(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 = User.get(caller_id)

    image = SystemImage.create(name=name, description=description, user=user, platform=platform,
                               disk_controller=disk_controller, network_device=network_device,
                               video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
示例#2
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    Downloads image depending on the \c data parameter.
    @cmview_user

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

    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}

    @raises{image_not_found,CMException}
    @raises{image_create,CMException}
    """
    user = User.get(caller_id)

    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 = User.get(caller_id)
    user.check_storage(size / (1024 * 1024))

    image = SystemImage.create(
        name=name,
        description=description,
        user=user,
        platform=platform,
        disk_controller=disk_controller,
        network_device=network_device,
        video_device=video_device,
    )

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

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

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

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

    @parameter{type,image_types} type of image, automatically set, type is in the URL requested

    @response{None}

    @raises{image_not_found,CMException}
    @raises{image_create,CMException}
    """
    user = User.get(caller_id)

    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 = User.get(caller_id)
    user.check_storage(size / (1024 * 1024))

    image = SystemImage.create(name=name, description=description, user=user, platform=platform,
                        disk_controller=disk_controller, network_device=network_device, video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
示例#5
0
def download(caller_id, description, name, path, disk_controller,
             network_device, platform, video_device):
    """
    Downloads specified SystemImage.

    @cmview_admin_cm
    @param_post{description,string}
    @param_post{name,string}
    @param_post{path,string} HTTP or FTP path to image to download
    @param_post{disk_controller}
    @param_post{network_device}
    @param_post{platform}
    @param_post{video_device}
    """

    # 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 = User.get(caller_id)

    image = SystemImage.create(name=name,
                               description=description,
                               user=user,
                               platform=platform,
                               disk_controller=disk_controller,
                               network_device=network_device,
                               video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
示例#6
0
def convert_to_system_image(caller_id, storage_image_id):
    """
    Converts specified StorageImage to SystemImage. After convertion it's not
    available as StorageImage anymore. File is moved and StorageImage entry is
    removed from database.

    @cmview_admin_cm
    @param_post{storage_image_id,int} ID of an StorageImage to convert
    """
    image = StorageImage.admin_get(storage_image_id)

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

    try:
        system_image.save()
        os.rename(image.path, system_image.path)
        image.delete()
    except Exception:
        raise CMException('image_change_type')
示例#7
0
def convert_to_system_image(caller_id, storage_image_id):
    """
    Changes type of the given Image.
    @cmview_admin_cm

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

    @response{None}
    """
    image = StorageImage.admin_get(storage_image_id)

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

    try:
        system_image.save()
        os.rename(image.path, system_image.path)
        image.delete()
    except Exception:
        raise CMException('image_change_type')
示例#8
0
def copy(caller_id, src_image_id, dest_user_id):
    """
    Copy selected image to user's images

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

    try:
        dest_image.save()
    except Exception, e:
        log.error(caller_id, "Unable to commit: %s" % str(e))
        raise CMException('image_create')
示例#9
0
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
    """
    Downloads specified SystemImage.

    @cmview_admin_cm
    @param_post{description,string}
    @param_post{name,string}
    @param_post{path,string} HTTP or FTP path to image to download
    @param_post{disk_controller}
    @param_post{network_device}
    @param_post{platform}
    @param_post{video_device}
    """

    # 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 = User.get(caller_id)

    image = SystemImage.create(name=name, description=description, user=user, platform=platform,
                               disk_controller=disk_controller, network_device=network_device,
                               video_device=video_device)

    try:
        image.save()
    except Exception, e:
        log.error(caller_id, "Unable to save image to DB: %s" % str(e))
        raise CMException('image_create')
示例#10
0
文件: vm.py 项目: cc1-cloud/cc1
    def save_image(self):
        """
        Method saves VM to image with VM's name, description and parameters.
        """
        self.set_state('saving')
        try:
            self.save(update_fields=['state'])
            transaction.commit()
        except Exception, e:
            log.exception(self.user.id, 'save img')
            return

        img = SystemImage.create(name=(self.name + "_autosave" if self.save_vm == 1 else self.name),
                                 description=self.description, user=self.user, platform=self.system_image.platform,
                                 disk_controller=self.system_image.disk_controller,
                                 network_device=self.system_image.network_device,
                                 video_device=self.system_image.video_device)
        img.size = self.system_image.size
        img.save()

        try:
            img.copy_to_storage(self, img)
        except Exception, e:
            self.set_state('saving failed')
            self.node.lock()
            message.error(self.user.id, 'vm_save', {'id': self.id, 'name': self.name})
            try:
                img.delete()
                transaction.commit()
            except Exception, e:
示例#11
0
    def save_image(self):
        """
        Method saves VM to image with VM's name, description and parameters.
        """
        self.set_state('saving')
        try:
            self.save(update_fields=['state'])
            transaction.commit()
        except Exception, e:
            log.exception(self.user.id, 'save img')
            return

        img = SystemImage.create(
            name=(self.name + "_autosave" if self.save_vm == 1 else self.name),
            description=self.description,
            user=self.user,
            platform=self.system_image.platform,
            disk_controller=self.system_image.disk_controller,
            network_device=self.system_image.network_device,
            video_device=self.system_image.video_device)
        img.size = self.system_image.size
        img.save()

        try:
            img.copy_to_storage(self, img)
        except Exception, e:
            self.set_state('saving failed')
            self.node.lock()
            message.error(self.user.id, 'vm_save', {
                'id': self.id,
                'name': self.name
            })