blob: 0c551f8439146ed87ca1be5ff50b44d2fb980dac [file] [log] [blame]
Scott Bakerb74f0042014-09-30 11:31:56 -07001#!/usr/bin/env python
2import argparse
3import imp
4import inspect
5import os
6import sys
Scott Baker86e132c2015-02-11 21:38:09 -08007os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Scott Bakereaeabad2015-02-09 11:18:46 -08008sys.path.append("/opt/xos")
Scott Baker86e132c2015-02-11 21:38:09 -08009from xos.config import Config, DEFAULT_CONFIG_FN, XOS_DIR
Scott Bakerb74f0042014-09-30 11:31:56 -070010from util.logger import Logger, logging
11from observer.syncstep import SyncStep
12
13try:
14 from django import setup as django_setup # django 1.7
15except:
16 django_setup = False
17
18logger = Logger(level=logging.INFO)
19
Scott Baker0162bb42015-02-18 16:02:46 -080020class XOSConsistencyCheck:
Scott Bakerb74f0042014-09-30 11:31:56 -070021 def __init__(self):
22 self.sync_steps = []
23 self.load_sync_step_modules()
24
25 def load_sync_step_modules(self, step_dir=None):
26 if step_dir is None:
27 if hasattr(Config(), "observer_steps_dir"):
28 step_dir = Config().observer_steps_dir
29 else:
Scott Bakereaeabad2015-02-09 11:18:46 -080030 step_dir = XOS_DIR+"/observer/steps"
Scott Bakerb74f0042014-09-30 11:31:56 -070031
32 for fn in os.listdir(step_dir):
33 pathname = os.path.join(step_dir,fn)
34 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
35 module = imp.load_source(fn[:-3],pathname)
36 for classname in dir(module):
37 c = getattr(module, classname, None)
38
39 # make sure 'c' is a descendent of SyncStep and has a
40 # provides field (this eliminates the abstract base classes
41 # since they don't have a provides)
42
43 if inspect.isclass(c) and issubclass(c, SyncStep) and hasattr(c,"provides") and (c not in self.sync_steps):
44 self.sync_steps.append(c)
45 logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps]))
46
47 def run(self):
48 updated = True
49 while updated:
50 updated = False
51
52 for step in self.sync_steps:
53 if hasattr(step, "consistency_check"):
54 updated = updated or step(driver=None).consistency_check()
55
56 if updated:
57 logger.info('re-running consistency checks because something changed')
58
59def main():
60 if not "-C" in sys.argv:
Scott Bakereaeabad2015-02-09 11:18:46 -080061 print >> sys.stderr, "You probably wanted to use -C " + XOS_DIR + "/hpc_observer/hpc_observer_config"
Scott Bakerb74f0042014-09-30 11:31:56 -070062
63 # Generate command line parser
64 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
65 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
66 # throwing unrecognized argument exceptions
Scott Bakerd71335d2015-02-02 15:10:13 -080067 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
Scott Bakerb74f0042014-09-30 11:31:56 -070068 help='Name of config file.')
69 args = parser.parse_args()
70
71 if django_setup: # 1.7
72 django_setup()
73
Scott Baker0162bb42015-02-18 16:02:46 -080074 cc = XOSConsistencyCheck()
Scott Bakerb74f0042014-09-30 11:31:56 -070075 cc.run()
76
77if __name__ == '__main__':
78 main()
79