Skip to content

Commit 9041ba0

Browse files
authored
[config] Adding sanity checks for config reload (#1664)
Fixed config reload to add some system sanity checks 1) To check if the system is in running state 2) Check if the services which are grouped under delayed target up 3) Check if swss is running for at least 120 seconds To force config reload and to avoid these checks an extra option -f/--force is added
1 parent 2cdadb5 commit 9041ba0

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

config/main.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,32 @@ def _restart_services():
701701
click.echo("Reloading Monit configuration ...")
702702
clicommon.run_command("sudo monit reload")
703703

704+
def _get_delay_timers():
705+
out = clicommon.run_command("systemctl list-dependencies sonic-delayed.target --plain |sed '1d'", return_cmd=True)
706+
return [timer.strip() for timer in out.splitlines()]
707+
708+
def _delay_timers_elapsed():
709+
for timer in _get_delay_timers():
710+
out = clicommon.run_command("systemctl show {} --property=LastTriggerUSecMonotonic --value".format(timer), return_cmd=True)
711+
if out.strip() == "0":
712+
return False
713+
return True
714+
715+
def _swss_ready():
716+
out = clicommon.run_command("systemctl show swss.service --property ActiveState --value", return_cmd=True)
717+
if out.strip() != "active":
718+
return False
719+
out = clicommon.run_command("systemctl show swss.service --property ActiveEnterTimestampMonotonic --value", return_cmd=True)
720+
swss_up_time = float(out.strip())/1000000
721+
now = time.monotonic()
722+
if (now - swss_up_time > 120):
723+
return True
724+
else:
725+
return False
726+
727+
def _system_running():
728+
out = clicommon.run_command("sudo systemctl is-system-running", return_cmd=True)
729+
return out.strip() == "running"
704730

705731
def interface_is_in_vlan(vlan_member_table, interface_name):
706732
""" Check if an interface is in a vlan """
@@ -1190,12 +1216,26 @@ def list_checkpoints(ctx, verbose):
11901216
@click.option('-l', '--load-sysinfo', is_flag=True, help='load system default information (mac, portmap etc) first.')
11911217
@click.option('-n', '--no_service_restart', default=False, is_flag=True, help='Do not restart docker services')
11921218
@click.option('-d', '--disable_arp_cache', default=False, is_flag=True, help='Do not cache ARP table before reloading (applies to dual ToR systems only)')
1219+
@click.option('-f', '--force', default=False, is_flag=True, help='Force config reload without system checks')
11931220
@click.argument('filename', required=False)
11941221
@clicommon.pass_db
1195-
def reload(db, filename, yes, load_sysinfo, no_service_restart, disable_arp_cache):
1222+
def reload(db, filename, yes, load_sysinfo, no_service_restart, disable_arp_cache, force):
11961223
"""Clear current configuration and import a previous saved config DB dump file.
11971224
<filename> : Names of configuration file(s) to load, separated by comma with no spaces in between
11981225
"""
1226+
if not force and not no_service_restart:
1227+
if not _system_running():
1228+
click.echo("System is not up. Retry later or use -f to avoid system checks")
1229+
return
1230+
1231+
if not _delay_timers_elapsed():
1232+
click.echo("Relevant services are not up. Retry later or use -f to avoid system checks")
1233+
return
1234+
1235+
if not _swss_ready():
1236+
click.echo("SwSS container is not ready. Retry later or use -f to avoid system checks")
1237+
return
1238+
11991239
if filename is None:
12001240
message = 'Clear current config and reload config from the default config file(s) ?'
12011241
else:

0 commit comments

Comments
 (0)