X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=snaps%2Fopenstack%2Fcreate_image.py;h=1a8aa124a03bdf2d3126c62f59f76cf2644fdd3d;hb=refs%2Fchanges%2F15%2F64515%2F1;hp=c8782bfa1da485042629971d3bee7d395140fc6b;hpb=b5822ef2be374cd6bf53a60a84f1c8449a90b35a;p=snaps.git diff --git a/snaps/openstack/create_image.py b/snaps/openstack/create_image.py index c8782bf..1a8aa12 100644 --- a/snaps/openstack/create_image.py +++ b/snaps/openstack/create_image.py @@ -19,6 +19,7 @@ import time from snaps.openstack.openstack_creator import OpenStackCloudObject from snaps.openstack.utils import glance_utils +from snaps.config import image __author__ = 'spisarski' @@ -38,8 +39,7 @@ class OpenStackImage(OpenStackCloudObject): """ Constructor :param os_creds: The OpenStack connection credentials - :param image_settings: The image settings - :return: + :param image_settings: An snaps.config.image.ImageConfig object """ super(self.__class__, self).__init__(os_creds) @@ -54,9 +54,13 @@ class OpenStackImage(OpenStackCloudObject): Loads the existing Image :return: The Image domain object or None """ - self.__glance = glance_utils.glance_client(self._os_creds) + super(self.__class__, self).initialize() + + self.__glance = glance_utils.glance_client( + self._os_creds, self._os_session) self.__image = glance_utils.get_image( self.__glance, image_settings=self.image_settings) + if self.__image: logger.info('Found image with name - ' + self.image_settings.name) return self.__image @@ -70,6 +74,7 @@ class OpenStackImage(OpenStackCloudObject): self.__kernel_image = glance_utils.get_image( self.__glance, image_settings=self.image_settings.kernel_image_settings) + if self.image_settings.ramdisk_image_settings: self.__ramdisk_image = glance_utils.get_image( self.__glance, @@ -97,6 +102,7 @@ class OpenStackImage(OpenStackCloudObject): self.__glance, self.image_settings.kernel_image_settings) extra_properties['kernel_id'] = self.__kernel_image.id + if self.image_settings.ramdisk_image_settings: if not self.__ramdisk_image: logger.info( @@ -113,6 +119,7 @@ class OpenStackImage(OpenStackCloudObject): logger.info( 'Created image with name - %s', self.image_settings.name) + if self.__image and self.image_active(block=True): logger.info( 'Image is now active with name - %s', @@ -122,8 +129,6 @@ class OpenStackImage(OpenStackCloudObject): raise ImageCreationError( 'Image was not created or activated in the alloted amount' 'of time') - else: - logger.info('Did not create image due to cleanup mode') return self.__image @@ -132,10 +137,10 @@ class OpenStackImage(OpenStackCloudObject): Cleanse environment of all artifacts :return: void """ - for image in [self.__image, self.__kernel_image, self.__ramdisk_image]: - if image: + for img in [self.__image, self.__kernel_image, self.__ramdisk_image]: + if img: try: - glance_utils.delete_image(self.__glance, image) + glance_utils.delete_image(self.__glance, img) except HTTPNotFound: pass @@ -143,6 +148,11 @@ class OpenStackImage(OpenStackCloudObject): self.__kernel_image = None self.__ramdisk_image = None + if self.__glance: + self.__glance.http_client.session.session.close() + + super(self.__class__, self).clean() + def get_image(self): """ Returns the domain Image object as it was populated when create() was @@ -236,100 +246,14 @@ class OpenStackImage(OpenStackCloudObject): return status == expected_status_code -class ImageSettings: - def __init__(self, **kwargs): - """ - Constructor - :param name: the image's name (required) - :param image_user: the image's default sudo user (required) - :param format or img_format: the image format type (required) - :param url or download_url: the image download location (requires url - or img_file) - :param image_file: the image file location (requires url or img_file) - :param extra_properties: dict() object containing extra parameters to - pass when loading the image; - can be ids of kernel and initramfs images for - a 3-part image - :param nic_config_pb_loc: the file location to the Ansible Playbook - that can configure multiple NICs - :param kernel_image_settings: the settings for a kernel image - :param ramdisk_image_settings: the settings for a ramdisk image - :param exists: When True, an image with the given name must exist - :param public: When True, an image will be created with public - visibility - """ - - self.name = kwargs.get('name') - self.image_user = kwargs.get('image_user') - self.format = kwargs.get('format') - if not self.format: - self.format = kwargs.get('img_format') - - self.url = kwargs.get('url') - if not self.url: - self.url = kwargs.get('download_url') - if self.url == 'None': - self.url = None - - self.image_file = kwargs.get('image_file') - if self.image_file == 'None': - self.image_file = None - - self.extra_properties = kwargs.get('extra_properties') - self.nic_config_pb_loc = kwargs.get('nic_config_pb_loc') - - kernel_image_settings = kwargs.get('kernel_image_settings') - if kernel_image_settings: - if isinstance(kernel_image_settings, dict): - self.kernel_image_settings = ImageSettings( - **kernel_image_settings) - else: - self.kernel_image_settings = kernel_image_settings - else: - self.kernel_image_settings = None - - ramdisk_image_settings = kwargs.get('ramdisk_image_settings') - if ramdisk_image_settings: - if isinstance(ramdisk_image_settings, dict): - self.ramdisk_image_settings = ImageSettings( - **ramdisk_image_settings) - else: - self.ramdisk_image_settings = ramdisk_image_settings - else: - self.ramdisk_image_settings = None - - if 'exists' in kwargs and kwargs['exists'] is True: - self.exists = True - else: - self.exists = False - - if 'public' in kwargs and kwargs['public'] is True: - self.public = True - else: - self.public = False - - if not self.name: - raise ImageSettingsError("The attribute name is required") - - if not (self.url or self.image_file) and not self.exists: - raise ImageSettingsError( - 'URL or image file must be set or image must already exist') - - if not self.image_user: - raise ImageSettingsError('Image user is required') - - if not self.format and not self.exists: - raise ImageSettingsError( - 'Format is required when the image should not already exist') - - -class ImageSettingsError(Exception): +class ImageSettings(image.ImageConfig): """ - Exception to be thrown when an image settings are incorrect + Deprecated, use snaps.config.image.ImageSettings instead """ - - def __init__(self, message): - Exception.__init__(self, message) + def __init__(self, **kwargs): + from warnings import warn + warn('Use snaps.config.image.ImageConfig instead', DeprecationWarning) + super(ImageSettings, self).__init__(**kwargs) class ImageCreationError(Exception):