Example #1
0
def get_setting(key):
    """Get setting value from database. Return None or value."""
    session = Session()
    value = session.query(Setting).get(key)
    session.close()

    return value
Example #2
0
    def logs(self, limit=10):
        """Find all log entries for script"""
        logs = []

        session = Session()

        for log in (session.query(Log).filter(Log.script == self.uid).order_by(
                Log.date.desc()).limit(limit).all()):
            try:
                stdout = log.text.decode("utf-8")
            except AttributeError:
                stdout = log.text

            try:
                stderr = log.error.decode("utf-8")
            except AttributeError:
                stderr = log.error

            logs.append({
                "stdout": stdout,
                "stderr": stderr,
                "date": log.date,
                "exitcode": log.exitcode,
            })

        session.close()

        return logs
Example #3
0
def execute_task(id):
    session = Session()
    task = session.query(Task).get(id)
    logger.debug("Processing task with ID: {}", id)
    task_uid = task.task_id
    task_module = importlib.import_module("chronos.tasks.{}".format(task_uid))
    task_id_dict = {"task_id": id}

    logger.debug("Starting task with ID: {}", id)
    task.time_started = datetime.datetime.now()
    task.status = "STARTED"
    session.commit()

    event.trigger("task_started", task_id_dict)
    arguments = {**json.loads(task.task_arguments), **task_id_dict}

    task.output = task_module.run(json.dumps(arguments), event)

    event.trigger("task_finished", task_id_dict)

    task.time_finished = datetime.datetime.now()
    task.status = "FINISHED"
    session.commit()
    logger.debug("Finished task with ID: {}", id)

    session.close()

    return
Example #4
0
def get_all_settings():
    """Get all settings from database."""
    session = Session()

    all_settings = session.query(Setting).all()
    session.close()

    return session
Example #5
0
    def enable(self):
        session = Session()
        script_from_database = session.query(ScriptModel).get(self.uid)
        script_from_database.enabled = True
        session.commit()
        session.close()

        self.enabled = True

        event.trigger("script_updated", self.__dict__())
        event.trigger("action_complete", {"action": "enable", "uid": self.uid})
Example #6
0
def scripts():
    scripts = []

    session = Session()

    for s in session.query(ScriptModel).all():
        scripts.append(chronos.script.Script(s.uid).to_dict())

    session.close()

    return jsonify(scripts), 200
Example #7
0
def set_setting(key, value):
    """Update setting or create new."""
    session = Session()
    if get_setting(key) is None:
        session.add(Setting(key=key, value=value))
        logger.debug("Created new 'setting': {} with value: '{}'", key, value)
    else:
        session.query(Setting).get(key).value = value
        logger.debug("Updated 'setting': {} with value: '{}'", key, value)

    session.commit()
    session.close()
Example #8
0
    def prune_logs(self):
        session = Session()

        if session.query(Log).count() > 10:
            logger.debug("Pruning logs for {}".format(self.uid))
            too_old = datetime.now() - timedelta(days=3)

            # logger.debug(too_old)

            logger.debug("Found {} logs to be pruned".format(
                session.query(Log).filter(Log.date < too_old).count()))
            session.query(Log).filter(Log.date < too_old).delete()

        session.commit()
        session.close()
Example #9
0
def dispatch_task(task_id, task_arguments, task_priority="ROUTINE"):
    session = Session()
    task = Task(
        task_id=task_id,
        task_arguments=json.dumps(task_arguments),
        priority=task_priority,
        status="WAITING",
    )

    session.add(task)
    session.commit()
    logger.debug("Dispatched task: {}", task_id)
    event.trigger("task_dispatched", {"task_id": task_id})

    session.close()

    return True
Example #10
0
def execute_next_task():
    session = Session()
    tasks = session.query(Task).filter(Task.status == "WAITING")

    # logger.debug("Executing next available task...")

    for task in tasks:
        if task.priority == "NOW":
            task_thread = threading.Thread(target=execute_task(task.id))
            task_thread.start()
            # logger.debug("Next task has been scheduled")

            return

    if tasks.count() > 0:
        task_thread = threading.Thread(target=execute_task(tasks[0].id))
        task_thread.start()
    #  logger.debug("Next task has been scheduled")

    session.close()
Example #11
0
def run(arguments, event):
    arguments = json.loads(arguments)
    uid = arguments["uid"]

    script = Script(uid)

    session = Session()

    # Remove script folder
    shutil.rmtree(script.folder)

    # Remove all logs from script
    session.query(Log).filter(Log.script == script.uid).delete()

    # Delete metadata
    session.delete(script.db)
    session.commit()
    session.close()

    event.trigger("action_complete", {"action": "delete", "uid": script.uid})
    event.trigger("script_deleted", {"uid": script.uid})

    return uid
Example #12
0
    def __init__(self, uid):
        """Initialise script class given UID."""

        session = Session()

        # UID
        self.uid = uid

        # Get database entry
        self.db = session.query(ScriptModel).get(uid)

        # Store dictionary version of model
        self.dict = {
            "name": self.db.name,
            "triggers": self.db.triggers,
            "logs": self.logs(),
            "created": str(self.db.created),
        }

        self.enabled = self.db.enabled

        # Get script folder
        self.folder = CHRONOS + os.path.sep + "scripts" + os.path.sep + self.uid

        # Get path of script
        self.path = self.folder + os.path.sep + self.uid + ".py"

        # Get path of requirements file
        self.requirements = self.folder + os.path.sep + "requirements.txt"

        # Get path of execute.sh script
        self.execute_path = self.folder + os.path.sep + "execute.sh"

        # Get path of install.sh script
        self.install_requirements_path = self.folder + os.path.sep + "install.sh"

        session.close()
Example #13
0
def run(arguments, event):
    arguments = json.loads(arguments)
    script_uid = arguments["script_uid"]
    task_id = arguments["task_id"]

    event.trigger("action_started", {"uid": script_uid, "action": "execute"})

    script = Script(script_uid)

    process = Popen(["bash", script.execute_path],
                    stdout=PIPE,
                    stderr=PIPE,
                    shell=False)

    exitcode = 0
    process_output = ""

    while True:
        output = process.stdout.readline()
        process_output += output.decode("utf-8")
        if process.poll() is not None:
            break

        if output:
            event.trigger(
                "task_output",
                {
                    "task_id": task_id,
                    "script_uid": script_uid,
                    "output": output.decode("utf-8"),
                    "task": "execute",
                },
            )

    exitcode = process.poll()

    stdout, stderr = process.communicate()

    if stderr:
        event.trigger(
            "task_output",
            {
                "task_id": task_id,
                "script_uid": script_uid,
                "output": stderr.decode("utf-8"),
                "task": "execute",
            },
        )

    session = Session()
    log = Log(script=script_uid,
              text=process_output,
              error=stderr,
              exitcode=exitcode)
    session.add(log)
    session.commit()
    session.close()

    event.trigger(
        "task_output",
        {
            "task_id": task_id,
            "output": "",
            "script_uid": script.uid,
            "task": "execute"
        },
    )
    script = script.to_dict()

    # Decode bytes to string
    try:
        stderr = stderr.decode("utf-8")
    except AttributeError:
        pass

    try:
        process_output = process_output.decode("utf-8")
    except AttributeError:
        pass

    script["logs"].insert(
        0,
        {
            "date": maya.now().rfc2822(),
            "stderr": stderr,
            "stdout": process_output,
            "exitcode": exitcode,
        },
    )
    event.trigger("script_executed", script)

    event.trigger("script_updated", script)
    event.trigger("action_complete", {
        "action": "execute",
        "uid": script["uid"]
    })

    return stdout
Example #14
0
 def prune_logs(self):
     session = Session()
     session.query(Log).filter(Log.date >= timedelta(days=3)).delete()
     session.commit()
     session.close()