1 #############################################################################
2 # Copyright (c) 2015-2017 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 """Heat template and stack management"""
12 from __future__ import absolute_import
13 from __future__ import print_function
14 from six.moves import range
27 from oslo_utils import encodeutils
29 import yardstick.common.openstack_utils as op_utils
30 from yardstick.common import template_format
32 log = logging.getLogger(__name__)
35 HEAT_KEY_UUID_LENGTH = 8
37 PROVIDER_SRIOV = "sriov"
40 def get_short_key_uuid(uuid):
41 return str(uuid)[:HEAT_KEY_UUID_LENGTH]
44 class HeatObject(object):
45 """base class for template and stack"""
48 self._heat_client = None
52 def heat_client(self):
53 """returns a heat client instance"""
55 if self._heat_client is None:
56 sess = op_utils.get_session()
57 heat_endpoint = op_utils.get_endpoint(service_type='orchestration')
58 self._heat_client = heatclient.client.Client(
59 op_utils.get_heat_api_version(),
60 endpoint=heat_endpoint, session=sess)
62 return self._heat_client
65 """returns stack state as a string"""
66 heat_client = self.heat_client
67 stack = heat_client.stacks.get(self.uuid)
68 return stack.stack_status
71 class HeatStack(HeatObject):
72 """Represents a Heat stack (deployed template) """
75 def __init__(self, name):
76 super(HeatStack, self).__init__()
80 HeatStack.stacks.append(self)
84 """check if any stack has been deployed"""
85 return len(HeatStack.stacks) > 0
88 """deletes a stack from the target cloud using heat"""
92 log.info("Deleting stack '%s', uuid:%s", self.name, self.uuid)
93 heat = self.heat_client
94 template = heat.stacks.get(self.uuid)
95 start_time = time.time()
98 for status in iter(self.status, u'DELETE_COMPLETE'):
99 log.debug("stack state %s", status)
100 if status == u'DELETE_FAILED':
102 heat.stacks.get(self.uuid).stack_status_reason)
106 end_time = time.time()
107 log.info("Deleted stack '%s' in %d secs", self.name,
108 end_time - start_time)
111 def delete(self, block=True, retries=3):
112 """deletes a stack in the target cloud using heat (with retry)
113 Sometimes delete fail with "InternalServerError" and the next attempt
114 succeeds. So it is worthwhile to test a couple of times.
116 if self.uuid is None:
123 for _ in range(retries):
127 except RuntimeError as err:
128 log.warning(err.args)
131 # if still not deleted try once more and let it fail everything
132 if self.uuid is not None:
135 HeatStack.stacks.remove(self)
139 for stack in HeatStack.stacks[:]:
144 raise RuntimeError("not implemented")
147 class HeatTemplate(HeatObject):
148 """Describes a Heat template and a method to deploy template to a stack"""
150 DESCRIPTION_TEMPLATE = """\
151 Stack built by the yardstick framework for %s on host %s %s.
152 All referred generated resources are prefixed with the template
156 def _init_template(self):
157 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
159 'heat_template_version': '2013-05-23',
160 'description': self.DESCRIPTION_TEMPLATE % (
162 socket.gethostname(),
170 # short hand for resources part of template
171 self.resources = self._template['resources']
173 def __init__(self, name, template_file=None, heat_parameters=None):
174 super(HeatTemplate, self).__init__()
176 self.state = "NOT_CREATED"
177 self.keystone_client = None
178 self.heat_parameters = {}
180 # heat_parameters is passed to heat in stack create, empty dict when
181 # yardstick creates the template (no get_param in resources part)
183 self.heat_parameters = heat_parameters
186 with open(template_file) as stream:
187 print("Parsing external template:", template_file)
188 template_str = stream.read()
189 self._template = template_format.parse(template_str)
190 self._parameters = heat_parameters
192 self._init_template()
194 # holds results of requested output after deployment
197 log.debug("template object '%s' created", name)
199 def add_flavor(self, name, vcpus=1, ram=1024, disk=1, ephemeral=0,
200 is_public=True, rxtx_factor=1.0, swap=0,
202 """add to the template a Flavor description"""
205 log.debug("adding Nova::Flavor '%s' vcpus '%d' ram '%d' disk '%d' " +
206 "ephemeral '%d' is_public '%s' rxtx_factor '%d' " +
207 "swap '%d' extra_specs '%s' ",
208 name, vcpus, ram, disk, ephemeral, is_public,
209 rxtx_factor, swap, str(extra_specs))
212 assert isinstance(extra_specs, collections.Mapping)
214 self.resources[name] = {
215 'type': 'OS::Nova::Flavor',
216 'properties': {'name': name,
221 'rxtx_factor': rxtx_factor,
223 'is_public': is_public,
224 'ephemeral': ephemeral,
225 'extra_specs': extra_specs}
228 self._template['outputs'][name] = {
229 'description': 'Flavor %s ID' % name,
230 'value': {'get_resource': name}
233 def add_network(self, name, physical_network='physnet1', provider=None,
234 segmentation_id=None, port_security_enabled=None):
235 """add to the template a Neutron Net"""
236 log.debug("adding Neutron::Net '%s'", name)
238 self.resources[name] = {
239 'type': 'OS::Neutron::Net',
245 self.resources[name] = {
246 'type': 'OS::Neutron::ProviderNet',
249 'network_type': 'vlan',
250 'physical_network': physical_network,
254 self.resources[name]['properties']['segmentation_id'] = segmentation_id
255 # if port security is not defined then don't add to template:
256 # some deployments don't have port security plugin installed
257 if port_security_enabled is not None:
258 self.resources[name]['properties']['port_security_enabled'] = port_security_enabled
260 def add_server_group(self, name, policies): # pragma: no cover
261 """add to the template a ServerGroup"""
262 log.debug("adding Nova::ServerGroup '%s'", name)
263 policies = policies if isinstance(policies, list) else [policies]
264 self.resources[name] = {
265 'type': 'OS::Nova::ServerGroup',
266 'properties': {'name': name,
267 'policies': policies}
270 def add_subnet(self, name, network, cidr, enable_dhcp='true', gateway_ip=None):
271 """add to the template a Neutron Subnet
273 log.debug("adding Neutron::Subnet '%s' in network '%s', cidr '%s'",
275 self.resources[name] = {
276 'type': 'OS::Neutron::Subnet',
277 'depends_on': network,
281 'network_id': {'get_resource': network},
282 'enable_dhcp': enable_dhcp,
285 if gateway_ip is not None:
286 self.resources[name]['properties']['gateway_ip'] = gateway_ip
288 self._template['outputs'][name] = {
289 'description': 'subnet %s ID' % name,
290 'value': {'get_resource': name}
292 self._template['outputs'][name + "-cidr"] = {
293 'description': 'subnet %s cidr' % name,
294 'value': {'get_attr': [name, 'cidr']}
296 self._template['outputs'][name + "-gateway_ip"] = {
297 'description': 'subnet %s gateway_ip' % name,
298 'value': {'get_attr': [name, 'gateway_ip']}
301 def add_router(self, name, ext_gw_net, subnet_name):
302 """add to the template a Neutron Router and interface"""
303 log.debug("adding Neutron::Router:'%s', gw-net:'%s'", name, ext_gw_net)
304 self.resources[name] = {
305 'type': 'OS::Neutron::Router',
306 'depends_on': [subnet_name],
309 'external_gateway_info': {
310 'network': ext_gw_net
315 def add_router_interface(self, name, router_name, subnet_name):
316 """add to the template a Neutron RouterInterface and interface"""
317 log.debug("adding Neutron::RouterInterface '%s' router:'%s', "
318 "subnet:'%s'", name, router_name, subnet_name)
319 self.resources[name] = {
320 'type': 'OS::Neutron::RouterInterface',
321 'depends_on': [router_name, subnet_name],
323 'router_id': {'get_resource': router_name},
324 'subnet_id': {'get_resource': subnet_name}
328 def add_port(self, name, network_name, subnet_name, sec_group_id=None, provider=None,
329 allowed_address_pairs=None):
330 """add to the template a named Neutron Port
332 log.debug("adding Neutron::Port '%s', network:'%s', subnet:'%s', "
333 "secgroup:%s", name, network_name, subnet_name, sec_group_id)
334 self.resources[name] = {
335 'type': 'OS::Neutron::Port',
336 'depends_on': [subnet_name],
339 'fixed_ips': [{'subnet': {'get_resource': subnet_name}}],
340 'network_id': {'get_resource': network_name},
341 'replacement_policy': 'AUTO',
345 if provider == PROVIDER_SRIOV:
346 self.resources[name]['properties']['binding:vnic_type'] = \
350 self.resources[name]['depends_on'].append(sec_group_id)
351 self.resources[name]['properties']['security_groups'] = \
354 if allowed_address_pairs:
355 self.resources[name]['properties'][
356 'allowed_address_pairs'] = allowed_address_pairs
358 self._template['outputs'][name] = {
359 'description': 'Address for interface %s' % name,
360 'value': {'get_attr': [name, 'fixed_ips', 0, 'ip_address']}
362 self._template['outputs'][name + "-subnet_id"] = {
363 'description': 'Address for interface %s' % name,
364 'value': {'get_attr': [name, 'fixed_ips', 0, 'subnet_id']}
366 self._template['outputs'][name + "-mac_address"] = {
367 'description': 'MAC Address for interface %s' % name,
368 'value': {'get_attr': [name, 'mac_address']}
370 self._template['outputs'][name + "-device_id"] = {
371 'description': 'Device ID for interface %s' % name,
372 'value': {'get_attr': [name, 'device_id']}
374 self._template['outputs'][name + "-network_id"] = {
375 'description': 'Network ID for interface %s' % name,
376 'value': {'get_attr': [name, 'network_id']}
379 def add_floating_ip(self, name, network_name, port_name, router_if_name,
381 """add to the template a Nova FloatingIP resource
382 see: https://bugs.launchpad.net/heat/+bug/1299259
384 log.debug("adding Nova::FloatingIP '%s', network '%s', port '%s', "
385 "rif '%s'", name, network_name, port_name, router_if_name)
387 self.resources[name] = {
388 'type': 'OS::Nova::FloatingIP',
389 'depends_on': [port_name, router_if_name],
396 self.resources[name]["depends_on"].append(secgroup_name)
398 self._template['outputs'][name] = {
399 'description': 'floating ip %s' % name,
400 'value': {'get_attr': [name, 'ip']}
403 def add_floating_ip_association(self, name, floating_ip_name, port_name):
404 """add to the template a Nova FloatingIP Association resource
406 log.debug("adding Nova::FloatingIPAssociation '%s', server '%s', "
407 "floating_ip '%s'", name, port_name, floating_ip_name)
409 self.resources[name] = {
410 'type': 'OS::Neutron::FloatingIPAssociation',
411 'depends_on': [port_name],
413 'floatingip_id': {'get_resource': floating_ip_name},
414 'port_id': {'get_resource': port_name}
418 def add_keypair(self, name, key_uuid):
419 """add to the template a Nova KeyPair"""
420 log.debug("adding Nova::KeyPair '%s'", name)
421 self.resources[name] = {
422 'type': 'OS::Nova::KeyPair',
425 # resource_string returns bytes, so we must decode to unicode
426 'public_key': encodeutils.safe_decode(
427 pkg_resources.resource_string(
428 'yardstick.resources',
429 'files/yardstick_key-' +
430 get_short_key_uuid(key_uuid) + '.pub'),
435 def add_servergroup(self, name, policy):
436 """add to the template a Nova ServerGroup"""
437 log.debug("adding Nova::ServerGroup '%s', policy '%s'", name, policy)
438 if policy not in ["anti-affinity", "affinity"]:
439 raise ValueError(policy)
441 self.resources[name] = {
442 'type': 'OS::Nova::ServerGroup',
449 self._template['outputs'][name] = {
450 'description': 'ID Server Group %s' % name,
451 'value': {'get_resource': name}
454 def add_security_group(self, name):
455 """add to the template a Neutron SecurityGroup"""
456 log.debug("adding Neutron::SecurityGroup '%s'", name)
457 self.resources[name] = {
458 'type': 'OS::Neutron::SecurityGroup',
461 'description': "Group allowing icmp and upd/tcp on all ports",
463 {'remote_ip_prefix': '0.0.0.0/0',
465 'port_range_min': '1',
466 'port_range_max': '65535'},
467 {'remote_ip_prefix': '0.0.0.0/0',
469 'port_range_min': '1',
470 'port_range_max': '65535'},
471 {'remote_ip_prefix': '0.0.0.0/0',
477 self._template['outputs'][name] = {
478 'description': 'ID of Security Group',
479 'value': {'get_resource': name}
482 def add_server(self, name, image, flavor, flavors, ports=None,
483 networks=None, scheduler_hints=None, user=None,
484 key_name=None, user_data=None, metadata=None,
485 additional_properties=None):
486 """add to the template a Nova Server"""
487 log.debug("adding Nova::Server '%s', image '%s', flavor '%s', "
488 "ports %s", name, image, flavor, ports)
490 self.resources[name] = {
491 'type': 'OS::Nova::Server',
495 server_properties = {
499 'networks': [] # list of dictionaries
502 if flavor in flavors:
503 self.resources[name]['depends_on'].append(flavor)
504 server_properties["flavor"] = {'get_resource': flavor}
506 server_properties["flavor"] = flavor
509 server_properties['admin_user'] = user
512 self.resources[name]['depends_on'].append(key_name)
513 server_properties['key_name'] = {'get_resource': key_name}
516 self.resources[name]['depends_on'].extend(ports)
518 server_properties['networks'].append(
519 {'port': {'get_resource': port}}
523 for i, _ in enumerate(networks):
524 server_properties['networks'].append({'network': networks[i]})
527 server_properties['scheduler_hints'] = scheduler_hints
530 server_properties['user_data'] = user_data
533 assert isinstance(metadata, collections.Mapping)
534 server_properties['metadata'] = metadata
536 if additional_properties:
537 assert isinstance(additional_properties, collections.Mapping)
538 for prop in additional_properties:
539 server_properties[prop] = additional_properties[prop]
541 server_properties['config_drive'] = True
543 self.resources[name]['properties'] = server_properties
545 self._template['outputs'][name] = {
546 'description': 'VM UUID',
547 'value': {'get_resource': name}
550 HEAT_WAIT_LOOP_INTERVAL = 2
551 HEAT_CREATE_COMPLETE_STATUS = u'CREATE_COMPLETE'
553 def create(self, block=True, timeout=3600):
555 creates a template in the target cloud using heat
556 returns a dict with the requested output values from the template
558 :param block: Wait for Heat create to finish
560 :param: timeout: timeout in seconds for Heat create, default 3600s
563 log.info("Creating stack '%s'", self.name)
565 # create stack early to support cleanup, e.g. ctrl-c while waiting
566 stack = HeatStack(self.name)
568 heat_client = self.heat_client
569 start_time = time.time()
570 stack.uuid = self.uuid = heat_client.stacks.create(
571 stack_name=self.name, template=self._template,
572 parameters=self.heat_parameters)['stack']['id']
575 self.outputs = stack.outputs = {}
576 end_time = time.time()
577 log.info("Created stack '%s' in %.3e secs",
578 self.name, end_time - start_time)
581 time_limit = start_time + timeout
582 for status in iter(self.status, self.HEAT_CREATE_COMPLETE_STATUS):
583 log.debug("stack state %s", status)
584 if status == u'CREATE_FAILED':
585 stack_status_reason = heat_client.stacks.get(self.uuid).stack_status_reason
586 heat_client.stacks.delete(self.uuid)
587 raise RuntimeError(stack_status_reason)
588 if time.time() > time_limit:
589 raise RuntimeError("Heat stack create timeout")
591 time.sleep(self.HEAT_WAIT_LOOP_INTERVAL)
593 end_time = time.time()
594 outputs = heat_client.stacks.get(self.uuid).outputs
595 log.info("Created stack '%s' in %.3e secs",
596 self.name, end_time - start_time)
598 # keep outputs as unicode
599 self.outputs = {output["output_key"]: output["output_value"] for output
602 stack.outputs = self.outputs