def _run_process(self, filename, command_name, command_args, flush_callback=None, message=None): ext = os.path.splitext(filename)[1] if ext in (".py", ".pyw", ".pyc"): command = self._python_runner # -u: sets unbuffered output args = [command, "-u", "-W ignore::DeprecationWarning", filename, command_name] else: command = filename args = [command, command_name] # Set timeout from command cmd_timeout = int(command_args.get("timeout", self.timeout)) if command_name: log.info("Running %s from %s (timeout: %i)" % (command_name, filename, cmd_timeout)) else: log.info("[INIT] Loading commands from %s" % filename) mem_clean("spawnProcess [start]") crp = CommandRunnerProcess(cmd_timeout, command_args, flush_callback, message) d = crp.getDeferredResult() reactor.spawnProcess(crp, command, args, env=self.env) del cmd_timeout, filename, command_name, command_args del flush_callback, message, args mem_clean("spawnProcess [end]") return d
def __onIq(self, msg): """ A new IQ message has been received and we should process it. """ log.debug('__onIq') mem_clean('__onIq [start]') self.running_commands += 1 log.debug("q Message received: \n%s" % msg.toXml()) log.debug("Message type: %s" % msg['type']) if msg['type'] == 'set': message = IqMessage(msg) if hasattr(message, 'command') and hasattr(message, 'from_'): log.debug('online contacts: %s' % self._online_contacts) if message.from_ not in self._online_contacts: log.warn('IQ sender not in roster (%s), dropping message' % message.from_) else: self._processCommand(message) else: log.warn('Unknown ecm_message received: Full XML:\n%s' % (msg.toXml())) del message else: log.warn('Unknown IQ type received: Full XML:\n%s' % (msg.toXml())) del msg mem_clean('__onIq [end]')
def _send(self, result, message): log.debug('Send Response') mem_clean('agent._send') message.toResult(*result) del result mem_clean('agent._send') self.send(message.toEtree())
def _check_memory(self, num_running_commands): rss = mem_clean('periodic memory clean') if not num_running_commands and rss > _CHECK_RAM_MAX_RSS_MB: log.critical("Max allowed RSS memory exceeded: %s MB, exiting." % _CHECK_RAM_MAX_RSS_MB) reactor.stop() del rss
def _processCommand(self, message): if not self.verify.signature(message): result = (_E_UNVERIFIED_COMMAND, '', 'Bad signature', 0) self._onCallFinished(result, message) return flush_callback = self._flush message.command_name = message.command.replace('.', '_') mem_clean('run_command [start]') d = self.command_runner.run_command(message, flush_callback) # Clean message message.command_args = {} if d: d.addCallbacks(self._onCallFinished, self._onCallFailed, callbackKeywords={'message': message}, errbackKeywords={'message': message}, ) del message mem_clean('run_command [end]') return d else: log.info("Command Ignored: Unknown command: %s" % message.command) result = (_E_RUNNING_COMMAND, '', "Unknown command: %s" % message.command, 0) self._onCallFinished(result, message) del message mem_clean('run_command [end]') return
def _onCallFinished(self, result, message): mem_clean('agent._onCallFinished') log.debug('Call Finished') self._send(result, message) self.running_commands -= 1
def __del__(self): log.debug("__del__ CommandRunner()") mem_clean("__del__: CommandRunner")