This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch brokerclient in repository python-x2go. commit 8014735e439c4f6a4d3c71e532d6e8d525103f27 Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de> Date: Tue Mar 4 13:58:21 2014 +0100 Provide basic session profile backend for a http broker. --- debian/changelog | 1 + x2go/backends/profiles/base.py | 2 + x2go/backends/profiles/httpbroker.py | 83 ++++++++++++++++++++++++++++------ x2go/client.py | 10 +++- 4 files changed, 80 insertions(+), 16 deletions(-) diff --git a/debian/changelog b/debian/changelog index 0c28057..3d245b2 100644 --- a/debian/changelog +++ b/debian/changelog @@ -9,6 +9,7 @@ python-x2go (0.5.0.0-0x2go1) UNRELEASED; urgency=low not really used by third-party products, if at all). - Fix setting default values in X2GoClientXConfig class. - Default to xdg-open as default PDF viewer command. + - Provide basic session profile backend for a http broker. -- Mike Gabriel <mike.gabriel@das-netzwerkteam.de> Sun, 05 Jan 2014 16:35:57 +0100 diff --git a/x2go/backends/profiles/base.py b/x2go/backends/profiles/base.py index 6985f9a..7660165 100644 --- a/x2go/backends/profiles/base.py +++ b/x2go/backends/profiles/base.py @@ -64,6 +64,8 @@ class X2GoSessionProfiles(): self._cached_profile_names = [] self._profiles_need_profile_id_renewal = [] self.write_user_config = False + self.config_files = None + self.user_config_file = None if logger is None: self.logger = log.X2GoLogger(loglevel=loglevel) diff --git a/x2go/backends/profiles/httpbroker.py b/x2go/backends/profiles/httpbroker.py index 0d27cb4..1739597 100644 --- a/x2go/backends/profiles/httpbroker.py +++ b/x2go/backends/profiles/httpbroker.py @@ -26,8 +26,15 @@ applications. """ __NAME__ = 'x2gosessionprofiles-pylib' +import copy +import re +import requests +try: import simplejson as json +except ImportError: import json + # Python X2Go modules -from x2go.defaults import X2GO_SESSIONPROFILE_DEFAULTS +from x2go.defaults import X2GO_SESSIONPROFILE_DEFAULTS as _X2GO_SESSIONPROFILE_DEFAULTS +from x2go.defaults import CURRENT_LOCAL_USER as _CURRENT_LOCAL_USER import x2go.backends.profiles.base as base import x2go.log as log @@ -35,14 +42,21 @@ from x2go.x2go_exceptions import X2GoNotImplementedYetException class X2GoSessionProfiles(base.X2GoSessionProfiles): - defaultSessionProfile = X2GO_SESSIONPROFILE_DEFAULTS + defaultSessionProfile = _X2GO_SESSIONPROFILE_DEFAULTS - def __init__(self, session_profile_defaults=None, logger=None, loglevel=log.loglevel_DEFAULT): + def __init__(self, session_profile_defaults=None, + broker_url="http://localhost:8080/json/", + broker_user=None, + broker_password=None, + logger=None, loglevel=log.loglevel_DEFAULT, + **kwargs): """\ Retrieve X2Go session profiles from a HTTP(S) session broker. @param session_profile_defaults: a default session profile @type session_profile_defaults: C{dict} + @param broker_url: URL for accessing the X2Go Session Broker + @type broker_url: C{str} @param logger: you can pass an L{X2GoLogger} object to the L{X2GoSessionProfilesHTTPSBROKER} constructor @type logger: L{X2GoLogger} instance @@ -51,8 +65,48 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles): @type loglevel: C{int} """ + match = re.match('^(?P<protocol>(http(|s)))://(|(?P<user>[a-zA-Z0-9_\.-]+)(|:(?P<password>.*))@)(?P<hostname>[a-zA-Z0-9\.-]+)(|:(?P<port>[0-9]+))($|/(?P<path>.*)$)', broker_url) + p = match.groupdict() + if p['user']: + self.broker_user = p['user'] + else: + self.broker_user = broker_user + if p['password']: + self.broker_password = p['password'] + else: + self.broker_password = broker_password + + # fine-tune the URL + p['path'] = "/{path}".format(**p) + if p['port'] is not None: + p['port'] = ":{port}".format(**p) + + self.broker_url = "{protocol}://{hostname}{port}{path}".format(**p) + base.X2GoSessionProfiles.__init__(self, session_profile_defaults=session_profile_defaults, logger=logger, loglevel=loglevel) + def broker_simpleauth(self, broker_user, broker_password): + payload = { + 'user': broker_user, + 'password': broker_password, + } + r = requests.post(self.broker_url, data=payload) + if r.status_code == 200: + self.broker_user = broker_user + self.broker_password = broker_password + return True + return False + + def broker_listsessions(self): + payload = { + 'task': 'listsessions', + 'user': self.broker_user, + 'password': self.broker_password, + } + r = requests.post(self.broker_url, data=payload) + if r.status_code == 200 and r.headers['content-type'].startswith("text/json"): + return json.loads(r.text) + def _populate_session_profiles(self): """\ Populate the set of session profiles by loading the session @@ -62,31 +116,30 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles): @rtype: C{dict} """ - session_profiles = LOAD + session_profiles = self.broker_listsessions() + _session_profiles = copy.deepcopy(session_profiles) - for session_profile in session_profiles: + for session_profile in _session_profiles: for key, default_value in self.defaultSessionProfile.iteritems(): - if not self.iniConfig.has_option(session_profile, key): - self._storeValue(session_profile, key, default_value) - # update cached meta type session profile information - self.get_profile_metatype(session_profile) + if not session_profiles[session_profile].has_key(key): + session_profiles[session_profile][key] = default_value return session_profiles def _write(self): - print "BROKER CLIENT: WRITING SESSION PROFILES IS NOT SUPPORTED" + print "not suported" def _delete_profile(self, profile_id): - print "BROKER CLIENT: DELETING SESSION PROFILES IS NOT SUPPORTED" + del self.session_profiles[profile_id] def _update_value(self, profile_id, option, value): - print "BROKER CLIENT: MODIFYING SESSION PROFILES IS NOT SUPPORTED" + self.session_profiles[profile_id][option] = value def _get_profile_parameter(self, profile_id, option, key_type): - print "TODO" + return key_type(self.session_profiles[profile_id][option]) def _get_profile_options(self, profile_id): - print "TODO" + return self.session_profiles[profile_id].keys() def _get_profile_ids(self): - print "TODO" + return self.session_profiles.keys() diff --git a/x2go/client.py b/x2go/client.py index debba28..b0bace6 100644 --- a/x2go/client.py +++ b/x2go/client.py @@ -182,6 +182,7 @@ class X2GoClient(object): profiles_backend=_BACKENDS['X2GoSessionProfiles']['default'], settings_backend=_BACKENDS['X2GoClientSettings']['default'], printing_backend=_BACKENDS['X2GoClientPrinting']['default'], + broker_url=None, client_rootdir=None, sessions_rootdir=None, ssh_rootdir=None, @@ -268,6 +269,13 @@ class X2GoClient(object): self.info_backend = utils._get_backend_class(info_backend, "X2GoServerSessionInfo") self.list_backend = utils._get_backend_class(list_backend, "X2GoServerSessionList") self.proxy_backend = utils._get_backend_class(proxy_backend, "X2GoProxy") + print broker_url + if broker_url is not None: + if broker_url.startswith('ssh://'): + profiles_backend = 'sshbroker' + elif broker_url.startswith('http://') or broker_url.startswith('https://'): + self.logger('using http(s) broker at URL %s' % broker_url, loglevel=log.loglevel_NOTICE) + profiles_backend = 'httpbroker' self.profiles_backend = utils._get_backend_class(profiles_backend, "X2GoSessionProfiles") self.settings_backend = utils._get_backend_class(settings_backend, "X2GoClientSettings") self.printing_backend = utils._get_backend_class(printing_backend, "X2GoClientPrinting") @@ -288,7 +296,7 @@ class X2GoClient(object): _settings_config_file = os.path.join(self.client_rootdir, _X2GO_SETTINGS_FILENAME) _printing_config_file = os.path.join(self.client_rootdir, _X2GO_PRINTING_FILENAME) _xconfig_config_file = os.path.join(self.client_rootdir, _X2GO_XCONFIG_FILENAME) - self.session_profiles = self.profiles_backend(config_files=[_sessions_config_file], logger=self.logger) + self.session_profiles = self.profiles_backend(config_files=[_sessions_config_file], logger=self.logger, broker_url=broker_url) self.client_settings = self.settings_backend(config_files=[_settings_config_file], logger=self.logger) self.client_printing = self.printing_backend(config_files=[_printing_config_file], client_instance=self, logger=self.logger) else: -- Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/python-x2go.git