| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 1 | """ A very simple Tosca daemon. Every ten seconds it looks for new programs in |
| 2 | "run" or "destroy" status, and executes them. |
| 3 | |
| 4 | TODO: Replace this with observer and/or model_policies ? |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | from threading import Thread |
| 10 | import time |
| 11 | |
| 12 | # add the parent directory to sys.path |
| 13 | import os,sys,inspect |
| 14 | currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 15 | parentdir = os.path.dirname(currentdir) |
| 16 | sys.path.append(parentdir) |
| 17 | |
| 18 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 19 | import django |
| 20 | django.setup() |
| 21 | |
| 22 | from core.models import Program, User |
| 23 | from nodeselect import XOSNodeSelector |
| 24 | from imageselect import XOSImageSelector |
| 25 | import traceback |
| 26 | |
| 27 | from engine import XOSTosca |
| 28 | |
| 29 | class ToscaDaemon(Thread): |
| 30 | def __init__(self): |
| 31 | Thread.__init__(self) |
| 32 | self.daemon = True |
| 33 | |
| 34 | def run_program(self, model): |
| 35 | try: |
| 36 | print "*** Run Program %s ***" % model.name |
| 37 | model.status = "executing" |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 38 | model.messages = "" |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 39 | model.save() |
| 40 | xt = XOSTosca(model.contents, parent_dir=currentdir, log_to_console=True) |
| 41 | xt.execute(model.owner) |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 42 | model.messages = "\n".join(xt.log_msgs) |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 43 | model.status = "complete" |
| 44 | except: |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 45 | model.messages = traceback.format_exc() |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 46 | model.status = "exception" |
| 47 | traceback.print_exc() |
| 48 | model.command = None |
| 49 | model.save() |
| 50 | |
| 51 | def destroy_program(self, model): |
| 52 | try: |
| 53 | print "*** Destroy Program %s ***" % model.name |
| 54 | model.status = "executing" |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 55 | model.messages = "" |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 56 | model.save() |
| 57 | xt = XOSTosca(model.contents, parent_dir=currentdir) |
| 58 | xt.destroy(model.owner) |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 59 | model.messages = "\n".join(xt.log_msgs) |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 60 | model.status = "complete" |
| 61 | except: |
| Scott Baker | 366b477 | 2015-08-12 19:05:35 -0700 | [diff] [blame] | 62 | model.messages = traceback.format_exc() |
| Scott Baker | 61b708f | 2015-08-11 17:24:08 -0700 | [diff] [blame] | 63 | model.status = "exception" |
| 64 | traceback.print_exc() |
| 65 | model.command = None |
| 66 | model.save() |
| 67 | |
| 68 | def run_once(self): |
| 69 | models = Program.objects.filter(kind="tosca", command="run") |
| 70 | for model in models: |
| 71 | self.run_program(model) |
| 72 | |
| 73 | models = Program.objects.filter(kind="tosca", command="destroy") |
| 74 | for model in models: |
| 75 | self.destroy_program(model) |
| 76 | |
| 77 | def run(self): |
| 78 | while True: |
| 79 | self.run_once() |
| 80 | time.sleep(10) |
| 81 | django.db.reset_queries() |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | if "--once" in sys.argv: |
| 85 | ToscaDaemon().execute_once() |
| 86 | else: |
| 87 | ToscaDaemon().start() |
| 88 | |
| 89 | print "Running forever..." |
| 90 | while True: |
| 91 | time.sleep(60) |
| 92 | |