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