Added members to VmInst that will contain the availability_zone
[snaps.git] / snaps / openstack / create_image.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 from glanceclient.exc import HTTPNotFound
17 import logging
18 import time
19
20 from snaps.openstack.openstack_creator import OpenStackCloudObject
21 from snaps.openstack.utils import glance_utils
22 from snaps.config import image
23
24 __author__ = 'spisarski'
25
26 logger = logging.getLogger('create_image')
27
28 IMAGE_ACTIVE_TIMEOUT = 600
29 POLL_INTERVAL = 3
30 STATUS_ACTIVE = 'active'
31
32
33 class OpenStackImage(OpenStackCloudObject):
34     """
35     Class responsible for managing an image in OpenStack
36     """
37
38     def __init__(self, os_creds, image_settings):
39         """
40         Constructor
41         :param os_creds: The OpenStack connection credentials
42         :param image_settings: An snaps.config.image.ImageConfig object
43         """
44         super(self.__class__, self).__init__(os_creds)
45
46         self.image_settings = image_settings
47         self.__image = None
48         self.__kernel_image = None
49         self.__ramdisk_image = None
50         self.__glance = None
51
52     def initialize(self):
53         """
54         Loads the existing Image
55         :return: The Image domain object or None
56         """
57         self.__glance = glance_utils.glance_client(self._os_creds)
58         self.__image = glance_utils.get_image(
59             self.__glance, image_settings=self.image_settings)
60
61         if self.__image:
62             logger.info('Found image with name - ' + self.image_settings.name)
63             return self.__image
64         elif (self.image_settings.exists and not self.image_settings.url
65                 and not self.image_settings.image_file):
66             raise ImageCreationError(
67                 'Image with does not exist with name - ' +
68                 self.image_settings.name)
69
70         if self.image_settings.kernel_image_settings:
71             self.__kernel_image = glance_utils.get_image(
72                 self.__glance,
73                 image_settings=self.image_settings.kernel_image_settings)
74
75         if self.image_settings.ramdisk_image_settings:
76             self.__ramdisk_image = glance_utils.get_image(
77                 self.__glance,
78                 image_settings=self.image_settings.ramdisk_image_settings)
79
80         return self.__image
81
82     def create(self):
83         """
84         Creates the image in OpenStack if it does not already exist and returns
85         the domain Image object
86         :return: The Image domain object or None
87         """
88         self.initialize()
89
90         if not self.__image:
91             extra_properties = self.image_settings.extra_properties or dict()
92
93             if self.image_settings.kernel_image_settings:
94                 if not self.__kernel_image:
95                     logger.info(
96                         'Creating associated kernel image with name - %s',
97                         self.image_settings.kernel_image_settings.name)
98                     self.__kernel_image = glance_utils.create_image(
99                         self.__glance,
100                         self.image_settings.kernel_image_settings)
101                 extra_properties['kernel_id'] = self.__kernel_image.id
102
103             if self.image_settings.ramdisk_image_settings:
104                 if not self.__ramdisk_image:
105                     logger.info(
106                         'Creating associated ramdisk image with name - %s',
107                         self.image_settings.ramdisk_image_settings.name)
108                     self.__ramdisk_image = glance_utils.create_image(
109                         self.__glance,
110                         self.image_settings.ramdisk_image_settings)
111                 extra_properties['ramdisk_id'] = self.__ramdisk_image.id
112
113             self.image_settings.extra_properties = extra_properties
114             self.__image = glance_utils.create_image(self.__glance,
115                                                      self.image_settings)
116
117             logger.info(
118                 'Created image with name - %s', self.image_settings.name)
119
120             if self.__image and self.image_active(block=True):
121                 logger.info(
122                     'Image is now active with name - %s',
123                     self.image_settings.name)
124                 return self.__image
125             else:
126                 raise ImageCreationError(
127                     'Image was not created or activated in the alloted amount'
128                     'of time')
129
130         return self.__image
131
132     def clean(self):
133         """
134         Cleanse environment of all artifacts
135         :return: void
136         """
137         for img in [self.__image, self.__kernel_image, self.__ramdisk_image]:
138             if img:
139                 try:
140                     glance_utils.delete_image(self.__glance, img)
141                 except HTTPNotFound:
142                     pass
143
144         self.__image = None
145         self.__kernel_image = None
146         self.__ramdisk_image = None
147
148     def get_image(self):
149         """
150         Returns the domain Image object as it was populated when create() was
151         called
152         :return: the object
153         """
154         return self.__image
155
156     def get_kernel_image(self):
157         """
158         Returns the OpenStack kernel image object as it was populated when
159         create() was called
160         :return: the object
161         """
162         return self.__kernel_image
163
164     def get_ramdisk_image(self):
165         """
166         Returns the OpenStack ramdisk image object as it was populated when
167         create() was called
168         :return: the object
169         """
170         return self.__ramdisk_image
171
172     def image_active(self, block=False, timeout=IMAGE_ACTIVE_TIMEOUT,
173                      poll_interval=POLL_INTERVAL):
174         """
175         Returns true when the image status returns the value of
176         expected_status_code
177         :param block: When true, thread will block until active or timeout
178                       value in seconds has been exceeded (False)
179         :param timeout: The timeout value
180         :param poll_interval: The polling interval in seconds
181         :return: T/F
182         """
183         return self._image_status_check(STATUS_ACTIVE, block, timeout,
184                                         poll_interval)
185
186     def _image_status_check(self, expected_status_code, block, timeout,
187                             poll_interval):
188         """
189         Returns true when the image status returns the value of
190         expected_status_code
191         :param expected_status_code: instance status evaluated with this string
192                                      value
193         :param block: When true, thread will block until active or timeout
194                       value in seconds has been exceeded (False)
195         :param timeout: The timeout value
196         :param poll_interval: The polling interval in seconds
197         :return: T/F
198         """
199         # sleep and wait for image status change
200         if block:
201             start = time.time()
202         else:
203             start = time.time() - timeout
204
205         while timeout > time.time() - start:
206             status = self._status(expected_status_code)
207             if status:
208                 logger.debug(
209                     'Image is active with name - ' + self.image_settings.name)
210                 return True
211
212             logger.debug('Retry querying image status in ' + str(
213                 poll_interval) + ' seconds')
214             time.sleep(poll_interval)
215             logger.debug('Image status query timeout in ' + str(
216                 timeout - (time.time() - start)))
217
218         logger.error(
219             'Timeout checking for image status for ' + expected_status_code)
220         return False
221
222     def _status(self, expected_status_code):
223         """
224         Returns True when active else False
225         :param expected_status_code: instance status evaluated with this string
226                                      value
227         :return: T/F
228         """
229         status = glance_utils.get_image_status(self.__glance, self.__image)
230         if not status:
231             logger.warning(
232                 'Cannot image status for image with ID - ' + self.__image.id)
233             return False
234
235         if status == 'ERROR':
236             raise ImageCreationError('Instance had an error during deployment')
237         logger.debug('Instance status is - ' + status)
238         return status == expected_status_code
239
240
241 class ImageSettings(image.ImageConfig):
242     """
243     Deprecated, use snaps.config.image.ImageSettings instead
244     """
245     def __init__(self, **kwargs):
246         from warnings import warn
247         warn('Use snaps.config.image.ImageConfig instead', DeprecationWarning)
248         super(ImageSettings, self).__init__(**kwargs)
249
250
251 class ImageCreationError(Exception):
252     """
253     Exception to be thrown when an image cannot be created
254     """
255
256     def __init__(self, message):
257         Exception.__init__(self, message)