Scott Baker | d9d7437 | 2018-05-03 14:39:38 -0700 | [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 | # xos-wait-dynamicload.py |
| 18 | # Wait for dynamic load to complete. |
| 19 | # Syntax: xos-wait-dynamicload.py <retries> <base_url> [service_names] |
| 20 | # |
| 21 | # Example: xos-wait-dynamicload.py 120 http://192.168.42.253:30006 vsg-hw volt fabric vrouter |
| 22 | |
| 23 | import sys |
| 24 | import time |
| 25 | import requests |
| 26 | |
| 27 | DELAY=1 |
| 28 | |
| 29 | def main(): |
| 30 | if len(sys.argv)<4: |
| 31 | print "Syntax: xos-wait-dynamicload.py <retries> <base_url> [service_names]" |
| 32 | sys.exit(-1) |
| 33 | |
| 34 | retries = int(sys.argv[1]) |
| 35 | base_url = sys.argv[2] |
| 36 | service_names = sys.argv[3:] |
| 37 | attempt = 0 |
| 38 | while True: |
| 39 | attempt += 1 |
| 40 | if (attempt > retries): |
| 41 | print "Exceeded maximum retries" |
| 42 | sys.exit(-1) |
| 43 | |
| 44 | print "Attempt %d of %d" % (attempt, retries), |
| 45 | |
| 46 | try: |
| 47 | r = requests.get(base_url + "/xosapi/v1/dynamicload/load_status") |
| 48 | except requests.exceptions.ConnectionError: |
| 49 | print "Connection error" |
| 50 | time.sleep(DELAY) |
| 51 | continue |
| 52 | |
| 53 | if r.status_code != 200: |
| 54 | print "Received error response", r.status_code |
| 55 | time.sleep(DELAY) |
| 56 | continue |
| 57 | |
| 58 | services_by_name = {} |
| 59 | for service in r.json()["services"]: |
| 60 | services_by_name[service["name"]] = service |
| 61 | missing = [] |
| 62 | for service_name in service_names: |
| 63 | service = services_by_name.get(service_name, {"state": "missing"}) |
| 64 | if service["state"] != "present": |
| 65 | missing.append(service_name) |
| 66 | if not missing: |
| 67 | print "All required services are present" |
| 68 | sys.exit(0) |
| 69 | |
| 70 | print "Waiting on services: ", ", ".join(missing) |
| 71 | time.sleep(DELAY) |
| 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | main() |