blob: 493b94afd6622ccff2f096b6b3dd2d737dbbb739 [file] [log] [blame]
Sapan Bhatia04f34c42016-01-18 09:31:25 -05001#!/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
10from xos.config import Config, DEFAULT_CONFIG_FN
Sapan Bhatia854e1832016-01-27 19:02:35 +010011from core.models import Instance,NetworkTemplate
Sapan Bhatia04f34c42016-01-18 09:31:25 -050012from xos.logger import Logger, logging, logger
13from django.db import ProgrammingError
14import time
15
16try:
17 from django import setup as django_setup # django 1.7
18except:
19 django_setup = False
20
21config = 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
33 logdir=os.path.dirname(config.observer_logfile)
34 # when installed in standalone we might not have httpd installed
35 if not os.path.isdir(logdir): os.mkdir(logdir)
36 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
37 os.dup2(crashlog, 1)
38 os.dup2(crashlog, 2)
39
40 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
49def main():
50 # Generate command line parser
51 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
52 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
53 help='Run as daemon.')
54 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
55 # throwing unrecognized argument exceptions
56 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
57 help='Name of config file.')
58 args = parser.parse_args()
59
60 if args.daemon: daemon()
61
62 if django_setup: # 1.7
63 django_setup()
64
65 models_active = False
66 wait = False
67 while not models_active:
68 try:
69 _ = Instance.objects.first()
Sapan Bhatia854e1832016-01-27 19:02:35 +010070 _ = NetworkTemplate.objects.first()
Sapan Bhatia04f34c42016-01-18 09:31:25 -050071 models_active = True
Sapan Bhatia854e1832016-01-27 19:02:35 +010072 except Exception,e:
73 logger.info(str(e))
Sapan Bhatia04f34c42016-01-18 09:31:25 -050074 logger.info('Waiting for data model to come up before starting...')
Sapan Bhatia854e1832016-01-27 19:02:35 +010075 time.sleep(10)
Sapan Bhatia04f34c42016-01-18 09:31:25 -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.
80 backend = Backend()
81 backend.run()
82
83if __name__ == '__main__':
84
85 main()