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
20 from snaps.openstack.utils import heat_utils
22 __author__ = 'spisarski'
24 logger = logging.getLogger('create_stack')
26 STACK_COMPLETE_TIMEOUT = 1200
28 STATUS_CREATE_FAILED = 'CREATE_FAILED'
29 STATUS_CREATE_COMPLETE = 'CREATE_COMPLETE'
30 STATUS_DELETE_COMPLETE = 'DELETE_COMPLETE'
33 class OpenStackHeatStack:
35 Class responsible for creating an heat stack in OpenStack
38 def __init__(self, os_creds, stack_settings):
41 :param os_creds: The OpenStack connection credentials
42 :param stack_settings: The stack settings
45 self.__os_creds = os_creds
46 self.stack_settings = stack_settings
48 self.__heat_cli = None
50 def create(self, cleanup=False):
52 Creates the heat stack in OpenStack if it does not already exist and
53 returns the domain Stack object
54 :param cleanup: Denotes whether or not this is being called for cleanup
55 :return: The OpenStack Stack object
57 self.__heat_cli = heat_utils.heat_client(self.__os_creds)
58 self.__stack = heat_utils.get_stack_by_name(self.__heat_cli,
59 self.stack_settings.name)
61 logger.info('Found stack with name - ' + self.stack_settings.name)
64 self.__stack = heat_utils.create_stack(self.__heat_cli,
67 'Created stack with name - ' + self.stack_settings.name)
68 if self.__stack and self.stack_complete(block=True):
70 'Stack is now active with name - ' +
71 self.stack_settings.name)
74 raise StackCreationError(
75 'Stack was not created or activated in the alloted amount '
78 logger.info('Did not create stack due to cleanup mode')
84 Cleanse environment of all artifacts
89 heat_utils.delete_stack(self.__heat_cli, self.__stack)
97 Returns the domain Stack object as it was populated when create() was
103 def get_outputs(self):
105 Returns the list of outputs as contained on the OpenStack Heat Stack
109 return heat_utils.get_stack_outputs(self.__heat_cli, self.__stack.id)
111 def get_status(self):
113 Returns the list of outputs as contained on the OpenStack Heat Stack
117 return heat_utils.get_stack_status(self.__heat_cli, self.__stack.id)
119 def stack_complete(self, block=False, timeout=None,
120 poll_interval=POLL_INTERVAL):
122 Returns true when the stack status returns the value of
124 :param block: When true, thread will block until active or timeout
125 value in seconds has been exceeded (False)
126 :param timeout: The timeout value
127 :param poll_interval: The polling interval in seconds
131 timeout = self.stack_settings.stack_create_timeout
132 return self._stack_status_check(STATUS_CREATE_COMPLETE, block, timeout,
135 def _stack_status_check(self, expected_status_code, block, timeout,
138 Returns true when the stack status returns the value of
140 :param expected_status_code: stack status evaluated with this string
142 :param block: When true, thread will block until active or timeout
143 value in seconds has been exceeded (False)
144 :param timeout: The timeout value
145 :param poll_interval: The polling interval in seconds
148 # sleep and wait for stack status change
152 start = time.time() - timeout
154 while timeout > time.time() - start:
155 status = self._status(expected_status_code)
158 'Stack is active with name - ' + self.stack_settings.name)
161 logger.debug('Retry querying stack status in ' + str(
162 poll_interval) + ' seconds')
163 time.sleep(poll_interval)
164 logger.debug('Stack status query timeout in ' + str(
165 timeout - (time.time() - start)))
168 'Timeout checking for stack status for ' + expected_status_code)
171 def _status(self, expected_status_code):
173 Returns True when active else False
174 :param expected_status_code: stack status evaluated with this string
178 status = self.get_status()
181 'Cannot stack status for stack with ID - ' + self.__stack.id)
184 if status == STATUS_CREATE_FAILED:
185 raise StackCreationError('Stack had an error during deployment')
186 logger.debug('Stack status is - ' + status)
187 return status == expected_status_code
191 def __init__(self, **kwargs):
194 :param name: the stack's name (required)
195 :param template: the heat template in dict() format (required if
196 template_path attribute is None)
197 :param template_path: the location of the heat template file (required
198 if template attribute is None)
199 :param env_values: k/v pairs of strings for substitution of template
200 default values (optional)
203 self.name = kwargs.get('name')
204 self.template = kwargs.get('template')
205 self.template_path = kwargs.get('template_path')
206 self.env_values = kwargs.get('env_values')
207 if 'stack_create_timeout' in kwargs:
208 self.stack_create_timeout = kwargs['stack_create_timeout']
210 self.stack_create_timeout = STACK_COMPLETE_TIMEOUT
213 raise StackSettingsError('name is required')
215 if not self.template and not self.template_path:
216 raise StackSettingsError('A Heat template is required')
219 class StackSettingsError(Exception):
221 Exception to be thrown when an stack settings are incorrect
224 def __init__(self, message):
225 Exception.__init__(self, message)
228 class StackCreationError(Exception):
230 Exception to be thrown when an stack cannot be created
233 def __init__(self, message):
234 Exception.__init__(self, message)