Skip to content

Commit a3ba411

Browse files
tahmed-devsanthosh-kt
authored andcommitted
[hostcfgd] Handle Both Service And Timer Units (sonic-net#5228)
Commit e484ae9 introduced systemd .timer unit to hostcfgd. However, when stopping service that has timer, there is possibility that timer is not running and the service would not be stopped. This PR address this situation by handling both .timer and .service units. signed-off-by: Tamer Ahmed <[email protected]>
1 parent 7e1bf5c commit a3ba411

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

files/image_config/hostcfgd/hostcfgd

+15-10
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ def obfuscate(data):
4242

4343

4444
def update_feature_state(feature_name, state, has_timer=False):
45-
feature_suffix = "timer" if has_timer else "service"
45+
feature_suffixes = ["service"] + (["timer"] if has_timer else [])
4646
if state == "enabled":
4747
start_cmds = []
48-
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, feature_suffix))
49-
start_cmds.append("sudo systemctl enable {}.{}".format(feature_name, feature_suffix))
50-
start_cmds.append("sudo systemctl start {}.{}".format(feature_name, feature_suffix))
48+
for suffix in feature_suffixes:
49+
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, suffix))
50+
start_cmds.append("sudo systemctl enable {}.{}".format(feature_name, suffix))
51+
# If feature has timer associated with it, start corresponding systemd .timer unit
52+
# otherwise, start corresponding systemd .service unit
53+
start_cmds.append("sudo systemctl start {}.{}".format(feature_name, feature_suffixes[-1]))
5154
for cmd in start_cmds:
5255
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
5356
try:
@@ -56,12 +59,14 @@ def update_feature_state(feature_name, state, has_timer=False):
5659
syslog.syslog(syslog.LOG_ERR, "'{}' failed. RC: {}, output: {}"
5760
.format(err.cmd, err.returncode, err.output))
5861
continue
59-
syslog.syslog(syslog.LOG_INFO, "Feature '{}.{}' is enabled and started".format(feature_name, feature_suffix))
62+
syslog.syslog(syslog.LOG_INFO, "Feature '{}.{}' is enabled and started"
63+
.format(feature_name, feature_suffixes[-1]))
6064
elif state == "disabled":
6165
stop_cmds = []
62-
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, feature_suffix))
63-
stop_cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffix))
64-
stop_cmds.append("sudo systemctl mask {}.{}".format(feature_name, feature_suffix))
66+
for suffix in reversed(feature_suffixes):
67+
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, suffix))
68+
stop_cmds.append("sudo systemctl disable {}.{}".format(feature_name, suffix))
69+
stop_cmds.append("sudo systemctl mask {}.{}".format(feature_name, suffix))
6570
for cmd in stop_cmds:
6671
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
6772
try:
@@ -72,8 +77,8 @@ def update_feature_state(feature_name, state, has_timer=False):
7277
continue
7378
syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(feature_name))
7479
else:
75-
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}.{}'"
76-
.format(state, feature_name, feature_suffix))
80+
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}'"
81+
.format(state, feature_name))
7782

7883

7984
class Iptables(object):

0 commit comments

Comments
 (0)