]> git.proxmox.com Git - mirror_ovs.git/blame - tests/appctl.py
Global replace of Nicira Networks.
[mirror_ovs.git] / tests / appctl.py
CommitLineData
e0edde6f 1# Copyright (c) 2012 Nicira, Inc.
0a68ffd2
EJ
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at:
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import argparse
f4ec6ff4 16import signal
0a68ffd2
EJ
17import sys
18
19import ovs.daemon
20import ovs.unixctl
21import ovs.util
22import ovs.vlog
23
24
25def connect_to_target(target):
26 error, str_result = ovs.unixctl.socket_name_from_target(target)
27 if error:
28 ovs.util.ovs_fatal(error, str_result)
29 else:
30 socket_name = str_result
31
32 error, client = ovs.unixctl.UnixctlClient.create(socket_name)
33 if error:
34 ovs.util.ovs_fatal(error, "cannot connect to \"%s\"" % socket_name)
35
36 return client
37
38
39def main():
40 parser = argparse.ArgumentParser(description="Python Implementation of"
41 " ovs-appctl.")
42 parser.add_argument("-t", "--target", default="ovs-vswitchd",
43 help="pidfile or socket to contact")
44
45 parser.add_argument("command", metavar="COMMAND",
46 help="Command to run.")
47 parser.add_argument("argv", metavar="ARG", nargs="*",
48 help="Arguments to the command.")
f4ec6ff4
EJ
49 parser.add_argument("-T", "--timeout", metavar="SECS",
50 help="wait at most SECS seconds for a response")
0a68ffd2
EJ
51 args = parser.parse_args()
52
f4ec6ff4
EJ
53 if args.timeout:
54 signal.alarm(int(args.timeout))
55
0a68ffd2
EJ
56 ovs.vlog.Vlog.init()
57 target = args.target
58 client = connect_to_target(target)
59 err_no, error, result = client.transact(args.command, args.argv)
60 client.close()
61
62 if err_no:
63 ovs.util.ovs_fatal(err_no, "%s: transaction error" % target)
64 elif error is not None:
65 sys.stderr.write(error)
66 ovs.util.ovs_error(0, "%s: server returned an error" % target)
67 sys.exit(2)
68 else:
69 assert result is not None
70 sys.stdout.write(result)
71
72
73if __name__ == '__main__':
74 main()