Skip to content

Commit 798ce2f

Browse files
[multi-asic]: Update reload of systemd services to support multi-asic platforms (#856)
* Update stop, reset failed status and restart of systemd services to support multi-asic platforms. * Create function to avoid code duplication. * Fixed errors due to pervious commit and review comments. * Minor update to fix spacing. * Minor update to fix spacing. * Minor update to fix spacing. * For multi asic platform updated logic of stopping/restarting of services to ensure that the right instances are stopped and restarted if a service is both global and multi-instance. * Fixed log error message with incorrect number of parameterts.
1 parent 6f51428 commit 798ce2f

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

config/main.py

+57-46
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@
2727
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
2828
SYSLOG_IDENTIFIER = "config"
2929
VLAN_SUB_INTERFACE_SEPARATOR = '.'
30+
ASIC_CONF_FILENAME = 'asic.conf'
3031

3132
INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
3233

34+
SYSTEMCTL_ACTION_STOP="stop"
35+
SYSTEMCTL_ACTION_RESTART="restart"
36+
SYSTEMCTL_ACTION_RESET_FAILED="reset-failed"
37+
3338
# ========================== Syslog wrappers ==========================
3439

3540
def log_debug(msg):
@@ -69,6 +74,31 @@ def log_error(msg):
6974
# Helper functions
7075
#
7176

77+
# Execute action on list of systemd services
78+
def execute_systemctl(list_of_services, action):
79+
num_asic = _get_num_asic()
80+
generated_services_list, generated_multi_instance_services = _get_sonic_generated_services(num_asic)
81+
if ((generated_services_list == []) and
82+
(generated_multi_instance_services == [])):
83+
log_error("Failed to get generated services")
84+
return
85+
86+
for service in list_of_services:
87+
if (service + '.service' in generated_services_list):
88+
try:
89+
click.echo("Executing {} of service {}...".format(action, service))
90+
run_command("systemctl {} {}".format(action, service))
91+
except SystemExit as e:
92+
log_error("Failed to execute {} of service {} with error {}".format(action, service, e))
93+
raise
94+
if (service + '.service' in generated_multi_instance_services):
95+
for inst in range(num_asic):
96+
try:
97+
click.echo("Executing {} of service {}@{}...".format(action, service, inst))
98+
run_command("systemctl {} {}@{}.service".format(action, service, inst))
99+
except SystemExit as e:
100+
log_error("Failed to execute {} of service {}@{} with error {}".format(action, service, inst, e))
101+
raise
72102

73103
def run_command(command, display_cmd=False, ignore_error=False):
74104
"""Run bash command and print output to stdout
@@ -395,14 +425,34 @@ def _get_platform():
395425
return tokens[1].strip()
396426
return ''
397427

398-
def _get_sonic_generated_services():
428+
def _get_num_asic():
429+
platform = _get_platform()
430+
num_asic = 1
431+
asic_conf_file = os.path.join('/usr/share/sonic/device/', platform, ASIC_CONF_FILENAME)
432+
if os.path.isfile(asic_conf_file):
433+
with open(asic_conf_file) as conf_file:
434+
for line in conf_file:
435+
line_info = line.split('=')
436+
if line_info[0].lower() == "num_asic":
437+
num_asic = int(line_info[1])
438+
return num_asic
439+
440+
def _get_sonic_generated_services(num_asic):
399441
if not os.path.isfile(SONIC_GENERATED_SERVICE_PATH):
400442
return None
401443
generated_services_list = []
444+
generated_multi_instance_services = []
402445
with open(SONIC_GENERATED_SERVICE_PATH) as generated_service_file:
403446
for line in generated_service_file:
404-
generated_services_list.append(line.rstrip('\n'))
405-
return None if not generated_services_list else generated_services_list
447+
if '@' in line:
448+
line = line.replace('@', '')
449+
if num_asic > 1:
450+
generated_multi_instance_services.append(line.rstrip('\n'))
451+
else:
452+
generated_services_list.append(line.rstrip('\n'))
453+
else:
454+
generated_services_list.append(line.rstrip('\n'))
455+
return generated_services_list, generated_multi_instance_services
406456

407457
# Callback for confirmation prompt. Aborts if user enters "n"
408458
def _abort_if_false(ctx, param, value):
@@ -419,25 +469,11 @@ def _stop_services():
419469
'hostcfgd',
420470
'nat'
421471
]
422-
generated_services_list = _get_sonic_generated_services()
423-
424-
if generated_services_list is None:
425-
log_error("Failed to get generated services")
426-
return
427472

428473
if asic_type == 'mellanox' and 'pmon' in services_to_stop:
429474
services_to_stop.remove('pmon')
430475

431-
for service in services_to_stop:
432-
if service + '.service' not in generated_services_list:
433-
continue
434-
try:
435-
click.echo("Stopping service {} ...".format(service))
436-
run_command("systemctl stop {}".format(service))
437-
438-
except SystemExit as e:
439-
log_error("Stopping {} failed with error {}".format(service, e))
440-
raise
476+
execute_systemctl(services_to_stop, SYSTEMCTL_ACTION_STOP)
441477

442478
def _reset_failed_services():
443479
services_to_reset = [
@@ -458,22 +494,9 @@ def _reset_failed_services():
458494
'nat',
459495
'sflow'
460496
]
497+
execute_systemctl(services_to_reset, SYSTEMCTL_ACTION_RESET_FAILED)
461498

462-
generated_services_list = _get_sonic_generated_services()
463499

464-
if generated_services_list is None:
465-
log_error("Failed to get generated services")
466-
return
467-
468-
for service in services_to_reset:
469-
if service + '.service' not in generated_services_list:
470-
continue
471-
try:
472-
click.echo("Resetting failed status for service {} ...".format(service))
473-
run_command("systemctl reset-failed {}".format(service))
474-
except SystemExit as e:
475-
log_error("Failed to reset failed status for service {}".format(service))
476-
raise
477500

478501
def _restart_services():
479502
# on Mellanox platform pmon is started by syncd
@@ -490,24 +513,12 @@ def _restart_services():
490513
'nat',
491514
'sflow',
492515
]
493-
generated_services_list = _get_sonic_generated_services()
494-
495-
if generated_services_list is None:
496-
log_error("Failed to get generated services")
497-
return
498516

499517
if asic_type == 'mellanox' and 'pmon' in services_to_restart:
500518
services_to_restart.remove('pmon')
501519

502-
for service in services_to_restart:
503-
if service + '.service' not in generated_services_list:
504-
continue
505-
try:
506-
click.echo("Restarting service {} ...".format(service))
507-
run_command("systemctl restart {}".format(service))
508-
except SystemExit as e:
509-
log_error("Restart {} failed with error {}".format(service, e))
510-
raise
520+
execute_systemctl(services_to_restart, SYSTEMCTL_ACTION_RESTART)
521+
511522

512523
def is_ipaddress(val):
513524
""" Validate if an entry is a valid IP """

0 commit comments

Comments
 (0)