c16197329dd646f4afbfdba490d2ad7e47c3939e
[snaps.git] / snaps / openstack / create_stack.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 import logging
17 import time
18
19 from heatclient.exc import HTTPNotFound
20
21 from snaps.openstack.create_instance import OpenStackVmInstance
22 from snaps.openstack.openstack_creator import OpenStackCloudObject
23 from snaps.openstack.utils import nova_utils, settings_utils, glance_utils
24
25 from snaps.openstack.create_network import OpenStackNetwork
26 from snaps.openstack.utils import heat_utils, neutron_utils
27
28 __author__ = 'spisarski'
29
30 logger = logging.getLogger('create_stack')
31
32 STACK_DELETE_TIMEOUT = 1200
33 STACK_COMPLETE_TIMEOUT = 1200
34 POLL_INTERVAL = 3
35 STATUS_CREATE_FAILED = 'CREATE_FAILED'
36 STATUS_CREATE_COMPLETE = 'CREATE_COMPLETE'
37 STATUS_DELETE_COMPLETE = 'DELETE_COMPLETE'
38 STATUS_DELETE_FAILED = 'DELETE_FAILED'
39
40
41 class OpenStackHeatStack(OpenStackCloudObject, object):
42     """
43     Class responsible for managing a heat stack in OpenStack
44     """
45
46     def __init__(self, os_creds, stack_settings, image_settings=None,
47                  keypair_settings=None):
48         """
49         Constructor
50         :param os_creds: The OpenStack connection credentials
51         :param stack_settings: The stack settings
52         :param image_settings: A list of ImageSettings objects that were used
53                                for spawning this stack
54         :param image_settings: A list of ImageSettings objects that were used
55                                for spawning this stack
56         :param keypair_settings: A list of KeypairSettings objects that were
57                                  used for spawning this stack
58         :return:
59         """
60         super(self.__class__, self).__init__(os_creds)
61
62         self.stack_settings = stack_settings
63
64         if image_settings:
65             self.image_settings = image_settings
66         else:
67             self.image_settings = None
68
69         if image_settings:
70             self.keypair_settings = keypair_settings
71         else:
72             self.keypair_settings = None
73
74         self.__stack = None
75         self.__heat_cli = None
76
77     def initialize(self):
78         """
79         Loads the existing heat stack
80         :return: The Stack domain object or None
81         """
82         self.__heat_cli = heat_utils.heat_client(self._os_creds)
83         self.__stack = heat_utils.get_stack(
84             self.__heat_cli, stack_settings=self.stack_settings)
85         if self.__stack:
86             logger.info('Found stack with name - ' + self.stack_settings.name)
87             return self.__stack
88
89     def create(self):
90         """
91         Creates the heat stack in OpenStack if it does not already exist and
92         returns the domain Stack object
93         :return: The Stack domain object or None
94         """
95         self.initialize()
96
97         if self.__stack:
98             logger.info('Found stack with name - ' + self.stack_settings.name)
99             return self.__stack
100         else:
101             self.__stack = heat_utils.create_stack(self.__heat_cli,
102                                                    self.stack_settings)
103             logger.info(
104                 'Created stack with name - ' + self.stack_settings.name)
105             if self.__stack and self.stack_complete(block=True):
106                 logger.info(
107                     'Stack is now active with name - ' +
108                     self.stack_settings.name)
109                 return self.__stack
110             else:
111                 status = heat_utils.get_stack_status_reason(self.__heat_cli,
112                                                             self.__stack.id)
113                 logger.error(
114                     'ERROR: STACK CREATION FAILED: ' + status)
115                 raise StackCreationError(
116                     'Failure while creating stack')
117
118     def clean(self):
119         """
120         Cleanse environment of all artifacts
121         :return: void
122         """
123         if self.__stack:
124             try:
125                 logger.info('Deleting stack - %s' + self.__stack.name)
126                 heat_utils.delete_stack(self.__heat_cli, self.__stack)
127
128                 try:
129                     self.stack_deleted(block=True)
130                 except StackError as e:
131                     # Stack deletion seems to fail quite a bit
132                     logger.warn('Stack did not delete properly - %s', e)
133
134                     # Delete VMs first
135                     for vm_inst_creator in self.get_vm_inst_creators():
136                         try:
137                             vm_inst_creator.clean()
138                             if not vm_inst_creator.vm_deleted(block=True):
139                                 logger.warn('Unable to deleted VM - %s',
140                                             vm_inst_creator.get_vm_inst().name)
141                         except:
142                             logger.warn('Unexpected error deleting VM - %s ',
143                                         vm_inst_creator.get_vm_inst().name)
144
145                 logger.info('Attempting to delete again stack - %s',
146                             self.__stack.name)
147
148                 # Delete Stack again
149                 heat_utils.delete_stack(self.__heat_cli, self.__stack)
150                 deleted = self.stack_deleted(block=True)
151                 if not deleted:
152                     raise StackError(
153                         'Stack could not be deleted ' + self.__stack.name)
154             except HTTPNotFound:
155                 pass
156
157             self.__stack = None
158
159     def get_stack(self):
160         """
161         Returns the domain Stack object as it was populated when create() was
162         called
163         :return: the object
164         """
165         return self.__stack
166
167     def get_outputs(self):
168         """
169         Returns the list of outputs as contained on the OpenStack Heat Stack
170         object
171         :return:
172         """
173         return heat_utils.get_outputs(self.__heat_cli, self.__stack)
174
175     def get_status(self):
176         """
177         Returns the list of outputs as contained on the OpenStack Heat Stack
178         object
179         :return:
180         """
181         return heat_utils.get_stack_status(self.__heat_cli, self.__stack.id)
182
183     def stack_complete(self, block=False, timeout=None,
184                        poll_interval=POLL_INTERVAL):
185         """
186         Returns true when the stack status returns the value of
187         expected_status_code
188         :param block: When true, thread will block until active or timeout
189                       value in seconds has been exceeded (False)
190         :param timeout: The timeout value
191         :param poll_interval: The polling interval in seconds
192         :return: T/F
193         """
194         if not timeout:
195             timeout = self.stack_settings.stack_create_timeout
196         return self._stack_status_check(STATUS_CREATE_COMPLETE, block, timeout,
197                                         poll_interval, STATUS_CREATE_FAILED)
198
199     def stack_deleted(self, block=False, timeout=STACK_DELETE_TIMEOUT,
200                       poll_interval=POLL_INTERVAL):
201         """
202         Returns true when the stack status returns the value of
203         expected_status_code
204         :param block: When true, thread will block until active or timeout
205                       value in seconds has been exceeded (False)
206         :param timeout: The timeout value
207         :param poll_interval: The polling interval in seconds
208         :return: T/F
209         """
210         return self._stack_status_check(STATUS_DELETE_COMPLETE, block, timeout,
211                                         poll_interval, STATUS_DELETE_FAILED)
212
213     def get_network_creators(self):
214         """
215         Returns a list of network creator objects as configured by the heat
216         template
217         :return: list() of OpenStackNetwork objects
218         """
219
220         neutron = neutron_utils.neutron_client(self._os_creds)
221
222         out = list()
223         stack_networks = heat_utils.get_stack_networks(
224             self.__heat_cli, neutron, self.__stack)
225
226         for stack_network in stack_networks:
227             net_settings = settings_utils.create_network_settings(
228                 neutron, stack_network)
229             net_creator = OpenStackNetwork(self._os_creds, net_settings)
230             out.append(net_creator)
231             net_creator.initialize()
232
233         return out
234
235     def get_vm_inst_creators(self, heat_keypair_option=None):
236         """
237         Returns a list of VM Instance creator objects as configured by the heat
238         template
239         :return: list() of OpenStackVmInstance objects
240         """
241
242         out = list()
243         nova = nova_utils.nova_client(self._os_creds)
244
245         stack_servers = heat_utils.get_stack_servers(
246             self.__heat_cli, nova, self.__stack)
247
248         neutron = neutron_utils.neutron_client(self._os_creds)
249         glance = glance_utils.glance_client(self._os_creds)
250
251         for stack_server in stack_servers:
252             vm_inst_settings = settings_utils.create_vm_inst_settings(
253                 nova, neutron, stack_server)
254             image_settings = settings_utils.determine_image_settings(
255                 glance, stack_server, self.image_settings)
256             keypair_settings = settings_utils.determine_keypair_settings(
257                 self.__heat_cli, self.__stack, stack_server,
258                 keypair_settings=self.keypair_settings,
259                 priv_key_key=heat_keypair_option)
260             vm_inst_creator = OpenStackVmInstance(
261                 self._os_creds, vm_inst_settings, image_settings,
262                 keypair_settings)
263             out.append(vm_inst_creator)
264             vm_inst_creator.initialize()
265
266         return out
267
268     def _stack_status_check(self, expected_status_code, block, timeout,
269                             poll_interval, fail_status):
270         """
271         Returns true when the stack status returns the value of
272         expected_status_code
273         :param expected_status_code: stack status evaluated with this string
274                                      value
275         :param block: When true, thread will block until active or timeout
276                       value in seconds has been exceeded (False)
277         :param timeout: The timeout value
278         :param poll_interval: The polling interval in seconds
279         :param fail_status: Returns false if the fail_status code is found
280         :return: T/F
281         """
282         # sleep and wait for stack status change
283         if block:
284             start = time.time()
285         else:
286             start = time.time() - timeout
287
288         while timeout > time.time() - start:
289             status = self._status(expected_status_code, fail_status)
290             if status:
291                 logger.debug(
292                     'Stack is active with name - ' + self.stack_settings.name)
293                 return True
294
295             logger.debug('Retry querying stack status in ' + str(
296                 poll_interval) + ' seconds')
297             time.sleep(poll_interval)
298             logger.debug('Stack status query timeout in ' + str(
299                 timeout - (time.time() - start)))
300
301         logger.error(
302             'Timeout checking for stack status for ' + expected_status_code)
303         return False
304
305     def _status(self, expected_status_code, fail_status=STATUS_CREATE_FAILED):
306         """
307         Returns True when active else False
308         :param expected_status_code: stack status evaluated with this string
309         value
310         :return: T/F
311         """
312         status = self.get_status()
313         if not status:
314             logger.warning(
315                 'Cannot stack status for stack with ID - ' + self.__stack.id)
316             return False
317
318         if fail_status and status == fail_status:
319             raise StackError('Stack had an error')
320         logger.debug('Stack status is - ' + status)
321         return status == expected_status_code
322
323
324 class StackSettings:
325     def __init__(self, **kwargs):
326         """
327         Constructor
328         :param name: the stack's name (required)
329         :param template: the heat template in dict() format (required if
330                          template_path attribute is None)
331         :param template_path: the location of the heat template file (required
332                               if template attribute is None)
333         :param env_values: k/v pairs of strings for substitution of template
334                            default values (optional)
335         """
336
337         self.name = kwargs.get('name')
338         self.template = kwargs.get('template')
339         self.template_path = kwargs.get('template_path')
340         self.env_values = kwargs.get('env_values')
341         if 'stack_create_timeout' in kwargs:
342             self.stack_create_timeout = kwargs['stack_create_timeout']
343         else:
344             self.stack_create_timeout = STACK_COMPLETE_TIMEOUT
345
346         if not self.name:
347             raise StackSettingsError('name is required')
348
349         if not self.template and not self.template_path:
350             raise StackSettingsError('A Heat template is required')
351
352     def __eq__(self, other):
353         return (self.name == other.name and
354                 self.template == other.template and
355                 self.template_path == other.template_path and
356                 self.env_values == other.env_values and
357                 self.stack_create_timeout == other.stack_create_timeout)
358
359
360 class StackSettingsError(Exception):
361     """
362     Exception to be thrown when an stack settings are incorrect
363     """
364
365
366 class StackCreationError(Exception):
367     """
368     Exception to be thrown when an stack cannot be created
369     """
370
371
372 class StackError(Exception):
373     """
374     General exception
375     """