]>
Commit | Line | Data |
---|---|---|
1 | # Copyright (c) 2012 Nicira, Inc. | |
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 | ||
15 | import argparse | |
16 | import sys | |
17 | ||
18 | import ovs.daemon | |
19 | import ovs.unixctl | |
20 | import ovs.unixctl.client | |
21 | import ovs.util | |
22 | import ovs.vlog | |
23 | from ovs.fatal_signal import signal_alarm | |
24 | ||
25 | ||
26 | def connect_to_target(target): | |
27 | error, str_result = ovs.unixctl.socket_name_from_target(target) | |
28 | if error: | |
29 | ovs.util.ovs_fatal(error, str_result) | |
30 | else: | |
31 | socket_name = str_result | |
32 | ||
33 | error, client = ovs.unixctl.client.UnixctlClient.create(socket_name) | |
34 | if error: | |
35 | ovs.util.ovs_fatal(error, "cannot connect to \"%s\"" % socket_name) | |
36 | ||
37 | return client | |
38 | ||
39 | ||
40 | def main(): | |
41 | parser = argparse.ArgumentParser(description="Python Implementation of" | |
42 | " ovs-appctl.") | |
43 | parser.add_argument("-t", "--target", default="ovs-vswitchd", | |
44 | help="pidfile or socket to contact") | |
45 | ||
46 | parser.add_argument("command", metavar="COMMAND", | |
47 | help="Command to run.") | |
48 | parser.add_argument("argv", metavar="ARG", nargs="*", | |
49 | help="Arguments to the command.") | |
50 | parser.add_argument("-T", "--timeout", metavar="SECS", | |
51 | help="wait at most SECS seconds for a response") | |
52 | args = parser.parse_args() | |
53 | ||
54 | signal_alarm(int(args.timeout) if args.timeout else None) | |
55 | ||
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 | ||
73 | if __name__ == '__main__': | |
74 | main() |