blob: 6cd8e7a25db605bfaa754c94d74daad435a83b80 [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")
Scott Bakerb63ea792016-08-11 10:24:48 -07009from xos.config import Config, DEFAULT_CONFIG_FN
Scott Bakerb63ea792016-08-11 10:24:48 -070010from xos.logger import Logger, logging, logger
Scott Bakerb63ea792016-08-11 10:24:48 -070011import time
12
Scott Bakeraf599eb2017-03-21 12:43:26 -070013from synchronizers.new_base.modelaccessor import *
14from synchronizers.new_base.backend import Backend
15from synchronizers.new_base.event_loop import set_driver
Scott Bakerb63ea792016-08-11 10:24:48 -070016
17config = Config()
18
Scott Bakeraf599eb2017-03-21 12:43:26 -070019logger = Logger(level=logging.INFO)
20
21# TODO: These two lines are the only difference between this file and
22# new_base/xos-synchronizer.py. Reconcile these.
Scott Bakerb63ea792016-08-11 10:24:48 -070023# set the driver.
Scott Baker04a37f52016-08-11 10:52:21 -070024from synchronizers.openstack.driver import OpenStackDriver
Scott Bakerb63ea792016-08-11 10:24:48 -070025set_driver(OpenStackDriver())
26
27# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
28def daemon():
29 """Daemonize the current process."""
30 if os.fork() != 0: os._exit(0)
31 os.setsid()
32 if os.fork() != 0: os._exit(0)
33 os.umask(0)
34 devnull = os.open(os.devnull, os.O_RDWR)
35 os.dup2(devnull, 0)
36 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
37 logdir=os.path.dirname(config.observer_logfile)
38 # when installed in standalone we might not have httpd installed
39 if not os.path.isdir(logdir): os.mkdir(logdir)
40 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
41 os.dup2(crashlog, 1)
42 os.dup2(crashlog, 2)
43
44 if hasattr(config, "observer_pidfile"):
45 pidfile = config.get("observer_pidfile")
46 else:
47 pidfile = "/var/run/xosobserver.pid"
48 try:
49 file(pidfile,"w").write(str(os.getpid()))
50 except:
51 print "failed to create pidfile %s" % pidfile
52
53def main():
54 # Generate command line parser
55 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
56 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
57 help='Run as daemon.')
58 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
59 # throwing unrecognized argument exceptions
60 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
61 help='Name of config file.')
62 args = parser.parse_args()
63
64 if args.daemon: daemon()
65
Scott Bakerb63ea792016-08-11 10:24:48 -070066 models_active = False
67 wait = False
68 while not models_active:
69 try:
70 _ = Instance.objects.first()
71 _ = NetworkTemplate.objects.first()
72 models_active = True
73 except Exception,e:
74 logger.info(str(e))
75 logger.info('Waiting for data model to come up before starting...')
76 time.sleep(10)
77 wait = True
78
79 if (wait):
80 time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up.
81 backend = Backend()
82 backend.run()
83
84if __name__ == '__main__':
85
86 main()