068ab2e02ced8c9d675babd18e04b8e280728437
[bottlenecks.git] / vstf / vstf / controller / vstfadm.py
1 import sys
2 import logging
3 import json
4 from vstf.common.vstfcli import VstfParser
5 from vstf.common import cliutil, constants, unix, message
6 from vstf.common.log import setup_logging
7 import vstf.common.constants as cst
8 import pprint
9
10 CONN = None
11
12
13 def print_stdout(msg):
14     # out = json.dumps(message.get_body(message.decode(msg)), indent=2)
15     out = message.get_body(message.decode(msg))
16     pprint.pprint(out, indent=2)
17
18
19 def call(msg):
20     """msg must be a dict"""
21     msg = message.add_context(msg, corr=message.gen_corrid())
22     CONN.send(message.encode(msg))
23     return message.decode(CONN.recv())
24
25
26 def make_msg(method, **kwargs):
27     return {"method": method, "args": kwargs}
28
29
30 @cliutil.arg("--host", dest="host", default="", action="store", help="list nic devices of specified host")
31 def do_list_devs(args):
32     """List the host's all netdev."""
33     ret = call(make_msg("list_devs", host=args.host))
34     print_stdout(ret)
35
36
37 @cliutil.arg("--host", dest="host", action="store", default=None,
38              help="which host to run src_install.")
39 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
40              help="the git repo config.")
41 def do_src_install(args):
42     """work agent to pull source code and compile.
43     use git as underlying mechanism, please make sure the host has access to git repo.
44     """
45     ret = call(make_msg("src_install", host=args.host, config_file=args.config_file))
46     print_stdout(ret)
47
48
49 @cliutil.arg("--host", dest="host", action="store", default=None,
50              help="which host to build, must exists in your config file, use default[None] value to build all hosts.")
51 @cliutil.arg("--model", dest="model", action="store", choices=('Tn', 'Ti', 'Tu', 'Tnv'),
52              help="which model to build, if specified, the according config file /etc/vstf/env/{model}.json must exist.")
53 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
54              help="if specified, the config file will replace the default config file from /etc/vstf/env.")
55 def do_apply_model(args):
56     """Apply model to the host."""
57     ret = call(make_msg("apply_model", host=args.host, model=args.model, config_file=args.config_file))
58     print_stdout(ret)
59
60
61 @cliutil.arg("--host", dest="host", action="store", default=None,
62              help="to which host you wish to create images")
63 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
64              help="configuration file for image creation.")
65 def do_create_images(args):
66     """create images on host, images are configed by configuration file."""
67     ret = call(make_msg("create_images", host=args.host, config_file=args.config_file))
68     print_stdout(ret)
69
70
71 @cliutil.arg("--host", dest="host", action="store", default=None,
72              help="to which host you wish to clean images")
73 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
74              help="configuration file for images.")
75 def do_clean_images(args):
76     """clean images on host, images are configed by configuration file."""
77     ret = call(make_msg("clean_images", host=args.host, config_file=args.config_file))
78     print_stdout(ret)
79
80
81 @cliutil.arg("--host", dest="host", action="store", default=None,
82              help="which host to clean, must exists in your config file, use default[None] value to clean all hosts.")
83 @cliutil.arg("--model", dest="model", action="store", choices=('Tn', 'Ti', 'Tu', 'Tnv'),
84              help="if specified, the according config file /etc/vstf/env/{model}.json must exist.")
85 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
86              help="if specified, the config file will replace the default config file from /etc/vstf/env.")
87 def do_disapply_model(args):
88     """Apply model to the host."""
89     ret = call(make_msg("disapply_model", host=args.host, model=args.model, config_file=args.config_file))
90     print_stdout(ret)
91
92
93 @cliutil.arg("--host", dest="host", action="store", help="collect host information about cpu/mem etc")
94 def do_collect_host_info(args):
95     """Show the host's CPU/MEN info"""
96     ret = call(make_msg("collect_host_info", target=args.host))
97     print_stdout(ret)
98
99
100 def do_show_tasks(args):
101     """List history performance test tasks. Can be used by report cmd to generate reports.
102     """
103     ret = call(make_msg("list_tasks"))
104     print_stdout(ret)
105
106
107 @cliutil.arg("case", action="store", help="test case like Ti-1, Tn-1, Tnv-1, Tu-1, see case definition in documents")
108 @cliutil.arg("tool", action="store", choices=cst.TOOLS, )
109 @cliutil.arg("protocol", action="store", choices=cst.TPROTOCOLS, )
110 @cliutil.arg("profile", action="store", choices=cst.PROFILES, )
111 @cliutil.arg("type", action="store", choices=cst.TTYPES)
112 @cliutil.arg("sizes", action="store", default="64", help='test size list "64 128"')
113 @cliutil.arg("--affctl", action="store_true", help="when affctl is True, it will do affctl before testing")
114 def do_perf_test(args):
115     """Runs a quick single software performance test without envbuild and generating reports.
116     Outputs the result to the stdout immediately."""
117     case_info = {
118         'case': args.case,
119         'tool': args.tool,
120         'protocol': args.protocol,
121         'profile': args.profile,
122         'type': args.type,
123         'sizes': map(lambda x: int(x), args.sizes.strip().split())
124     }
125     ret = call(make_msg("run_perf_cmd",
126                         case=case_info,
127                         rpath=cst.REPORT_DEFAULTS,
128                         affctl=args.affctl,
129                         build_on=False,
130                         save_on=False,
131                         report_on=False,
132                         mail_on=False
133                         ))
134     print_stdout(ret)
135
136
137 @cliutil.arg("-rpath",
138              help="path of result",
139              default=cst.REPORT_DEFAULTS,
140              action="store")
141 @cliutil.arg("--report_off",
142              help="when report_off is True, it will not generate the report",
143              action="store_true")
144 @cliutil.arg("--mail_off",
145              help="when mail_off is True, it will not send mail",
146              action="store_true")
147 @cliutil.arg("--affctl",
148              help="when affctl is True, it will do affctl before testing",
149              action="store_true")
150 def do_batch_perf_test(args):
151     """run soft performance test cases defined in /etc/vstf/perf/sw_perf.batch-settings"""
152     ret = call(make_msg("run_perf_file",
153                         affctl=args.affctl,
154                         rpath=args.rpath,
155                         report_on=not args.report_off,
156                         mail_on=not args.mail_off
157                         ))
158     print_stdout(ret)
159
160
161 @cliutil.arg('-rpath',
162              action='store',
163              default=cst.REPORT_DEFAULTS,
164              help=" the path name of test results  ")
165 @cliutil.arg("--mail_off",
166              help="when mail_off is True, it will not send mail",
167              action="store_true")
168 @cliutil.arg("--taskid",
169              help="report depend of a history task id",
170              default=-1,
171              action="store")
172 def do_report(args):
173     """generate the report from the database"""
174     ret = call(make_msg("report",
175                         rpath=args.rpath,
176                         mail_off=args.mail_off,
177                         taskid=args.taskid
178                         ))
179     print_stdout(ret)
180
181
182 @cliutil.arg("--conner",
183              dest="conner",
184              action="store",
185              help="tester")
186 @cliutil.arg("--measurand",
187              dest="measurand",
188              action="store",
189              help="tested")
190 @cliutil.arg("-m", "--model",
191              dest="model",
192              action="store",
193              help="Test scene name : Tnv")
194 @cliutil.arg("-e", "--virtenv",
195              dest="virtenv",
196              action="store",
197              help="virt env_build number(s): [1-8]")
198 @cliutil.arg("-q", "--queues",
199              dest="queues",
200              action="store",
201              help="VM nic queues.")
202 @cliutil.arg("-f", "--flows",
203              dest="flows",
204              action="store",
205              help="Flow queue(s) : [1-8]")
206 @cliutil.arg("-v", "--vlans",
207              dest="vlans",
208              action="store_true",
209              help="vlan setting : 100-150;200-250")
210 @cliutil.arg("-d", "--direct",
211              dest="direct",
212              action="store",
213              choices=["single", "double"],
214              help="Flow Direction")
215 @cliutil.arg("-b", "--bind",
216              dest="strategy",
217              action="store",
218              help="CPU bind strategy :  1 | 2 | 3 ")
219 @cliutil.arg("--config_file",
220              dest="config_file",
221              default='/etc/vstf/spirent/optimize.ini',
222              action="store",
223              help="config file for optimize.")
224 @cliutil.arg("--strategyfile",
225              dest="strategyfile",
226              default='/etc/vstf/spirent/strategy.ini',
227              action="store",
228              help="config file for strategy.")
229 def do_spirent_test(args):
230     ret = call(make_msg("perf_test",
231                         plugin="spirent",
232                         conner=args.conner,
233                         measurand=args.measurand,
234                         virtenv=args.virtenv,
235                         queues=args.queues,
236                         direct=args.direct,
237                         flows=args.flows,
238                         strategy=args.strategy,
239                         model=args.model,
240                         vlans=args.vlans,
241                         configfile=args.config_file,
242                         strategyfile=args.strategyfile))
243     print_stdout(ret)
244
245
246 @cliutil.arg("--host", dest="host", action="store", default=None,
247              help="which host to list affctl info")
248 def do_affctl_list(args):
249     ret = call(make_msg("affctl_list", host=args.host))
250     print_stdout(ret)
251
252
253 def main():
254     parser = VstfParser(prog="vstfadm", description="vstf administration")
255     parser.set_subcommand_parser(sys.modules[__name__], "functions")
256     args = parser.parse_args()
257     if args.func is None:
258         sys.exit(-1)
259     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-adm.log", clevel=logging.INFO)
260     # connect to manage
261     global CONN
262     try:
263         CONN = unix.UdpClient()
264         CONN.connect(constants.sockaddr)
265     except Exception as e:
266         raise e
267
268     args.func(args)
269     # call functions of manage
270     sys.exit(CONN.close())