1 ##############################################################################
2 # Copyright (c) 2015 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 """ Handler for yardstick command 'task' """
19 from yardstick.benchmark.context.model import Context
20 from yardstick.benchmark.runners import base as base_runner
22 from yardstick.common.utils import cliargs
24 output_file_default = "/tmp/yardstick.out"
27 class TaskCommands(object):
30 Set of commands to manage benchmark tasks.
33 @cliargs("taskfile", type=str, help="path to taskfile", nargs=1)
34 @cliargs("--keep-deploy", help="keep context deployed in cloud",
36 @cliargs("--parse-only", help="parse the benchmark config file and exit",
38 @cliargs("--output-file", help="file where output is stored, default %s" %
39 output_file_default, default=output_file_default)
40 def do_start(self, args):
41 '''Start a benchmark scenario.'''
43 atexit.register(atexit_handler)
45 parser = TaskParser(args.taskfile[0])
46 scenarios, run_in_parallel = parser.parse()
51 if os.path.isfile(args.output_file):
52 os.remove(args.output_file)
54 for context in Context.list:
59 for scenario in scenarios:
60 runner = run_one_scenario(scenario, args.output_file)
61 runners.append(runner)
63 # Wait for runners to finish
64 for runner in runners:
66 print "Runner ended, output in", args.output_file
69 for scenario in scenarios:
70 runner = run_one_scenario(scenario, args.output_file)
72 print "Runner ended, output in", args.output_file
75 # keep deployment, forget about stack (hide it for exit handler)
78 for context in Context.list:
84 # TODO: Move stuff below into TaskCommands class !?
86 class TaskParser(object):
87 '''Parser for task config files in yaml format'''
88 def __init__(self, path):
92 '''parses the task file and return an context and scenario instances'''
93 print "Parsing task config:", self.path
95 with open(self.path) as stream:
96 cfg = yaml.load(stream)
97 except IOError as ioerror:
100 if cfg["schema"] != "yardstick:task:0.1":
101 sys.exit("error: file %s has unknown schema %s" % (self.path,
104 # TODO: support one or many contexts? Many would simpler and precise
106 context_cfgs = [cfg["context"]]
108 context_cfgs = cfg["contexts"]
110 for cfg_attrs in context_cfgs:
112 context.init(cfg_attrs)
114 run_in_parallel = cfg.get("run_in_parallel", False)
116 # TODO we need something better here, a class that represent the file
117 return cfg["scenarios"], run_in_parallel
120 def atexit_handler():
121 '''handler for process termination'''
122 base_runner.Runner.terminate_all()
124 if len(Context.list) > 0:
125 print "Undeploying all contexts"
126 for context in Context.list:
130 def is_ip_addr(addr):
131 '''check if string addr is an IP address'''
133 ipaddress.ip_address(unicode(addr))
139 def run_one_scenario(scenario_cfg, output_file):
140 '''run one scenario using context'''
141 key_filename = pkg_resources.resource_filename(
142 'yardstick.resources', 'files/yardstick_key')
144 host = Context.get_server(scenario_cfg["host"])
146 runner_cfg = scenario_cfg["runner"]
147 runner_cfg['host'] = host.public_ip
148 runner_cfg['user'] = host.context.user
149 runner_cfg['key_filename'] = key_filename
150 runner_cfg['output_filename'] = output_file
152 if "target" in scenario_cfg:
153 if is_ip_addr(scenario_cfg["target"]):
154 scenario_cfg["ipaddr"] = scenario_cfg["target"]
156 target = Context.get_server(scenario_cfg["target"])
158 # get public IP for target server, some scenarios require it
160 runner_cfg['target'] = target.public_ip
162 # TODO scenario_cfg["ipaddr"] is bad naming
163 if host.context != target.context:
164 # target is in another context, get its public IP
165 scenario_cfg["ipaddr"] = target.public_ip
167 # target is in the same context, get its private IP
168 scenario_cfg["ipaddr"] = target.private_ip
170 runner = base_runner.Runner.get(runner_cfg)
172 print "Starting runner of type '%s'" % runner_cfg["type"]
173 runner.run(scenario_cfg["type"], scenario_cfg)
178 def runner_join(runner):
179 '''join (wait for) a runner, exit process at runner failure'''
180 status = runner.join()
181 base_runner.Runner.release(runner)
183 sys.exit("Runner failed")