1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
29 import gevent
30 import threading
31 import copy
32
33
34 from cleanup import x2go_cleanup
35 import log
36
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 refresh_interval=5,
54 logger=None, loglevel=log.loglevel_DEFAULT):
55 """\
56 @param auto_update_listsessions_cache: let L{X2goSessionGuardian} refresh the session list cache for all L{X2goSession} objects
57 @type auto_update_listsessions_cache: C{bool}
58 @param auto_update_listdesktops_cache: let L{X2goSessionGuardian} refresh desktop lists in the session list cache for all L{X2goSession} objects
59 @type auto_update_listdesktops_cache: C{bool}
60 @param auto_update_listmounts_cache: let L{X2goSessionGuardian} refresh mount lists in the session list cache for all L{X2goSession} objects
61 @type auto_update_listmounts_cache: C{bool}
62 @param auto_update_sessionregistry: if set to C{True} the session status will be updated in regular intervals
63 @type auto_update_sessionregistry: C{bool}
64 @param auto_register_sessions: register new sessions automatically once they appear in the X2Go session (e.g.
65 instantiated by another client that is connected to the same X2Go server under same user ID)
66 @type auto_register_sessions: C{bool}
67 @param refresh_interval: refresh cache and session registry every <refresh_interval> seconds
68 @type refresh_interval: C{int}
69 @param logger: you can pass an L{X2goLogger} object to the L{X2goSessionGuardian} constructor
70 @type logger: C{obj}
71 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
72 constructed with the given loglevel
73 @type loglevel: C{int}
74
75 """
76 if logger is None:
77 self.logger = log.X2goLogger(loglevel=loglevel)
78 else:
79 self.logger = copy.deepcopy(logger)
80 self.logger.tag = __NAME__
81
82 self.client_instance = client_instance
83 self.auto_update_listsessions_cache = auto_update_listsessions_cache
84 self.auto_update_listdesktops_cache = auto_update_listdesktops_cache
85 self.auto_update_listmounts_cache = auto_update_listmounts_cache
86 self.auto_update_sessionregistry = auto_update_sessionregistry
87 self.auto_register_sessions = auto_register_sessions
88 self.refresh_interval = refresh_interval
89
90 threading.Thread.__init__(self, target=self.guardian)
91 self.daemon = True
92 self.start()
93
95 """\
96 The handler of this L{X2goSessionGuardian} thread.
97
98 """
99 seconds = 0
100 self._keepalive = True
101 while self._keepalive:
102 gevent.sleep(1)
103 seconds += 1
104
105 if seconds % self.refresh_interval == 0:
106
107 self.logger('Entering X2Go Guardian client management loop...', loglevel=log.loglevel_DEBUG)
108
109 if self.auto_update_listsessions_cache:
110 self.client_instance.update_cache_all_profiles(update_sessions=self.auto_update_listsessions_cache,
111 update_desktops=self.auto_update_listdesktops_cache,
112 update_mounts=self.auto_update_listmounts_cache,
113 )
114
115 if self.auto_update_sessionregistry and not self.auto_register_sessions:
116 self.client_instance.update_sessionregistry_status_all_profiles()
117
118
119 if self.auto_register_sessions:
120 self.client_instance.register_available_server_sessions_all_profiles()
121
122 self.logger('X2Go session guardian thread waking up after %s seconds' % seconds, loglevel=log.loglevel_DEBUG)
123
124 for session_uuid in self.client_instance.session_registry.keys():
125 session_summary = self.client_instance.get_session_summary(session_uuid)
126 self.logger('calling session cleanup on profile %s for terminal session: %s' % (session_summary['profile_name'], session_summary['session_name']), loglevel=log.loglevel_DEBUG)
127 x2go_cleanup(threads=session_summary['active_threads'])
128
130 """\
131 Stop this L{X2goSessionGuardian} thread.
132
133 """
134 self._keepalive = False
135