X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fbenchmark%2Fcontexts%2Fheat.py;h=0b9a4d2943c501d4ca0612ed13cb55f01e2bf7fe;hb=5420dd71763226d78fb1ff486b78918432c73e49;hp=b504cd4df715b2b8925a1401ee3ac40d0a02e4cf;hpb=3a3de7c54c9a970979672391e06b613a12f5ab62;p=yardstick.git diff --git a/yardstick/benchmark/contexts/heat.py b/yardstick/benchmark/contexts/heat.py index b504cd4df..0b9a4d294 100644 --- a/yardstick/benchmark/contexts/heat.py +++ b/yardstick/benchmark/contexts/heat.py @@ -7,21 +7,31 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from __future__ import absolute_import +from __future__ import print_function + +import collections +import logging import os import sys -import pkg_resources +import uuid + import paramiko +import pkg_resources from yardstick.benchmark.contexts.base import Context -from yardstick.benchmark.contexts.model import Server -from yardstick.benchmark.contexts.model import PlacementGroup from yardstick.benchmark.contexts.model import Network +from yardstick.benchmark.contexts.model import PlacementGroup +from yardstick.benchmark.contexts.model import Server from yardstick.benchmark.contexts.model import update_scheduler_hints -from yardstick.orchestrator.heat import HeatTemplate +from yardstick.orchestrator.heat import HeatTemplate, get_short_key_uuid +from yardstick.definitions import YARDSTICK_ROOT_PATH + +LOG = logging.getLogger(__name__) class HeatContext(Context): - '''Class that represents a context in the logical model''' + """Class that represents a context in the logical model""" __context_type__ = "Heat" @@ -39,11 +49,16 @@ class HeatContext(Context): self._user = None self.template_file = None self.heat_parameters = None - self.key_filename = 'yardstick/resources/files/yardstick_key' - super(self.__class__, self).__init__() + # generate an uuid to identify yardstick_key + # the first 8 digits of the uuid will be used + self.key_uuid = uuid.uuid4() + self.key_filename = ''.join( + [YARDSTICK_ROOT_PATH, 'yardstick/resources/files/yardstick_key-', + get_short_key_uuid(self.key_uuid)]) + super(HeatContext, self).__init__() def init(self, attrs): - '''initializes itself from the supplied arguments''' + """initializes itself from the supplied arguments""" self.name = attrs["name"] if "user" in attrs: @@ -77,35 +92,32 @@ class HeatContext(Context): self.servers.append(server) self._server_map[server.dn] = server - print "Generating RSA host key ..." rsa_key = paramiko.RSAKey.generate(bits=2048, progress_func=None) - print "Writing yardstick_key ..." rsa_key.write_private_key_file(self.key_filename) - print "Writing yardstick_key.pub ..." - open(self.key_filename + ".pub", "w").write("%s %s\n" % - (rsa_key.get_name(), - rsa_key.get_base64())) + print("Writing %s ..." % self.key_filename) + with open(self.key_filename + ".pub", "w") as pubkey_file: + pubkey_file.write( + "%s %s\n" % (rsa_key.get_name(), rsa_key.get_base64())) del rsa_key - print "... done!" @property def image(self): - '''returns application's default image name''' + """returns application's default image name""" return self._image @property def flavor(self): - '''returns application's default flavor name''' + """returns application's default flavor name""" return self._flavor @property def user(self): - '''return login user name corresponding to image''' + """return login user name corresponding to image""" return self._user def _add_resources_to_template(self, template): - '''add to the template the resources represented by this context''' - template.add_keypair(self.keypair_name) + """add to the template the resources represented by this context""" + template.add_keypair(self.keypair_name, self.key_uuid) template.add_security_group(self.secgroup_name) for network in self.networks: @@ -189,8 +201,8 @@ class HeatContext(Context): server.add_to_template(template, self.networks, {}) def deploy(self): - '''deploys template into a stack using cloud''' - print "Deploying context '%s'" % self.name + """deploys template into a stack using cloud""" + print("Deploying context '%s'" % self.name) heat_template = HeatTemplate(self.name, self.template_file, self.heat_parameters) @@ -211,39 +223,40 @@ class HeatContext(Context): for server in self.servers: if len(server.ports) > 0: # TODO(hafe) can only handle one internal network for now - port = server.ports.values()[0] + port = list(server.ports.values())[0] server.private_ip = self.stack.outputs[port["stack_name"]] if server.floating_ip: server.public_ip = \ self.stack.outputs[server.floating_ip["stack_name"]] - print "Context '%s' deployed" % self.name + print("Context '%s' deployed" % self.name) def undeploy(self): - '''undeploys stack from cloud''' + """undeploys stack from cloud""" if self.stack: - print "Undeploying context '%s'" % self.name + print("Undeploying context '%s'" % self.name) self.stack.delete() self.stack = None - print "Context '%s' undeployed" % self.name + print("Context '%s' undeployed" % self.name) if os.path.exists(self.key_filename): try: os.remove(self.key_filename) os.remove(self.key_filename + ".pub") - except OSError, e: - print ("Error: %s - %s." % (e.key_filename, e.strerror)) + except OSError: + LOG.exception("Key filename %s", self.key_filename) def _get_server(self, attr_name): - '''lookup server info by name from context + """lookup server info by name from context attr_name: either a name for a server created by yardstick or a dict with attribute name mapping when using external heat templates - ''' + """ key_filename = pkg_resources.resource_filename( - 'yardstick.resources', 'files/yardstick_key') + 'yardstick.resources', + 'files/yardstick_key-' + get_short_key_uuid(self.key_uuid)) - if type(attr_name) is dict: + if isinstance(attr_name, collections.Mapping): cname = attr_name["name"].split(".")[1] if cname != self.name: return None