Skip to content

Commit ec2c3bc

Browse files
authored
Migrate from sonic-daemon-base package to sonic-py-common package (sonic-net#74)
As part of consolidating all common Python-based functionality into the new sonic-py-common package, this pull request migrates from importing the sonic-daemon-base package to importing the sonic-py-common package. This is the next step toward resolving sonic-net#4999. - Also reorganize imports for consistency
1 parent 49d145c commit ec2c3bc

File tree

7 files changed

+185
-205
lines changed

7 files changed

+185
-205
lines changed

sonic-ledd/scripts/ledd

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
try:
99
import getopt
1010
import sys
11+
12+
from sonic_py_common import daemon_base
1113
from swsscommon import swsscommon
12-
from sonic_daemon_base import daemon_base
13-
from sonic_daemon_base.daemon_base import Logger
14-
from sonic_daemon_base.daemon_base import DaemonBase
1514
except ImportError, e:
1615
raise ImportError (str(e) + " - required module not found")
1716

@@ -36,11 +35,7 @@ SELECT_TIMEOUT = 1000
3635

3736
LEDUTIL_LOAD_ERROR = 1
3837

39-
logger = Logger(SYSLOG_IDENTIFIER)
40-
41-
class DaemonLedd(DaemonBase):
42-
def __init__(self):
43-
DaemonBase.__init__(self)
38+
class DaemonLedd(daemon_base.DaemonBase):
4439

4540
# Run daemon
4641
def run(self):
@@ -67,7 +62,7 @@ class DaemonLedd(DaemonBase):
6762
try:
6863
led_control = self.load_platform_util(LED_MODULE_NAME, LED_CLASS_NAME)
6964
except Exception as e:
70-
logger.log_error("Failed to load ledutil: %s" % (str(e)), True)
65+
self.log_error("Failed to load ledutil: %s" % (str(e)), True)
7166
sys.exit(LEDUTIL_LOAD_ERROR)
7267

7368
# Open a handle to the Application database
@@ -88,7 +83,7 @@ class DaemonLedd(DaemonBase):
8883
# Do not flood log when select times out
8984
continue
9085
if state != swsscommon.Select.OBJECT:
91-
logger.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
86+
self.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
9287
continue
9388

9489
(key, op, fvp) = sst.pop()
@@ -106,7 +101,7 @@ class DaemonLedd(DaemonBase):
106101
return 1
107102

108103
def main():
109-
ledd = DaemonLedd()
104+
ledd = DaemonLedd(SYSLOG_IDENTIFIER)
110105
ledd.run()
111106

112107
if __name__ == '__main__':

sonic-pcied/scripts/pcied

+20-26
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ try:
1313
import threading
1414

1515
import swsssdk
16-
from sonic_daemon_base.daemon_base import Logger
17-
from sonic_daemon_base.daemon_base import DaemonBase
16+
from sonic_py_common.daemon_base import DaemonBase
17+
from sonic_py_common.device_info import get_platform
1818
except ImportError, e:
1919
raise ImportError(str(e) + " - required module not found")
2020

@@ -28,30 +28,24 @@ PCIE_TABLE_NAME = "PCIE_STATUS"
2828

2929
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
3030
PCIE_CONF_FILE = 'pcie.yaml'
31-
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
32-
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
33-
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
3431

3532
PCIED_MAIN_THREAD_SLEEP_SECS = 60
3633
REDIS_HOSTIP = "127.0.0.1"
3734

38-
# Global logger class instance
39-
logger = Logger(SYSLOG_IDENTIFIER)
40-
4135
#
4236
# Daemon =======================================================================
4337
#
4438

4539

4640
class DaemonPcied(DaemonBase):
47-
def __init__(self):
48-
DaemonBase.__init__(self)
41+
def __init__(self, log_identifier):
42+
super(DaemonPcied, self).__init__(log_identifier)
4943

50-
platform, hwsku = DaemonBase.get_platform_and_hwsku(self)
51-
pciefilePath = "/".join([PLATFORM_ROOT_PATH, platform, "plugins", PCIE_CONF_FILE])
44+
platform = get_platform()
45+
pciefilePath = os.path.join(PLATFORM_ROOT_PATH, platform, "plugins", PCIE_CONF_FILE)
5246
sys.path.append(os.path.abspath(pciefilePath))
5347
if not os.path.exists(pciefilePath):
54-
logger.log_error("Platform pcie configuration file doesn't exist! exit pcied")
48+
self.log_error("Platform pcie configuration file doesn't exist! Exiting ...")
5549
sys.exit("Platform PCIe Configuration file doesn't exist!")
5650

5751
self.timeout = PCIED_MAIN_THREAD_SLEEP_SECS
@@ -70,10 +64,10 @@ class DaemonPcied(DaemonBase):
7064
if PCIE_RESULT_REGEX in line:
7165
if "PASSED" in line and "PASSED" not in pcie_db_state:
7266
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "PASSED")
73-
logger.log_info("PCIe device status check : PASSED")
67+
self.log_info("PCIe device status check : PASSED")
7468
elif "FAILED" in line and "PASSED" in pcie_db_state:
7569
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "FAILED")
76-
logger.log_info("PCIe device status check : FAILED")
70+
self.log_info("PCIe device status check : FAILED")
7771

7872
def read_state_db(self, key1, key2):
7973
return self.state_db.get('STATE_DB', key1, key2)
@@ -84,52 +78,52 @@ class DaemonPcied(DaemonBase):
8478
# Signal handler
8579
def signal_handler(self, sig, frame):
8680
if sig == signal.SIGHUP:
87-
logger.log_info("Caught SIGHUP - ignoring...")
81+
self.log_info("Caught SIGHUP - ignoring...")
8882
elif sig == signal.SIGINT:
89-
logger.log_info("Caught SIGINT - exiting...")
83+
self.log_info("Caught SIGINT - exiting...")
9084
self.stop_event.set()
9185
elif sig == signal.SIGTERM:
92-
logger.log_info("Caught SIGTERM - exiting...")
86+
self.log_info("Caught SIGTERM - exiting...")
9387
self.stop_event.set()
9488
else:
95-
logger.log_warning("Caught unhandled signal '" + sig + "'")
89+
self.log_warning("Caught unhandled signal '" + sig + "'")
9690

9791
# Initialize daemon
9892
def init(self):
99-
logger.log_info("Start daemon init...")
93+
self.log_info("Start daemon init...")
10094

10195
# Deinitialize daemon
10296
def deinit(self):
103-
logger.log_info("Start daemon deinit...")
97+
self.log_info("Start daemon deinit...")
10498

10599
# Run daemon
106100
def run(self):
107-
logger.log_info("Starting up...")
101+
self.log_info("Starting up...")
108102

109103
# Start daemon initialization sequence
110104
self.init()
111105

112106
# Start main loop
113-
logger.log_info("Start daemon main loop")
107+
self.log_info("Start daemon main loop")
114108

115109
while not self.stop_event.wait(self.timeout):
116110
# Check the Pcie device status
117111
self.check_pcie_devices()
118112

119-
logger.log_info("Stop daemon main loop")
113+
self.log_info("Stop daemon main loop")
120114

121115
# Start daemon deinitialization sequence
122116
self.deinit()
123117

124-
logger.log_info("Shutting down...")
118+
self.log_info("Shutting down...")
125119

126120
#
127121
# Main =========================================================================
128122
#
129123

130124

131125
def main():
132-
pcied = DaemonPcied()
126+
pcied = DaemonPcied(SYSLOG_IDENTIFIER)
133127
pcied.run()
134128

135129
if __name__ == '__main__':

sonic-psud/scripts/psud

+23-26
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
"""
1111

1212
try:
13-
import sys
1413
import signal
14+
import sys
1515
import threading
16+
17+
from sonic_py_common import daemon_base
1618
from swsscommon import swsscommon
17-
from sonic_daemon_base import daemon_base
18-
from sonic_daemon_base.daemon_base import Logger
19-
from sonic_daemon_base.daemon_base import DaemonBase
2019
except ImportError, e:
2120
raise ImportError (str(e) + " - required module not found")
2221

@@ -49,8 +48,6 @@ PSU_INFO_UPDATE_PERIOD_SECS = 3
4948

5049
PSUUTIL_LOAD_ERROR = 1
5150

52-
logger = Logger(SYSLOG_IDENTIFIER)
53-
5451
platform_psuutil = None
5552
platform_chassis = None
5653

@@ -119,9 +116,9 @@ def log_on_status_changed(normal_status, normal_log, abnormal_log):
119116
:return:
120117
"""
121118
if normal_status:
122-
logger.log_notice(normal_log)
119+
self.log_notice(normal_log)
123120
else:
124-
logger.log_warning(abnormal_log)
121+
self.log_warning(abnormal_log)
125122

126123
#
127124
# PSU status ===================================================================
@@ -162,7 +159,7 @@ class PsuStatus(object):
162159
def set_voltage(self, voltage, high_threshold, low_threshold):
163160
if not voltage or not high_threshold or not low_threshold:
164161
if self.voltage_good is not True:
165-
logger.log_warning('PSU voltage or high_threshold or low_threshold become unavailable, '
162+
self.log_warning('PSU voltage or high_threshold or low_threshold become unavailable, '
166163
'voltage={}, high_threshold={}, low_threshold={}'.format(voltage, high_threshold, low_threshold))
167164
self.voltage_good = True
168165
return False
@@ -177,7 +174,7 @@ class PsuStatus(object):
177174
def set_temperature(self, temperature, high_threshold):
178175
if not temperature or not high_threshold:
179176
if self.temperature_good is not True:
180-
logger.log_warning('PSU temperature or high_threshold become unavailable, '
177+
self.log_warning('PSU temperature or high_threshold become unavailable, '
181178
'temperature={}, high_threshold={}'.format(temperature, high_threshold))
182179
self.temperature_good = True
183180
return False
@@ -196,46 +193,46 @@ class PsuStatus(object):
196193
# Daemon =======================================================================
197194
#
198195

199-
class DaemonPsud(DaemonBase):
200-
def __init__(self):
201-
DaemonBase.__init__(self)
196+
class DaemonPsud(daemon_base.DaemonBase):
197+
def __init__(self, log_identifier):
198+
super(DaemonPsud, self).__init__(log_identifier)
202199

203200
self.stop = threading.Event()
204201
self.psu_status_dict = {}
205202

206203
# Signal handler
207204
def signal_handler(self, sig, frame):
208205
if sig == signal.SIGHUP:
209-
logger.log_info("Caught SIGHUP - ignoring...")
206+
self.log_info("Caught SIGHUP - ignoring...")
210207
elif sig == signal.SIGINT:
211-
logger.log_info("Caught SIGINT - exiting...")
208+
self.log_info("Caught SIGINT - exiting...")
212209
self.stop.set()
213210
elif sig == signal.SIGTERM:
214-
logger.log_info("Caught SIGTERM - exiting...")
211+
self.log_info("Caught SIGTERM - exiting...")
215212
self.stop.set()
216213
else:
217-
logger.log_warning("Caught unhandled signal '" + sig + "'")
214+
self.log_warning("Caught unhandled signal '" + sig + "'")
218215

219216
# Run daemon
220217
def run(self):
221218
global platform_psuutil
222219
global platform_chassis
223220

224-
logger.log_info("Starting up...")
221+
self.log_info("Starting up...")
225222

226223
# Load new platform api class
227224
try:
228225
import sonic_platform.platform
229226
platform_chassis = sonic_platform.platform.Platform().get_chassis()
230227
except Exception as e:
231-
logger.log_warning("Failed to load chassis due to {}".format(repr(e)))
228+
self.log_warning("Failed to load chassis due to {}".format(repr(e)))
232229

233230
# Load platform-specific psuutil class
234231
if platform_chassis is None:
235232
try:
236233
platform_psuutil = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME)
237234
except Exception as e:
238-
logger.log_error("Failed to load psuutil: %s" % (str(e)), True)
235+
self.log_error("Failed to load psuutil: %s" % (str(e)), True)
239236
sys.exit(PSUUTIL_LOAD_ERROR)
240237

241238
# Connect to STATE_DB and create psu/chassis info tables
@@ -249,22 +246,22 @@ class DaemonPsud(DaemonBase):
249246
chassis_tbl.set(CHASSIS_INFO_KEY_TEMPLATE.format(1), fvs)
250247

251248
# Start main loop
252-
logger.log_info("Start daemon main loop")
249+
self.log_info("Start daemon main loop")
253250

254251
while not self.stop.wait(PSU_INFO_UPDATE_PERIOD_SECS):
255252
psu_db_update(psu_tbl, psu_num)
256253
self.update_psu_data(psu_tbl)
257254
self._update_led_color(psu_tbl)
258255

259-
logger.log_info("Stop daemon main loop")
256+
self.log_info("Stop daemon main loop")
260257

261258
# Delete all the information from DB and then exit
262259
for psu_index in range(1, psu_num + 1):
263260
psu_tbl._del(PSU_INFO_KEY_TEMPLATE.format(psu_index))
264261

265262
chassis_tbl._del(CHASSIS_INFO_KEY_TEMPLATE.format(1))
266263

267-
logger.log_info("Shutting down...")
264+
self.log_info("Shutting down...")
268265

269266
def update_psu_data(self, psu_tbl):
270267
if not platform_chassis:
@@ -274,7 +271,7 @@ class DaemonPsud(DaemonBase):
274271
try:
275272
self._update_single_psu_data(index + 1, psu, psu_tbl)
276273
except Exception as e:
277-
logger.log_warning("Failed to update PSU data - {}".format(e))
274+
self.log_warning("Failed to update PSU data - {}".format(e))
278275

279276
def _update_single_psu_data(self, index, psu, psu_tbl):
280277
name = try_get(psu.get_name)
@@ -358,7 +355,7 @@ class DaemonPsud(DaemonBase):
358355
('led_status', str(try_get(psu_status.psu.get_status_led)))
359356
])
360357
except Exception as e:
361-
logger.log_warning('Failed to get led status for psu {}'.format(index))
358+
self.log_warning('Failed to get led status for psu {}'.format(index))
362359
fvs = swsscommon.FieldValuePairs([
363360
('led_status', NOT_AVAILABLE)
364361
])
@@ -368,7 +365,7 @@ class DaemonPsud(DaemonBase):
368365
#
369366

370367
def main():
371-
psud = DaemonPsud()
368+
psud = DaemonPsud(SYSLOG_IDENTIFIER)
372369
psud.run()
373370

374371
if __name__ == '__main__':

0 commit comments

Comments
 (0)