Skip to content

Commit

Permalink
[201911] Migrate from sonic-daemon-base package to sonic-py-common pa…
Browse files Browse the repository at this point in the history
…ckage (#83)

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/sonic-buildimage#4999.

- Also reorganize imports for consistency
  • Loading branch information
jleveque authored Aug 13, 2020
1 parent e2160e5 commit c65d1d6
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 258 deletions.
18 changes: 6 additions & 12 deletions sonic-ledd/scripts/ledd
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ try:
import signal
import subprocess
import sys
import syslog

from sonic_py_common import daemon_base
from swsscommon import swsscommon
from sonic_daemon_base import daemon_base
from sonic_daemon_base.daemon_base import Logger
from sonic_daemon_base.daemon_base import DaemonBase
except ImportError, e:
raise ImportError (str(e) + " - required module not found")

Expand All @@ -41,11 +39,7 @@ SELECT_TIMEOUT = 1000

LEDUTIL_LOAD_ERROR = 1

logger = Logger(SYSLOG_IDENTIFIER)

class DaemonLedd(DaemonBase):
def __init__(self):
DaemonBase.__init__(self)
class DaemonLedd(daemon_base.DaemonBase):

# Run daemon
def run(self):
Expand All @@ -72,7 +66,7 @@ class DaemonLedd(DaemonBase):
try:
led_control = self.load_platform_util(LED_MODULE_NAME, LED_CLASS_NAME)
except Exception as e:
logger.log_error("Failed to load ledutil: %s" % (str(e)), True)
self.log_error("Failed to load ledutil: %s" % (str(e)), True)
sys.exit(LEDUTIL_LOAD_ERROR)

# Open a handle to the Application database
Expand All @@ -93,7 +87,7 @@ class DaemonLedd(DaemonBase):
# Do not flood log when select times out
continue
if state != swsscommon.Select.OBJECT:
logger.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
self.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
continue

(key, op, fvp) = sst.pop()
Expand All @@ -111,7 +105,7 @@ class DaemonLedd(DaemonBase):
return 1

def main():
ledd = DaemonLedd()
ledd = DaemonLedd(SYSLOG_IDENTIFIER)
ledd.run()

if __name__ == '__main__':
Expand Down
38 changes: 17 additions & 21 deletions sonic-psud/scripts/psud
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
"""

try:
import sys
import time
import signal
import sys
import threading

from sonic_py_common import daemon_base
from swsscommon import swsscommon
from sonic_daemon_base import daemon_base
from sonic_daemon_base.daemon_base import Logger
from sonic_daemon_base.daemon_base import DaemonBase
except ImportError, e:
raise ImportError (str(e) + " - required module not found")

Expand All @@ -43,8 +41,6 @@ PSU_INFO_UPDATE_PERIOD_SECS = 3

PSUUTIL_LOAD_ERROR = 1

logger = Logger(SYSLOG_IDENTIFIER)

platform_psuutil = None
platform_chassis = None

Expand Down Expand Up @@ -89,45 +85,45 @@ def psu_db_update(psu_tbl, psu_num):
# Daemon =======================================================================
#

class DaemonPsud(DaemonBase):
def __init__(self):
DaemonBase.__init__(self)
class DaemonPsud(daemon_base.DaemonBase):
def __init__(self, log_identifier):
super(DaemonPsud, self).__init__(log_identifier)

self.stop = threading.Event()

# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
logger.log_info("Caught SIGHUP - ignoring...")
self.log_info("Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
logger.log_info("Caught SIGINT - exiting...")
self.log_info("Caught SIGINT - exiting...")
self.stop.set()
elif sig == signal.SIGTERM:
logger.log_info("Caught SIGTERM - exiting...")
self.log_info("Caught SIGTERM - exiting...")
self.stop.set()
else:
logger.log_warning("Caught unhandled signal '" + sig + "'")
self.log_warning("Caught unhandled signal '" + sig + "'")

# Run daemon
def run(self):
global platform_psuutil
global platform_chassis

logger.log_info("Starting up...")
self.log_info("Starting up...")

# Load new platform api class
try:
import sonic_platform.platform
platform_chassis = sonic_platform.platform.Platform().get_chassis()
except Exception as e:
logger.log_warning("Failed to load chassis due to {}".format(repr(e)))
self.log_warning("Failed to load chassis due to {}".format(repr(e)))

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

# Connect to STATE_DB and create psu/chassis info tables
Expand All @@ -141,27 +137,27 @@ class DaemonPsud(DaemonBase):
chassis_tbl.set(CHASSIS_INFO_KEY_TEMPLATE.format(1), fvs)

# Start main loop
logger.log_info("Start daemon main loop")
self.log_info("Start daemon main loop")

while not self.stop.wait(PSU_INFO_UPDATE_PERIOD_SECS):
psu_db_update(psu_tbl, psu_num)

logger.log_info("Stop daemon main loop")
self.log_info("Stop daemon main loop")

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

chassis_tbl._del(CHASSIS_INFO_KEY_TEMPLATE.format(1))

logger.log_info("Shutting down...")
self.log_info("Shutting down...")

#
# Main =========================================================================
#

def main():
psud = DaemonPsud()
psud = DaemonPsud(SYSLOG_IDENTIFIER)
psud.run()

if __name__ == '__main__':
Expand Down
47 changes: 22 additions & 25 deletions sonic-syseepromd/scripts/syseepromd
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
'''

try:
import sys
import signal
import sys
import threading
from sonic_daemon_base.daemon_base import DaemonBase
from sonic_daemon_base.daemon_base import Logger
from sonic_daemon_base import daemon_base

from sonic_py_common import daemon_base
from swsscommon import swsscommon
except ImportError, e:
raise ImportError (str(e) + " - required module not found")
Expand All @@ -34,12 +33,10 @@ ERR_EEPROMUTIL_LOAD = 5
EEPROM_TABLE_NAME = 'EEPROM_INFO'
SYSLOG_IDENTIFIER = 'syseepromd'

# Global logger class instance
logger = Logger(SYSLOG_IDENTIFIER)

class DaemonSyseeprom(DaemonBase):
def __init__(self):
DaemonBase.__init__(self)
class DaemonSyseeprom(daemon_base.DaemonBase):
def __init__(self, log_identifier):
super(DaemonSyseeprom, self).__init__(log_identifier)

self.stop_event = threading.Event()
self.eeprom = None
Expand Down Expand Up @@ -72,12 +69,12 @@ class DaemonSyseeprom(DaemonBase):
def post_eeprom_to_db(self):
eeprom = self._wrapper_read_eeprom()
if eeprom is None :
logger.log_error("Failed to read eeprom")
self.log_error("Failed to read eeprom")
return ERR_FAILED_EEPROM

err = self._wrapper_update_eeprom_db(eeprom)
if err:
logger.log_error("Failed to update eeprom info to database")
self.log_error("Failed to update eeprom info to database")
return ERR_FAILED_UPDATE_DB

self.eepromtbl_keys = self.eeprom_tbl.getKeys()
Expand All @@ -104,69 +101,69 @@ class DaemonSyseeprom(DaemonBase):
# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
logger.log_info("Caught SIGHUP - ignoring...")
self.log_info("Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
logger.log_info("Caught SIGINT - exiting...")
self.log_info("Caught SIGINT - exiting...")
self.stop_event.set()
elif sig == signal.SIGTERM:
logger.log_info("Caught SIGTERM - exiting...")
self.log_info("Caught SIGTERM - exiting...")
self.stop_event.set()
else:
logger.log_warning("Caught unhandled signal '" + sig + "'")
self.log_warning("Caught unhandled signal '" + sig + "'")

# Run daemon
def run(self):
logger.log_info("Starting up...")
self.log_info("Starting up...")

# First, try to load the new platform API
try:
import sonic_platform
self.chassis = sonic_platform.platform.Platform().get_chassis()
self.eeprom = self.chassis.get_eeprom()
except Exception as e:
logger.log_warning("Failed to load platform-specific eeprom from sonic_platform package due to {}".format(repr(e)))
self.log_warning("Failed to load platform-specific eeprom from sonic_platform package due to {}".format(repr(e)))

# If we didn't successfully load the class from the sonic_platform package, try loading the old plugin
if not self.eeprom:
try:
self.eeprom = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME)
except Exception as e:
logger.log_error("Failed to load platform-specific eeprom implementation: {}".format(repr(e)))
self.log_error("Failed to load platform-specific eeprom implementation: {}".format(repr(e)))

if not self.eeprom:
sys.exit(ERR_EEPROMUTIL_LOAD)

# Connect to STATE_DB and post syseeprom info to state DB
rc = self.post_eeprom_to_db()
if rc != POST_EEPROM_SUCCESS:
logger.log_error("Failed to post eeprom to database")
self.log_error("Failed to post eeprom to database")

# Start main loop
logger.log_info("Start daemon main loop")
self.log_info("Start daemon main loop")

while not self.stop_event.wait(EEPROM_INFO_UPDATE_PERIOD_SECS):
rc = self.detect_eeprom_table_integrity()
if not rc:
logger.log_info("sys eeprom table was changed, need update")
self.log_info("sys eeprom table was changed, need update")
self.clear_db()
rcs = self.post_eeprom_to_db()
if rcs != POST_EEPROM_SUCCESS:
logger.log_error("Failed to post eeprom to database")
self.log_error("Failed to post eeprom to database")
continue

logger.log_info("Stop daemon main loop")
self.log_info("Stop daemon main loop")

# Delete all the information from DB and then exit
self.clear_db()

logger.log_info("Shutting down...")
self.log_info("Shutting down...")

#
# Main =========================================================================
#

def main():
syseepromd = DaemonSyseeprom()
syseepromd = DaemonSyseeprom(SYSLOG_IDENTIFIER)
syseepromd.run()

if __name__ == '__main__':
Expand Down
Loading

0 comments on commit c65d1d6

Please sign in to comment.