add main cmd for yardstick
[yardstick.git] / yardstick / main.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 """ yardstick - command line tool for managing benchmarks
13
14     Example invocation:
15     $ yardstick samples/ping-task.yaml
16 """
17
18 import sys
19 import yaml
20 import atexit
21 import pkg_resources
22
23 from yardstick.benchmark.context.model import Context
24 from yardstick.benchmark.runners import base as base_runner
25 from yardstick.cmdparser import CmdParser
26 from yardstick.orchestrator.heat import HeatStack
27
28
29 class TaskParser(object):
30     '''Parser for task config files in yaml format'''
31     def __init__(self, path):
32         self.path = path
33
34     def parse(self):
35         '''parses the task file and return an context and scenario instances'''
36         print "Parsing task config:", self.path
37         try:
38             with open(self.path) as stream:
39                 cfg = yaml.load(stream)
40         except IOError as ioerror:
41             sys.exit(ioerror)
42
43         if cfg["schema"] != "yardstick:task:0.1":
44             sys.exit("error: file %s has unknown schema %s" % (self.path,
45                                                                cfg["schema"]))
46         context = Context()
47         context.init(cfg["context"])
48
49         run_in_parallel = cfg.get("run_in_parallel", False)
50
51         # TODO we need something better here, a class that represent the file
52         return cfg["scenarios"], run_in_parallel, context
53
54
55 def atexit_handler():
56     '''handler for process termination'''
57     if HeatStack.stacks_exist():
58         print "Deleting all stacks"
59     HeatStack.delete_all()
60
61
62 def run_one_scenario(scenario_cfg, context, output_file):
63     '''run one scenario using context'''
64     key_filename = pkg_resources.resource_filename(
65         'yardstick.resources', 'files/yardstick_key')
66
67     host = context.get_server(scenario_cfg["host"])
68
69     runner_cfg = scenario_cfg["runner"]
70     runner_cfg['host'] = host.floating_ip["ipaddr"]
71     runner_cfg['user'] = context.user
72     runner_cfg['key_filename'] = key_filename
73     runner_cfg['output_filename'] = output_file
74
75     target = context.get_server(scenario_cfg["target"])
76     if target.floating_ip:
77         runner_cfg['target'] = target.floating_ip["ipaddr"]
78
79     # TODO hardcoded name below, a server can be attached to several nets
80     scenario_cfg["ipaddr"] = target.ports["test"]["ipaddr"]
81
82     runner = base_runner.Runner.get(runner_cfg)
83
84     print "Starting runner of type '%s'" % runner_cfg["type"]
85     runner.run(scenario_cfg["type"], scenario_cfg)
86
87     return runner
88
89
90 def main():
91     '''yardstick main'''
92
93     atexit.register(atexit_handler)
94
95     prog_args = CmdParser().parse_args()
96
97     parser = TaskParser(prog_args.taskfile[0])
98     scenarios, run_in_parallel, context = parser.parse()
99
100     if prog_args.parse_only:
101         sys.exit(0)
102
103     context.deploy()
104
105     runners = []
106     if run_in_parallel:
107         for scenario in scenarios:
108             runner = run_one_scenario(scenario, context, prog_args.output_file)
109             runners.append(runner)
110
111         # Wait for runners to finish
112         for runner in runners:
113             runner.join()
114             print "Runner ended, output in", prog_args.output_file
115             base_runner.Runner.release(runner)
116     else:
117         # run serially
118         for scenario in scenarios:
119             runner = run_one_scenario(scenario, context, prog_args.output_file)
120             runner.join()
121             print "Runner ended, output in", prog_args.output_file
122             base_runner.Runner.release(runner)
123
124     if prog_args.keep_deploy:
125         # keep deployment, forget about stack (hide it for exit handler)
126         context.stack = None
127     else:
128         context.undeploy()
129
130     print "Done, exiting"
131
132 if __name__ == '__main__':
133     main()