Exemple #1
0
def run_action(name, params={}, log_level=logging.DEBUG, output_length=1000):
    db = db_factory.get_database()
    action = actions[name]
    cmdline = bunch.Bunch(params)

    with captured_output_combined() as cap_stdout:
        # Logger and fake stdout to capture the output of the action
        root = logging.getLogger()
        ch = logging.StreamHandler(cap_stdout)
        ch.setLevel(log_level)
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        ch.setFormatter(formatter)
        root.addHandler(ch)
        try:
            action.run(cmdline, db)
        except Exception as e:
            logging.exception(e)
            db.session.rollback()
            output = cap_stdout.getvalue()
            # There seems to be no way to pass a FAILURE status other than raising
            # an exception. self.update_status(status="FAILURE") didn't work.
            raise ActionError(output[-output_length:])

        db.session.rollback()
        output = cap_stdout.getvalue()
        return output[-output_length:]
Exemple #2
0
def run_action(name,
               params=None,
               log_level=logging.DEBUG,
               output_length=1000) -> str:
    db = db_factory.get_database()
    action = actions[name]
    cmdline = munch.Munch(params or {})

    with captured_output_combined() as cap_stdout:
        # Logger and fake stdout to capture the output of the action
        root = logging.getLogger()
        ch = logging.StreamHandler(cap_stdout)
        ch.setLevel(log_level)
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        ch.setFormatter(formatter)
        root.addHandler(ch)
        try:
            action.run(cmdline, db)
        except Exception as e:
            logging.exception(e)
            db.session.rollback()
            output = cap_stdout.getvalue()
            # There seems to be no way to pass a FAILURE status other than raising
            # an exception. self.update_status(status="FAILURE") didn't work.
            raise ActionError(output[-output_length:])

        db.session.rollback()
        output = cap_stdout.getvalue()
        return output[-output_length:]