|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This is to verify if Database has critical tables present before warmboot can proceed. |
| 5 | +If warmboot is allowed with missing critical tables, it can lead to issues in going |
| 6 | +down path or during the recovery path. This test detects such issues before proceeding. |
| 7 | +The verification procedure here uses JSON schemas to verify the DB entities. |
| 8 | +
|
| 9 | +In future, to verify new tables or their content, just the schema modification is needed. |
| 10 | +No modification may be needed to the integrity check logic. |
| 11 | +""" |
| 12 | + |
| 13 | +import os, sys |
| 14 | +import json, jsonschema |
| 15 | +import syslog |
| 16 | +import subprocess |
| 17 | +import traceback |
| 18 | + |
| 19 | +DB_SCHEMA = { |
| 20 | + "COUNTERS_DB": |
| 21 | + { |
| 22 | + "$schema": "http://json-schema.org/draft-06/schema", |
| 23 | + "type": "object", |
| 24 | + "title": "Schema for COUNTERS DB's entities", |
| 25 | + "required": ["COUNTERS_PORT_NAME_MAP"], |
| 26 | + "properties": { |
| 27 | + "COUNTERS_PORT_NAME_MAP": {"$id": "#/properties/COUNTERS_PORT_NAME_MAP", "type": "object"} |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + if not DB_SCHEMA: |
| 35 | + return 0 |
| 36 | + |
| 37 | + for db_name, schema in DB_SCHEMA.items(): |
| 38 | + db_dump_file = "/tmp/{}.json".format(db_name) |
| 39 | + dump_db_cmd = "sonic-db-dump -n 'COUNTERS_DB' -y > {}".format(db_dump_file) |
| 40 | + p = subprocess.Popen(dump_db_cmd, shell=True, text=True, |
| 41 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 42 | + (_, err) = p.communicate() |
| 43 | + rc = p.wait() |
| 44 | + if rc != 0: |
| 45 | + print("Failed to dump db {}. Return code: {} with err: {}".format(db_name, rc, err)) |
| 46 | + |
| 47 | + try: |
| 48 | + with open(db_dump_file) as fp: |
| 49 | + db_dump_data = json.load(fp) |
| 50 | + except ValueError as err: |
| 51 | + syslog.syslog(syslog.LOG_DEBUG, "DB json file is not a valid json file. " +\ |
| 52 | + "Error: {}".format(str(err))) |
| 53 | + return 1 |
| 54 | + |
| 55 | + # What: Validate if critical tables and entries are present in DB. |
| 56 | + # Why: This is needed to avoid warmbooting with a bad DB; which can |
| 57 | + # potentially trigger failures in the reboot recovery path. |
| 58 | + # How: Validate DB against a schema which defines required tables. |
| 59 | + try: |
| 60 | + jsonschema.validate(instance=db_dump_data, schema=schema) |
| 61 | + except jsonschema.exceptions.ValidationError as err: |
| 62 | + syslog.syslog(syslog.LOG_ERR, "Database is missing tables/entries needed for reboot procedure. " +\ |
| 63 | + "DB integrity check failed with:\n{}".format(str(err.message))) |
| 64 | + return 1 |
| 65 | + syslog.syslog(syslog.LOG_DEBUG, "Database integrity checks passed.") |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + res = 0 |
| 71 | + try: |
| 72 | + res = main() |
| 73 | + except KeyboardInterrupt: |
| 74 | + syslog.syslog(syslog.LOG_NOTICE, "SIGINT received. Quitting") |
| 75 | + res = 1 |
| 76 | + except Exception as e: |
| 77 | + syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc())) |
| 78 | + res = 2 |
| 79 | + finally: |
| 80 | + syslog.closelog() |
| 81 | + try: |
| 82 | + sys.exit(res) |
| 83 | + except SystemExit: |
| 84 | + os._exit(res) |
0 commit comments