[hostcfgd] Handle Both Service And Timer Units (#5228)
Commit e484ae9dd
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 <tamer.ahmed@microsoft.com>
This commit is contained in:
parent
1a805e7409
commit
90cbb4d78c
@ -42,12 +42,15 @@ def obfuscate(data):
|
|||||||
|
|
||||||
|
|
||||||
def update_feature_state(feature_name, state, has_timer=False):
|
def update_feature_state(feature_name, state, has_timer=False):
|
||||||
feature_suffix = "timer" if has_timer else "service"
|
feature_suffixes = ["service"] + (["timer"] if has_timer else [])
|
||||||
if state == "enabled":
|
if state == "enabled":
|
||||||
start_cmds = []
|
start_cmds = []
|
||||||
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, feature_suffix))
|
for suffix in feature_suffixes:
|
||||||
start_cmds.append("sudo systemctl enable {}.{}".format(feature_name, feature_suffix))
|
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, suffix))
|
||||||
start_cmds.append("sudo systemctl start {}.{}".format(feature_name, feature_suffix))
|
start_cmds.append("sudo systemctl enable {}.{}".format(feature_name, suffix))
|
||||||
|
# If feature has timer associated with it, start corresponding systemd .timer unit
|
||||||
|
# otherwise, start corresponding systemd .service unit
|
||||||
|
start_cmds.append("sudo systemctl start {}.{}".format(feature_name, feature_suffixes[-1]))
|
||||||
for cmd in start_cmds:
|
for cmd in start_cmds:
|
||||||
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
|
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
|
||||||
try:
|
try:
|
||||||
@ -56,12 +59,14 @@ def update_feature_state(feature_name, state, has_timer=False):
|
|||||||
syslog.syslog(syslog.LOG_ERR, "'{}' failed. RC: {}, output: {}"
|
syslog.syslog(syslog.LOG_ERR, "'{}' failed. RC: {}, output: {}"
|
||||||
.format(err.cmd, err.returncode, err.output))
|
.format(err.cmd, err.returncode, err.output))
|
||||||
continue
|
continue
|
||||||
syslog.syslog(syslog.LOG_INFO, "Feature '{}.{}' is enabled and started".format(feature_name, feature_suffix))
|
syslog.syslog(syslog.LOG_INFO, "Feature '{}.{}' is enabled and started"
|
||||||
|
.format(feature_name, feature_suffixes[-1]))
|
||||||
elif state == "disabled":
|
elif state == "disabled":
|
||||||
stop_cmds = []
|
stop_cmds = []
|
||||||
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, feature_suffix))
|
for suffix in reversed(feature_suffixes):
|
||||||
stop_cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffix))
|
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, suffix))
|
||||||
stop_cmds.append("sudo systemctl mask {}.{}".format(feature_name, feature_suffix))
|
stop_cmds.append("sudo systemctl disable {}.{}".format(feature_name, suffix))
|
||||||
|
stop_cmds.append("sudo systemctl mask {}.{}".format(feature_name, suffix))
|
||||||
for cmd in stop_cmds:
|
for cmd in stop_cmds:
|
||||||
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
|
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
|
||||||
try:
|
try:
|
||||||
@ -72,8 +77,8 @@ def update_feature_state(feature_name, state, has_timer=False):
|
|||||||
continue
|
continue
|
||||||
syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(feature_name))
|
syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(feature_name))
|
||||||
else:
|
else:
|
||||||
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}.{}'"
|
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}'"
|
||||||
.format(state, feature_name, feature_suffix))
|
.format(state, feature_name))
|
||||||
|
|
||||||
|
|
||||||
class Iptables(object):
|
class Iptables(object):
|
||||||
|
Reference in New Issue
Block a user