Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import os |
| 3 | import argparse |
| 4 | import sys |
| 5 | |
| 6 | sys.path.append('/opt/xos') |
| 7 | |
| 8 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 9 | from synchronizers.base.backend import Backend |
Scott Baker | 33d251a | 2016-08-11 13:23:19 -0700 | [diff] [blame] | 10 | from synchronizers.base.event_loop import set_driver |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 11 | from xos.config import Config, DEFAULT_CONFIG_FN |
| 12 | from core.models import Instance,NetworkTemplate |
| 13 | from xos.logger import Logger, logging, logger |
| 14 | from django.db import ProgrammingError |
| 15 | import time |
| 16 | |
| 17 | try: |
| 18 | from django import setup as django_setup # django 1.7 |
| 19 | except: |
| 20 | django_setup = False |
| 21 | |
| 22 | config = Config() |
| 23 | |
| 24 | # set the driver. |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame] | 25 | from synchronizers.openstack.driver import OpenStackDriver |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 26 | set_driver(OpenStackDriver()) |
| 27 | |
| 28 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 29 | def daemon(): |
| 30 | """Daemonize the current process.""" |
| 31 | if os.fork() != 0: os._exit(0) |
| 32 | os.setsid() |
| 33 | if os.fork() != 0: os._exit(0) |
| 34 | os.umask(0) |
| 35 | devnull = os.open(os.devnull, os.O_RDWR) |
| 36 | os.dup2(devnull, 0) |
| 37 | # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull |
| 38 | logdir=os.path.dirname(config.observer_logfile) |
| 39 | # when installed in standalone we might not have httpd installed |
| 40 | if not os.path.isdir(logdir): os.mkdir(logdir) |
| 41 | crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) |
| 42 | os.dup2(crashlog, 1) |
| 43 | os.dup2(crashlog, 2) |
| 44 | |
| 45 | if hasattr(config, "observer_pidfile"): |
| 46 | pidfile = config.get("observer_pidfile") |
| 47 | else: |
| 48 | pidfile = "/var/run/xosobserver.pid" |
| 49 | try: |
| 50 | file(pidfile,"w").write(str(os.getpid())) |
| 51 | except: |
| 52 | print "failed to create pidfile %s" % pidfile |
| 53 | |
| 54 | def main(): |
| 55 | # Generate command line parser |
| 56 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
| 57 | parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False, |
| 58 | help='Run as daemon.') |
| 59 | # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid |
| 60 | # throwing unrecognized argument exceptions |
| 61 | parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN, |
| 62 | help='Name of config file.') |
| 63 | args = parser.parse_args() |
| 64 | |
| 65 | if args.daemon: daemon() |
| 66 | |
| 67 | if django_setup: # 1.7 |
| 68 | django_setup() |
| 69 | |
| 70 | models_active = False |
| 71 | wait = False |
| 72 | while not models_active: |
| 73 | try: |
| 74 | _ = Instance.objects.first() |
| 75 | _ = NetworkTemplate.objects.first() |
| 76 | models_active = True |
| 77 | except Exception,e: |
| 78 | logger.info(str(e)) |
| 79 | logger.info('Waiting for data model to come up before starting...') |
| 80 | time.sleep(10) |
| 81 | wait = True |
| 82 | |
| 83 | if (wait): |
| 84 | time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up. |
| 85 | backend = Backend() |
| 86 | backend.run() |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | |
| 90 | main() |