Ejemplo n.º 1
0
    def get_script_logs(script_id, start_date, last_id):
        logging.info('[SERVICE]: Getting script logs of script %s: ' %
                     (script_id))
        logging.info('[DB]: QUERY')
        try:
            val = UUID(script_id, version=4)
            script = Script.query.filter_by(id=script_id).first()
        except ValueError:
            script = Script.query.filter_by(slug=script_id).first()
        except Exception as error:
            raise error
        if not script:
            raise ScriptNotFound(message='Script with id ' + script_id +
                                 ' does not exist')

        if start_date:
            logging.debug(start_date)
            return ScriptLog.query.filter(
                ScriptLog.script_id == script.id,
                ScriptLog.register_date > start_date).order_by(
                    ScriptLog.register_date).all()
        elif last_id:
            return ScriptLog.query.filter(ScriptLog.script_id == script.id,
                                          ScriptLog.id > last_id).order_by(
                                              ScriptLog.register_date).all()
        else:
            return script.logs
Ejemplo n.º 2
0
 def unpublish_script(script_id, user):
     logging.info('[SERVICE]: Unpublishing script: ' + script_id)
     if user.role == 'ADMIN':
         try:
             val = UUID(script_id, version=4)
             script = Script.query.filter_by(id=script_id).first()
         except ValueError:
             script = Script.query.filter_by(slug=script_id).first()
         except Exception as error:
             raise error
     else:
         try:
             val = UUID(script_id, version=4)
             script = db.session.query(Script) \
                 .filter(Script.id == script_id) \
                 .filter(Script.user_id == user.id) \
                 .first()
         except ValueError:
             script = db.session.query(Script) \
                 .filter(Script.slug == script_id) \
                 .filter(Script.user_id == user.id) \
                 .first()
     if not script:
         raise ScriptNotFound(message='Script with id ' + script_id +
                              ' does not exist')
     script.public = False
     try:
         logging.info('[DB]: SAVE')
         db.session.add(script)
         db.session.commit()
     except Exception as error:
         raise error
     return script
Ejemplo n.º 3
0
 def get_script(script_id, user='******'):
     logging.info('[SERVICE]: Getting script: ' + script_id)
     logging.info('[DB]: QUERY')
     if user == 'fromservice' or user.role == 'ADMIN':
         try:
             val = UUID(script_id, version=4)
             script = Script.query.filter_by(id=script_id).first()
         except ValueError:
             script = Script.query.filter_by(slug=script_id).first()
         except Exception as error:
             raise error
     else:
         try:
             val = UUID(script_id, version=4)
             script = db.session.query(Script) \
                 .filter(Script.id == script_id) \
                 .filter(or_(Script.user_id == user.id, Script.public == True)) \
                 .first()
         except ValueError:
             script = db.session.query(Script) \
                 .filter(Script.slug == script_id) \
                 .filter(or_(Script.user_id == user.id, Script.public == True)) \
                 .first()
         except Exception as error:
             raise error
     if not script:
         raise ScriptNotFound(message='Script with id ' + script_id +
                              ' does not exist')
     return script
Ejemplo n.º 4
0
    def create_execution(script_id, params, user):
        logging.info('[SERVICE]: Creating execution')
        script = ScriptService.get_script(script_id, user)
        if not script:
            raise ScriptNotFound(message='Script with id ' + script_id +
                                 ' does not exist')
        if script.status != 'SUCCESS':
            raise ScriptStateNotValid(message='Script with id ' + script_id +
                                      ' is not BUILT')
        execution = Execution(script_id=script.id,
                              params=params,
                              user_id=user.id)
        try:
            logging.info('[DB]: ADD')
            db.session.add(execution)
            db.session.commit()
        except Exception as error:
            raise error

        try:
            environment = SETTINGS.get('environment', {})
            environment['EXECUTION_ID'] = execution.id
            param_serial = json.dumps(params).encode('utf-8')
            param_serial = str(base64.b64encode(param_serial)).replace(
                '\'', '')
            logging.debug(param_serial)
            docker_run.delay(execution.id, script.slug, environment,
                             param_serial)
        except Exception as e:
            raise e
        return execution
Ejemplo n.º 5
0
 def update_script(script_id, sent_file, user):
     logging.info('[SERVICE]: Updating script')
     script = ScriptService.get_script(script_id, user)
     if not script:
         raise ScriptNotFound(message='Script with id ' + script_id +
                              ' does not exist')
     if user.role == 'ADMIN' or user.email == '*****@*****.**' or user.id == script.user_id:
         return ScriptService.create_script(sent_file, user, script)
     raise NotAllowed(message='Operation not allowed to this user')
Ejemplo n.º 6
0
 def update_script(script_id, sent_file, user):
     logging.info('[SERVICE]: Updating script')
     script = ScriptService.get_script(script_id, user)
     if not script:
         raise ScriptNotFound(message='Script with id ' + script_id +
                              ' does not exist')
     if user.id != script.user_id and user.role != 'ADMIN':
         raise NotAllowed(message='Operation not allowed to this user')
     return ScriptService.create_script(sent_file, user, script)
Ejemplo n.º 7
0
    def delete_script(script_id, user):
        logging.info('[SERVICE]: Deleting script' + script_id)
        try:
            script = ScriptService.get_script(script_id, user)
        except Exception as error:
            raise error
        if not script:
            raise ScriptNotFound(message='Script with id ' + script_id +
                                 ' does not exist')

        try:
            logging.info('[DB]: DELETE')
            db.session.delete(script)
            db.session.commit()
        except Exception as error:
            raise error
        return script