From 5318bb30326f7ac61bffe3deae25402fc4e2f184 Mon Sep 17 00:00:00 2001 From: Ashutosh Baghel Date: Sat, 23 Aug 2025 15:50:18 -0400 Subject: [PATCH 1/4] Fix hostcfgd, so mgmt interface can be deleted when no gw was given during add --- scripts/hostcfgd | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 4076a3b7..13ace166 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1585,21 +1585,42 @@ class MgmtIfaceCfg(object): def update_mgmt_iface(self, iface, key, data): """Handle update management interface config """ - syslog.syslog(syslog.LOG_DEBUG, 'MgmtIfaceCfg: mgmt iface update') + syslog.syslog(syslog.LOG_DEBUG, 'TEST-1 MgmtIfaceCfg: Starting interface update') - # Restart management interface service when config was changed - if data != self.iface_config_data.get(key): - cfg = {key: data} - syslog.syslog(syslog.LOG_INFO, f'MgmtIfaceCfg: Set new interface ' - f'config {cfg} for {iface}') - try: - run_cmd(['sudo', 'systemctl', 'restart', 'interfaces-config'], True, True) - except subprocess.CalledProcessError: - syslog.syslog(syslog.LOG_ERR, f'Failed to restart management ' - 'interface services') - return + existing_data = self.iface_config_data.get(key) + # Check if data has changed + if data != existing_data: + syslog.syslog(syslog.LOG_INFO, f'TEST-2 MgmtIfaceCfg: Updating config for {iface} with {data}') + # Update the interface configuration self.iface_config_data[key] = data + elif not data: + # If new data is empty, remove the key if it exists + if key in self.iface_config_data: + self.iface_config_data.pop(key) + syslog.syslog(syslog.LOG_INFO, f'TEST-3 MgmtIfaceCfg: Removed config for {iface}') + + # This entry is created explicitly by sonic-gnmi, but during delete, this entry is not deleted. + # This causes the interface.j2 to think static ip is still configured + subprocess.run(['redis-cli', '-n', '4', 'DEL', f'MGMT_INTERFACE|{iface}', '', ''], check=True) + else: + syslog.syslog(syslog.LOG_DEBUG, f'TEST-4 MgmtIfaceCfg: No existing config to remove for {iface}') + # No change; no need to restart services + return + else: + # Data hasn't changed; no need to proceed + syslog.syslog(syslog.LOG_DEBUG, f'TEST-5 MgmtIfaceCfg: No changes detected for {iface}') + return + + # Restart services after configuration change + try: + syslog.syslog(syslog.LOG_INFO, f'TEST-6 MgmtIfaceCfg: Updated:{self.iface_config_data}') + + run_cmd(['sudo', 'systemctl', 'restart', 'interfaces-config'], True, True) + + syslog.syslog(syslog.LOG_INFO, 'TEST-7 MgmtIfaceCfg: Successfully restarted services') + except subprocess.CalledProcessError: + syslog.syslog(syslog.LOG_ERR, 'TEST-8 MgmtIfaceCfg: Failed to restart management interface services') def update_mgmt_vrf(self, data): """Handle update management VRF state From 113c9fcbff50402c16aefa52fdba0954aeff1469 Mon Sep 17 00:00:00 2001 From: Ashutosh Baghel Date: Sat, 23 Aug 2025 19:38:39 -0400 Subject: [PATCH 2/4] Updated comments and syslog --- scripts/hostcfgd | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 13ace166..74c42a90 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1585,42 +1585,42 @@ class MgmtIfaceCfg(object): def update_mgmt_iface(self, iface, key, data): """Handle update management interface config """ - syslog.syslog(syslog.LOG_DEBUG, 'TEST-1 MgmtIfaceCfg: Starting interface update') + syslog.syslog(syslog.LOG_DEBUG, 'MgmtIfaceCfg: Starting interface update') existing_data = self.iface_config_data.get(key) # Check if data has changed if data != existing_data: - syslog.syslog(syslog.LOG_INFO, f'TEST-2 MgmtIfaceCfg: Updating config for {iface} with {data}') + syslog.syslog(syslog.LOG_INFO, f'MgmtIfaceCfg: Updating config for {iface} with {data}') # Update the interface configuration self.iface_config_data[key] = data elif not data: # If new data is empty, remove the key if it exists if key in self.iface_config_data: self.iface_config_data.pop(key) - syslog.syslog(syslog.LOG_INFO, f'TEST-3 MgmtIfaceCfg: Removed config for {iface}') + syslog.syslog(syslog.LOG_INFO, f'MgmtIfaceCfg: Removed config for {iface}') # This entry is created explicitly by sonic-gnmi, but during delete, this entry is not deleted. # This causes the interface.j2 to think static ip is still configured subprocess.run(['redis-cli', '-n', '4', 'DEL', f'MGMT_INTERFACE|{iface}', '', ''], check=True) else: - syslog.syslog(syslog.LOG_DEBUG, f'TEST-4 MgmtIfaceCfg: No existing config to remove for {iface}') + syslog.syslog(syslog.LOG_DEBUG, f'MgmtIfaceCfg: No existing config to remove for {iface}') # No change; no need to restart services return else: # Data hasn't changed; no need to proceed - syslog.syslog(syslog.LOG_DEBUG, f'TEST-5 MgmtIfaceCfg: No changes detected for {iface}') + syslog.syslog(syslog.LOG_DEBUG, f'MgmtIfaceCfg: No changes detected for {iface}') return # Restart services after configuration change try: - syslog.syslog(syslog.LOG_INFO, f'TEST-6 MgmtIfaceCfg: Updated:{self.iface_config_data}') + syslog.syslog(syslog.LOG_INFO, f'MgmtIfaceCfg: Updated:{self.iface_config_data}') run_cmd(['sudo', 'systemctl', 'restart', 'interfaces-config'], True, True) - syslog.syslog(syslog.LOG_INFO, 'TEST-7 MgmtIfaceCfg: Successfully restarted services') + syslog.syslog(syslog.LOG_INFO, 'MgmtIfaceCfg: Successfully restarted services') except subprocess.CalledProcessError: - syslog.syslog(syslog.LOG_ERR, 'TEST-8 MgmtIfaceCfg: Failed to restart management interface services') + syslog.syslog(syslog.LOG_ERR, 'MgmtIfaceCfg: Failed to restart management interface services') def update_mgmt_vrf(self, data): """Handle update management VRF state From 665a2f51891f388854ec391299cb1ba835d7d629 Mon Sep 17 00:00:00 2001 From: Ashutosh Baghel Date: Sat, 23 Aug 2025 20:43:28 -0400 Subject: [PATCH 3/4] Updated code --- scripts/hostcfgd | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 74c42a90..2456e0e4 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1599,10 +1599,6 @@ class MgmtIfaceCfg(object): if key in self.iface_config_data: self.iface_config_data.pop(key) syslog.syslog(syslog.LOG_INFO, f'MgmtIfaceCfg: Removed config for {iface}') - - # This entry is created explicitly by sonic-gnmi, but during delete, this entry is not deleted. - # This causes the interface.j2 to think static ip is still configured - subprocess.run(['redis-cli', '-n', '4', 'DEL', f'MGMT_INTERFACE|{iface}', '', ''], check=True) else: syslog.syslog(syslog.LOG_DEBUG, f'MgmtIfaceCfg: No existing config to remove for {iface}') # No change; no need to restart services From a7905aa89d9aa82c9a917b72968374d6e2954b7c Mon Sep 17 00:00:00 2001 From: Ashutosh Baghel Date: Sat, 23 Aug 2025 20:45:11 -0400 Subject: [PATCH 4/4] Updated logs --- scripts/hostcfgd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 2456e0e4..7aaaef26 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1585,7 +1585,7 @@ class MgmtIfaceCfg(object): def update_mgmt_iface(self, iface, key, data): """Handle update management interface config """ - syslog.syslog(syslog.LOG_DEBUG, 'MgmtIfaceCfg: Starting interface update') + syslog.syslog(syslog.LOG_DEBUG, 'MgmtIfaceCfg: mgmt iface update') existing_data = self.iface_config_data.get(key)