From 095bf54511d3fa97fbf40d5682810074d22c68e6 Mon Sep 17 00:00:00 2001 From: Renuka Manavalan <47282725+renukamanavalan@users.noreply.github.com> Date: Fri, 5 Nov 2021 12:02:01 -0700 Subject: [PATCH] generic_updater: Fix service validator related issues (#1901) 1) Copy generic_updater_config.conf.json as part of install 2) Read conf file from install dir 3) Drop empty keys & tables upon jsonpatch.JsonPatch.apply to be in sync with redis update 4) Prefix service_validator module path with "generic_updater" --- generic_config_updater/change_applier.py | 19 ++++++++-- .../generic_updater_config.conf.json | 38 +++++++++---------- setup.py | 1 + 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/generic_config_updater/change_applier.py b/generic_config_updater/change_applier.py index b5ed4a637b..f01c4edc70 100644 --- a/generic_config_updater/change_applier.py +++ b/generic_config_updater/change_applier.py @@ -8,8 +8,8 @@ from swsscommon.swsscommon import ConfigDBConnector from .gu_common import genericUpdaterLogging - -UPDATER_CONF_FILE = "/etc/sonic/generic_config_updater.conf" +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) +UPDATER_CONF_FILE = f"{SCRIPT_DIR}/generic_updater_config.conf.json" logger = genericUpdaterLogging.get_logger(title="Change Applier") print_to_console = False @@ -44,6 +44,19 @@ def set_config(config_db, tbl, key, data): config_db.set_entry(tbl, key, data) +def prune_empty_table(data): + # For JSON Patch empty entries are valid + # With redis, when last key is removed, the table gets removed too. + # + # Hence where required, prune tables with no keys. + # + tables = list(data.keys()) + for tbl in tables: + if not data[tbl]: + data.pop(tbl) + return data + + class ChangeApplier: updater_conf = None @@ -111,7 +124,7 @@ def _report_mismatch(self, run_data, upd_data): def apply(self, change): run_data = self._get_running_config() - upd_data = change.apply(copy.deepcopy(run_data)) + upd_data = prune_empty_table(change.apply(copy.deepcopy(run_data))) upd_keys = defaultdict(dict) for tbl in sorted(set(run_data.keys()).union(set(upd_data.keys()))): diff --git a/generic_config_updater/generic_updater_config.conf.json b/generic_config_updater/generic_updater_config.conf.json index f86844cca5..647e62342e 100644 --- a/generic_config_updater/generic_updater_config.conf.json +++ b/generic_config_updater/generic_updater_config.conf.json @@ -1,21 +1,4 @@ { - "tables": { - "": { - "services_to_validate": [ "system_health" ] - }, - "PORT": { - "services_to_validate": [ "port_service" ] - }, - "SYSLOG_SERVER":{ - "services_to_validate": [ "rsyslog" ] - }, - "DHCP_RELAY": { - "services_to_validate": [ "dhcp-relay" ] - }, - "DHCP_SERVER": { - "services_to_validate": [ "dhcp-relay" ] - } - }, "README": [ "Validate_commands provides, module & method name as ", " .", @@ -41,6 +24,23 @@ "Note: The commands may be called in any order", "" ], + "tables": { + "": { + "services_to_validate": [ "system_health" ] + }, + "PORT": { + "services_to_validate": [ "port_service" ] + }, + "SYSLOG_SERVER":{ + "services_to_validate": [ "rsyslog" ] + }, + "DHCP_RELAY": { + "services_to_validate": [ "dhcp-relay" ] + }, + "DHCP_SERVER": { + "services_to_validate": [ "dhcp-relay" ] + } + }, "services": { "system_health": { "validate_commands": [ ] @@ -49,10 +49,10 @@ "validate_commands": [ ] }, "rsyslog": { - "validate_commands": [ "services_validator.ryslog_validator" ] + "validate_commands": [ "generic_config_updater.services_validator.ryslog_validator" ] }, "dhcp-relay": { - "validate_commands": [ "services_validator.dhcp_validator" ] + "validate_commands": [ "generic_config_updater.services_validator.dhcp_validator" ] } } } diff --git a/setup.py b/setup.py index 30e8c7bfd4..1dba5e6bf7 100644 --- a/setup.py +++ b/setup.py @@ -58,6 +58,7 @@ 'watchdogutil', ], package_data={ + 'generic_config_updater': ['generic_updater_config.conf.json'], 'show': ['aliases.ini'], 'sonic_installer': ['aliases.ini'], 'tests': ['acl_input/*',