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") |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 9 | from xos.config import Config, DEFAULT_CONFIG_FN |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 10 | from xos.logger import Logger, logging, logger |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 11 | import time |
| 12 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 13 | from synchronizers.new_base.modelaccessor import * |
| 14 | from synchronizers.new_base.backend import Backend |
| 15 | from synchronizers.new_base.event_loop import set_driver |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 16 | |
| 17 | config = Config() |
| 18 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 19 | logger = 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | # set the driver. |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame] | 24 | from synchronizers.openstack.driver import OpenStackDriver |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 25 | set_driver(OpenStackDriver()) |
| 26 | |
| 27 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 28 | def 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 | |
| 53 | def 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 66 | 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 | |
| 84 | if __name__ == '__main__': |
| 85 | |
| 86 | main() |