[Kimchi-devel] [PATCH] [Kimchi] Use Websocket facilities from WoK

dhbarboza82 at gmail.com dhbarboza82 at gmail.com
Fri Feb 24 13:45:34 UTC 2017


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

The 'websocket' module was moved from Kimchi to WoK to allow
WoK and all its plug-ins to have websocket capabilities.

This patch removes the existing websocket module inside
Kimchi and updates all the references to use the WoK
module instead.

The initialization of the ws_proxy was also removed from
root.py - this process is also being done by WoK now.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 model/vms.py |   2 +-
 root.py      |   4 +-
 websocket.py | 121 -----------------------------------------------------------
 3 files changed, 2 insertions(+), 125 deletions(-)
 delete mode 100644 websocket.py

diff --git a/model/vms.py b/model/vms.py
index e7ed7c2..6da4f3b 100644
--- a/model/vms.py
+++ b/model/vms.py
@@ -36,6 +36,7 @@ from lxml import etree, objectify
 from lxml.builder import E
 from xml.etree import ElementTree
 
+from wok import websocket
 from wok.asynctask import AsyncTask
 from wok.config import config
 from wok.exception import InvalidOperation, InvalidParameter
@@ -48,7 +49,6 @@ from wok.xmlutils.utils import dictize, xpath_get_text, xml_item_insert
 from wok.xmlutils.utils import xml_item_remove, xml_item_update
 
 from wok.plugins.kimchi import model
-from wok.plugins.kimchi import websocket
 from wok.plugins.kimchi import serialconsole
 from wok.plugins.kimchi.config import READONLY_POOL_TYPE, get_kimchi_version
 from wok.plugins.kimchi.kvmusertests import UserTests
diff --git a/root.py b/root.py
index 5ba4f68..4f131e7 100644
--- a/root.py
+++ b/root.py
@@ -22,7 +22,7 @@ import json
 import os
 import tempfile
 
-from wok.plugins.kimchi import config, mockmodel, websocket
+from wok.plugins.kimchi import config, mockmodel
 from wok.plugins.kimchi.i18n import messages
 from wok.plugins.kimchi.control import sub_nodes
 from wok.plugins.kimchi.model import model as kimchiModel
@@ -59,8 +59,6 @@ class Kimchi(WokRoot):
             cherrypy.engine.subscribe('exit', remove_objectstore)
         else:
             self.model = kimchiModel.Model()
-            ws_proxy = websocket.new_ws_proxy()
-            cherrypy.engine.subscribe('exit', ws_proxy.terminate)
 
         dev_env = wok_options.environment != 'production'
         super(Kimchi, self).__init__(self.model, dev_env)
diff --git a/websocket.py b/websocket.py
deleted file mode 100644
index 6268c8a..0000000
--- a/websocket.py
+++ /dev/null
@@ -1,121 +0,0 @@
-#!/usr/bin/env python2
-#
-# Project Kimchi
-#
-# Copyright IBM Corp, 2016
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
-
-import base64
-import errno
-import os
-
-from multiprocessing import Process
-from websockify import WebSocketProxy
-
-from wok.config import config, PluginPaths
-
-
-try:
-    from websockify.token_plugins import TokenFile
-    tokenFile = True
-except ImportError:
-    tokenFile = False
-
-try:
-    from websockify import ProxyRequestHandler as request_proxy
-except:
-    from websockify import WebSocketProxy as request_proxy
-
-
-WS_TOKENS_DIR = os.path.join(PluginPaths('kimchi').state_dir, 'ws-tokens')
-
-
-class CustomHandler(request_proxy):
-
-    def get_target(self, target_plugin, path):
-        if issubclass(CustomHandler, object):
-            target = super(CustomHandler, self).get_target(target_plugin,
-                                                           path)
-        else:
-            target = request_proxy.get_target(self, target_plugin, path)
-
-        if target[0] == 'unix_socket':
-            try:
-                self.server.unix_target = target[1]
-            except:
-                self.unix_target = target[1]
-        else:
-            try:
-                self.server.unix_target = None
-            except:
-                self.unix_target = None
-        return target
-
-
-def new_ws_proxy():
-    try:
-        os.makedirs(WS_TOKENS_DIR, mode=0755)
-    except OSError as e:
-        if e.errno == errno.EEXIST:
-            pass
-
-    params = {'listen_host': '127.0.0.1',
-              'listen_port': config.get('server', 'websockets_port'),
-              'ssl_only': False}
-
-    # old websockify: do not use TokenFile
-    if not tokenFile:
-        params['target_cfg'] = WS_TOKENS_DIR
-
-    # websockify 0.7 and higher: use TokenFile
-    else:
-        params['token_plugin'] = TokenFile(src=WS_TOKENS_DIR)
-
-    def start_proxy():
-        try:
-            server = WebSocketProxy(RequestHandlerClass=CustomHandler,
-                                    **params)
-        except TypeError:
-            server = CustomHandler(**params)
-
-        server.start_server()
-
-    proc = Process(target=start_proxy)
-    proc.start()
-    return proc
-
-
-def add_proxy_token(name, port, is_unix_socket=False):
-    with open(os.path.join(WS_TOKENS_DIR, name), 'w') as f:
-        """
-        From python documentation base64.urlsafe_b64encode(s)
-        substitutes - instead of + and _ instead of / in the
-        standard Base64 alphabet, BUT the result can still
-        contain = which is not safe in a URL query component.
-        So remove it when needed as base64 can work well without it.
-        """
-        name = base64.urlsafe_b64encode(name).rstrip('=')
-        if is_unix_socket:
-            f.write('%s: unix_socket:%s' % (name.encode('utf-8'), port))
-        else:
-            f.write('%s: localhost:%s' % (name.encode('utf-8'), port))
-
-
-def remove_proxy_token(name):
-    try:
-        os.unlink(os.path.join(WS_TOKENS_DIR, name))
-    except OSError:
-        pass
-- 
2.9.3



More information about the Kimchi-devel mailing list