[X2Go-Commits] [x2gobroker] 05/07: Fully rewrite agent.py

git-admin at x2go.org git-admin at x2go.org
Fri Mar 28 23:58:42 CET 2014


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2gobroker.

commit f92fc103e73bef1f6ed92e0d7ec60cdf1133e531
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Mon Mar 24 14:39:48 2014 +0100

    Fully rewrite agent.py
---
 debian/changelog    |    1 +
 x2gobroker/agent.py |  135 ++++++++++++++++++++++-----------------------------
 2 files changed, 59 insertions(+), 77 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index a65728d..fd5259b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -113,6 +113,7 @@ x2gobroker (0.0.3.0-0x2go1) UNRELEASED; urgency=low
     - Fix https brokerage in x2gobroker-daemon-debug.
     - Load X2GOBROKER_DAEMON_USER's known_hosts key file before doing
       remote agent calls.
+    - Fully rewrite agent.py.
   * debian/control:
     + Replace LDAP support with session brokerage support in LONG_DESCRIPTION.
     + Fix SYNOPSIS texts.
diff --git a/x2gobroker/agent.py b/x2gobroker/agent.py
index 55fc083..b5f0631 100644
--- a/x2gobroker/agent.py
+++ b/x2gobroker/agent.py
@@ -78,21 +78,41 @@ def has_remote_broker_agent_setup():
     elif os.path.exists(os.path.join(home, '.ssh', 'id_ecdsa')):
         return True
 
+def call_broker_agent(username, task, cmdline_args=[], remote_agent=None, **kwargs):
+    """\
+    Launch X2Go Broker Agent and process its output.
 
-def call_local_broker_agent(username, mode, cmdline_args=[]):
+    @param username: run the broker agent for this user
+    @type username: C{unicode}
+    @param task: task name to execute via the broker agent (listsessions, getservers, etc.)
+    @type task: C{unicode}
+    @param cmdline_args: additional command line parameters for the broker agent
+    @type cmdline_args: C{list}
+    @param remote_agent: if not C{None} call a remote broker agent via SSH
+    @type remoate_agent: C{dict}
+
+    """
+    if remote_agent in (u'LOCAL', None):
+        result = _call_local_broker_agent(username=username, task=task, cmdline_args=cmdline_args)
+    else:
+        result = _call_remote_broker_agent(username=username, task=task, cmdline_args=cmdline_args, remote_agent=remote_agent)
+
+def _call_local_broker_agent(username, task, cmdline_args=[]):
     """\
     Launch X2Go Broker Agent locally and process its output.
 
     @param username: run the broker agent for this user
     @type username: C{unicode}
-    @param mode: execution mode of the broker (listsessions, getservers, etc.)
-    @type mode: C{unicode}
+    @param task: task name to execute via the broker agent (listsessions, getservers, etc.)
+    @type task: C{unicode}
+    @param cmdline_args: additional command line parameters for the broker agent
+    @type cmdline_args: C{list}
 
     """
     cmd_line = [
         '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD),
         '{username}'.format(username=username),
-        '{mode}'.format(mode=mode),
+        '{task}'.format(task=task),
     ]
 
     for cmdline_arg in cmdline_args:
@@ -118,14 +138,16 @@ def call_local_broker_agent(username, mode, cmdline_args=[]):
     raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Query to local X2Go Broker Agent failed with no response')
 
 
-def call_remote_broker_agent(username, mode, cmdline_args=[], remote_agent=None):
+def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None):
     """\
     Launch remote X2Go Broker Agent via SSH and process its output.
 
     @param username: run the broker agent for this user
     @type username: C{unicode}
-    @param mode: execution mode of the broker (listsessions, getservers, etc.)
-    @type mode: C{unicode}
+    @param task: task name to execute via the broker agent (listsessions, getservers, etc.)
+    @type task: C{unicode}
+    @param cmdline_args: additional command line parameters for the broker agent
+    @type cmdline_args: C{list}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
@@ -138,7 +160,7 @@ def call_remote_broker_agent(username, mode, cmdline_args=[], remote_agent=None)
     cmd_line = [
         '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD),
         '{username}'.format(username=username),
-        '{mode}'.format(mode=mode),
+        '{task}'.format(task=task),
     ]
 
     for cmdline_arg in cmdline_args:
@@ -212,92 +234,73 @@ def icmp_ping(hostname):
     return True
 
 
-def ping(query_mode='LOCAL', remote_agent=None, **kwargs):
+def ping(remote_agent=None, **kwargs):
     """\
     Ping X2Go Broker Agent.
 
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
     username='foo'
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='ping')
+    if remote_agent is None:
+        return _call_local_broker_agent(username)
     else:
         return remote_agent is not None and \
                icmp_ping(remote_agent['hostname']) and \
-               call_remote_broker_agent(username, mode='ping', remote_agent=remote_agent)
+               _call_remote_broker_agent(username, task='ping', remote_agent=remote_agent)
 tasks['ping'] = ping
 
 
-def list_sessions(username, query_mode='LOCAL', remote_agent=None):
+def list_sessions(username, remote_agent=None, **kwargs):
     """\
     Query X2Go Broker Agent for a session list for a given username.
 
     @param username: run the query on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='listsessions')
-    else:
-        return call_remote_broker_agent(username, mode='listsessions', remote_agent=remote_agent)
+    return call_broker_agent(username, task='listsessions', remote_agent=remote_agent, **kwargs)
 tasks['listsessions'] = list_sessions
 
 
-def suspend_session(username, session_name, query_mode='LOCAL', remote_agent=None, **kwargs):
+def suspend_session(username, session_name, remote_agent=None, **kwargs):
     """\
     Trigger a session suspensions via the X2Go Broker Agent.
 
     @param username: suspend the session on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='suspendsession', cmdline_args=[session_name, ], )
-    else:
-        return call_remote_broker_agent(username, mode='suspendsession', cmdline_args=[session_name, ], remote_agent=remote_agent)
+    return call_broker_agent(username, task='suspendsession', cmdline_args=[session_name, ], remote_agent=remote_agent, **kwargs)
 tasks['suspendsession'] = suspend_session
 
 
-def terminate_session(username, session_name, query_mode='LOCAL', remote_agent=None, **kwargs):
+def terminate_session(username, session_name, remote_agent=None, **kwargs):
     """\
     Trigger a session termination via the X2Go Broker Agent.
 
     @param username: terminate the session on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='terminatesession', cmdline_args=[session_name, ], )
-    else:
-        return call_remote_broker_agent(username, mode='terminatesession', cmdline_args=[session_name, ], remote_agent=remote_agent)
+    return call_broker_agent(username, task='terminatesession', cmdline_args=[session_name, ], remote_agent=remote_agent, **kwargs)
 tasks['terminatesession'] = terminate_session
 
 
-def has_sessions(username, query_mode='LOCAL', remote_agent=None):
+def has_sessions(username, remote_agent=None, **kwargs):
     """\
     Query X2Go Broker Agent to detect running/suspended sessions on
     the remote X2Go Server (farm).
 
     @param username: run the query on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
@@ -305,11 +308,14 @@ def has_sessions(username, query_mode='LOCAL', remote_agent=None):
     @rtype: C{tuple}
 
     """
-    _session_list = list_sessions(username, query_mode=query_mode, remote_agent=remote_agent)
-    return ([ s.split('|')[3] for s in _session_list if s.split('|')[4] == 'R' ], [ s.split('|')[3] for s in _session_list if s.split('|')[4] == 'S' ])
+    _session_list = list_sessions(username, remote_agent=remote_agent, **kwargs)
+    if type(_session_list) is types.ListType:
+        return ([ s.split('|')[3] for s in _session_list if s.split('|')[4] == 'R' ], [ s.split('|')[3] for s in _session_list if s.split('|')[4] == 'S' ])
+    else:
+        return ([], [])
 
 
-def find_busy_servers(username, query_mode='LOCAL', remote_agent=None, **kwargs):
+def find_busy_servers(username, remote_agent=None, **kwargs):
     """\
     Query X2Go Broker Agent for a list of  servers with running
     and/or suspended sessions and a percentage that tells about
@@ -319,16 +325,11 @@ def find_busy_servers(username, query_mode='LOCAL', remote_agent=None, **kwargs)
 
     @param username: run the query on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        server_list = call_local_broker_agent(username, mode='findbusyservers')
-    else:
-        server_list = call_remote_broker_agent(username, mode='findbusyservers', remote_agent=remote_agent)
+    server_list = call_broker_agent(username, task='findbusyservers', remote_agent=remote_agent, **kwargs)
 
     server_usage = {}
 
@@ -343,7 +344,7 @@ def find_busy_servers(username, query_mode='LOCAL', remote_agent=None, **kwargs)
 tasks['findbusyservers'] = find_busy_servers
 
 
-def add_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/authorized_keys', query_mode='LOCAL', remote_agent=None, **kwargs):
+def add_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/authorized_keys', remote_agent=None, **kwargs):
     """\
     Add a public key hash to the user's authorized_keys file.
 
@@ -353,20 +354,15 @@ def add_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/aut
     @type pubkey_hash: C{unicode}
     @param authorized_keys_file: the full path to the remote X2Go server's authorized_keys file
     @type authorized_keys_file: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='addauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ])
-    else:
-        return call_remote_broker_agent(username, mode='addauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ], remote_agent=remote_agent)
+    return call_broker_agent(username, task='addauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ], remote_agent=remote_agent, **kwargs)
 tasks['addauthkey'] = add_authorized_key
 
 
-def delete_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/authorized_keys', query_mode='LOCAL', remote_agent=None, delay_deletion=0, **kwargs):
+def delete_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/authorized_keys', remote_agent=None, delay_deletion=0, **kwargs):
     """\
     Remove a public key hash from the user's authorized_keys file.
 
@@ -376,30 +372,25 @@ def delete_authorized_key(username, pubkey_hash, authorized_keys_file='%h/.x2go/
     @type pubkey_hash: C{unicode}
     @param authorized_keys_file: the full path to the remote X2Go server's authorized_keys file
     @type authorized_keys_file: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
     # this is for the logger output
-    if remote_agent in  (None, u'LOCAL'):
+    if remote_agent in ('LOCAL', None):
         _hostname = 'LOCAL'
     else:
         _hostname = remote_agent['hostname']
 
     if delay_deletion > 0:
-        delayed_execution(delete_authorized_key, delay=delay_deletion, username=username, pubkey_hash=pubkey_hash, authorized_keys_file=authorized_keys_file, query_mode=query_mode, remote_agent=remote_agent, )
+        delayed_execution(delete_authorized_key, delay=delay_deletion, username=username, pubkey_hash=pubkey_hash, authorized_keys_file=authorized_keys_file, remote_agent=remote_agent, )
         logger_broker.debug('Scheduled deletion of authorized key in {delay}s: user={user}, host={host}'.format(delay=delay_deletion, user=username, host=_hostname))
     else:
-        if unicode(query_mode).upper() == u'LOCAL':
-            return call_local_broker_agent(username, mode='delauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ])
-        else:
-            return call_remote_broker_agent(username, mode='delauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ], remote_agent=remote_agent)
+        return call_broker_agent(username, task='delauthkey', cmdline_args=[pubkey_hash, authorized_keys_file, ], remote_agent=remote_agent, **kwargs)
 tasks['delauthkey'] = delete_authorized_key
 
 
-def get_servers(username, query_mode='LOCAL', remote_agent=None, **kwargs):
+def get_servers(username, remote_agent=None, **kwargs):
     """\
     Query X2Go Broker Agent for the list of currently used servers.
 
@@ -407,20 +398,15 @@ def get_servers(username, query_mode='LOCAL', remote_agent=None, **kwargs):
 
     @param username: run the query on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='getservers')
-    else:
-        return call_remote_broker_agent(username, mode='getservers', remote_agent=remote_agent)
+    return call_broker_agent(username, task='getservers', remote_agent=remote_agent, **kwargs)
 tasks['getservers'] = get_servers
 
 
-def tasks_available(username, query_mode='LOCAL', remote_agent=None, **kwargs):
+def tasks_available(username, remote_agent=None, **kwargs):
     """\
     Query X2Go Broker Agent for the list of available tasks.
 
@@ -429,16 +415,11 @@ def tasks_available(username, query_mode='LOCAL', remote_agent=None, **kwargs):
 
     @param username: run the query on behalf of this username
     @type username: C{unicode}
-    @param query_mode: query mode used when calling X2Go Broker Agent (C{LOCAL} or C{SSH})
-    @type query_mode: C{unicode}
     @param remote_agent: information about the remote agent that is to be called.
     @type remote_agent: C{dict}
 
     """
-    if unicode(query_mode).upper() == u'LOCAL':
-        return call_local_broker_agent(username, mode='availabletasks')
-    else:
-        return call_remote_broker_agent(username, mode='availabletasks', remote_agent=remote_agent)
+    return call_broker_agent(username, task='availabletasks', remote_agent=remote_agent, **kwargs)
 tasks['availabletasks'] = tasks_available
 
 

--
Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/x2gobroker.git



More information about the x2go-commits mailing list