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 | |
| 17 | import os |
| 18 | import sys |
| 19 | import time |
| 20 | |
| 21 | from xosconfig import Config |
| 22 | from multistructlog import create_logger |
| 23 | |
| 24 | class Synchronizer(object): |
| 25 | def __init__(self): |
| 26 | self.log = create_logger(Config().get("logging")) |
| 27 | |
| 28 | def create_model_accessor(self): |
| 29 | from modelaccessor import model_accessor |
| 30 | |
| 31 | self.model_accessor = model_accessor |
| 32 | |
| 33 | def wait_for_ready(self): |
| 34 | models_active = False |
| 35 | wait = False |
| 36 | while not models_active: |
| 37 | try: |
| 38 | _i = self.model_accessor.Instance.objects.first() |
| 39 | _n = self.model_accessor.NetworkTemplate.objects.first() |
| 40 | models_active = True |
| 41 | except Exception as e: |
| 42 | self.log.info("Exception", e=e) |
| 43 | self.log.info("Waiting for data model to come up before starting...") |
| 44 | time.sleep(10) |
| 45 | wait = True |
| 46 | |
| 47 | if wait: |
| 48 | time.sleep( |
| 49 | 60 |
| 50 | ) # Safety factor, seeing that we stumbled waiting for the data model to come up. |
| 51 | |
| 52 | def run(self): |
| 53 | self.create_model_accessor() |
| 54 | self.wait_for_ready() |
| 55 | |
| 56 | # Don't import backend until after the model accessor has been initialized. This is to support sync steps that |
| 57 | # use `from xossynchronizer.modelaccessor import ...` and require the model accessor to be initialized before |
| 58 | # their code can be imported. |
| 59 | |
| 60 | from backend import Backend |
| 61 | |
| 62 | log_closure = self.log.bind(synchronizer_name=Config().get("name")) |
| 63 | backend = Backend(log=log_closure) |
| 64 | backend.run() |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | main() |