Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/sonic-host-services/scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,25 @@ class HostConfigDaemon:

return feature_names, feature_suffixes

def get_systemd_unit_state(self, unit):
""" Returns service configuration """

cmd = "sudo systemctl show {} --property UnitFileState".format(unit)
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
syslog.syslog(syslog.LOG_ERR, "Failed to get status of {}: rc={} stderr={}".format(unit, proc.returncode, stderr))
return 'invalid' # same as systemd's "invalid indicates that it could not be determined whether the unit file is enabled".

props = dict([line.split("=") for line in stdout.decode().strip().splitlines()])
return props["UnitFileState"]

def enable_feature(self, feature_names, feature_suffixes):
start_cmds = []
for feature_name in feature_names:
# Check if it is already enabled, if yes skip the system call
cmd = "sudo systemctl status {}.{} | grep Loaded".format(feature_name, feature_suffixes[-1])
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if "enabled" in str(stdout):
unit_file_state = self.get_systemd_unit_state("{}.{}".format(feature_name, feature_suffixes[-1]))
if unit_file_state == "enabled":
continue

for suffix in feature_suffixes:
Expand All @@ -485,10 +496,8 @@ class HostConfigDaemon:
stop_cmds = []
for feature_name in feature_names:
# Check if it is already disabled, if yes skip the system call
cmd = "sudo systemctl status {}.{} | grep Loaded".format(feature_name, feature_suffixes[-1])
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if "disabled" in str(stdout) or "masked" in str(stdout):
unit_file_state = self.get_systemd_unit_state("{}.{}".format(feature_name, feature_suffixes[-1]))
if unit_file_state in ("disabled", "masked"):
continue

for suffix in reversed(feature_suffixes):
Expand Down