blob: 493b94afd6622ccff2f096b6b3dd2d737dbbb739 [file] [log] [blame]
Tony Mack9b7a8bd2013-06-24 15:08:01 -04001#!/usr/bin/env python
2import os
Tony Mack2c911102014-04-16 19:52:09 -04003import argparse
Sapan Bhatiae7841d82016-01-15 11:54:42 -05004import sys
5
6sys.path.append('/opt/xos')
7
Scott Baker76a840e2015-02-11 21:38:09 -08008os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Sapan Bhatia67ea0d72016-01-14 11:41:38 -05009from synchronizers.base.backend import Backend
Scott Baker76a840e2015-02-11 21:38:09 -080010from xos.config import Config, DEFAULT_CONFIG_FN
Sapan Bhatia8cda3a22016-01-27 19:02:35 +010011from core.models import Instance,NetworkTemplate
Scott Bakerf154cc22016-01-14 16:07:32 -080012from xos.logger import Logger, logging, logger
Sapan Bhatia3e3c05f2016-01-11 16:18:47 -050013from django.db import ProgrammingError
14import time
Scott Bakera277d6b2014-09-16 17:34:21 -070015
16try:
17 from django import setup as django_setup # django 1.7
18except:
19 django_setup = False
Tony Mack9b7a8bd2013-06-24 15:08:01 -040020
Tony Mack2c911102014-04-16 19:52:09 -040021config = Config()
22
23# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
24def daemon():
25 """Daemonize the current process."""
26 if os.fork() != 0: os._exit(0)
27 os.setsid()
28 if os.fork() != 0: os._exit(0)
29 os.umask(0)
30 devnull = os.open(os.devnull, os.O_RDWR)
31 os.dup2(devnull, 0)
32 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
Tony Mack6eb1ef82014-04-16 20:47:20 -040033 logdir=os.path.dirname(config.observer_logfile)
Tony Mack2c911102014-04-16 19:52:09 -040034 # when installed in standalone we might not have httpd installed
35 if not os.path.isdir(logdir): os.mkdir(logdir)
Tony Mack6eb1ef82014-04-16 20:47:20 -040036 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
Tony Mack2c911102014-04-16 19:52:09 -040037 os.dup2(crashlog, 1)
38 os.dup2(crashlog, 2)
39
Scott Bakerbd14a872015-02-19 13:50:06 -080040 if hasattr(config, "observer_pidfile"):
41 pidfile = config.get("observer_pidfile")
42 else:
43 pidfile = "/var/run/xosobserver.pid"
44 try:
45 file(pidfile,"w").write(str(os.getpid()))
46 except:
47 print "failed to create pidfile %s" % pidfile
48
Tony Mack2c911102014-04-16 19:52:09 -040049def main():
50 # Generate command line parser
51 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
Scott Bakera277d6b2014-09-16 17:34:21 -070052 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
Tony Mack2c911102014-04-16 19:52:09 -040053 help='Run as daemon.')
Scott Baker6ef76152014-04-30 09:40:23 -070054 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
55 # throwing unrecognized argument exceptions
Scott Baker37adc3e2015-02-02 15:10:13 -080056 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
Scott Baker6ef76152014-04-30 09:40:23 -070057 help='Name of config file.')
Tony Mack2c911102014-04-16 19:52:09 -040058 args = parser.parse_args()
Scott Bakera277d6b2014-09-16 17:34:21 -070059
Tony Mack2c911102014-04-16 19:52:09 -040060 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040061
Scott Bakera277d6b2014-09-16 17:34:21 -070062 if django_setup: # 1.7
63 django_setup()
64
Sapan Bhatia3e3c05f2016-01-11 16:18:47 -050065 models_active = False
66 wait = False
67 while not models_active:
68 try:
69 _ = Instance.objects.first()
Sapan Bhatia8cda3a22016-01-27 19:02:35 +010070 _ = NetworkTemplate.objects.first()
Sapan Bhatia3e3c05f2016-01-11 16:18:47 -050071 models_active = True
Sapan Bhatia8cda3a22016-01-27 19:02:35 +010072 except Exception,e:
73 logger.info(str(e))
Sapan Bhatia3e3c05f2016-01-11 16:18:47 -050074 logger.info('Waiting for data model to come up before starting...')
Sapan Bhatia8cda3a22016-01-27 19:02:35 +010075 time.sleep(10)
Sapan Bhatia3e3c05f2016-01-11 16:18:47 -050076 wait = True
77
78 if (wait):
79 time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up.
Tony Mack9b7a8bd2013-06-24 15:08:01 -040080 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040081 backend.run()
82
83if __name__ == '__main__':
84
85 main()