Skip to content

Commit

Permalink
[hostcfgd] Handle Both Service And Timer Units (#5228)
Browse files Browse the repository at this point in the history
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 <tamer.ahmed@microsoft.com>
  • Loading branch information
tahmed-dev authored and abdosi committed Aug 22, 2020
1 parent 8437f4a commit dd3e7a6
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions files/image_config/hostcfgd/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ def obfuscate(data):


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":
start_cmds = []
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, feature_suffix))
start_cmds.append("sudo systemctl enable {}.{}".format(feature_name, feature_suffix))
start_cmds.append("sudo systemctl start {}.{}".format(feature_name, feature_suffix))
for suffix in feature_suffixes:
start_cmds.append("sudo systemctl unmask {}.{}".format(feature_name, 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:
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
try:
Expand All @@ -56,12 +59,14 @@ def update_feature_state(feature_name, state, has_timer=False):
syslog.syslog(syslog.LOG_ERR, "'{}' failed. RC: {}, output: {}"
.format(err.cmd, err.returncode, err.output))
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":
stop_cmds = []
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, feature_suffix))
stop_cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffix))
stop_cmds.append("sudo systemctl mask {}.{}".format(feature_name, feature_suffix))
for suffix in reversed(feature_suffixes):
stop_cmds.append("sudo systemctl stop {}.{}".format(feature_name, 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:
syslog.syslog(syslog.LOG_INFO, "Running cmd: '{}'".format(cmd))
try:
Expand All @@ -72,8 +77,8 @@ def update_feature_state(feature_name, state, has_timer=False):
continue
syslog.syslog(syslog.LOG_INFO, "Feature '{}' is stopped and disabled".format(feature_name))
else:
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}.{}'"
.format(state, feature_name, feature_suffix))
syslog.syslog(syslog.LOG_ERR, "Unexpected state value '{}' for feature '{}'"
.format(state, feature_name))


class Iptables(object):
Expand Down

2 comments on commit dd3e7a6

@kperumalbfn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tahmed-dev

When NAT feature is enabled through CLI 'sudo config feature state nat enabled', NAT docker fails to start with the below error. We had to do 'config reload' to enable NAT feature, is this mandatory after this change?

Aug 30 19:21:16.118919 sonic INFO systemd[1]: Reloading.
Aug 30 19:21:16.187453 sonic INFO hostcfgd: Running cmd: 'sudo systemctl enable nat.timer'
Aug 30 19:21:16.197835 sonic INFO hostcfgd[7525]: Failed to enable unit: File nat.timer: No such file or directory
Aug 30 19:21:16.198804 sonic ERR hostcfgd: 'sudo systemctl enable nat.timer' failed. RC: 1, output: None
Aug 30 19:21:16.199541 sonic INFO hostcfgd: Running cmd: 'sudo systemctl start nat.timer'
Aug 30 19:21:16.210134 sonic INFO hostcfgd[7525]: Failed to start nat.timer: Unit nat.timer not found.
Aug 30 19:21:16.211125 sonic ERR hostcfgd: 'sudo systemctl start nat.timer' failed. RC: 5, output: None
Aug 30 19:21:16.211508 sonic INFO hostcfgd: Feature 'nat.timer' is enabled and started

@tahmed-dev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kperumalbfn will be fixed via PR:5248

Please sign in to comment.