mirror of
https://github.com/openvswitch/ovs
synced 2025-08-31 06:15:47 +00:00
python: Break unixctl implementation into registry, client, and server.
I wish to add some unixctl commands to the Python vlog module. However, importing ovs.unixctl in ovs.vlog creates a circular dependency, because ovs.unixctl imports ovs.vlog already. The solution, in this commit, is to break the unixctl module into three parts: a register (ovs.unixctl) that does not depend on ovs.vlog, and client (ovs.unixctl.client) and server (ovs.unixctl.server) modules that do. This breaks the circular dependency. Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
@@ -27,7 +27,9 @@ ovs_pyfiles = \
|
||||
python/ovs/socket_util.py \
|
||||
python/ovs/stream.py \
|
||||
python/ovs/timeval.py \
|
||||
python/ovs/unixctl.py \
|
||||
python/ovs/unixctl/__init__.py \
|
||||
python/ovs/unixctl/client.py \
|
||||
python/ovs/unixctl/server.py \
|
||||
python/ovs/util.py \
|
||||
python/ovs/version.py \
|
||||
python/ovs/vlog.py
|
||||
|
83
python/ovs/unixctl/__init__.py
Normal file
83
python/ovs/unixctl/__init__.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# Copyright (c) 2011, 2012 Nicira, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at:
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import types
|
||||
|
||||
import ovs.util
|
||||
|
||||
commands = {}
|
||||
strtypes = types.StringTypes
|
||||
|
||||
|
||||
class _UnixctlCommand(object):
|
||||
def __init__(self, usage, min_args, max_args, callback, aux):
|
||||
self.usage = usage
|
||||
self.min_args = min_args
|
||||
self.max_args = max_args
|
||||
self.callback = callback
|
||||
self.aux = aux
|
||||
|
||||
|
||||
def _unixctl_help(conn, unused_argv, unused_aux):
|
||||
reply = "The available commands are:\n"
|
||||
command_names = sorted(commands.keys())
|
||||
for name in command_names:
|
||||
reply += " "
|
||||
usage = commands[name].usage
|
||||
if usage:
|
||||
reply += "%-23s %s" % (name, usage)
|
||||
else:
|
||||
reply += name
|
||||
reply += "\n"
|
||||
conn.reply(reply)
|
||||
|
||||
|
||||
def command_register(name, usage, min_args, max_args, callback, aux):
|
||||
""" Registers a command with the given 'name' to be exposed by the
|
||||
UnixctlServer. 'usage' describes the arguments to the command; it is used
|
||||
only for presentation to the user in "help" output.
|
||||
|
||||
'callback' is called when the command is received. It is passed a
|
||||
UnixctlConnection object, the list of arguments as unicode strings, and
|
||||
'aux'. Normally 'callback' should reply by calling
|
||||
UnixctlConnection.reply() or UnixctlConnection.reply_error() before it
|
||||
returns, but if the command cannot be handled immediately, then it can
|
||||
defer the reply until later. A given connection can only process a single
|
||||
request at a time, so a reply must be made eventually to avoid blocking
|
||||
that connection."""
|
||||
|
||||
assert isinstance(name, strtypes)
|
||||
assert isinstance(usage, strtypes)
|
||||
assert isinstance(min_args, int)
|
||||
assert isinstance(max_args, int)
|
||||
assert isinstance(callback, types.FunctionType)
|
||||
|
||||
if name not in commands:
|
||||
commands[name] = _UnixctlCommand(usage, min_args, max_args, callback,
|
||||
aux)
|
||||
|
||||
def socket_name_from_target(target):
|
||||
assert isinstance(target, strtypes)
|
||||
|
||||
if target.startswith("/"):
|
||||
return 0, target
|
||||
|
||||
pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target)
|
||||
pid = ovs.daemon.read_pidfile(pidfile_name)
|
||||
if pid < 0:
|
||||
return -pid, "cannot read pidfile \"%s\"" % pidfile_name
|
||||
|
||||
return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid)
|
||||
|
||||
command_register("help", "", 0, 0, _unixctl_help, None)
|
70
python/ovs/unixctl/client.py
Normal file
70
python/ovs/unixctl/client.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# Copyright (c) 2011, 2012 Nicira, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at:
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import copy
|
||||
import errno
|
||||
import os
|
||||
import types
|
||||
|
||||
import ovs.jsonrpc
|
||||
import ovs.stream
|
||||
import ovs.util
|
||||
|
||||
|
||||
vlog = ovs.vlog.Vlog("unixctl_client")
|
||||
strtypes = types.StringTypes
|
||||
|
||||
|
||||
class UnixctlClient(object):
|
||||
def __init__(self, conn):
|
||||
assert isinstance(conn, ovs.jsonrpc.Connection)
|
||||
self._conn = conn
|
||||
|
||||
def transact(self, command, argv):
|
||||
assert isinstance(command, strtypes)
|
||||
assert isinstance(argv, list)
|
||||
for arg in argv:
|
||||
assert isinstance(arg, strtypes)
|
||||
|
||||
request = ovs.jsonrpc.Message.create_request(command, argv)
|
||||
error, reply = self._conn.transact_block(request)
|
||||
|
||||
if error:
|
||||
vlog.warn("error communicating with %s: %s"
|
||||
% (self._conn.name, os.strerror(error)))
|
||||
return error, None, None
|
||||
|
||||
if reply.error is not None:
|
||||
return 0, str(reply.error), None
|
||||
else:
|
||||
assert reply.result is not None
|
||||
return 0, None, str(reply.result)
|
||||
|
||||
def close(self):
|
||||
self._conn.close()
|
||||
self.conn = None
|
||||
|
||||
@staticmethod
|
||||
def create(path):
|
||||
assert isinstance(path, str)
|
||||
|
||||
unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
|
||||
error, stream = ovs.stream.Stream.open_block(
|
||||
ovs.stream.Stream.open(unix))
|
||||
|
||||
if error:
|
||||
vlog.warn("failed to connect to %s" % path)
|
||||
return error, None
|
||||
|
||||
return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))
|
@@ -17,89 +17,19 @@ import errno
|
||||
import os
|
||||
import types
|
||||
|
||||
import ovs.daemon
|
||||
import ovs.dirs
|
||||
import ovs.jsonrpc
|
||||
import ovs.stream
|
||||
import ovs.unixctl
|
||||
import ovs.util
|
||||
import ovs.version
|
||||
import ovs.vlog
|
||||
|
||||
Message = ovs.jsonrpc.Message
|
||||
vlog = ovs.vlog.Vlog("unixctl")
|
||||
commands = {}
|
||||
vlog = ovs.vlog.Vlog("unixctl_server")
|
||||
strtypes = types.StringTypes
|
||||
|
||||
|
||||
class _UnixctlCommand(object):
|
||||
def __init__(self, usage, min_args, max_args, callback, aux):
|
||||
self.usage = usage
|
||||
self.min_args = min_args
|
||||
self.max_args = max_args
|
||||
self.callback = callback
|
||||
self.aux = aux
|
||||
|
||||
|
||||
def _unixctl_help(conn, unused_argv, unused_aux):
|
||||
assert isinstance(conn, UnixctlConnection)
|
||||
reply = "The available commands are:\n"
|
||||
command_names = sorted(commands.keys())
|
||||
for name in command_names:
|
||||
reply += " "
|
||||
usage = commands[name].usage
|
||||
if usage:
|
||||
reply += "%-23s %s" % (name, usage)
|
||||
else:
|
||||
reply += name
|
||||
reply += "\n"
|
||||
conn.reply(reply)
|
||||
|
||||
|
||||
def _unixctl_version(conn, unused_argv, version):
|
||||
assert isinstance(conn, UnixctlConnection)
|
||||
version = "%s (Open vSwitch) %s" % (ovs.util.PROGRAM_NAME, version)
|
||||
conn.reply(version)
|
||||
|
||||
|
||||
def command_register(name, usage, min_args, max_args, callback, aux):
|
||||
""" Registers a command with the given 'name' to be exposed by the
|
||||
UnixctlServer. 'usage' describes the arguments to the command; it is used
|
||||
only for presentation to the user in "help" output.
|
||||
|
||||
'callback' is called when the command is received. It is passed a
|
||||
UnixctlConnection object, the list of arguments as unicode strings, and
|
||||
'aux'. Normally 'callback' should reply by calling
|
||||
UnixctlConnection.reply() or UnixctlConnection.reply_error() before it
|
||||
returns, but if the command cannot be handled immediately, then it can
|
||||
defer the reply until later. A given connection can only process a single
|
||||
request at a time, so a reply must be made eventually to avoid blocking
|
||||
that connection."""
|
||||
|
||||
assert isinstance(name, strtypes)
|
||||
assert isinstance(usage, strtypes)
|
||||
assert isinstance(min_args, int)
|
||||
assert isinstance(max_args, int)
|
||||
assert isinstance(callback, types.FunctionType)
|
||||
|
||||
if name not in commands:
|
||||
commands[name] = _UnixctlCommand(usage, min_args, max_args, callback,
|
||||
aux)
|
||||
|
||||
|
||||
def socket_name_from_target(target):
|
||||
assert isinstance(target, strtypes)
|
||||
|
||||
if target.startswith("/"):
|
||||
return 0, target
|
||||
|
||||
pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target)
|
||||
pid = ovs.daemon.read_pidfile(pidfile_name)
|
||||
if pid < 0:
|
||||
return -pid, "cannot read pidfile \"%s\"" % pidfile_name
|
||||
|
||||
return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid)
|
||||
|
||||
|
||||
class UnixctlConnection(object):
|
||||
def __init__(self, rpc):
|
||||
assert isinstance(rpc, ovs.jsonrpc.Connection)
|
||||
@@ -177,7 +107,7 @@ class UnixctlConnection(object):
|
||||
error = None
|
||||
params = request.params
|
||||
method = request.method
|
||||
command = commands.get(method)
|
||||
command = ovs.unixctl.commands.get(method)
|
||||
if command is None:
|
||||
error = '"%s" is not a valid command' % method
|
||||
elif len(params) < command.min_args:
|
||||
@@ -200,6 +130,11 @@ class UnixctlConnection(object):
|
||||
self.reply_error(error)
|
||||
|
||||
|
||||
def _unixctl_version(conn, unused_argv, version):
|
||||
assert isinstance(conn, UnixctlConnection)
|
||||
version = "%s (Open vSwitch) %s" % (ovs.util.PROGRAM_NAME, version)
|
||||
conn.reply(version)
|
||||
|
||||
class UnixctlServer(object):
|
||||
def __init__(self, listener):
|
||||
assert isinstance(listener, ovs.stream.PassiveStream)
|
||||
@@ -262,8 +197,8 @@ class UnixctlServer(object):
|
||||
% path)
|
||||
return error, None
|
||||
|
||||
command_register("help", "", 0, 0, _unixctl_help, None)
|
||||
command_register("version", "", 0, 0, _unixctl_version, version)
|
||||
ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version,
|
||||
version)
|
||||
|
||||
return 0, UnixctlServer(listener)
|
||||
|
Reference in New Issue
Block a user