rename sanity_check to consistency_check; run it from an external program rather...
[plstackapi.git] / planetstack / hpc_observer / fsck.py
1 #!/usr/bin/env python
2 import argparse
3 import imp
4 import inspect
5 import os
6 import sys
7 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
8 sys.path.append("/opt/planetstack")
9 from planetstack.config import Config
10 from util.logger import Logger, logging
11 from observer.syncstep import SyncStep
12
13 try:
14     from django import setup as django_setup # django 1.7
15 except:
16     django_setup = False
17
18 logger = Logger(level=logging.INFO)
19
20 class PlanetStackConsistencyCheck:
21         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:
30                                 step_dir = "/opt/planetstack/observer/steps"
31
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
59 def main():
60     if not "-C" in sys.argv:
61         print >> sys.stderr, "You probably wanted to use -C /opt/planetstack/hpc_observer/hpc_observer_config"
62
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
67     parser.add_argument('-C', '--config', dest='config_file', action='store', default="/opt/planetstack/plstackapi_config",
68                         help='Name of config file.')
69     args = parser.parse_args()
70
71     if django_setup: # 1.7
72         django_setup()
73
74     cc = PlanetStackConsistencyCheck()
75     cc.run()
76
77 if __name__ == '__main__':
78     main()
79