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.
19 from heatclient.exc import HTTPNotFound
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
25 from snaps.openstack.create_network import OpenStackNetwork
26 from snaps.openstack.utils import heat_utils, neutron_utils
28 __author__ = 'spisarski'
30 logger = logging.getLogger('create_stack')
32 STACK_DELETE_TIMEOUT = 1200
33 STACK_COMPLETE_TIMEOUT = 1200
35 STATUS_CREATE_FAILED = 'CREATE_FAILED'
36 STATUS_CREATE_COMPLETE = 'CREATE_COMPLETE'
37 STATUS_DELETE_COMPLETE = 'DELETE_COMPLETE'
38 STATUS_DELETE_FAILED = 'DELETE_FAILED'
41 class OpenStackHeatStack(OpenStackCloudObject, object):
43 Class responsible for managing a heat stack in OpenStack
46 def __init__(self, os_creds, stack_settings, image_settings=None,
47 keypair_settings=None):
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
60 super(self.__class__, self).__init__(os_creds)
62 self.stack_settings = stack_settings
65 self.image_settings = image_settings
67 self.image_settings = None
70 self.keypair_settings = keypair_settings
72 self.keypair_settings = None
75 self.__heat_cli = None
79 Loads the existing heat stack
80 :return: The Stack domain object or None
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)
86 logger.info('Found stack with name - ' + self.stack_settings.name)
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
98 logger.info('Found stack with name - ' + self.stack_settings.name)
101 self.__stack = heat_utils.create_stack(self.__heat_cli,
104 'Created stack with name - ' + self.stack_settings.name)
105 if self.__stack and self.stack_complete(block=True):
107 'Stack is now active with name - ' +
108 self.stack_settings.name)
111 status = heat_utils.get_stack_status_reason(self.__heat_cli,
114 'ERROR: STACK CREATION FAILED: ' + status)
115 raise StackCreationError(
116 'Failure while creating stack')
120 Cleanse environment of all artifacts
125 logger.info('Deleting stack - %s' + self.__stack.name)
126 heat_utils.delete_stack(self.__heat_cli, self.__stack)
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)
135 for vm_inst_creator in self.get_vm_inst_creators():
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)
142 logger.warn('Unexpected error deleting VM - %s ',
143 vm_inst_creator.get_vm_inst().name)
145 logger.info('Attempting to delete again stack - %s',
149 heat_utils.delete_stack(self.__heat_cli, self.__stack)
150 deleted = self.stack_deleted(block=True)
153 'Stack could not be deleted ' + self.__stack.name)
161 Returns the domain Stack object as it was populated when create() was
167 def get_outputs(self):
169 Returns the list of outputs as contained on the OpenStack Heat Stack
173 return heat_utils.get_outputs(self.__heat_cli, self.__stack)
175 def get_status(self):
177 Returns the list of outputs as contained on the OpenStack Heat Stack
181 return heat_utils.get_stack_status(self.__heat_cli, self.__stack.id)
183 def stack_complete(self, block=False, timeout=None,
184 poll_interval=POLL_INTERVAL):
186 Returns true when the stack status returns the value of
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
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)
199 def stack_deleted(self, block=False, timeout=STACK_DELETE_TIMEOUT,
200 poll_interval=POLL_INTERVAL):
202 Returns true when the stack status returns the value of
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
210 return self._stack_status_check(STATUS_DELETE_COMPLETE, block, timeout,
211 poll_interval, STATUS_DELETE_FAILED)
213 def get_network_creators(self):
215 Returns a list of network creator objects as configured by the heat
217 :return: list() of OpenStackNetwork objects
220 neutron = neutron_utils.neutron_client(self._os_creds)
223 stack_networks = heat_utils.get_stack_networks(
224 self.__heat_cli, neutron, self.__stack)
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()
235 def get_vm_inst_creators(self, heat_keypair_option=None):
237 Returns a list of VM Instance creator objects as configured by the heat
239 :return: list() of OpenStackVmInstance objects
243 nova = nova_utils.nova_client(self._os_creds)
245 stack_servers = heat_utils.get_stack_servers(
246 self.__heat_cli, nova, self.__stack)
248 neutron = neutron_utils.neutron_client(self._os_creds)
249 glance = glance_utils.glance_client(self._os_creds)
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,
263 out.append(vm_inst_creator)
264 vm_inst_creator.initialize()
268 def _stack_status_check(self, expected_status_code, block, timeout,
269 poll_interval, fail_status):
271 Returns true when the stack status returns the value of
273 :param expected_status_code: stack status evaluated with this string
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
282 # sleep and wait for stack status change
286 start = time.time() - timeout
288 while timeout > time.time() - start:
289 status = self._status(expected_status_code, fail_status)
292 'Stack is active with name - ' + self.stack_settings.name)
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)))
302 'Timeout checking for stack status for ' + expected_status_code)
305 def _status(self, expected_status_code, fail_status=STATUS_CREATE_FAILED):
307 Returns True when active else False
308 :param expected_status_code: stack status evaluated with this string
312 status = self.get_status()
315 'Cannot stack status for stack with ID - ' + self.__stack.id)
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
325 def __init__(self, **kwargs):
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)
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']
344 self.stack_create_timeout = STACK_COMPLETE_TIMEOUT
347 raise StackSettingsError('name is required')
349 if not self.template and not self.template_path:
350 raise StackSettingsError('A Heat template is required')
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)
360 class StackSettingsError(Exception):
362 Exception to be thrown when an stack settings are incorrect
366 class StackCreationError(Exception):
368 Exception to be thrown when an stack cannot be created
372 class StackError(Exception):