def bofhd_run_command(self, session_id, cmd, *args): """Call command with session details. Execute the callable function (in the correct module) with the given name after mapping session_id to username """ # First, drop the short-lived sessions FIXME: if this is too # CPU-intensive, introduce a timestamp in this class, and drop the # short-lived sessions ONLY if more than BofhdSession._short_timeout session = BofhdSession(self.db, self.logger) session.remove_short_timeout_sessions() self.db_commit() # Set up session object session = BofhdSession(self.db, self.logger, session_id, self.client_address) entity_id = self.check_session_validity(session) self.db.cl_init(change_by=entity_id) self.logger.info(u'Run command: %s (%r) by %i', cmd, args, entity_id) if cmd not in self.server.classmap: raise CerebrumError("Illegal command {!r}".format(cmd)) implementation = self.server.classmap[cmd](self.db, self.logger) func = getattr(implementation, cmd) # We know cmd is a safe statsd-prefix, since it's also a valid # attribute name stats_client = statsd.make_client( self.server.stats_config, prefix='bofhd.command.{0}'.format(cmd)) with stats_client.pipeline() as stats: with stats.timer('time'): try: has_tuples = False for x in args: if isinstance(x, (tuple, list)): has_tuples = True break ret = [] self._run_command_with_tuples(func, session, args, ret) if not has_tuples: ret = ret[0] self.db_commit() # TBD: What should be returned if `args' contains tuple, # indicating that `func` should be called multiple times? stats.incr('success') return self.db.pythonify_data(ret) except Exception: self.db_rollback() stats.incr('error') raise
def bofhd_run_command(self, session_id, cmd, *args): """Call command with session details. Execute the callable function (in the correct module) with the given name after mapping session_id to username """ # First, drop the short-lived sessions FIXME: if this is too # CPU-intensive, introduce a timestamp in this class, and drop the # short-lived sessions ONLY if more than BofhdSession._short_timeout session = BofhdSession(self.db, self.logger) session.remove_short_timeout_sessions() self.db_commit() # Set up session object session = BofhdSession(self.db, self.logger, session_id, self.client_address) entity_id = self.check_session_validity(session) self.db.cl_init(change_by=entity_id) self.logger.debug(u'Run command: %s (%s) by %i', cmd, args, entity_id) if cmd not in self.server.classmap: raise CerebrumError(u"Illegal command '{!s}'".format(cmd)) implementation = self.server.classmap[cmd](self.db, self.logger) func = getattr(implementation, cmd) try: has_tuples = False for x in args: if isinstance(x, (tuple, list)): has_tuples = True break ret = [] self._run_command_with_tuples(func, session, args, ret) if not has_tuples: ret = ret[0] self.db_commit() # TBD: What should be returned if `args' contains tuple, # indicating that `func` should be called multiple times? return self.db.pythonify_data(ret) except Exception: self.db_rollback() raise