Package x2go :: Module guardian
[frames] | no frames]

Source Code for Module x2go.guardian

  1  # -*- coding: utf-8 -*- 
  2   
  3  # Copyright (C) 2010-2015 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> 
  4  # 
  5  # Python X2Go is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU Affero General Public License as published by 
  7  # the Free Software Foundation; either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Python X2Go is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU Affero General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU Affero General Public License 
 16  # along with this program; if not, write to the 
 17  # Free Software Foundation, Inc., 
 18  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 
 19   
 20  """\ 
 21  X2GoSessionGuardian class - a guardian thread that controls X2Go session threads 
 22  and their sub-threads (like reverse forwarding tunnels, Paramiko transport threads, 
 23  etc.). 
 24   
 25  """ 
 26  __NAME__ = 'x2goguardian-pylib' 
 27   
 28  # modules 
 29  import gevent 
 30  import threading 
 31  import copy 
 32   
 33  # Python X2Go modules 
 34  from cleanup import x2go_cleanup 
 35  import log 
 36   
37 -class X2GoSessionGuardian(threading.Thread):
38 """\ 39 L{X2GoSessionGuardian} thread controls X2Go session threads and their sub-threads (like 40 reverse forwarding tunnels, Paramiko transport threads, etc.). Its main function is 41 to tidy up once a session gets interrupted (SIGTERM, SIGINT). 42 43 There is one L{X2GoSessionGuardian} for each L{X2GoClient} instance (thus: for normal 44 setups there should be _one_ L{X2GoClient} and _one_ L{X2GoSessionGuardian} in use). 45 46 """
47 - def __init__(self, client_instance, 48 auto_update_listsessions_cache=False, 49 auto_update_listdesktops_cache=False, 50 auto_update_listmounts_cache=False, 51 auto_update_sessionregistry=False, 52 auto_register_sessions=False, 53 no_auto_reg_pubapp_sessions=False, 54 refresh_interval=5, 55 logger=None, loglevel=log.loglevel_DEFAULT):
56 """\ 57 @param auto_update_listsessions_cache: let L{X2GoSessionGuardian} refresh the session list cache for all L{X2GoSession} objects 58 @type auto_update_listsessions_cache: C{bool} 59 @param auto_update_listdesktops_cache: let L{X2GoSessionGuardian} refresh desktop lists in the session list cache for all L{X2GoSession} objects 60 @type auto_update_listdesktops_cache: C{bool} 61 @param auto_update_listmounts_cache: let L{X2GoSessionGuardian} refresh mount lists in the session list cache for all L{X2GoSession} objects 62 @type auto_update_listmounts_cache: C{bool} 63 @param auto_update_sessionregistry: if set to C{True} the session status will be updated in regular intervals 64 @type auto_update_sessionregistry: C{bool} 65 @param auto_register_sessions: register new sessions automatically once they appear in the X2Go session (e.g. 66 instantiated by another client that is connected to the same X2Go server under same user ID) 67 @type auto_register_sessions: C{bool} 68 @param no_auto_reg_pubapp_sessions: do not auto-register published applications sessions 69 @type no_auto_reg_pubapp_sessions: C{bool} 70 @param refresh_interval: refresh cache and session registry every <refresh_interval> seconds 71 @type refresh_interval: C{int} 72 @param logger: you can pass an L{X2GoLogger} object to the L{X2GoSessionGuardian} constructor 73 @type logger: C{obj} 74 @param loglevel: if no L{X2GoLogger} object has been supplied a new one will be 75 constructed with the given loglevel 76 @type loglevel: C{int} 77 78 """ 79 if logger is None: 80 self.logger = log.X2GoLogger(loglevel=loglevel) 81 else: 82 self.logger = copy.deepcopy(logger) 83 self.logger.tag = __NAME__ 84 85 self.client_instance = client_instance 86 self.auto_update_listsessions_cache = auto_update_listsessions_cache 87 self.auto_update_listdesktops_cache = auto_update_listdesktops_cache 88 self.auto_update_listmounts_cache = auto_update_listmounts_cache 89 self.auto_update_sessionregistry = auto_update_sessionregistry 90 self.auto_register_sessions = auto_register_sessions 91 self.no_auto_reg_pubapp_sessions = no_auto_reg_pubapp_sessions 92 self.refresh_interval = refresh_interval 93 94 threading.Thread.__init__(self, target=self.guardian) 95 self.daemon = True 96 self.start()
97
98 - def guardian(self):
99 """\ 100 The handler of this L{X2GoSessionGuardian} thread. 101 102 """ 103 seconds = 0 104 self._keepalive = True 105 while self._keepalive: 106 gevent.sleep(1) 107 seconds += 1 108 109 if seconds % self.refresh_interval == 0: 110 111 self.logger('Entering X2Go Guardian client management loop...', loglevel=log.loglevel_DEBUG) 112 113 if self.auto_update_listsessions_cache: 114 self.client_instance.update_cache_all_profiles(update_sessions=self.auto_update_listsessions_cache, 115 update_desktops=self.auto_update_listdesktops_cache, 116 update_mounts=self.auto_update_listmounts_cache, 117 ) 118 119 if self.auto_update_sessionregistry and not self.auto_register_sessions: 120 self.client_instance.update_sessionregistry_status_all_profiles() 121 122 # session auto-registration will automatically trigger an update of the session registry status 123 if self.auto_register_sessions: 124 self.client_instance.register_available_server_sessions_all_profiles(skip_pubapp_sessions=self.no_auto_reg_pubapp_sessions) 125 126 self.logger('X2Go session guardian thread waking up after %s seconds' % seconds, loglevel=log.loglevel_DEBUG) 127 128 for session_uuid in self.client_instance.session_registry.keys(): 129 session_summary = self.client_instance.get_session_summary(session_uuid) 130 self.logger('calling session cleanup on profile %s for terminal session: %s' % (session_summary['profile_name'], session_summary['session_name']), loglevel=log.loglevel_DEBUG) 131 x2go_cleanup(threads=session_summary['active_threads'])
132
133 - def stop_thread(self):
134 """\ 135 Stop this L{X2GoSessionGuardian} thread. 136 137 """ 138 self._keepalive = False
139