Matteo Scandolo | ede125b | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 17 | #!/usr/bin/env python |
| 18 | import argparse |
| 19 | import imp |
| 20 | import inspect |
| 21 | import os |
| 22 | import sys |
| 23 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 24 | sys.path.append("/opt/xos") |
| 25 | from xos.config import Config, DEFAULT_CONFIG_FN, XOS_DIR |
| 26 | from xos.logger import Logger, logging |
| 27 | from synchronizers.base.syncstep import SyncStep |
| 28 | |
| 29 | try: |
| 30 | from django import setup as django_setup # django 1.7 |
| 31 | except: |
| 32 | django_setup = False |
| 33 | |
| 34 | logger = Logger(level=logging.INFO) |
| 35 | |
| 36 | class XOSConsistencyCheck: |
| 37 | def __init__(self): |
| 38 | self.sync_steps = [] |
| 39 | self.load_sync_step_modules() |
| 40 | |
| 41 | def load_sync_step_modules(self, step_dir=None): |
| 42 | if step_dir is None: |
| 43 | if hasattr(Config(), "observer_steps_dir"): |
| 44 | step_dir = Config().observer_steps_dir |
| 45 | else: |
| 46 | step_dir = XOS_DIR+"/observer/steps" |
| 47 | |
| 48 | for fn in os.listdir(step_dir): |
| 49 | pathname = os.path.join(step_dir,fn) |
| 50 | if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"): |
| 51 | module = imp.load_source(fn[:-3],pathname) |
| 52 | for classname in dir(module): |
| 53 | c = getattr(module, classname, None) |
| 54 | |
| 55 | # make sure 'c' is a descendent of SyncStep and has a |
| 56 | # provides field (this eliminates the abstract base classes |
| 57 | # since they don't have a provides) |
| 58 | |
| 59 | if inspect.isclass(c) and issubclass(c, SyncStep) and hasattr(c,"provides") and (c not in self.sync_steps): |
| 60 | self.sync_steps.append(c) |
| 61 | logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps])) |
| 62 | |
| 63 | def run(self): |
| 64 | updated = True |
| 65 | while updated: |
| 66 | updated = False |
| 67 | |
| 68 | for step in self.sync_steps: |
| 69 | if hasattr(step, "consistency_check"): |
| 70 | updated = updated or step(driver=None).consistency_check() |
| 71 | |
| 72 | if updated: |
| 73 | logger.info('re-running consistency checks because something changed') |
| 74 | |
| 75 | def main(): |
| 76 | if not "-C" in sys.argv: |
| 77 | print >> sys.stderr, "You probably wanted to use -C " + XOS_DIR + "/hpc_observer/hpc_observer_config" |
| 78 | |
| 79 | # Generate command line parser |
| 80 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
| 81 | # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid |
| 82 | # throwing unrecognized argument exceptions |
| 83 | parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN, |
| 84 | help='Name of config file.') |
| 85 | args = parser.parse_args() |
| 86 | |
| 87 | if django_setup: # 1.7 |
| 88 | django_setup() |
| 89 | |
| 90 | cc = XOSConsistencyCheck() |
| 91 | cc.run() |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | main() |
| 95 | |