blob: c2d9e43ed3ae8ebddec0780adc42fd14067f7ab1 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001#!/usr/bin/env python
2import os
3import argparse
4import sys
5
6sys.path.append('/opt/xos')
7
8os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
9from synchronizers.base.backend import Backend
Scott Baker33d251a2016-08-11 13:23:19 -070010from synchronizers.base.event_loop import set_driver
Scott Bakerb63ea792016-08-11 10:24:48 -070011from xos.config import Config, DEFAULT_CONFIG_FN
12from core.models import Instance,NetworkTemplate
13from xos.logger import Logger, logging, logger
14from django.db import ProgrammingError
15import time
16
17try:
18 from django import setup as django_setup # django 1.7
19except:
20 django_setup = False
21
22config = Config()
23
24# set the driver.
Scott Baker04a37f52016-08-11 10:52:21 -070025from synchronizers.openstack.driver import OpenStackDriver
Scott Bakerb63ea792016-08-11 10:24:48 -070026set_driver(OpenStackDriver())
27
28# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
29def 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
54def 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
88if __name__ == '__main__':
89
90 main()