Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | import time |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | class Synchronizer(object): |
| 23 | def __init__(self): |
| 24 | self.log = create_logger(Config().get("logging")) |
| 25 | |
| 26 | def create_model_accessor(self): |
| 27 | from modelaccessor import model_accessor |
| 28 | |
| 29 | self.model_accessor = model_accessor |
| 30 | |
| 31 | def wait_for_ready(self): |
| 32 | models_active = False |
| 33 | wait = False |
| 34 | while not models_active: |
| 35 | try: |
| 36 | _i = self.model_accessor.Instance.objects.first() |
| 37 | _n = self.model_accessor.NetworkTemplate.objects.first() |
| 38 | models_active = True |
| 39 | except Exception as e: |
| 40 | self.log.info("Exception", e=e) |
| 41 | self.log.info("Waiting for data model to come up before starting...") |
| 42 | time.sleep(10) |
| 43 | wait = True |
| 44 | |
| 45 | if wait: |
| 46 | time.sleep( |
| 47 | 60 |
| 48 | ) # Safety factor, seeing that we stumbled waiting for the data model to come up. |
| 49 | |
| 50 | def run(self): |
| 51 | self.create_model_accessor() |
| 52 | self.wait_for_ready() |
| 53 | |
| 54 | # Don't import backend until after the model accessor has been initialized. This is to support sync steps that |
| 55 | # use `from xossynchronizer.modelaccessor import ...` and require the model accessor to be initialized before |
| 56 | # their code can be imported. |
| 57 | |
| 58 | from backend import Backend |
| 59 | |
| 60 | log_closure = self.log.bind(synchronizer_name=Config().get("name")) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 61 | backend = Backend(log=log_closure, model_accessor=self.model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 62 | backend.run() |
| 63 | |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | main() |