[Kimchi-devel] [PATCH] [Kimchi 1/6] Rename vnc.py to websocket.py

Aline Manera alinefm at linux.vnet.ibm.com
Fri Feb 5 16:11:46 UTC 2016



On 02/04/2016 05:47 PM, Jose Ricardo Ziviani wrote:
>   - vnc.py doesn't reflect the source file, which starts the websockify
>     process and manages its security tokens.
>
> Signed-off-by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
> ---
>   model/vms.py |  6 ++--
>   root.py      |  8 +++---
>   vnc.py       | 92 ------------------------------------------------------------
>   websocket.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   4 files changed, 99 insertions(+), 99 deletions(-)
>   delete mode 100644 vnc.py
>   create mode 100644 websocket.py
>
> diff --git a/model/vms.py b/model/vms.py
> index 3faee5a..c76641c 100644
> --- a/model/vms.py
> +++ b/model/vms.py
> @@ -45,7 +45,7 @@ from wok.xmlutils.utils import xpath_get_text, xml_item_update
>   from wok.xmlutils.utils import dictize
>
>   from wok.plugins.kimchi import model
> -from wok.plugins.kimchi import vnc
> +from wok.plugins.kimchi import websocket
>   from wok.plugins.kimchi.config import READONLY_POOL_TYPE, get_kimchi_version
>   from wok.plugins.kimchi.kvmusertests import UserTests
>   from wok.plugins.kimchi.model.config import CapabilitiesModel
> @@ -1295,7 +1295,7 @@ class VMModel(object):
>               wok_log.error('Error deleting vm information from database: '
>                             '%s', e.message)
>
> -        vnc.remove_proxy_token(name)
> +        websocket.remove_proxy_token(name)
>
>       def start(self, name):
>           # make sure the ISO file has read permission
> @@ -1394,7 +1394,7 @@ class VMModel(object):
>           # (type, listen, port, passwd, passwdValidTo)
>           graphics_port = self._vm_get_graphics(name)[2]
>           if graphics_port is not None:
> -            vnc.add_proxy_token(name.encode('utf-8'), graphics_port)
> +            websocket.add_proxy_token(name.encode('utf-8'), graphics_port)
>           else:
>               raise OperationFailed("KCHVM0010E", {'name': name})
>
> diff --git a/root.py b/root.py
> index cee30f6..8269e3a 100644
> --- a/root.py
> +++ b/root.py
> @@ -1,7 +1,7 @@
>   #
>   # Project Kimchi
>   #
> -# Copyright IBM, Corp. 2013-2015
> +# Copyright IBM, Corp. 2013-2016
>   #
>   # This library is free software; you can redistribute it and/or
>   # modify it under the terms of the GNU Lesser General Public
> @@ -21,7 +21,7 @@ import json
>   import os
>   import cherrypy
>
> -from wok.plugins.kimchi import config, mockmodel, vnc
> +from wok.plugins.kimchi import config, mockmodel, websocket
>   from wok.plugins.kimchi.i18n import messages
>   from wok.plugins.kimchi.control import sub_nodes
>   from wok.plugins.kimchi.model import model as kimchiModel
> @@ -56,8 +56,8 @@ class KimchiRoot(WokRoot):
>               setattr(self, ident, node(self.model))
>
>           if isinstance(self.model, kimchiModel.Model):
> -            vnc_ws_proxy = vnc.new_ws_proxy()
> -            cherrypy.engine.subscribe('exit', vnc_ws_proxy.terminate)
> +            ws_proxy = websocket.new_ws_proxy()
> +            cherrypy.engine.subscribe('exit', ws_proxy.terminate)
>
>           self.api_schema = json.load(open(os.path.join(os.path.dirname(
>                                       os.path.abspath(__file__)), 'API.json')))
> diff --git a/vnc.py b/vnc.py
> deleted file mode 100644
> index 4f94ab2..0000000
> --- a/vnc.py
> +++ /dev/null
> @@ -1,92 +0,0 @@
> -#!/usr/bin/env python2
> -#
> -# Project Kimchi
> -#
> -# Copyright IBM, Corp. 2013-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, paths, PluginPaths
> -
> -
> -try:
> -    from websockify.token_plugins import TokenFile
> -    tokenFile = True
> -except ImportError:
> -    tokenFile = False
> -
> -
> -WS_TOKENS_DIR = os.path.join(PluginPaths('kimchi').state_dir, 'vnc-tokens')
> -
> -
> -def new_ws_proxy():
> -    try:
> -        os.makedirs(WS_TOKENS_DIR, mode=0755)
> -    except OSError as e:
> -        if e.errno == errno.EEXIST:
> -            pass
> -
> -    cert = config.get('server', 'ssl_cert')
> -    key = config.get('server', 'ssl_key')
> -    if not (cert and key):
> -        cert = '%s/wok-cert.pem' % paths.conf_dir
> -        key = '%s/wok-key.pem' % paths.conf_dir
> -
> -    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():
> -        server = WebSocketProxy(**params)
> -        server.start_server()
> -
> -    proc = Process(target=start_proxy)
> -    proc.start()
> -    return proc
> -
> -
> -def add_proxy_token(name, port):
> -    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('=')
> -        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
> diff --git a/websocket.py b/websocket.py
> new file mode 100644
> index 0000000..4f94ab2
> --- /dev/null
> +++ b/websocket.py
> @@ -0,0 +1,92 @@
> +#!/usr/bin/env python2
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013-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, paths, PluginPaths
> +
> +
> +try:
> +    from websockify.token_plugins import TokenFile
> +    tokenFile = True
> +except ImportError:
> +    tokenFile = False
> +
> +
> +WS_TOKENS_DIR = os.path.join(PluginPaths('kimchi').state_dir, 'vnc-tokens')
> +
> +

As it will be used for generic connections, I suggest to rename the 
directory to 'ws-tokens'

> +def new_ws_proxy():
> +    try:
> +        os.makedirs(WS_TOKENS_DIR, mode=0755)
> +    except OSError as e:
> +        if e.errno == errno.EEXIST:
> +            pass
> +
> +    cert = config.get('server', 'ssl_cert')
> +    key = config.get('server', 'ssl_key')
> +    if not (cert and key):
> +        cert = '%s/wok-cert.pem' % paths.conf_dir
> +        key = '%s/wok-key.pem' % paths.conf_dir
> +
> +    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():
> +        server = WebSocketProxy(**params)
> +        server.start_server()
> +
> +    proc = Process(target=start_proxy)
> +    proc.start()
> +    return proc
> +
> +
> +def add_proxy_token(name, port):
> +    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('=')
> +        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




More information about the Kimchi-devel mailing list