Esempio n. 1
0
    def get_execution_logs(execution_id, start_date, last_id):
        logging.info('[SERVICE]: Getting execution logs of execution %s: ' %
                     (execution_id))
        logging.info('[DB]: QUERY')
        try:
            execution = ExecutionService.get_execution(
                execution_id=execution_id)
        except Exception as error:
            raise error
        if not execution:
            raise ExecutionNotFound(message='Execution with id ' +
                                    execution_id + ' does not exist')

        if start_date:
            logging.debug(start_date)
            return ExecutionLog.query.filter(
                ExecutionLog.execution_id == execution.id,
                ExecutionLog.register_date > start_date).order_by(
                    ExecutionLog.register_date).all()
        elif last_id:
            return ExecutionLog.query.filter(
                ExecutionLog.execution_id == execution.id,
                ExecutionLog.id > last_id).order_by(
                    ExecutionLog.register_date).all()
        else:
            return execution.logs
 def update_execution(execution, execution_id):
     logging.info('[SERVICE]: Updating execution')
     status = execution.get('status', None)
     progress = execution.get('progress', None)
     results = execution.get('results', None)
     if status is None and progress is None and results is None:
         raise Exception
     execution = ExecutionService.get_execution(execution_id=execution_id)
     if not execution:
         raise ExecutionNotFound(message='Execution with id '+execution_id+' does not exist')
     if status is not None:
         execution.status = status
         if status == 'FINISHED' or status == 'FAILED':
             execution.end_date = datetime.datetime.utcnow()
             execution.progress = 100
             user = UserService.get_user(str(execution.user_id))
             script = ScriptService.get_script(str(execution.script_id))
             email = EmailService.send_html_email(
                 recipients=[user.email],
                 html=EXECUTION_FINISHED_MAIL_CONTENT.format(status, execution.params.get('task_name'), script.name, str(execution.id), execution.start_date, execution.end_date, status),
                 subject='[MISLAND] Execution finished'
             )
     if progress is not None:
         execution.progress = progress
     if results is not None:
         execution.results = results
     try:
         logging.info('[DB]: ADD')
         db.session.add(execution)
         db.session.commit()
     except Exception as error:
         raise error
     return execution
 def create_execution_log(log, execution_id):
     logging.info('[SERVICE]: Creating execution log')
     text = log.get('text', None)
     level = log.get('level', None)
     if text is None or level is None:
         raise Exception
     execution = ExecutionService.get_execution(execution_id=execution_id)
     if not execution:
         raise ExecutionNotFound(message='Execution with id '+execution_id+' does not exist')
     execution_log = ExecutionLog(text=text, level=level, execution_id=execution.id)
     try:
         logging.info('[DB]: ADD')
         db.session.add(execution_log)
         db.session.commit()
     except Exception as error:
         raise error
     return execution_log
Esempio n. 4
0
 def get_execution(execution_id, user='******'):
     logging.info('[SERVICE]: Getting execution ' + execution_id)
     logging.info('[DB]: QUERY')
     # user = '******' just in case the requests comes from the service
     if user == 'fromservice' or user.role == 'ADMIN':
         try:
             val = UUID(execution_id, version=4)
             execution = Execution.query.filter_by(id=execution_id).first()
         except Exception as error:
             raise error
     else:
         try:
             val = UUID(execution_id, version=4)
             execution = db.session.query(Execution) \
                 .filter(Execution.id == execution_id) \
                 .filter(Execution.user_id == user.id) \
                 .first()
         except Exception as error:
             raise error
     if not execution:
         raise ExecutionNotFound(message='Ticket Not Found')
     return execution