From 3de94dbab8bbbce4f57f01016eb863f47e3f0580 Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Tue, 22 Oct 2019 18:41:28 -0700 Subject: [PATCH 1/8] add support in hostcfgd to enable /disable telemetry --- files/image_config/hostcfgd/hostcfgd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index b32e3d8cd668..c0e9697b4fdd 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -304,6 +304,25 @@ class HostConfigDaemon: add = False self.iptables.iptables_handler(key, data, add) + + def feature_status_handler(self, key, data): + status_data = self.config_db.get_table('FEATURES') + for key in status_data.keys(): + if not key: + syslog.syslog(syslog.LOG_WARNING, "FEATURES key is missing") + return + status = status_data[key]['status'] + if not status: + syslog.syslog(syslog.LOG_WARNING, "status is missing for {}".format(key)) + return + if status == "enabled": + sudo systemctl enable key + sudo systemctl start key + syslog.syslog(syslog.LOG_INFO, "FEATURES {} is enabled and started".format(key)) + if status == "disabled": + sudo systemctl stop key + sudo systemctl disable key + syslog.syslog(syslog.LOG_INFO, "FEATURES {} is stopped and disabled".format(key)) def start(self): self.config_db.subscribe('AAA', lambda table, key, data: self.aaa_handler(key, data)) @@ -311,6 +330,7 @@ class HostConfigDaemon: self.config_db.subscribe('TACPLUS', lambda table, key, data: self.tacacs_global_handler(key, data)) self.config_db.subscribe('DEVICE_METADATA', lambda table, key, data: self.hostname_handler(key, data)) self.config_db.subscribe('LOOPBACK_INTERFACE', lambda table, key, data: self.lpbk_handler(key, data)) + self.config_db.subscribe('FEATURES', lambda table, key, data: self.feature_status_handler(key, data)) self.config_db.listen() From e819cbd923f849021a989fa7eb0bc2dfdd4cec69 Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Wed, 23 Oct 2019 12:06:48 -0700 Subject: [PATCH 2/8] changing table name to feature --- files/image_config/hostcfgd/hostcfgd | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index c0e9697b4fdd..08e00f7ede50 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -306,10 +306,10 @@ class HostConfigDaemon: self.iptables.iptables_handler(key, data, add) def feature_status_handler(self, key, data): - status_data = self.config_db.get_table('FEATURES') + status_data = self.config_db.get_table('FEATURE') for key in status_data.keys(): if not key: - syslog.syslog(syslog.LOG_WARNING, "FEATURES key is missing") + syslog.syslog(syslog.LOG_WARNING, "FEATURE key is missing") return status = status_data[key]['status'] if not status: @@ -318,11 +318,11 @@ class HostConfigDaemon: if status == "enabled": sudo systemctl enable key sudo systemctl start key - syslog.syslog(syslog.LOG_INFO, "FEATURES {} is enabled and started".format(key)) + syslog.syslog(syslog.LOG_INFO, "FEATURE {} is enabled and started".format(key)) if status == "disabled": sudo systemctl stop key sudo systemctl disable key - syslog.syslog(syslog.LOG_INFO, "FEATURES {} is stopped and disabled".format(key)) + syslog.syslog(syslog.LOG_INFO, "FEATURE {} is stopped and disabled".format(key)) def start(self): self.config_db.subscribe('AAA', lambda table, key, data: self.aaa_handler(key, data)) @@ -330,7 +330,7 @@ class HostConfigDaemon: self.config_db.subscribe('TACPLUS', lambda table, key, data: self.tacacs_global_handler(key, data)) self.config_db.subscribe('DEVICE_METADATA', lambda table, key, data: self.hostname_handler(key, data)) self.config_db.subscribe('LOOPBACK_INTERFACE', lambda table, key, data: self.lpbk_handler(key, data)) - self.config_db.subscribe('FEATURES', lambda table, key, data: self.feature_status_handler(key, data)) + self.config_db.subscribe('FEATURE', lambda table, key, data: self.feature_status_handler(key, data)) self.config_db.listen() From 1038b1c26ae50e25e5ee462c8304169e3b90a12a Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Mon, 18 Nov 2019 18:51:31 -0800 Subject: [PATCH 3/8] correcting command calling in script --- files/image_config/hostcfgd/hostcfgd | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 08e00f7ede50..4b034d61ed12 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -308,20 +308,36 @@ class HostConfigDaemon: def feature_status_handler(self, key, data): status_data = self.config_db.get_table('FEATURE') for key in status_data.keys(): - if not key: - syslog.syslog(syslog.LOG_WARNING, "FEATURE key is missing") - return + if not key: + syslog.syslog(syslog.LOG_WARNING, "FEATURE key is missing") + return status = status_data[key]['status'] if not status: syslog.syslog(syslog.LOG_WARNING, "status is missing for {}".format(key)) return if status == "enabled": - sudo systemctl enable key - sudo systemctl start key + start_cmds=[] + start_cmds.append("sudo systemctl enable {}".format(key)) + start_cmds.append("sudo systemctl start {}".format(key)) + for cmd in start_cmds: + syslog.syslog(syslog.LOG_INFO, "Running cmd - {}".format(cmd)) + try: + subprocess.check_call(cmd, shell=True) + except subprocess.CalledProcessError as err: + syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" + .format(err.cmd, err.returncode, err.output)) syslog.syslog(syslog.LOG_INFO, "FEATURE {} is enabled and started".format(key)) if status == "disabled": - sudo systemctl stop key - sudo systemctl disable key + stop_cmds=[] + stop_cmds.append("sudo systemctl stop {}".format(key)) + stop_cmds.append("sudo systemctl disable {}".format(key)) + for cmd in stop_cmds: + syslog.syslog(syslog.LOG_INFO, "Running cmd - {}".format(cmd)) + try: + subprocess.check_call(cmd, shell=True) + except subprocess.CalledProcessError as err: + syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" + .format(err.cmd, err.returncode, err.output)) syslog.syslog(syslog.LOG_INFO, "FEATURE {} is stopped and disabled".format(key)) def start(self): From 91e1b9db7cfda9c445982799f5c2515998eda449 Mon Sep 17 00:00:00 2001 From: pra-moh <49077256+pra-moh@users.noreply.github.com> Date: Wed, 20 Nov 2019 16:25:01 -0800 Subject: [PATCH 4/8] Update files/image_config/hostcfgd/hostcfgd Co-Authored-By: Joe LeVeque --- files/image_config/hostcfgd/hostcfgd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 4b034d61ed12..16a267b1dd67 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -338,7 +338,7 @@ class HostConfigDaemon: except subprocess.CalledProcessError as err: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) - syslog.syslog(syslog.LOG_INFO, "FEATURE {} is stopped and disabled".format(key)) + syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(key)) def start(self): self.config_db.subscribe('AAA', lambda table, key, data: self.aaa_handler(key, data)) @@ -357,4 +357,3 @@ def main(): if __name__ == "__main__": main() - From b38744f0f18737b5d6bedc39176d3971178b147d Mon Sep 17 00:00:00 2001 From: pra-moh <49077256+pra-moh@users.noreply.github.com> Date: Wed, 20 Nov 2019 16:25:07 -0800 Subject: [PATCH 5/8] Update files/image_config/hostcfgd/hostcfgd Co-Authored-By: Joe LeVeque --- files/image_config/hostcfgd/hostcfgd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 16a267b1dd67..353eaf77c19b 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -326,7 +326,7 @@ class HostConfigDaemon: except subprocess.CalledProcessError as err: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) - syslog.syslog(syslog.LOG_INFO, "FEATURE {} is enabled and started".format(key)) + syslog.syslog(syslog.LOG_INFO, "Feature '{}' is enabled and started".format(key)) if status == "disabled": stop_cmds=[] stop_cmds.append("sudo systemctl stop {}".format(key)) From 668b1596ab62ded5b40c4ea5aba4679464976295 Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Thu, 21 Nov 2019 14:21:55 -0800 Subject: [PATCH 6/8] adding else to log error in case of unexpected status value --- files/image_config/hostcfgd/hostcfgd | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 4b034d61ed12..aa0cf1f83a9e 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -327,7 +327,7 @@ class HostConfigDaemon: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) syslog.syslog(syslog.LOG_INFO, "FEATURE {} is enabled and started".format(key)) - if status == "disabled": + elif status == "disabled": stop_cmds=[] stop_cmds.append("sudo systemctl stop {}".format(key)) stop_cmds.append("sudo systemctl disable {}".format(key)) @@ -339,6 +339,8 @@ class HostConfigDaemon: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) syslog.syslog(syslog.LOG_INFO, "FEATURE {} is stopped and disabled".format(key)) + else: + syslog.syslog(syslog.LOG_ERR, "Unexpected status value for {}".format(key)) def start(self): self.config_db.subscribe('AAA', lambda table, key, data: self.aaa_handler(key, data)) From 3ed0120c86ac5b634b2142de2076875002f08e0a Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Thu, 21 Nov 2019 14:34:36 -0800 Subject: [PATCH 7/8] adding status value in error --- files/image_config/hostcfgd/hostcfgd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index ab1c91df618b..319c873d21c1 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -340,7 +340,7 @@ class HostConfigDaemon: .format(err.cmd, err.returncode, err.output)) syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(key)) else: - syslog.syslog(syslog.LOG_ERR, "Unexpected status value for {}".format(key)) + syslog.syslog(syslog.LOG_ERR, "Unexpected status value '{}' for '{}'".format(status, key)) def start(self): self.config_db.subscribe('AAA', lambda table, key, data: self.aaa_handler(key, data)) From 7310b1a40c2a9f895b4b7b00535b4bc90a9504f1 Mon Sep 17 00:00:00 2001 From: Pradnya Mohite Date: Thu, 21 Nov 2019 16:22:38 -0800 Subject: [PATCH 8/8] adding return when sub process execution creates error --- files/image_config/hostcfgd/hostcfgd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 319c873d21c1..7fa8ed2ba205 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -326,6 +326,7 @@ class HostConfigDaemon: except subprocess.CalledProcessError as err: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) + return syslog.syslog(syslog.LOG_INFO, "Feature '{}' is enabled and started".format(key)) elif status == "disabled": stop_cmds=[] @@ -338,6 +339,7 @@ class HostConfigDaemon: except subprocess.CalledProcessError as err: syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) + return syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(key)) else: syslog.syslog(syslog.LOG_ERR, "Unexpected status value '{}' for '{}'".format(status, key))