示例#1
0
    def clone_image(self, parent_image_id, image_name, download_dir,
                    download_location, **kwargs):
        """
	Creates an image of an image in the image-list
        Required Args:
            parent_image_id - The parent image that will be cloned
            image_name - The name of the new image
            download_location OR download_dir - Where to download the image
            if download_dir:
                download_location = download_dir/username/image_name.qcow2
	"""
        parent_image = self.get_image(parent_image_id)
        #Step 1 download a local copy
        if not os.path.exists(download_location) or kwargs.get('force', False):
            self.download_image(parent_image_id, download_location)

    #Step 2: Clean the local copy
        if kwargs.get('clean_image', True):
            mount_and_clean(download_location,
                            os.path.join(download_dir, 'mount/'),
                            status_hook=getattr(self, 'hook', None),
                            method_hook=getattr(self, 'clean_hook', None),
                            **kwargs)

    #Step 3: Upload the local copy as a 'real' image
    # with seperate kernel & ramdisk
        if kwargs.get('upload_image', True):
            if hasattr(parent_image, 'properties'):  # Treated as an obj.
                properties = parent_image.properties
                properties.update({
                    'container_format': parent_image.container_format,
                    'disk_format': parent_image.disk_format,
                })
            elif hasattr(parent_image, 'items'):  # Treated as a dict.
                properties = dict(parent_image.items())
            upload_args = self.parse_upload_args(
                image_name,
                download_location,
                kernel_id=properties.get('kernel_id'),
                ramdisk_id=properties.get('ramdisk_id'),
                disk_format=properties.get('disk_format'),
                container_format=properties.get('container_format'),
                **kwargs)
            new_image = self.upload_local_image(**upload_args)

        if kwargs.get('remove_local_image', True):
            wildcard_remove(download_dir)

        if kwargs.get('remove_image', False):
            wildcard_remove(download_dir)
            try:
                self.delete_images(image_id=parent_image.id)
            except Exception as exc:
                logger.exception("Could not delete the image %s - %s" %
                                 (parent_image.id, exc.message))

        return new_image
    def clone_image(self, parent_image_id, image_name, download_dir, download_location, **kwargs):
	"""
	Creates an image of an image in the image-list
        Required Args:
            parent_image_id - The parent image that will be cloned
            image_name - The name of the new image
            download_location OR download_dir - Where to download the image
            if download_dir:
                download_location = download_dir/username/image_name.qcow2
	"""
        parent_image = self.get_image(parent_image_id)
        #Step 1 download a local copy
        if not os.path.exists(download_location) or kwargs.get('force',False):
            self.download_image(parent_image_id, download_location)

        #Step 2: Clean the local copy
        if kwargs.get('clean_image',True):
            mount_and_clean(
                    download_location,
                    status_hook=getattr(self, 'hook', None),
                    method_hook=getattr(self, 'clean_hook',None),
                    **kwargs)

        #Step 3: Upload the local copy as a 'real' image
        # with seperate kernel & ramdisk
        if kwargs.get('upload_image',True):
            if hasattr(parent_image, 'properties'):  # Treated as an obj.
                properties = parent_image.properties
                properties.update({
                    'container_format': parent_image.container_format,
                    'disk_format': parent_image.disk_format,
                })
            elif hasattr(parent_image, 'items'):  # Treated as a dict.
                properties = dict(parent_image.items())
            upload_args = self.parse_upload_args(image_name, download_location,
                                                 kernel_id=properties.get('kernel_id'),
                                                 ramdisk_id=properties.get('ramdisk_id'),
                                                 disk_format=properties.get('disk_format'),
                                                 container_format=properties.get('container_format'),
                                                 **kwargs)
            new_image = self.upload_local_image(**upload_args)

        if kwargs.get('remove_local_image', True):
            wildcard_remove(download_dir)

        if kwargs.get('remove_image', False):
            wildcard_remove(download_dir)
            try:
                self.delete_images(image_id=parent_image.id)
            except Exception as exc:
                logger.exception("Could not delete the image %s - %s" % (parent_image.id, exc.message))

        return new_image
示例#3
0
def start_migration(migrationCls, migration_creds, download_location, **imaging_args):
    """
    Whether your migration starts by image or by instance, they all end the
    same:
    * Clean-up the local image file
    * Upload the local image file
    """
    dest_manager = migrationCls(**migration_creds)
    dest_manager.hook = imaging_args.get('machine_request', None)
    download_dir = os.path.dirname(download_location)
    mount_point = os.path.join(download_dir, 'mount_point/')
    if not os.path.exists(mount_point):
        os.makedirs(mount_point)
    #2. clean using dest manager
    if imaging_args.get('clean_image',True):
        mount_and_clean(
                download_location,
                mount_point,
                status_hook=getattr(dest_manager, 'hook', None),
                method_hook=getattr(dest_manager, 'clean_hook', None),
                **imaging_args)

    #3. Convert from KVM-->Xen or Xen-->KVM (If necessary)
    if imaging_args.get('kvm_to_xen', False):
        (image_path, kernel_path, ramdisk_path) =\
            KVM2Xen.convert(download_location, download_dir)
        imaging_args['image_path'] = image_path
        imaging_args['kernel_path'] = kernel_path
        imaging_args['ramdisk_path'] = ramdisk_path
    elif imaging_args.get('xen_to_kvm', False):
        (image_path, kernel_path, ramdisk_path) =\
            Xen2KVM.convert(download_location, download_dir)
        imaging_args['image_path'] = image_path
        imaging_args['kernel_path'] = kernel_path
        imaging_args['ramdisk_path'] = ramdisk_path
    else:
        logger.info("Upload requires no conversion between Xen and KVM.")
        imaging_args['image_path'] = download_location
    #4. Upload on new
    imaging_args['download_location'] = download_location
    upload_kwargs = dest_manager.parse_upload_args(**imaging_args)
    new_image_id = dest_manager.upload_image(**upload_kwargs)

    #5. Cleanup, return
    if not imaging_args.get('keep_image',False):
        wildcard_remove(download_dir)
    return new_image_id
示例#4
0
def start_migration(migrationCls, migration_creds, download_location, **imaging_args):
    """
    Whether your migration starts by image or by instance, they all end the
    same:
    * Clean-up the local image file
    * Upload the local image file
    """
    dest_manager = migrationCls(**migration_creds)
    download_dir = os.path.dirname(download_location)
    mount_point = os.path.join(download_dir, 'mount_point/')
    if not os.path.exists(mount_point):
        os.makedirs(mount_point)
    #2. clean using dest manager
    if imaging_args.get('clean_image',True):
        dest_manager.mount_and_clean(
                download_location,
                mount_point,
                **imaging_args)

    #3. Convert from KVM-->Xen or Xen-->KVM (If necessary)
    if imaging_args.get('kvm_to_xen', False):
        (image_path, kernel_path, ramdisk_path) =\
            KVM2Xen.convert(download_location, download_dir)
        imaging_args['image_path'] = image_path
        imaging_args['kernel_path'] = kernel_path
        imaging_args['ramdisk_path'] = ramdisk_path
    elif imaging_args.get('xen_to_kvm', False):
        (image_path, kernel_path, ramdisk_path) =\
            Xen2KVM.convert(download_location, download_dir)
        imaging_args['image_path'] = image_path
        imaging_args['kernel_path'] = kernel_path
        imaging_args['ramdisk_path'] = ramdisk_path
    else:
        logger.info("Upload requires no conversion between Xen and KVM.")
        imaging_args['image_path'] = download_location
    #4. Upload on new
    imaging_args['download_location'] = download_location
    upload_kwargs = dest_manager.parse_upload_args(**imaging_args)
    new_image_id = dest_manager.upload_image(**upload_kwargs)

    #5. Cleanup, return
    if not imaging_args.get('keep_image',False):
        wildcard_remove(download_dir)
    return new_image_id
示例#5
0
    def create_image(self, instance_id, image_name, *args, **kwargs):
        """
        Creates an image of a running instance
        Required Args:
            instance_id - The instance that will be imaged
            image_name - The name of the image
        """
        #Does the instance still exist?
        reservation, instance = self.get_reservation(instance_id)
        if not reservation or not instance:
            raise Exception("Instance %s does not exist" % instance_id)


        #Step 1. Download
        download_args = self.download_instance_args(instance_id, image_name, **kwargs)
        logger.debug("Downloading instance: %s" % instance_id)
        download_location = self.download_instance(**download_args)
        logger.debug("Instance downloaded to %s" % download_location)
        download_dir = download_args['download_dir']
        mount_point = self.build_imaging_dirs(download_dir)

        #Step 2. Mount and Clean image 
        if kwargs.get('clean_image',True):
            logger.debug("Cleaning image file: %s" % download_location)
            mount_and_clean(
                    download_location,
                    mount_point,
                    **kwargs)

        ##Step 3. Upload image
        upload_kwargs = self.parse_upload_args(
                instance_id, image_name, download_location, **kwargs)
        logger.debug("Uploading image file: %s" % download_location)
        new_image_id = self.upload_local_image(**upload_kwargs)
        logger.debug("image file uploaded. New Image: %s" % new_image_id)

        #Step 4. Cleanup, return
        if not kwargs.get('keep_image',False):
            logger.debug("Removing all files/directories in %s" % download_dir)
            wildcard_remove(download_dir)
        return new_image_id
示例#6
0
    def create_image(self, instance_id, image_name, *args, **kwargs):
        """
        Creates an image of a running instance
        Required Args:
            instance_id - The instance that will be imaged
            image_name - The name of the image
        """
        #Does the instance still exist?
        reservation, instance = self.get_reservation(instance_id)
        if not reservation or not instance:
            raise Exception("Instance %s does not exist" % instance_id)


        #Step 1. Download
        download_args = self.download_instance_args(instance_id, image_name, **kwargs)
        logger.debug("Downloading instance: %s" % instance_id)
        download_location = self.download_instance(**download_args)
        logger.debug("Instance downloaded to %s" % download_location)
        download_dir = download_args['download_dir']
        mount_point = self.build_imaging_dirs(download_dir)

        #Step 2. Mount and Clean image 
        if kwargs.get('clean_image',True):
            logger.debug("Cleaning image file: %s" % download_location)
            self.mount_and_clean(
                    download_location,
                    mount_point,
                    **kwargs)

        ##Step 3. Upload image
        upload_kwargs = self.parse_upload_args(
                instance_id, image_name, download_location, **kwargs)
        logger.debug("Uploading image file: %s" % download_location)
        new_image_id = self.upload_local_image(**upload_kwargs)
        logger.debug("image file uploaded. New Image: %s" % new_image_id)

        #Step 4. Cleanup, return
        if not kwargs.get('keep_image',False):
            logger.debug("Removing all files/directories in %s" % download_dir)
            wildcard_remove(download_dir)
        return new_image_id
示例#7
0
    def create_image(self, instance_id, image_name, *args, **kwargs):
        """
        Creates an image of a running instance
        Required Args:
            instance_id - The instance that will be imaged
            image_name - The name of the image
            download_location OR download_dir - Where to download the image
            if download_dir:
                download_location = download_dir/username/image_name.qcow2
        """
        #Step 1: Retrieve a copy of the instance ( Use snapshot_id if given )
        download_kwargs = self.download_instance_args(instance_id, image_name, **kwargs)
        snapshot_id, download_location = self.download_instance(**download_kwargs)
        download_dir = os.path.dirname(download_location)
        snapshot = self.get_image(snapshot_id)
        #Step 2: Clean the local copy
        if kwargs.get('clean_image',True):
            self.mount_and_clean(
                    download_location,
                    os.path.join(download_dir, 'mount/'),
                    **kwargs)

        #Step 3: Upload the local copy as a 'real' image
        # with seperate kernel & ramdisk
        upload_args = self.parse_upload_args(image_name, download_location,
                                             kernel_id=snapshot.properties.get('kernel_id'),
                                             ramdisk_id=snapshot.properties.get('ramdisk_id'),
                                             disk_format=snapshot.disk_format,
                                             container_format=snapshot.container_format,
                                             **kwargs)

        new_image = self.upload_local_image(**upload_args)

        if not kwargs.get('keep_image',False):
            wildcard_remove(download_dir)
            snapshot.delete()

        return new_image