Skip to content

Commit 80bae33

Browse files
Addressed the code review comments
1) Reused the existing Python functions 2) Modified the function names to use snake case 3) Changed the print to logger.log_info 4) Fixed the indentation 5) Modified it to work for both single and multi asic 6) Reset the env variable 7) When user issues TSB when the service is running, stop the service Signed-off-by: saksarav <[email protected]>
1 parent d432d32 commit 80bae33

File tree

2 files changed

+89
-74
lines changed

2 files changed

+89
-74
lines changed

dockers/docker-fpm-frr/base_image_files/TSB

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ then
2121
sudo config mux mode auto all
2222
fi
2323

24+
if [ -z "$STARTED_BY_TSA_TSB_SERVICE" ]; then
25+
service='startup_tsa_tsb.service'
26+
if [[ $(/bin/systemctl show $service --property ActiveState --value) == "active" ]] && \
27+
[[ $(/bin/systemctl show $service --property SubState --value) == "running" ]]; then
28+
echo "Stopping $service before configuring TSB"
29+
systemctl stop $service
30+
fi
31+
fi
32+
2433
/usr/bin/TS TSB
2534
if [[ "$(sonic-cfggen -d -v DEVICE_METADATA.localhost.type)" == *"SpineRouter"* ]] ; then
2635
if [[ "$1" != "chassis" ]] ; then

files/scripts/startup_tsa_tsb.py

+80-74
Original file line numberDiff line numberDiff line change
@@ -3,146 +3,152 @@
33
# Name: startup_tsa_tsb.py, version: 1.0
44
#
55
# Description: Module contains the definitions to the VOQ Startup TSA-TSB service
6-
6+
from sonic_py_common import multi_asic, device_info
7+
from sonic_py_common.logger import Logger
78
import subprocess
89
import sys, getopt
910
from threading import Timer
10-
import os.path
11-
12-
def getPlatform():
13-
platform_key = "DEVICE_METADATA['localhost']['platform']"
14-
platform = (subprocess.check_output(['sonic-cfggen', '-d', '-v', platform_key.replace('"',"'")]).strip()).decode()
15-
return platform
11+
import os
1612

17-
def getNumAsics():
18-
platform = getPlatform()
19-
asic_config_file = '/usr/share/sonic/device/{}/asic.conf'.format(platform)
20-
file = open(asic_config_file, 'r')
21-
Lines = file.readlines()
22-
for line in Lines:
23-
field = line.split('=')[0].strip()
24-
if field == "NUM_ASIC":
25-
return line.split('=')[1].strip()
26-
return 0
13+
# Global Logger class instance
14+
logger = Logger("startup_tsa_tsb")
15+
logger.set_min_log_priority_info()
2716

28-
def getTsbTimerInterval():
29-
platform = getPlatform()
17+
def get_tsb_timer_interval():
18+
platform = device_info.get_platform()
3019
conf_file = '/usr/share/sonic/device/{}/startup-tsa-tsb.conf'.format(platform)
3120
file = open(conf_file, 'r')
3221
Lines = file.readlines()
3322
for line in Lines:
34-
field = line.split('=')[0].strip()
35-
if field == "STARTUP_TSB_TIMER":
36-
return line.split('=')[1].strip()
23+
field = line.split('=')[0].strip()
24+
if field == "STARTUP_TSB_TIMER":
25+
return line.split('=')[1].strip()
3726
return 0
3827

39-
def getSonicConfig(ns, config_name):
40-
return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), '-n', ns.replace('"', "'")]).strip()
28+
def get_sonic_config(ns, config_name):
29+
if ns == "":
30+
return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), ]).strip()
31+
else:
32+
return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), '-n', ns.replace('"', "'")]).strip()
4133

42-
def getSubRole(asic_ns):
34+
def get_sub_role(asic_ns):
4335
sub_role_config = "DEVICE_METADATA['localhost']['sub_role']"
44-
sub_role = (getSonicConfig(asic_ns, sub_role_config)).decode()
36+
sub_role = (get_sonic_config(asic_ns, sub_role_config)).decode()
4537
return sub_role
4638

47-
def getTsaConfig(asic_ns):
39+
def get_tsa_config(asic_ns):
4840
tsa_config = 'BGP_DEVICE_GLOBAL.STATE.tsa_enabled'
49-
tsa_ena = (getSonicConfig(asic_ns, tsa_config)).decode()
50-
print('{}: {} - CONFIG_DB.{} : {}'.format(__file__, asic_ns, tsa_config, tsa_ena))
41+
tsa_ena = (get_sonic_config(asic_ns, tsa_config)).decode()
42+
if asic_ns == "":
43+
logger.log_info('CONFIG_DB.{} : {}'.format(tsa_config, tsa_ena))
44+
else:
45+
logger.log_info('{} - CONFIG_DB.{} : {}'.format(asic_ns, tsa_config, tsa_ena))
5146
return tsa_ena
5247

53-
def get_tsa_status():
54-
asic_num = getNumAsics()
55-
counter = 0
56-
for asic_id in range(int(asic_num)):
57-
asic_ns = 'asic{}'.format(asic_id)
58-
sub_role = getSubRole(asic_ns)
59-
if sub_role == 'FrontEnd':
60-
tsa_enabled = getTsaConfig(asic_ns)
61-
if tsa_enabled == 'false':
62-
counter += 1
63-
if counter == int(asic_num):
64-
return True;
48+
def get_tsa_status(num_asics):
49+
if num_asics > 1:
50+
counter = 0
51+
for asic_id in range(int(asic_num)):
52+
asic_ns = 'asic{}'.format(asic_id)
53+
sub_role = get_sub_role(asic_ns)
54+
if sub_role == 'FrontEnd':
55+
tsa_enabled = get_tsa_config(asic_ns)
56+
if tsa_enabled == 'false':
57+
counter += 1
58+
if counter == int(asic_num):
59+
return True;
6560
else:
66-
return False;
61+
tsa_enabled = get_tsa_config("")
62+
if tsa_enabled == 'false':
63+
return True;
64+
return False;
6765

6866
def config_tsa():
69-
tsa_ena = get_tsa_status()
67+
num_asics = multi_asic.get_num_asics()
68+
tsa_ena = get_tsa_status(num_asics)
7069
if tsa_ena == True:
71-
print("{}: Configuring TSA".format(__file__))
72-
subprocess.check_output(['TSA']).strip()
70+
logger.log_info("Configuring TSA")
71+
subprocess.check_output(['TSA']).strip()
7372
else:
74-
print("{}: Either TSA is already configured or switch sub_role is not Frontend - not configuring TSA".format(__file__))
73+
if num_asics > 1:
74+
logger.log_info("Either TSA is already configured or switch sub_role is not Frontend - not configuring TSA")
75+
else:
76+
logger.log_info("Either TSA is already configured - not configuring TSA")
7577
return tsa_ena
7678

7779
def config_tsb():
78-
print("startup_tsa_tsb: Configuring TSB")
80+
logger.log_info("startup_tsa_tsb: Configuring TSB")
7981
subprocess.check_output(['TSB']).strip()
8082
tsb_issued = True
8183
return
8284

8385
def start_tsb_timer(interval):
8486
global timer
85-
print("{}: Starting timer with interval {} seconds to configure TSB".format(__file__, interval))
87+
logger.log_info("Starting timer with interval {} seconds to configure TSB".format(interval))
8688
timer = Timer(int(interval), config_tsb)
8789
timer.start()
8890
timer.join()
8991
return
9092

9193
def print_usage():
92-
print ("Usage: startup_tsa_tsb.py [options] command")
93-
print ("options:")
94-
print(" -h | --help : this help message")
95-
print("command:")
96-
print("start : start the TSA/TSB")
97-
print("stop : stop the TSA/TSB")
94+
logger.log_info("Usage: startup_tsa_tsb.py [options] command")
95+
logger.log_info("options:")
96+
logger.log_info(" -h | --help : this help message")
97+
logger.log_info("command:")
98+
logger.log_info("start : start the TSA/TSB")
99+
logger.log_info("stop : stop the TSA/TSB")
98100
return
99101

100-
def start_tsa_tsb(timer):
102+
def reset_env_variables():
103+
logger.log_info("Resetting environment variable")
104+
os.environ.pop('STARTED_BY_TSA_TSB_SERVICE')
105+
return
101106

107+
def start_tsa_tsb(timer):
102108
#Configure TSA if it was not configured already in CONFIG_DB
103109
tsa_enabled = config_tsa()
104110
if tsa_enabled == True:
105-
#Start the timer to configure TSB
106-
start_tsb_timer(timer)
111+
#Start the timer to configure TSB
112+
start_tsb_timer(timer)
107113
return
108114

109115
def stop_tsa_tsb():
110-
#for future use
116+
reset_env_variables()
111117
return
112118

113119
def main():
114-
platform = getPlatform()
120+
platform = device_info.get_platform()
115121
conf_file = '/usr/share/sonic/device/{}/startup-tsa-tsb.conf'.format(platform)
116122
#This check should be moved to service file or make this feature as configurable.
117123
#Adding it here for now.
118124
if not os.path.exists(conf_file):
119-
print ("{} does not exist, exiting the service".format(conf_file))
120-
return
125+
logger.log_info("{} does not exist, exiting the service".format(conf_file))
126+
return
121127
if len(sys.argv) <= 1:
122-
print_usage()
123-
return
128+
print_usage()
129+
return
124130

125131
# parse command line options:
126132
try:
127-
opts, args = getopt.getopt(sys.argv[1:], 'h:', ['help' ])
133+
opts, args = getopt.getopt(sys.argv[1:], 'h:', ['help' ])
128134
except getopt.GetoptError:
129-
print_usage()
130-
return
135+
print_usage()
136+
return
131137

132138
for opt, arg in opts:
133-
if opt in ("-h", "--help"):
134-
print_usage()
135-
return
139+
if opt in ("-h", "--help"):
140+
print_usage()
141+
return
136142

137143
for arg in args:
138144
if arg == 'start':
139-
tsb_timer = getTsbTimerInterval()
140-
start_tsa_tsb(tsb_timer)
145+
tsb_timer = get_tsb_timer_interval()
146+
start_tsa_tsb(tsb_timer)
141147
elif arg == 'stop':
142-
stop_tsa_tsb()
148+
stop_tsa_tsb()
143149
else:
144-
print_usage()
145-
return
150+
print_usage()
151+
return
146152

147153
return
148154

0 commit comments

Comments
 (0)