Skip to content

Commit 31f5fa8

Browse files
authored
Improve load_mingraph to wait eth0 restart before exit (sonic-net#3365)
* Improve load_mingraph to wait eth0 restart before exist
1 parent d0856af commit 31f5fa8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

config/main.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,10 +898,47 @@ def _reset_failed_services():
898898
for service in _get_sonic_services():
899899
clicommon.run_command(['systemctl', 'reset-failed', str(service)])
900900

901+
902+
def get_service_finish_timestamp(service):
903+
out, _ = clicommon.run_command(['sudo',
904+
'systemctl',
905+
'show',
906+
'--no-pager',
907+
service,
908+
'-p',
909+
'ExecMainExitTimestamp',
910+
'--value'],
911+
return_cmd=True)
912+
return out.strip(' \t\n\r')
913+
914+
915+
def wait_service_restart_finish(service, last_timestamp, timeout=30):
916+
start_time = time.time()
917+
elapsed_time = 0
918+
while elapsed_time < timeout:
919+
current_timestamp = get_service_finish_timestamp(service)
920+
if current_timestamp and (current_timestamp != last_timestamp):
921+
return
922+
923+
time.sleep(1)
924+
elapsed_time = time.time() - start_time
925+
926+
log.log_warning("Service: {} does not restart in {} seconds, stop waiting".format(service, timeout))
927+
928+
901929
def _restart_services():
930+
last_interface_config_timestamp = get_service_finish_timestamp('interfaces-config')
931+
last_networking_timestamp = get_service_finish_timestamp('networking')
932+
902933
click.echo("Restarting SONiC target ...")
903934
clicommon.run_command(['sudo', 'systemctl', 'restart', 'sonic.target'])
904935

936+
# These service will restart eth0 and cause device lost network for 10 seconds
937+
# When enable TACACS, every remote user commands will authorize by TACACS service via network
938+
# If load_minigraph exit before eth0 restart, commands after load_minigraph may failed
939+
wait_service_restart_finish('interfaces-config', last_interface_config_timestamp)
940+
wait_service_restart_finish('networking', last_networking_timestamp)
941+
905942
try:
906943
subprocess.check_call(['sudo', 'monit', 'status'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
907944
click.echo("Enabling container monitoring ...")

tests/config_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import datetime
23
import pytest
34
import filecmp
45
import importlib
@@ -244,6 +245,10 @@ def mock_run_command_side_effect(*args, **kwargs):
244245
return 'enabled', 0
245246
elif command == 'cat /var/run/dhclient.eth0.pid':
246247
return '101', 0
248+
elif command == 'sudo systemctl show --no-pager interfaces-config -p ExecMainExitTimestamp --value':
249+
return f'{datetime.datetime.now()}', 0
250+
elif command == 'sudo systemctl show --no-pager networking -p ExecMainExitTimestamp --value':
251+
return f'{datetime.datetime.now()}', 0
247252
else:
248253
return '', 0
249254

@@ -656,7 +661,7 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic):
656661
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == load_minigraph_command_output
657662
# Verify "systemctl reset-failed" is called for services under sonic.target
658663
mock_run_command.assert_any_call(['systemctl', 'reset-failed', 'swss'])
659-
assert mock_run_command.call_count == 8
664+
assert mock_run_command.call_count == 12
660665

661666
@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=(load_minigraph_platform_path, None)))
662667
def test_load_minigraph_platform_plugin(self, get_cmd_module, setup_single_broadcom_asic):
@@ -671,7 +676,7 @@ def test_load_minigraph_platform_plugin(self, get_cmd_module, setup_single_broad
671676
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == load_minigraph_platform_plugin_command_output
672677
# Verify "systemctl reset-failed" is called for services under sonic.target
673678
mock_run_command.assert_any_call(['systemctl', 'reset-failed', 'swss'])
674-
assert mock_run_command.call_count == 8
679+
assert mock_run_command.call_count == 12
675680

676681
@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=(load_minigraph_platform_false_path, None)))
677682
def test_load_minigraph_platform_plugin_fail(self, get_cmd_module, setup_single_broadcom_asic):

0 commit comments

Comments
 (0)