X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Forchestrator%2Fheat.py;h=a99d4631d550cc318049c05fc8c94debdc43669b;hb=cd5bb5f2dd2ec89e6c814c2771402aa8e39e10ef;hp=8d535c252d4de7a9425c779dadb415a0d5c9cc98;hpb=9d36842e3966185e97cc5732aa7a0edd2050bfe2;p=yardstick.git diff --git a/yardstick/orchestrator/heat.py b/yardstick/orchestrator/heat.py index 8d535c252..a99d4631d 100644 --- a/yardstick/orchestrator/heat.py +++ b/yardstick/orchestrator/heat.py @@ -1,5 +1,5 @@ ############################################################################## -# Copyright (c) 2015 Ericsson AB and others. +# Copyright (c) 2015-2017 Ericsson AB and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 @@ -7,66 +7,69 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -""" Heat template and stack management -""" +"""Heat template and stack management""" -import os -import time +from __future__ import absolute_import +from __future__ import print_function +from six.moves import range + +import collections import datetime import getpass -import socket import logging + +import socket +import time + +import heatclient import pkg_resources -import json -import heatclient.client -import keystoneclient -from yardstick.common import template_format +from oslo_utils import encodeutils +import yardstick.common.openstack_utils as op_utils +from yardstick.common import template_format log = logging.getLogger(__name__) +HEAT_KEY_UUID_LENGTH = 8 + +PROVIDER_SRIOV = "sriov" + + +def get_short_key_uuid(uuid): + return str(uuid)[:HEAT_KEY_UUID_LENGTH] + + class HeatObject(object): - ''' base class for template and stack''' + """base class for template and stack""" + def __init__(self): - self._keystone_client = None self._heat_client = None self.uuid = None - def _get_keystone_client(self): - '''returns a keystone client instance''' - - if self._keystone_client is None: - self._keystone_client = keystoneclient.v2_0.client.Client( - auth_url=os.environ.get('OS_AUTH_URL'), - username=os.environ.get('OS_USERNAME'), - password=os.environ.get('OS_PASSWORD'), - tenant_name=os.environ.get('OS_TENANT_NAME')) - - return self._keystone_client - - def _get_heat_client(self): - '''returns a heat client instance''' + @property + def heat_client(self): + """returns a heat client instance""" if self._heat_client is None: - keystone = self._get_keystone_client() - heat_endpoint = keystone.service_catalog.url_for( - service_type='orchestration') + sess = op_utils.get_session() + heat_endpoint = op_utils.get_endpoint(service_type='orchestration') self._heat_client = heatclient.client.Client( - '1', endpoint=heat_endpoint, token=keystone.auth_token) + op_utils.get_heat_api_version(), + endpoint=heat_endpoint, session=sess) return self._heat_client def status(self): - '''returns stack state as a string''' - heat = self._get_heat_client() - stack = heat.stacks.get(self.uuid) - return getattr(stack, 'stack_status') + """returns stack state as a string""" + heat_client = self.heat_client + stack = heat_client.stacks.get(self.uuid) + return stack.stack_status class HeatStack(HeatObject): - ''' Represents a Heat stack (deployed template) ''' + """Represents a Heat stack (deployed template) """ stacks = [] def __init__(self, name): @@ -78,29 +81,27 @@ class HeatStack(HeatObject): @staticmethod def stacks_exist(): - '''check if any stack has been deployed''' + """check if any stack has been deployed""" return len(HeatStack.stacks) > 0 def _delete(self): - '''deletes a stack from the target cloud using heat''' + """deletes a stack from the target cloud using heat""" if self.uuid is None: return log.info("Deleting stack '%s', uuid:%s", self.name, self.uuid) - heat = self._get_heat_client() + heat = self.heat_client template = heat.stacks.get(self.uuid) start_time = time.time() template.delete() - status = self.status() - while status != u'DELETE_COMPLETE': + for status in iter(self.status, u'DELETE_COMPLETE'): log.debug("stack state %s", status) if status == u'DELETE_FAILED': raise RuntimeError( heat.stacks.get(self.uuid).stack_status_reason) time.sleep(2) - status = self.status() end_time = time.time() log.info("Deleted stack '%s' in %d secs", self.name, @@ -108,10 +109,10 @@ class HeatStack(HeatObject): self.uuid = None def delete(self, block=True, retries=3): - '''deletes a stack in the target cloud using heat (with retry) + """deletes a stack in the target cloud using heat (with retry) Sometimes delete fail with "InternalServerError" and the next attempt succeeds. So it is worthwhile to test a couple of times. - ''' + """ if self.uuid is None: return @@ -119,15 +120,13 @@ class HeatStack(HeatObject): self._delete() return - i = 0 - while i < retries: + for _ in range(retries): try: self._delete() break except RuntimeError as err: - log.warn(err.args) + log.warning(err.args) time.sleep(2) - i += 1 # if still not deleted try once more and let it fail everything if self.uuid is not None: @@ -141,35 +140,41 @@ class HeatStack(HeatObject): stack.delete() def update(self): - '''update a stack''' + """update a stack""" raise RuntimeError("not implemented") class HeatTemplate(HeatObject): - '''Describes a Heat template and a method to deploy template to a stack''' + """Describes a Heat template and a method to deploy template to a stack""" - def _init_template(self): - self._template = {} - self._template['heat_template_version'] = '2013-05-23' + DESCRIPTION_TEMPLATE = """\ +Stack built by the yardstick framework for %s on host %s %s. +All referred generated resources are prefixed with the template +name (i.e. %s).\ +""" + def _init_template(self): timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self._template['description'] = \ - '''Stack built by the yardstick framework for %s on host %s %s. - All referred generated resources are prefixed with the template - name (i.e. %s).''' % (getpass.getuser(), socket.gethostname(), - timestamp, self.name) + self._template = { + 'heat_template_version': '2013-05-23', + 'description': self.DESCRIPTION_TEMPLATE % ( + getpass.getuser(), + socket.gethostname(), + timestamp, + self.name + ), + 'resources': {}, + 'outputs': {} + } # short hand for resources part of template - self.resources = self._template['resources'] = {} - - self._template['outputs'] = {} + self.resources = self._template['resources'] def __init__(self, name, template_file=None, heat_parameters=None): super(HeatTemplate, self).__init__() self.name = name self.state = "NOT_CREATED" self.keystone_client = None - self.heat_client = None self.heat_parameters = {} # heat_parameters is passed to heat in stack create, empty dict when @@ -179,9 +184,9 @@ class HeatTemplate(HeatObject): if template_file: with open(template_file) as stream: - print "Parsing external template:", template_file + print("Parsing external template:", template_file) template_str = stream.read() - self._template = template_format.parse(template_str) + self._template = template_format.parse(template_str) self._parameters = heat_parameters else: self._init_template() @@ -191,16 +196,70 @@ class HeatTemplate(HeatObject): log.debug("template object '%s' created", name) - def add_network(self, name): - '''add to the template a Neutron Net''' + def add_flavor(self, name, vcpus=1, ram=1024, disk=1, ephemeral=0, + is_public=True, rxtx_factor=1.0, swap=0, + extra_specs=None): + """add to the template a Flavor description""" + if name is None: + name = 'auto' + log.debug("adding Nova::Flavor '%s' vcpus '%d' ram '%d' disk '%d' " + + "ephemeral '%d' is_public '%s' rxtx_factor '%d' " + + "swap '%d' extra_specs '%s' ", + name, vcpus, ram, disk, ephemeral, is_public, + rxtx_factor, swap, str(extra_specs)) + + if extra_specs: + assert isinstance(extra_specs, collections.Mapping) + + self.resources[name] = { + 'type': 'OS::Nova::Flavor', + 'properties': {'name': name, + 'disk': disk, + 'vcpus': vcpus, + 'swap': swap, + 'flavorid': name, + 'rxtx_factor': rxtx_factor, + 'ram': ram, + 'is_public': is_public, + 'ephemeral': ephemeral, + 'extra_specs': extra_specs} + } + + self._template['outputs'][name] = { + 'description': 'Flavor %s ID' % name, + 'value': {'get_resource': name} + } + + def add_network(self, name, physical_network='physnet1', provider=None): + """add to the template a Neutron Net""" log.debug("adding Neutron::Net '%s'", name) + if provider is None: + self.resources[name] = { + 'type': 'OS::Neutron::Net', + 'properties': {'name': name} + } + else: + self.resources[name] = { + 'type': 'OS::Neutron::ProviderNet', + 'properties': { + 'name': name, + 'network_type': 'vlan', + 'physical_network': physical_network + } + } + + def add_server_group(self, name, policies): # pragma: no cover + """add to the template a ServerGroup""" + log.debug("adding Nova::ServerGroup '%s'", name) + policies = policies if isinstance(policies, list) else [policies] self.resources[name] = { - 'type': 'OS::Neutron::Net', - 'properties': {'name': name} + 'type': 'OS::Nova::ServerGroup', + 'properties': {'name': name, + 'policies': policies} } def add_subnet(self, name, network, cidr): - '''add to the template a Neutron Subnet''' + """add to the template a Neutron Subnet""" log.debug("adding Neutron::Subnet '%s' in network '%s', cidr '%s'", name, network, cidr) self.resources[name] = { @@ -217,11 +276,18 @@ class HeatTemplate(HeatObject): 'description': 'subnet %s ID' % name, 'value': {'get_resource': name} } + self._template['outputs'][name + "-cidr"] = { + 'description': 'subnet %s cidr' % name, + 'value': {'get_attr': [name, 'cidr']} + } + self._template['outputs'][name + "-gateway_ip"] = { + 'description': 'subnet %s gateway_ip' % name, + 'value': {'get_attr': [name, 'gateway_ip']} + } def add_router(self, name, ext_gw_net, subnet_name): - '''add to the template a Neutron Router and interface''' + """add to the template a Neutron Router and interface""" log.debug("adding Neutron::Router:'%s', gw-net:'%s'", name, ext_gw_net) - self.resources[name] = { 'type': 'OS::Neutron::Router', 'depends_on': [subnet_name], @@ -234,10 +300,9 @@ class HeatTemplate(HeatObject): } def add_router_interface(self, name, router_name, subnet_name): - '''add to the template a Neutron RouterInterface and interface''' + """add to the template a Neutron RouterInterface and interface""" log.debug("adding Neutron::RouterInterface '%s' router:'%s', " "subnet:'%s'", name, router_name, subnet_name) - self.resources[name] = { 'type': 'OS::Neutron::RouterInterface', 'depends_on': [router_name, subnet_name], @@ -247,8 +312,9 @@ class HeatTemplate(HeatObject): } } - def add_port(self, name, network_name, subnet_name, sec_group_id=None): - '''add to the template a named Neutron Port''' + def add_port(self, name, network_name, subnet_name, sec_group_id=None, + provider=None): + """add to the template a named Neutron Port""" log.debug("adding Neutron::Port '%s', network:'%s', subnet:'%s', " "secgroup:%s", name, network_name, subnet_name, sec_group_id) self.resources[name] = { @@ -262,6 +328,10 @@ class HeatTemplate(HeatObject): } } + if provider == PROVIDER_SRIOV: + self.resources[name]['properties']['binding:vnic_type'] = \ + 'direct' + if sec_group_id: self.resources[name]['depends_on'].append(sec_group_id) self.resources[name]['properties']['security_groups'] = \ @@ -271,21 +341,36 @@ class HeatTemplate(HeatObject): 'description': 'Address for interface %s' % name, 'value': {'get_attr': [name, 'fixed_ips', 0, 'ip_address']} } + self._template['outputs'][name + "-subnet_id"] = { + 'description': 'Address for interface %s' % name, + 'value': {'get_attr': [name, 'fixed_ips', 0, 'subnet_id']} + } + self._template['outputs'][name + "-mac_address"] = { + 'description': 'MAC Address for interface %s' % name, + 'value': {'get_attr': [name, 'mac_address']} + } + self._template['outputs'][name + "-device_id"] = { + 'description': 'Device ID for interface %s' % name, + 'value': {'get_attr': [name, 'device_id']} + } + self._template['outputs'][name + "-network_id"] = { + 'description': 'Network ID for interface %s' % name, + 'value': {'get_attr': [name, 'network_id']} + } def add_floating_ip(self, name, network_name, port_name, router_if_name, secgroup_name=None): - '''add to the template a Neutron FloatingIP resource + """add to the template a Nova FloatingIP resource see: https://bugs.launchpad.net/heat/+bug/1299259 - ''' - log.debug("adding Neutron::FloatingIP '%s', network '%s', port '%s', " + """ + log.debug("adding Nova::FloatingIP '%s', network '%s', port '%s', " "rif '%s'", name, network_name, port_name, router_if_name) self.resources[name] = { - 'type': 'OS::Neutron::FloatingIP', + 'type': 'OS::Nova::FloatingIP', 'depends_on': [port_name, router_if_name], 'properties': { - 'floating_network': network_name, - 'port_id': {'get_resource': port_name} + 'pool': network_name } } @@ -294,23 +379,43 @@ class HeatTemplate(HeatObject): self._template['outputs'][name] = { 'description': 'floating ip %s' % name, - 'value': {'get_attr': [name, 'floating_ip_address']} + 'value': {'get_attr': [name, 'ip']} + } + + def add_floating_ip_association(self, name, floating_ip_name, port_name): + """add to the template a Nova FloatingIP Association resource + """ + log.debug("adding Nova::FloatingIPAssociation '%s', server '%s', " + "floating_ip '%s'", name, port_name, floating_ip_name) + + self.resources[name] = { + 'type': 'OS::Neutron::FloatingIPAssociation', + 'depends_on': [port_name], + 'properties': { + 'floatingip_id': {'get_resource': floating_ip_name}, + 'port_id': {'get_resource': port_name} + } } - def add_keypair(self, name): - '''add to the template a Nova KeyPair''' + def add_keypair(self, name, key_uuid): + """add to the template a Nova KeyPair""" log.debug("adding Nova::KeyPair '%s'", name) self.resources[name] = { 'type': 'OS::Nova::KeyPair', 'properties': { 'name': name, - 'public_key': pkg_resources.resource_string( - 'yardstick.resources', 'files/yardstick_key.pub') + # resource_string returns bytes, so we must decode to unicode + 'public_key': encodeutils.safe_decode( + pkg_resources.resource_string( + 'yardstick.resources', + 'files/yardstick_key-' + + get_short_key_uuid(key_uuid) + '.pub'), + 'utf-8') } } def add_servergroup(self, name, policy): - '''add to the template a Nova ServerGroup''' + """add to the template a Nova ServerGroup""" log.debug("adding Nova::ServerGroup '%s', policy '%s'", name, policy) if policy not in ["anti-affinity", "affinity"]: raise ValueError(policy) @@ -329,7 +434,7 @@ class HeatTemplate(HeatObject): } def add_security_group(self, name): - '''add to the template a Neutron SecurityGroup''' + """add to the template a Neutron SecurityGroup""" log.debug("adding Neutron::SecurityGroup '%s'", name) self.resources[name] = { 'type': 'OS::Neutron::SecurityGroup', @@ -338,9 +443,13 @@ class HeatTemplate(HeatObject): 'description': "Group allowing icmp and upd/tcp on all ports", 'rules': [ {'remote_ip_prefix': '0.0.0.0/0', - 'protocol': 'tcp'}, + 'protocol': 'tcp', + 'port_range_min': '1', + 'port_range_max': '65535'}, {'remote_ip_prefix': '0.0.0.0/0', - 'protocol': 'udp'}, + 'protocol': 'udp', + 'port_range_min': '1', + 'port_range_max': '65535'}, {'remote_ip_prefix': '0.0.0.0/0', 'protocol': 'icmp'} ] @@ -352,41 +461,48 @@ class HeatTemplate(HeatObject): 'value': {'get_resource': name} } - def add_server(self, name, image, flavor, ports=None, networks=None, - scheduler_hints=None, user=None, key_name=None, - user_data=None, metadata=None, additional_properties=None): - '''add to the template a Nova Server''' + def add_server(self, name, image, flavor, flavors, ports=None, + networks=None, scheduler_hints=None, user=None, + key_name=None, user_data=None, metadata=None, + additional_properties=None): + """add to the template a Nova Server""" log.debug("adding Nova::Server '%s', image '%s', flavor '%s', " "ports %s", name, image, flavor, ports) self.resources[name] = { - 'type': 'OS::Nova::Server' + 'type': 'OS::Nova::Server', + 'depends_on': [] } server_properties = { 'name': name, 'image': image, - 'flavor': flavor, + 'flavor': {}, 'networks': [] # list of dictionaries } + if flavor in flavors: + self.resources[name]['depends_on'].append(flavor) + server_properties["flavor"] = {'get_resource': flavor} + else: + server_properties["flavor"] = flavor + if user: server_properties['admin_user'] = user if key_name: - self.resources[name]['depends_on'] = [key_name] + self.resources[name]['depends_on'].append(key_name) server_properties['key_name'] = {'get_resource': key_name} if ports: - self.resources[name]['depends_on'] = ports - + self.resources[name]['depends_on'].extend(ports) for port in ports: server_properties['networks'].append( {'port': {'get_resource': port}} ) if networks: - for i in range(len(networks)): + for i, _ in enumerate(networks): server_properties['networks'].append({'network': networks[i]}) if scheduler_hints: @@ -396,11 +512,11 @@ class HeatTemplate(HeatObject): server_properties['user_data'] = user_data if metadata: - assert type(metadata) is dict + assert isinstance(metadata, collections.Mapping) server_properties['metadata'] = metadata if additional_properties: - assert type(additional_properties) is dict + assert isinstance(additional_properties, collections.Mapping) for prop in additional_properties: server_properties[prop] = additional_properties[prop] @@ -413,42 +529,52 @@ class HeatTemplate(HeatObject): 'value': {'get_resource': name} } - def create(self, block=True): - '''creates a template in the target cloud using heat - returns a dict with the requested output values from the template''' + HEAT_WAIT_LOOP_INTERVAL = 2 + + def create(self, block=True, timeout=3600): + """ + creates a template in the target cloud using heat + returns a dict with the requested output values from the template + + :param block: Wait for Heat create to finish + :type block: bool + :param: timeout: timeout in seconds for Heat create, default 3600s + :type timeout: int + """ log.info("Creating stack '%s'", self.name) # create stack early to support cleanup, e.g. ctrl-c while waiting stack = HeatStack(self.name) - heat = self._get_heat_client() - json_template = json.dumps(self._template) + heat_client = self.heat_client start_time = time.time() - stack.uuid = self.uuid = heat.stacks.create( - stack_name=self.name, template=json_template, + stack.uuid = self.uuid = heat_client.stacks.create( + stack_name=self.name, template=self._template, parameters=self.heat_parameters)['stack']['id'] - status = self.status() - - if block: - while status != u'CREATE_COMPLETE': - log.debug("stack state %s", status) - if status == u'CREATE_FAILED': - raise RuntimeError(getattr(heat.stacks.get(self.uuid), - 'stack_status_reason')) - - time.sleep(2) - status = self.status() + if not block: + self.outputs = stack.outputs = {} + return stack - end_time = time.time() - outputs = getattr(heat.stacks.get(self.uuid), 'outputs') + time_limit = start_time + timeout + for status in iter(self.status, u'CREATE_COMPLETE'): + log.debug("stack state %s", status) + if status == u'CREATE_FAILED': + raise RuntimeError( + heat_client.stacks.get(self.uuid).stack_status_reason) + if time.time() > time_limit: + raise RuntimeError("Heat stack create timeout") - for output in outputs: - self.outputs[output["output_key"].encode("ascii")] = \ - output["output_value"].encode("ascii") + time.sleep(self.HEAT_WAIT_LOOP_INTERVAL) + end_time = time.time() + outputs = heat_client.stacks.get(self.uuid).outputs log.info("Created stack '%s' in %d secs", self.name, end_time - start_time) + # keep outputs as unicode + self.outputs = {output["output_key"]: output["output_value"] for output + in outputs} + stack.outputs = self.outputs return stack