############################################################################## # Copyright (c) 2015 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 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## """ Heat template and stack management """ import os import time import datetime import getpass import socket import logging import pkg_resources import json import heatclient.client import keystoneclient from yardstick.common import template_format log = logging.getLogger(__name__) class HeatObject(object): ''' 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''' if self._heat_client is None: keystone = self._get_keystone_client() heat_endpoint = keystone.service_catalog.url_for( service_type='orchestration') self._heat_client = heatclient.client.Client( '1', endpoint=heat_endpoint, token=keystone.auth_token) 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') class HeatStack(HeatObject): ''' Represents a Heat stack (deployed template) ''' stacks = [] def __init__(self, name): super(HeatStack, self).__init__() self.uuid = None self.name = name self.outputs = None HeatStack.stacks.append(self) @staticmethod def stacks_exist(): '''check if any stack has been deployed''' return len(HeatStack.stacks) > 0 def _delete(self): '''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() template = heat.stacks.get(self.uuid) start_time = time.time() template.delete() status = self.status() while 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, end_time - start_time) self.uuid = None def delete(self, block=True, retries=3): '''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 if not block: self._delete() return i = 0 while i < retries: try: self._delete() break except RuntimeError as err: log.warn(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: self._delete() HeatStack.stacks.remove(self) @staticmethod def delete_all(): for stack in HeatStack.stacks[:]: stack.delete() def update(self): '''update a stack''' raise RuntimeError("not implemented") class HeatTemplate(HeatObject): '''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' 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) # short hand for resources part of template self.resources = self._template['resources'] = {} self._template['outputs'] = {} 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
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
---
# Sample benchmark task config file
# measure network latency using ping

schema: "yardstick:task:0.1"

scenarios:
-
  type: Ping
  options:
    packetsize: 200
  host: athena.demo
  target: ares.demo

  runner:
    type: Iteration
    iterations: 60
    interval: 1

  sla:
    max_rtt: