Martin Sivák has uploaded a new change for review. Change subject: Allow using unix socket for the RPC port ......................................................................
Allow using unix socket for the RPC port This adds a code that is able to start an XML-RPC server listening on unix socket. MOM first tries to see if the port can be parsed as a number and uses it as a path when it is not. port -1 - no RPC port <number> - TCP based XML-RPC port <anything else> - unixsock based XML-RPC Change-Id: I6617d92c94259dff604acaa2b6481ef9595522a4 Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1227714 Signed-off-by: Martin Sivak <msi...@redhat.com> --- M configure.ac M mom/Makefile.am M mom/RPCServer.py A mom/unixrpc.py 4 files changed, 102 insertions(+), 8 deletions(-) git pull ssh://gerrit.ovirt.org:29418/mom refs/changes/28/42228/1 diff --git a/configure.ac b/configure.ac index 205522e..b43f558 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ define([VERSION_MAJOR], [0]) define([VERSION_MINOR], [4]) -define([VERSION_FIX], [4]) +define([VERSION_FIX], [5]) define([VERSION_NUMBER], VERSION_MAJOR[.]VERSION_MINOR[.]VERSION_FIX) define([VERSION_SUFFIX], [_master]) diff --git a/mom/Makefile.am b/mom/Makefile.am index c825b3f..a35b867 100644 --- a/mom/Makefile.am +++ b/mom/Makefile.am @@ -33,6 +33,7 @@ Plotter.py \ PolicyEngine.py \ RPCServer.py \ + unixrpc.py \ __init__.py \ $(NULL) diff --git a/mom/RPCServer.py b/mom/RPCServer.py index baa87cd..75b8c38 100644 --- a/mom/RPCServer.py +++ b/mom/RPCServer.py @@ -15,11 +15,10 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import threading -import ConfigParser -import time -import logging +import os.path from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler +from unixrpc import UnixXmlRpcServer from LogUtils import * @@ -47,14 +46,22 @@ def create_server(self): try: + unix_port = None port = self.config.getint('main', 'rpc-port') except ValueError: - self.logger.error("Unable to parse 'rpc-port' configuration setting") + port = None + unix_port = self.config.get('main', 'rpc-port') + self.logger.info("Using unix socket "+unix_port) + + if unix_port is None and (port is None or port < 0): return None - if port is None or port < 0: - return None - self.server = SimpleXMLRPCServer(("localhost", port), + + if unix_port: + self.server = UnixXmlRpcServer(unix_port) + else: + self.server = SimpleXMLRPCServer(("localhost", port), requestHandler=RequestHandler, logRequests=0) + self.server.register_introspection_functions() self.server.register_instance(self.momFuncs) diff --git a/mom/unixrpc.py b/mom/unixrpc.py new file mode 100644 index 0000000..4ae8ff6 --- /dev/null +++ b/mom/unixrpc.py @@ -0,0 +1,86 @@ +# 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. + +# This module contains code that implements a XML-RPC client/server. +# Taken from https://github.com/tmbx/kpython on the 11th of June 2015 at 14:52 CEST +# using the git commit revision dea4a6cc07 +# Authors: +# Karim Yaghmour +# Martin Sivak +# License: +# Apache License, Version 2.0 +# + +import os +import SocketServer +import SimpleXMLRPCServer +import xmlrpclib +import httplib +import socket +import string + +# This function converts a string to hexadecimal. Function taken from the Python +# Cookbook. +def str_to_hex(s): + lst = [] + for ch in s: + hv = hex(ord(ch)).replace('0x', '') + if len(hv) == 1: + hv = '0'+hv + lst.append(hv) + + return reduce(lambda x,y:x+y, lst) + +# This function converts an hexadecimal number to a string. +def hex_to_str(s): + return s and chr(string.atoi(s[:2], base=16)) + hex_to_str(s[2:]) or '' + +class UnixXmlRpcHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): + disable_nagle_algorithm = False + +# This class implements a XML-RPC server that binds to a UNIX socket. The path +# to the UNIX socket to create and the instance containing the user-defined RPC +# methods must be provided. The caller should call 'serve_forever()' to handle +# the RPC calls. +class UnixXmlRpcServer(SocketServer.UnixStreamServer, SimpleXMLRPCServer.SimpleXMLRPCDispatcher): + address_family = socket.AF_UNIX + allow_address_reuse = True + + def __init__(self, sock_path, request_handler=UnixXmlRpcHandler): + if os.path.exists(sock_path): os.unlink(sock_path) + self.logRequests = 0 + SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, encoding=None, allow_none=1) + SocketServer.UnixStreamServer.__init__(self, sock_path, request_handler) + +# This class implements a XML-RPC client that connects to a UNIX socket. The +# path to the UNIX socket to create must be provided. The RPC calls can be made +# directly from this object by calling the RPC methods by their name (i.e. as if +# they belonged to this object). +class UnixXmlRpcClient(xmlrpclib.ServerProxy): + def __init__(self, sock_path): + + # We can't pass funny characters in the host part of a URL, so we encode + # the socket path in hexadecimal. + xmlrpclib.ServerProxy.__init__(self, 'http://' + str_to_hex(sock_path), transport=UnixXmlRpcTransport(), + allow_none=1) + +# Helper classes for UnixXmlRpcClient. +class UnixXmlRpcTransport(xmlrpclib.Transport): + def make_connection(self, host): + return UnixXmlRpcHttpConn(host) + +class UnixXmlRpcHttpConn(httplib.HTTPConnection): + def connect(self): + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.connect(hex_to_str(self.host)) + + -- To view, visit https://gerrit.ovirt.org/42228 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6617d92c94259dff604acaa2b6481ef9595522a4 Gerrit-PatchSet: 1 Gerrit-Project: mom Gerrit-Branch: master Gerrit-Owner: Martin Sivák <msi...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches