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 heatclient.client import Client
19 from heatclient.common.template_format import yaml_loader
20 from novaclient.exceptions import NotFound
21 from oslo_serialization import jsonutils
23 from snaps import file_utils
24 from snaps.domain.stack import Stack, Resource, Output
26 from snaps.openstack.utils import keystone_utils, neutron_utils, nova_utils, \
29 __author__ = 'spisarski'
31 logger = logging.getLogger('heat_utils')
34 def heat_client(os_creds):
36 Retrieves the Heat client
37 :param os_creds: the OpenStack credentials
40 logger.debug('Retrieving Nova Client')
41 return Client(os_creds.heat_api_version,
42 session=keystone_utils.keystone_session(os_creds),
43 region_name=os_creds.region_name)
46 def get_stack(heat_cli, stack_settings=None, stack_name=None):
48 Returns the first domain Stack object found. When stack_setting
49 is not None, the filter created will take the name attribute. When
50 stack_settings is None and stack_name is not, stack_name will be used
51 instead. When both are None, the first stack object received will be
53 :param heat_cli: the OpenStack heat client
54 :param stack_settings: a StackSettings object
55 :param stack_name: the name of the heat stack to return
56 :return: the Stack domain object else None
61 stack_filter['stack_name'] = stack_settings.name
63 stack_filter['stack_name'] = stack_name
65 stacks = heat_cli.stacks.list(**stack_filter)
67 return Stack(name=stack.identifier, stack_id=stack.id)
70 def get_stack_by_id(heat_cli, stack_id):
72 Returns a domain Stack object for a given ID
73 :param heat_cli: the OpenStack heat client
74 :param stack_id: the ID of the heat stack to retrieve
75 :return: the Stack domain object else None
77 stack = heat_cli.stacks.get(stack_id)
78 return Stack(name=stack.identifier, stack_id=stack.id)
81 def get_stack_status(heat_cli, stack_id):
83 Returns the current status of the Heat stack
84 :param heat_cli: the OpenStack heat client
85 :param stack_id: the ID of the heat stack to retrieve
88 return heat_cli.stacks.get(stack_id).stack_status
91 def get_stack_status_reason(heat_cli, stack_id):
93 Returns the current status of the Heat stack
94 :param heat_cli: the OpenStack heat client
95 :param stack_id: the ID of the heat stack to retrieve
96 :return: reason for stack creation failure
98 return heat_cli.stacks.get(stack_id).stack_status_reason
101 def create_stack(heat_cli, stack_settings):
103 Executes an Ansible playbook to the given host
104 :param heat_cli: the OpenStack heat client object
105 :param stack_settings: the stack configuration
106 :return: the Stack domain object
110 if stack_settings.template:
111 args['template'] = stack_settings.template
113 args['template'] = parse_heat_template_str(
114 file_utils.read_file(stack_settings.template_path))
115 args['stack_name'] = stack_settings.name
117 if stack_settings.env_values:
118 args['parameters'] = stack_settings.env_values
120 stack = heat_cli.stacks.create(**args)
122 return get_stack_by_id(heat_cli, stack_id=stack['stack']['id'])
125 def delete_stack(heat_cli, stack):
127 Deletes the Heat stack
128 :param heat_cli: the OpenStack heat client object
129 :param stack: the OpenStack Heat stack object
131 heat_cli.stacks.delete(stack.id)
134 def __get_os_resources(heat_cli, stack):
136 Returns all of the OpenStack resource objects for a given stack
137 :param heat_cli: the OpenStack heat client
138 :param stack: the SNAPS-OO Stack domain object
141 return heat_cli.resources.list(stack.id)
144 def get_resources(heat_cli, stack, res_type=None):
146 Returns all of the OpenStack resource objects for a given stack
147 :param heat_cli: the OpenStack heat client
148 :param stack: the SNAPS-OO Stack domain object
149 :param res_type: the type name to filter
150 :return: a list of Resource domain objects
152 os_resources = __get_os_resources(heat_cli, stack)
156 for os_resource in os_resources:
157 if ((res_type and os_resource.resource_type == res_type)
160 resource_type=os_resource.resource_type,
161 resource_id=os_resource.physical_resource_id))
165 def get_outputs(heat_cli, stack):
167 Returns all of the SNAPS-OO Output domain objects for the defined outputs
169 :param heat_cli: the OpenStack heat client
170 :param stack: the SNAPS-OO Stack domain object
171 :return: a list of Output domain objects
175 os_stack = heat_cli.stacks.get(stack.id)
179 outputs = os_stack.outputs
182 for output in outputs:
183 out.append(Output(**output))
188 def get_stack_networks(heat_cli, neutron, stack):
190 Returns a list of Network domain objects deployed by this stack
191 :param heat_cli: the OpenStack heat client object
192 :param neutron: the OpenStack neutron client object
193 :param stack: the SNAPS-OO Stack domain object
194 :return: a list of Network objects
198 resources = get_resources(heat_cli, stack, 'OS::Neutron::Net')
199 for resource in resources:
200 network = neutron_utils.get_network_by_id(neutron, resource.id)
207 def get_stack_routers(heat_cli, neutron, stack):
209 Returns a list of Network domain objects deployed by this stack
210 :param heat_cli: the OpenStack heat client object
211 :param neutron: the OpenStack neutron client object
212 :param stack: the SNAPS-OO Stack domain object
213 :return: a list of Network objects
217 resources = get_resources(heat_cli, stack, 'OS::Neutron::Router')
218 for resource in resources:
219 router = neutron_utils.get_router_by_id(neutron, resource.id)
226 def get_stack_servers(heat_cli, nova, stack):
228 Returns a list of VMInst domain objects associated with a Stack
229 :param heat_cli: the OpenStack heat client object
230 :param nova: the OpenStack nova client object
231 :param stack: the SNAPS-OO Stack domain object
232 :return: a list of VMInst domain objects
236 resources = get_resources(heat_cli, stack, 'OS::Nova::Server')
237 for resource in resources:
239 server = nova_utils.get_server_object_by_id(nova, resource.id)
243 logger.warn('VmInst cannot be located with ID %s', resource.id)
248 def get_stack_keypairs(heat_cli, nova, stack):
250 Returns a list of Keypair domain objects associated with a Stack
251 :param heat_cli: the OpenStack heat client object
252 :param nova: the OpenStack nova client object
253 :param stack: the SNAPS-OO Stack domain object
254 :return: a list of VMInst domain objects
258 resources = get_resources(heat_cli, stack, 'OS::Nova::KeyPair')
259 for resource in resources:
261 keypair = nova_utils.get_keypair_by_id(nova, resource.id)
265 logger.warn('Keypair cannot be located with ID %s', resource.id)
270 def get_stack_volumes(heat_cli, cinder, stack):
272 Returns an instance of Volume domain objects created by this stack
273 :param heat_cli: the OpenStack heat client object
274 :param cinder: the OpenStack cinder client object
275 :param stack: the SNAPS-OO Stack domain object
276 :return: a list of Volume domain objects
280 resources = get_resources(heat_cli, stack, 'OS::Cinder::Volume')
281 for resource in resources:
283 server = cinder_utils.get_volume_by_id(cinder, resource.id)
287 logger.warn('Volume cannot be located with ID %s', resource.id)
292 def get_stack_volume_types(heat_cli, cinder, stack):
294 Returns an instance of VolumeType domain objects created by this stack
295 :param heat_cli: the OpenStack heat client object
296 :param cinder: the OpenStack cinder client object
297 :param stack: the SNAPS-OO Stack domain object
298 :return: a list of VolumeType domain objects
302 resources = get_resources(heat_cli, stack, 'OS::Cinder::VolumeType')
303 for resource in resources:
305 vol_type = cinder_utils.get_volume_type_by_id(cinder, resource.id)
309 logger.warn('VolumeType cannot be located with ID %s', resource.id)
314 def get_stack_flavors(heat_cli, nova, stack):
316 Returns an instance of Flavor SNAPS domain object for each flavor created
318 :param heat_cli: the OpenStack heat client object
319 :param nova: the OpenStack cinder client object
320 :param stack: the SNAPS-OO Stack domain object
321 :return: a list of Volume domain objects
325 resources = get_resources(heat_cli, stack, 'OS::Nova::Flavor')
326 for resource in resources:
328 flavor = nova_utils.get_flavor_by_id(nova, resource.id)
332 logger.warn('Flavor cannot be located with ID %s', resource.id)
337 def parse_heat_template_str(tmpl_str):
339 Takes a heat template string, performs some simple validation and returns a
340 dict containing the parsed structure. This function supports both JSON and
341 YAML Heat template formats.
343 if tmpl_str.startswith('{'):
344 tpl = jsonutils.loads(tmpl_str)
347 tpl = yaml.load(tmpl_str, Loader=yaml_loader)
348 except yaml.YAMLError as yea:
349 raise ValueError(yea)
353 # Looking for supported version keys in the loaded template
354 if not ('HeatTemplateFormatVersion' in tpl or
355 'heat_template_version' in tpl or
356 'AWSTemplateFormatVersion' in tpl):
357 raise ValueError("Template format version not found.")