/usr/share/pyshared/ClusterShell/Gateway.py is in clustershell 1.6-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #!/usr/bin/env python
#
# Copyright CEA/DAM/DIF (2010, 2011, 2012)
# Contributor: Henri DOREAU <henri.doreau@gmail.com>
# Contributor: Stephane THIELL <stephane.thiell@cea.fr>
#
# This file is part of the ClusterShell library.
#
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL-C
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.
"""
ClusterShell agent launched on remote gateway nodes. This script reads messages
on stdin via the SSH connexion, interprets them, takes decisions, and prints out
replies on stdout.
"""
import logging
import os
import sys
from ClusterShell.Event import EventHandler
from ClusterShell.NodeSet import NodeSet
from ClusterShell.Task import task_self, _getshorthostname
from ClusterShell.Engine.Engine import EngineAbortException
from ClusterShell.Worker.fastsubprocess import set_nonblock_flag
from ClusterShell.Worker.Worker import WorkerSimple
from ClusterShell.Worker.Tree import WorkerTree
from ClusterShell.Communication import Channel, ConfigurationMessage, \
ControlMessage, ACKMessage, ErrorMessage, EndMessage, StdOutMessage, \
StdErrMessage, RetcodeMessage, TimeoutMessage
class WorkerTreeResponder(EventHandler):
"""Gateway WorkerTree handler"""
def __init__(self, task, gwchan, srcwkr):
EventHandler.__init__(self)
self.gwchan = gwchan # gateway channel
self.srcwkr = srcwkr # id of distant parent WorkerTree
self.worker = None # local WorkerTree instance
# For messages grooming
qdelay = task.info("grooming_delay")
self.timer = task.timer(qdelay, self, qdelay, autoclose=True)
self.logger = logging.getLogger(__name__)
self.logger.debug("WorkerTreeResponder: initialized")
def ev_start(self, worker):
self.logger.debug("WorkerTreeResponder: ev_start")
self.worker = worker
def ev_timer(self, timer):
"""perform gateway traffic grooming"""
if not self.worker:
return
logger = self.logger
# check for grooming opportunities
for msg_elem, nodes in self.worker.iter_errors():
logger.debug("iter(stderr): %s: %d bytes" % \
(nodes, len(msg_elem.message())))
self.gwchan.send(StdErrMessage(nodes, msg_elem.message(), \
self.srcwkr))
for msg_elem, nodes in self.worker.iter_buffers():
logger.debug("iter(stdout): %s: %d bytes" % \
(nodes, len(msg_elem.message())))
self.gwchan.send(StdOutMessage(nodes, msg_elem.message(), \
self.srcwkr))
self.worker.flush_buffers()
def ev_error(self, worker):
self.logger.debug("WorkerTreeResponder: ev_error %s" % \
worker.current_errmsg)
def ev_timeout(self, worker):
"""Received timeout event: some nodes did timeout"""
self.gwchan.send(TimeoutMessage( \
NodeSet._fromlist1(worker.iter_keys_timeout()), self.srcwkr))
def ev_close(self, worker):
"""End of responder"""
self.logger.debug("WorkerTreeResponder: ev_close")
# finalize grooming
self.ev_timer(None)
# send retcodes
for rc, nodes in self.worker.iter_retcodes():
self.logger.debug("iter(rc): %s: rc=%d" % (nodes, rc))
self.gwchan.send(RetcodeMessage(nodes, rc, self.srcwkr))
self.timer.invalidate()
# clean channel closing
####self.gwchan.close()
class GatewayChannel(Channel):
"""high level logic for gateways"""
def __init__(self, task, hostname):
"""
"""
Channel.__init__(self)
self.task = task
self.hostname = hostname
self.topology = None
self.propagation = None
self.logger = logging.getLogger(__name__)
self.current_state = None
self.states = {
'CFG': self._state_cfg,
'CTL': self._state_ctl,
'GTR': self._state_gtr,
}
def start(self):
"""initialization"""
self._open()
# prepare to receive topology configuration
self.current_state = self.states['CFG']
self.logger.debug('entering config state')
def close(self):
"""close gw channel"""
self.logger.debug('closing gw channel')
self._close()
self.current_state = None
def recv(self, msg):
"""handle incoming message"""
try:
self.logger.debug('handling incoming message: %s', str(msg))
if msg.ident == EndMessage.ident:
self.logger.debug('recv: got EndMessage')
self.worker.abort()
else:
self.current_state(msg)
except Exception, ex:
self.logger.exception('on recv(): %s', str(ex))
self.send(ErrorMessage(str(ex)))
def _state_cfg(self, msg):
"""receive topology configuration"""
if msg.type == ConfigurationMessage.ident:
self.topology = msg.data_decode()
task_self().topology = self.topology
self.logger.debug('decoded propagation tree')
self.logger.debug('%s' % str(self.topology))
self._ack(msg)
self.current_state = self.states['CTL']
self.logger.debug('entering control state')
else:
logging.error('unexpected message: %s', str(msg))
def _state_ctl(self, msg):
"""receive control message with actions to perform"""
if msg.type == ControlMessage.ident:
self.logger.debug('GatewayChannel._state_ctl')
self._ack(msg)
if msg.action == 'shell':
data = msg.data_decode()
cmd = data['cmd']
stderr = data['stderr']
timeout = data['timeout']
#self.propagation.invoke_gateway = data['invoke_gateway']
self.logger.debug('decoded gw invoke (%s)', \
data['invoke_gateway'])
taskinfo = data['taskinfo']
task = task_self()
task._info = taskinfo
task._engine.info = taskinfo
#logging.setLevel(logging.DEBUG)
self.logger.debug('assigning task infos (%s)' % \
str(data['taskinfo']))
self.logger.debug('inherited fanout value=%d', \
task.info("fanout"))
#self.current_state = self.states['GTR']
self.logger.debug('launching execution/enter gathering state')
responder = WorkerTreeResponder(task, self, msg.srcid)
self.propagation = WorkerTree(msg.target, responder, timeout,
command=cmd,
topology=self.topology,
newroot=self.hostname,
stderr=stderr)
responder.worker = self.propagation # FIXME ev_start-not-called workaround
self.propagation.upchannel = self
task.schedule(self.propagation)
self.logger.debug("WorkerTree scheduled")
else:
logging.error('unexpected message: %s', str(msg))
def _state_gtr(self, msg):
"""gather outputs"""
# FIXME
self.logger.debug('GatewayChannel._state_gtr')
self.logger.debug('incoming output msg: %s' % str(msg))
def _ack(self, msg):
"""acknowledge a received message"""
self.send(ACKMessage(msg.msgid))
def gateway_main():
"""ClusterShell gateway entry point"""
host = _getshorthostname()
# configure root logger
logdir = os.path.expanduser(os.environ.get('CLUSTERSHELL_GW_LOG_DIR', \
'/tmp'))
loglevel = os.environ.get('CLUSTERSHELL_GW_LOG_LEVEL', 'INFO')
logging.basicConfig(level=getattr(logging, loglevel.upper(), logging.INFO),
format='%(asctime)s %(name)s %(levelname)s %(message)s',
filename=os.path.join(logdir, "%s.gw.log" % host))
logger = logging.getLogger(__name__)
logger.debug('Starting gateway on %s', host)
logger.debug("environ=%s" % os.environ)
set_nonblock_flag(sys.stdin.fileno())
set_nonblock_flag(sys.stdout.fileno())
set_nonblock_flag(sys.stderr.fileno())
task = task_self()
# Pre-enable MsgTree buffering on gateway (not available at runtime - #181)
task.set_default("stdout_msgtree", True)
task.set_default("stderr_msgtree", True)
if sys.stdin.isatty():
logger.critical('Gateway failure: sys.stdin.isatty() is True')
sys.exit(1)
worker = WorkerSimple(sys.stdin, sys.stdout, sys.stderr, None,
handler=GatewayChannel(task, host))
task.schedule(worker)
logger.debug('Starting task')
try:
task.resume()
logger.debug('Task performed')
except EngineAbortException, exc:
pass
except IOError, exc:
logger.debug('Broken pipe (%s)' % exc)
raise
except Exception, exc:
logger.exception('Gateway failure: %s' % exc)
logger.debug('The End')
if __name__ == '__main__':
__name__ = 'ClusterShell.Gateway'
# To enable gateway profiling:
#import cProfile
#cProfile.run('gateway_main()', '/tmp/gwprof')
gateway_main()
|