1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from glanceclient.exc import HTTPNotFound
20 from snaps.openstack.utils import glance_utils
22 __author__ = 'spisarski'
24 logger = logging.getLogger('create_image')
26 IMAGE_ACTIVE_TIMEOUT = 600
28 STATUS_ACTIVE = 'active'
33 Class responsible for creating an image in OpenStack
36 def __init__(self, os_creds, image_settings):
39 :param os_creds: The OpenStack connection credentials
40 :param image_settings: The image settings
43 self.__os_creds = os_creds
44 self.image_settings = image_settings
46 self.__glance = glance_utils.glance_client(os_creds)
48 def create(self, cleanup=False):
50 Creates the image in OpenStack if it does not already exist and returns the domain Image object
51 :param cleanup: Denotes whether or not this is being called for cleanup or not
52 :return: The OpenStack Image object
54 self.__image = glance_utils.get_image(self.__glance, self.image_settings.name)
56 logger.info('Found image with name - ' + self.image_settings.name)
59 self.__image = glance_utils.create_image(self.__glance, self.image_settings)
60 logger.info('Creating image')
61 if self.__image and self.image_active(block=True):
62 logger.info('Image is now active with name - ' + self.image_settings.name)
65 raise Exception('Image was not created or activated in the alloted amount of time')
67 logger.info('Did not create image due to cleanup mode')
73 Cleanse environment of all artifacts
78 glance_utils.delete_image(self.__glance, self.__image)
85 Returns the domain Image object as it was populated when create() was called
90 def image_active(self, block=False, timeout=IMAGE_ACTIVE_TIMEOUT, poll_interval=POLL_INTERVAL):
92 Returns true when the image status returns the value of expected_status_code
93 :param block: When true, thread will block until active or timeout value in seconds has been exceeded (False)
94 :param timeout: The timeout value
95 :param poll_interval: The polling interval in seconds
98 return self._image_status_check(STATUS_ACTIVE, block, timeout, poll_interval)
100 def _image_status_check(self, expected_status_code, block, timeout, poll_interval):
102 Returns true when the image status returns the value of expected_status_code
103 :param expected_status_code: instance status evaluated with this string value
104 :param block: When true, thread will block until active or timeout value in seconds has been exceeded (False)
105 :param timeout: The timeout value
106 :param poll_interval: The polling interval in seconds
109 # sleep and wait for image status change
113 start = time.time() - timeout
115 while timeout > time.time() - start:
116 status = self._status(expected_status_code)
118 logger.info('Image is active with name - ' + self.image_settings.name)
121 logger.debug('Retry querying image status in ' + str(poll_interval) + ' seconds')
122 time.sleep(poll_interval)
123 logger.debug('Image status query timeout in ' + str(timeout - (time.time() - start)))
125 logger.error('Timeout checking for image status for ' + expected_status_code)
128 def _status(self, expected_status_code):
130 Returns True when active else False
131 :param expected_status_code: instance status evaluated with this string value
134 # TODO - Place this API call into glance_utils.
135 status = glance_utils.get_image_status(self.__glance, self.__image)
137 logger.warn('Cannot image status for image with ID - ' + self.__image.id)
140 if status == 'ERROR':
141 raise Exception('Instance had an error during deployment')
142 logger.debug('Instance status is - ' + status)
143 return status == expected_status_code
147 def __init__(self, config=None, name=None, image_user=None, img_format=None, url=None, image_file=None,
148 extra_properties=None, nic_config_pb_loc=None):
151 :param config: dict() object containing the configuration settings using the attribute names below as each
152 member's the key and overrides any of the other parameters.
153 :param name: the image's name (required)
154 :param image_user: the image's default sudo user (required)
155 :param img_format: the image type (required)
156 :param url: the image download location (requires url or img_file)
157 :param image_file: the image file location (requires url or img_file)
158 :param extra_properties: dict() object containing extra parameters to pass when loading the image;
159 can be ids of kernel and initramfs images for a 3-part image
160 :param nic_config_pb_loc: the file location to the Ansible Playbook that can configure multiple NICs
164 self.name = config.get('name')
165 self.image_user = config.get('image_user')
166 self.format = config.get('format')
167 self.url = config.get('download_url')
168 self.image_file = config.get('image_file')
169 self.extra_properties = config.get('extra_properties')
170 self.nic_config_pb_loc = config.get('nic_config_pb_loc')
173 self.image_user = image_user
174 self.format = img_format
176 self.image_file = image_file
177 self.extra_properties = extra_properties
178 self.nic_config_pb_loc = nic_config_pb_loc
180 if not self.name or not self.image_user or not self.format:
181 raise Exception("The attributes name, image_user, format, and url are required for ImageSettings")
183 if not self.url and not self.image_file:
184 raise Exception('URL or image file must be set')
186 if self.url and self.image_file:
187 raise Exception('Please set either URL or image file, not both')