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