From e55c04f3e44fd9e559dc7af0ec1454743a40a376 Mon Sep 17 00:00:00 2001 From: "Marty Y. Lok" <76118573+mlok-nokia@users.noreply.github.com> Date: Mon, 9 Jan 2023 13:26:00 -0500 Subject: [PATCH] [hostcfgd] Optimize the hostcfgs by moving the definition cmds into the loop to optimize the enable/disable service command run. (#13064) Signed-off-by: mlok Signed-off-by: mlok --- src/sonic-host-services/scripts/hostcfgd | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index bacd64f16e..ad0c832c64 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -324,7 +324,7 @@ class FeatureHandler(object): None. """ - cmds = [] + feature_names, feature_suffixes = self.get_multiasic_feature_instances(feature_config, True) for feature_name in feature_names: if '@' not in feature_name: @@ -333,6 +333,7 @@ class FeatureHandler(object): if not unit_file_state: continue if unit_file_state != "disabled" and not feature_config.has_per_asic_scope: + cmds = [] for suffix in reversed(feature_suffixes): cmds.append("sudo systemctl stop {}.{}".format(feature_name, suffix)) cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffixes[-1])) @@ -420,14 +421,13 @@ class FeatureHandler(object): return props["UnitFileState"] def enable_feature(self, feature): - cmds = [] feature_names, feature_suffixes = self.get_multiasic_feature_instances(feature) for feature_name in feature_names: # Check if it is already enabled, if yes skip the system call unit_file_state = self.get_systemd_unit_state("{}.{}".format(feature_name, feature_suffixes[-1])) if unit_file_state == "enabled" or not unit_file_state: continue - + cmds = [] for suffix in feature_suffixes: cmds.append("sudo systemctl unmask {}.{}".format(feature_name, suffix)) @@ -450,14 +450,13 @@ class FeatureHandler(object): self.set_feature_state(feature, self.FEATURE_STATE_ENABLED) def disable_feature(self, feature): - cmds = [] feature_names, feature_suffixes = self.get_multiasic_feature_instances(feature) for feature_name in feature_names: # Check if it is already disabled, if yes skip the system call unit_file_state = self.get_systemd_unit_state("{}.{}".format(feature_name, feature_suffixes[-1])) if unit_file_state in ("disabled", "masked") or not unit_file_state: continue - + cmds = [] for suffix in reversed(feature_suffixes): cmds.append("sudo systemctl stop {}.{}".format(feature_name, suffix)) cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffixes[-1]))