def run(arguments, event): session = Session() for script in session.query(ScriptModel).all(): script = Script(script.uid) script.prune_logs() return
def run(arguments, event): arguments = json.loads(arguments) script_uid = arguments["script_uid"] task_id = arguments["task_id"] script = Script(script_uid) process = Popen( ["bash", script.install_requirements_path], stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=-1, ) process_output = "" exitcode = 0 while True: output = process.stdout.readline().decode("utf-8") if process.poll() is not None: exitcode = process.poll() break if output: process_output += output event.trigger( "task_output", { "task_id": task_id, "output": output, "script_uid": script.uid, "task": "install_requirements", }, ) output, error = process.communicate() event.trigger( "task_output", { "task_id": task_id, "output": "", "script_uid": script.uid, "task": "install_requirements", }, ) event.trigger("pip_requirements_installed", script.to_dict()) event.trigger("action_complete", { "action": "install_requirements", "uid": script.uid }) return process_output
def evalaute_script_cron_triggers(tick, interval): second = tick * interval / 1000 for script in session.query(ScriptModel).all(): s = Script(script.uid) if script.enabled: for trigger in script.triggers: if trigger["type"] == "cron": # Evaluate cron expression try: cron = cronex.CronExpression( trigger["options"]["expression"]) time = tuple(list(datetime.now().timetuple())[:5]) if cron.check_trigger(time): # Execute script in seperate thread, such that the loop is not affected dispatch_task( "execute_script", {"script_uid": script.uid}, task_priority="NOW", ) except (ValueError): logger.error("CRON expression yielded error: {}", trigger["options"]["expression"])
def run(arguments, event): arguments = json.loads(arguments) name = arguments["name"] session = Session() """Create a new script by creating a virtualenv, creating .sh scripts and registering metadata.""" if name is "": # Generate random UID if no name is given uid = generate_uid() else: # Convert name to UID by "sluggifying" it (e.g. "Simon's script" -> "simons-script") uid = for_uid(name) # Check that the scripts folder exists (important for first-time users) if not os.path.isdir(CHRONOS + os.path.sep + "scripts"): os.mkdir(CHRONOS + os.path.sep + "scripts") # Find script path given UID path = CHRONOS + os.path.sep + "scripts" + os.path.sep + uid # Create folder, if it doesn't already exist if not os.path.isdir(path): os.mkdir(path) else: os.rmdir(path) os.mkdir(path) # Create virtual environment create_env(uid) # Create database entry script = ScriptModel(name=name, uid=uid, enabled=True, triggers=[]) session.add(script) session.commit() # Create script and requirements.txt file script_path = path + os.path.sep + uid + ".py" requirements_path = path + os.path.sep + "requirements.txt" open(script_path, "a").close() open(requirements_path, "a").close() # Create execute script # TODO: Move this script to a folder so it can be copied instead with open(path + os.path.sep + "execute.sh", "w") as file: file.write('''#!/bin/bash cd "{}" source "{}" python -u "{}"'''.format(path, get_activate(uid), script_path)) # Create pip install # TODO: Move this script to a folder so it can be copied instead with open(path + os.path.sep + "install.sh", "w") as file: file.write('''#!/bin/bash source "{}" pip install -r "{}"'''.format(get_activate(uid), requirements_path)) event.trigger("script_created", Script(uid).to_dict()) return uid
def evalaute_script_interval_triggers(tick, interval): second = tick * interval / 1000 for script in session.query(ScriptModel).all(): s = Script(script.uid) if script.enabled: for trigger in script.triggers: if trigger["type"] == "interval": if second % int(trigger["options"]["interval"]) == 0: dispatch_task( "execute_script", {"script_uid": script.uid}, task_priority="NOW", ) # Check that the script is enabled to run and that the interval is above 0 """if script.interval != 0 and script.enabled:
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
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