Frank Kobzik has uploaded a new change for review. Change subject: core: Adjust websockify to the new API ......................................................................
core: Adjust websockify to the new API python-websockify v0.6.0 changes client API. This change makes it possible to use both legacy and newer versions of python-websockify. After new python-websockify land in dowstream, ovirt-websocket-proxy can be simplified by cutting some parts of the code. Upgrading to websockify 0.6.0 improves 2 things: 1, Logging - library uses standard python logging facilities. 2, Killing zombies - library takes care of killling zombie processes (we had to handle it ourselves). Change-Id: I13f94f91a1bdee0bfa93dc263de7e51395f7d7ce Signed-off-by: Frantisek Kobzik <fkob...@redhat.com> Bug-Url: https://bugzilla.redhat.com/1092611 Bug-Url: https://bugzilla.redhat.com/1098574 --- M packaging/services/ovirt-websocket-proxy/ovirt-websocket-proxy.py 1 file changed, 77 insertions(+), 26 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/38/32138/1 diff --git a/packaging/services/ovirt-websocket-proxy/ovirt-websocket-proxy.py b/packaging/services/ovirt-websocket-proxy/ovirt-websocket-proxy.py index 4477f23..6b76039 100755 --- a/packaging/services/ovirt-websocket-proxy/ovirt-websocket-proxy.py +++ b/packaging/services/ovirt-websocket-proxy/ovirt-websocket-proxy.py @@ -36,39 +36,81 @@ from ovirt_engine import service -class OvirtWebSocketProxy(websockify.WebSocketProxy): - """" - Websocket proxy for usage with oVirt engine. - Leverages websocket.py by Joel Martin - """ +class OvirtProxyRequestHandlerBase(): + def get_target(self, target_cfg, path): + """ + Parses the path, extracts a token, and looks for a valid + target for that token in the configuration file(s). Returns + target_host and target_port if successful and sets an ssl_target + flag. + """ + self._ticketDecoder = TicketDecoder(True, None) + connection_data = json.loads( + urllib.unquote(self._ticketDecoder.decode(path[1:])) + ) + target_host = connection_data['host'].encode('utf8') + target_port = connection_data['port'].encode('utf8') + self.ssl_target = connection_data['ssl_target'] - def __init__(self, *args, **kwargs): - self._ticketDecoder = kwargs.pop('ticketDecoder') - super(OvirtWebSocketProxy, self).__init__(*args, **kwargs) + return (target_host, target_port) - def get_target(self, target_cfg, path): + +if getattr(websockify, 'ProxyRequestHandler', None) is not None: + class OvirtWebSocketProxy(websockify.WebSocketProxy): + """" + Websocket proxy for usage with oVirt engine. + Leverages websocket.py by Joel Martin """ - Parses the path, extracts a token, and looks for a valid - target for that token in the configuration file(s). Returns - target_host and target_port if successful and sets an ssl_target - flag. + + _logger = None + + def __init__(self, *args, **kwargs): + self._ticketDecoder = kwargs.pop('ticketDecoder') + super(OvirtWebSocketProxy, self).__init__(*args, **kwargs) + + @staticmethod + def get_logger(): + return OvirtWebSocketProxy._logger + + class OvirtProxyRequestHandler(OvirtProxyRequestHandlerBase, + websockify.ProxyRequestHandler): + pass + +else: # remove this branch when python-websockify is >= 0.6 + class OvirtWebSocketProxy(OvirtProxyRequestHandlerBase, + websockify.WebSocketProxy): + """" + Websocket proxy for oVirt (backwards compatibility version). """ - connection_data = json.loads( - urllib.unquote(self._ticketDecoder.decode(path[1:])) - ) - target_host = connection_data['host'].encode('utf8') - target_port = connection_data['port'].encode('utf8') - self.ssl_target = connection_data['ssl_target'] - return (target_host, target_port) + + _logger = None + + def __init__(self, *args, **kwargs): + kwargs.pop('RequestHandlerClass') # unused + self._ticketDecoder = kwargs.pop('ticketDecoder') + super(OvirtWebSocketProxy, self).__init__(*args, **kwargs) + + def msg(self, *args, **kwargs): + OvirtWebSocketProxy._logger.info(*args, **kwargs) + + def vmsg(self, *args, **kwargs): + OvirtWebSocketProxy._logger.debug(*args, **kwargs) + + def warn(self, *args, **kwargs): + OvirtWebSocketProxy._logger.warn(*args, **kwargs) + + def print_traffic(self, token="."): + if self.traffic: + sys.stdout.write(token) + sys.stdout.flush() + + class OvirtProxyRequestHandler(): + pass class TicketDecoder(object): - def __init__( - self, - insecure, - certificate - ): + def __init__(self, insecure, certificate): self._insecure = insecure if not insecure: self._key = X509.load_cert( @@ -179,6 +221,9 @@ # mode only (SIGINT). # it also expect exit at the middle of processing. # so we comply. + # + # TODO - after upgrade to python-websockify >= 0.6, remove + # this workaround def myterm(signo, frame): sys.exit(0) oldterm = signal.getsignal(signal.SIGTERM) @@ -186,6 +231,8 @@ # WORKAROUND-END try: + if OvirtWebSocketProxy._logger is None: + OvirtWebSocketProxy._logger = self.logger OvirtWebSocketProxy( listen_host=self._config.get('PROXY_HOST'), listen_port=self._config.get('PROXY_PORT'), @@ -212,11 +259,15 @@ target_host=None, target_port=None, wrap_mode='exit', - wrap_cmd=None + wrap_cmd=None, + RequestHandlerClass=OvirtProxyRequestHandler ).start_server() # WORKAROUND-BEGIN # websockify exit because of signals. # redirect it to expected termination sequence. + # + # TODO - after upgrade to python-websockify >= 0.6, remove + # this workaround except SystemExit: self.logger.debug('SystemExit', exc_info=True) finally: -- To view, visit http://gerrit.ovirt.org/32138 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I13f94f91a1bdee0bfa93dc263de7e51395f7d7ce Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Frank Kobzik <fkob...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches