]>
Commit | Line | Data |
---|---|---|
53cf9963 BP |
1 | # Copyright (c) 2011, 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 | ||
1ccbf95c | 15 | import six |
03693b2f | 16 | import sys |
1ccbf95c | 17 | |
53cf9963 BP |
18 | import ovs.util |
19 | ||
20 | commands = {} | |
1ccbf95c | 21 | strtypes = six.string_types |
53cf9963 BP |
22 | |
23 | ||
24 | class _UnixctlCommand(object): | |
25 | def __init__(self, usage, min_args, max_args, callback, aux): | |
26 | self.usage = usage | |
27 | self.min_args = min_args | |
28 | self.max_args = max_args | |
29 | self.callback = callback | |
30 | self.aux = aux | |
31 | ||
32 | ||
33 | def _unixctl_help(conn, unused_argv, unused_aux): | |
34 | reply = "The available commands are:\n" | |
35 | command_names = sorted(commands.keys()) | |
36 | for name in command_names: | |
37 | reply += " " | |
38 | usage = commands[name].usage | |
39 | if usage: | |
40 | reply += "%-23s %s" % (name, usage) | |
41 | else: | |
42 | reply += name | |
43 | reply += "\n" | |
44 | conn.reply(reply) | |
45 | ||
46 | ||
47 | def command_register(name, usage, min_args, max_args, callback, aux): | |
48 | """ Registers a command with the given 'name' to be exposed by the | |
49 | UnixctlServer. 'usage' describes the arguments to the command; it is used | |
50 | only for presentation to the user in "help" output. | |
51 | ||
52 | 'callback' is called when the command is received. It is passed a | |
53 | UnixctlConnection object, the list of arguments as unicode strings, and | |
54 | 'aux'. Normally 'callback' should reply by calling | |
55 | UnixctlConnection.reply() or UnixctlConnection.reply_error() before it | |
56 | returns, but if the command cannot be handled immediately, then it can | |
57 | defer the reply until later. A given connection can only process a single | |
58 | request at a time, so a reply must be made eventually to avoid blocking | |
59 | that connection.""" | |
60 | ||
61 | assert isinstance(name, strtypes) | |
62 | assert isinstance(usage, strtypes) | |
63 | assert isinstance(min_args, int) | |
64 | assert isinstance(max_args, int) | |
58de9fc3 | 65 | assert callable(callback) |
53cf9963 BP |
66 | |
67 | if name not in commands: | |
68 | commands[name] = _UnixctlCommand(usage, min_args, max_args, callback, | |
69 | aux) | |
70 | ||
bdca6c4b | 71 | |
53cf9963 BP |
72 | def socket_name_from_target(target): |
73 | assert isinstance(target, strtypes) | |
74 | ||
03693b2f PB |
75 | """ On Windows an absolute path contains ':' ( i.e: C:\ ) """ |
76 | if target.startswith('/') or target.find(':') > -1: | |
53cf9963 BP |
77 | return 0, target |
78 | ||
79 | pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target) | |
80 | pid = ovs.daemon.read_pidfile(pidfile_name) | |
81 | if pid < 0: | |
82 | return -pid, "cannot read pidfile \"%s\"" % pidfile_name | |
83 | ||
03693b2f PB |
84 | if sys.platform == "win32": |
85 | return 0, "%s/%s.ctl" % (ovs.dirs.RUNDIR, target) | |
86 | else: | |
87 | return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid) | |
53cf9963 | 88 | |
884e0dfe | 89 | |
53cf9963 | 90 | command_register("help", "", 0, 0, _unixctl_help, None) |