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' """
18 from yardstick.benchmark.context.model import Context
19 from yardstick.benchmark.runners import base as base_runner
21 from yardstick.common.utils import cliargs
23 output_file_default = "/tmp/yardstick.out"
26 class TaskCommands(object):
29 Set of commands to manage benchmark tasks.
32 @cliargs("taskfile", type=str, help="path to taskfile", nargs=1)
33 @cliargs("--keep-deploy", help="keep context deployed in cloud",
35 @cliargs("--parse-only", help="parse the benchmark config file and exit",
37 @cliargs("--output-file", help="file where output is stored, default %s" %
38 output_file_default, default=output_file_default)
39 def do_start(self, args):
40 '''Start a benchmark scenario.'''
42 atexit.register(atexit_handler)
44 parser = TaskParser(args.taskfile[0])
45 scenarios, run_in_parallel = parser.parse()
50 for context in Context.list:
55 for scenario in scenarios:
56 runner = run_one_scenario(scenario, args.output_file)
57 runners.append(runner)
59 # Wait for runners to finish
60 for runner in runners:
62 print "Runner ended, output in", args.output_file
65 for scenario in scenarios:
66 runner = run_one_scenario(scenario, args.output_file)
68 print "Runner ended, output in", args.output_file
71 # keep deployment, forget about stack (hide it for exit handler)
74 for context in Context.list:
80 # TODO: Move stuff below into TaskCommands class !?
82 class TaskParser(object):
83 '''Parser for task config files in yaml format'''
84 def __init__(self, path):
88 '''parses the task file and return an context and scenario instances'''
89 print "Parsing task config:", self.path
91 with open(self.path) as stream:
92 cfg = yaml.load(stream)
93 except IOError as ioerror:
96 if cfg["schema"] != "yardstick:task:0.1":
97 sys.exit("error: file %s has unknown schema %s" % (self.path,
100 # TODO: support one or many contexts? Many would simpler and precise
102 context_cfgs = [cfg["context"]]
104 context_cfgs = cfg["contexts"]
106 for cfg_attrs in context_cfgs:
108 context.init(cfg_attrs)
110 run_in_parallel = cfg.get("run_in_parallel", False)
112 # TODO we need something better here, a class that represent the file
113 return cfg["scenarios"], run_in_parallel
116 def atexit_handler():
117 '''handler for process termination'''
118 base_runner.Runner.terminate_all()
120 if len(Context.list) > 0:
121 print "Undeploying all contexts"
122 for context in Context.list:
126 def is_ip_addr(addr):
127 '''check if string addr is an IP address'''
129 ipaddress.ip_address(unicode(addr))
135 def run_one_scenario(scenario_cfg, output_file):
136 '''run one scenario using context'''
137 key_filename = pkg_resources.resource_filename(
138 'yardstick.resources', 'files/yardstick_key')
140 host = Context.get_server(scenario_cfg["host"])
142 runner_cfg = scenario_cfg["runner"]
143 runner_cfg['host'] = host.public_ip
144 runner_cfg['user'] = host.context.user
145 runner_cfg['key_filename'] = key_filename
146 runner_cfg['output_filename'] = output_file
148 if "target" in scenario_cfg:
149 if is_ip_addr(scenario_cfg["target"]):
150 scenario_cfg["ipaddr"] = scenario_cfg["target"]
152 target = Context.get_server(scenario_cfg["target"])
154 # get public IP for target server, some scenarios require it
156 runner_cfg['target'] = target.public_ip
158 # TODO scenario_cfg["ipaddr"] is bad naming
159 if host.context != target.context:
160 # target is in another context, get its public IP
161 scenario_cfg["ipaddr"] = target.public_ip
163 # target is in the same context, get its private IP
164 scenario_cfg["ipaddr"] = target.private_ip
166 runner = base_runner.Runner.get(runner_cfg)
168 print "Starting runner of type '%s'" % runner_cfg["type"]
169 runner.run(scenario_cfg["type"], scenario_cfg)
174 def runner_join(runner):
175 '''join (wait for) a runner, exit process at runner failure'''
176 status = runner.join()
177 base_runner.Runner.release(runner)
179 sys.exit("Runner failed")