From 263b2669adfc0666a66062a06a78e145715a95cc Mon Sep 17 00:00:00 2001 From: keboliu Date: Thu, 14 Mar 2019 09:16:00 +0200 Subject: [PATCH 01/15] add post-syseeprom scripts to pmon --- sonic-post-syseeprom/scripts/post-syseeprom | 43 +++++++++++++++++++++ sonic-post-syseeprom/setup.py | 29 ++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 sonic-post-syseeprom/scripts/post-syseeprom create mode 100644 sonic-post-syseeprom/setup.py diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom new file mode 100644 index 000000000..e0d6c4ce2 --- /dev/null +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -0,0 +1,43 @@ +#!/usr/bin/env python2 + +''' + post-syseeprom + Syseeprom infomation gathering task for SONiC + This task will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. + It's an one-shot task since syseeprom info are static. + With this task, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. +''' + +try: + from sonic_daemon_base.daemon_base import DaemonBase +except ImportError, e: + raise ImportError (str(e) + " - required module not found") + +PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' +PLATFORM_SPECIFIC_CLASS_NAME = 'board' + +def main(): + Daemon_Base = DaemonBase() + (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() + + # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. + # So for now this task can not support Arista platform, but this will not cause anny issue on them. + # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. + if 'arista' in platform_path: + Daemon_Base.log_warning('Arista platform not support this yet') + return 1 + + eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) + + eeprom = eeprom_util.read_eeprom() + if eeprom is None : + Daemon_Base.log_error('Failed to read eeprom') + return 2 + + err = eeprom_util.update_eeprom_db(eeprom) + if err: + Daemon_Base.log_error('Failed to update eeprom to database') + return 3 + +if __name__ == '__main__': + main() diff --git a/sonic-post-syseeprom/setup.py b/sonic-post-syseeprom/setup.py new file mode 100644 index 000000000..53d9df2ee --- /dev/null +++ b/sonic-post-syseeprom/setup.py @@ -0,0 +1,29 @@ +from setuptools import setup + +setup( + name='sonic-post-syseeprom', + version='1.0', + description='Syseeprom gathering task for SONiC', + license='Apache 2.0', + author='SONiC Team', + author_email='linuxnetdev@microsoft.com', + url='https://github.com/Azure/sonic-platform-daemons', + maintainer='Kebo Liu', + maintainer_email='kebol@mellanox.com', + scripts=[ + 'scripts/post-syseeprom', + ], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Environment :: No Input/Output (Daemon)', + 'Intended Audience :: Developers', + 'Intended Audience :: Information Technology', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: Apache Software License', + 'Natural Language :: English', + 'Operating System :: POSIX :: Linux', + 'Programming Language :: Python :: 2.7', + 'Topic :: System :: Hardware', + ], + keywords='sonic SONiC SYSEEPROM syseeprom POST-SYSEEPROM post-syseeprom', +) From 99f7b7fcb40b2facf348fd737cba665b246ee434 Mon Sep 17 00:00:00 2001 From: keboliu Date: Thu, 14 Mar 2019 11:55:32 +0200 Subject: [PATCH 02/15] remove hardcoded magic numbers --- sonic-post-syseeprom/scripts/post-syseeprom | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index e0d6c4ce2..c701e2b7f 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -16,6 +16,10 @@ except ImportError, e: PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' +ERR_ARISTA_PLATFORM = 1 +ERR_FAILED_EEPROM = 2 +ERR_FAILED_UPDATE_DB = 3 + def main(): Daemon_Base = DaemonBase() (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() @@ -25,19 +29,19 @@ def main(): # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. if 'arista' in platform_path: Daemon_Base.log_warning('Arista platform not support this yet') - return 1 + return ERR_ARISTA_PLATFORM eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) eeprom = eeprom_util.read_eeprom() if eeprom is None : Daemon_Base.log_error('Failed to read eeprom') - return 2 + return ERR_FAILED_EEPROM err = eeprom_util.update_eeprom_db(eeprom) if err: Daemon_Base.log_error('Failed to update eeprom to database') - return 3 + return ERR_FAILED_UPDATE_DB if __name__ == '__main__': main() From c932c615ea6bfe0b3eed9f948ee8ddb99eb209ac Mon Sep 17 00:00:00 2001 From: keboliu Date: Mon, 18 Mar 2019 03:32:12 +0200 Subject: [PATCH 03/15] reword and fix typo --- sonic-post-syseeprom/scripts/post-syseeprom | 12 ++++++------ sonic-post-syseeprom/setup.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index c701e2b7f..f7c7eb365 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -2,10 +2,10 @@ ''' post-syseeprom - Syseeprom infomation gathering task for SONiC - This task will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. + Syseeprom information gathering tool for SONiC + This tool will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. It's an one-shot task since syseeprom info are static. - With this task, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. + With this tool, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. ''' try: @@ -25,10 +25,10 @@ def main(): (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. - # So for now this task can not support Arista platform, but this will not cause anny issue on them. + # So for now this tool can not support Arista platform, but this will not cause anny issue on them. # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. if 'arista' in platform_path: - Daemon_Base.log_warning('Arista platform not support this yet') + Daemon_Base.log_warning('Arista platform does not support this yet') return ERR_ARISTA_PLATFORM eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) @@ -40,7 +40,7 @@ def main(): err = eeprom_util.update_eeprom_db(eeprom) if err: - Daemon_Base.log_error('Failed to update eeprom to database') + Daemon_Base.log_error('Failed to update eeprom info to database') return ERR_FAILED_UPDATE_DB if __name__ == '__main__': diff --git a/sonic-post-syseeprom/setup.py b/sonic-post-syseeprom/setup.py index 53d9df2ee..b24e08aee 100644 --- a/sonic-post-syseeprom/setup.py +++ b/sonic-post-syseeprom/setup.py @@ -3,7 +3,7 @@ setup( name='sonic-post-syseeprom', version='1.0', - description='Syseeprom gathering task for SONiC', + description='Tool which posts syseeprom to DB in SONiC', license='Apache 2.0', author='SONiC Team', author_email='linuxnetdev@microsoft.com', From df6fdf96bd4d42fb4e5250cf470fbf599a87de93 Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 27 Mar 2019 10:30:48 +0200 Subject: [PATCH 04/15] add options for scripts to handle clear db function --- sonic-post-syseeprom/scripts/post-syseeprom | 43 ++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index f7c7eb365..17c26dd60 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -10,6 +10,8 @@ try: from sonic_daemon_base.daemon_base import DaemonBase + import argparse + from swsscommon import swsscommon except ImportError, e: raise ImportError (str(e) + " - required module not found") @@ -20,28 +22,43 @@ ERR_ARISTA_PLATFORM = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 +EEPROM_TABLE_NAME = 'EEPROM_INFO' + def main(): + parser = argparse.ArgumentParser(description='post-syseeprom tool') + parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') + parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') + args = parser.parse_args() + Daemon_Base = DaemonBase() (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() - + # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. # So for now this tool can not support Arista platform, but this will not cause anny issue on them. # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. if 'arista' in platform_path: Daemon_Base.log_warning('Arista platform does not support this yet') return ERR_ARISTA_PLATFORM - - eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) - - eeprom = eeprom_util.read_eeprom() - if eeprom is None : - Daemon_Base.log_error('Failed to read eeprom') - return ERR_FAILED_EEPROM - - err = eeprom_util.update_eeprom_db(eeprom) - if err: - Daemon_Base.log_error('Failed to update eeprom info to database') - return ERR_FAILED_UPDATE_DB + + if args.w: + eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) + eeprom = eeprom_util.read_eeprom() + if eeprom is None : + Daemon_Base.log_error('Failed to read eeprom') + return ERR_FAILED_EEPROM + + err = eeprom_util.update_eeprom_db(eeprom) + if err: + Daemon_Base.log_error('Failed to update eeprom info to database') + return ERR_FAILED_UPDATE_DB + elif args.c: + state_db = Daemon_Base.db_connect(swsscommon.STATE_DB) + eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) + keys = eeprom_tbl.getKeys() + for key in keys: + eeprom_tbl._del(key) + else: + Daemon_Base.log_error("Invalid parameter") if __name__ == '__main__': main() From 674b981083944c51119e67e276dd7f11e453ff45 Mon Sep 17 00:00:00 2001 From: keboliu Date: Fri, 19 Apr 2019 05:58:47 +0300 Subject: [PATCH 05/15] refactor the scripts with adding class --- sonic-post-syseeprom/scripts/post-syseeprom | 68 ++++++++++++++------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index 17c26dd60..c93ed3978 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -12,53 +12,79 @@ try: from sonic_daemon_base.daemon_base import DaemonBase import argparse 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") PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' +EEPROM_TABLE_NAME = 'EEPROM_INFO' ERR_ARISTA_PLATFORM = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 -EEPROM_TABLE_NAME = 'EEPROM_INFO' +SYSLOG_IDENTIFIER = "post-syseeprom" -def main(): - parser = argparse.ArgumentParser(description='post-syseeprom tool') - parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') - parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') - args = parser.parse_args() +# Global logger class instance +logger = Logger(SYSLOG_IDENTIFIER) - Daemon_Base = DaemonBase() - (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() +class PostSyseeprom(DaemonBase): + def __init__(self): + DaemonBase.__init__(self) - # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. - # So for now this tool can not support Arista platform, but this will not cause anny issue on them. - # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. - if 'arista' in platform_path: - Daemon_Base.log_warning('Arista platform does not support this yet') - return ERR_ARISTA_PLATFORM + def _is_arista_platform(self): + (platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku() + + return 'arista' in platform_path - if args.w: - eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) + def write_info_to_db(self): + if self._is_arista_platform(): + # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. + # So for now this tool can not support Arista platform, but this will not cause any issue on them. + # decode-syseeprom scripts will cover Arista case, it will still read from HW/cache instead from DB on Arista platform. + logger.log_warning('Arista platform does not support this yet') + return ERR_ARISTA_PLATFORM + + eeprom_util = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) eeprom = eeprom_util.read_eeprom() if eeprom is None : - Daemon_Base.log_error('Failed to read eeprom') + logger.log_error('Failed to read eeprom') return ERR_FAILED_EEPROM err = eeprom_util.update_eeprom_db(eeprom) if err: - Daemon_Base.log_error('Failed to update eeprom info to database') + logger.log_error('Failed to update eeprom info to database') return ERR_FAILED_UPDATE_DB - elif args.c: - state_db = Daemon_Base.db_connect(swsscommon.STATE_DB) + + def clear_db(self): + # Same case as in above write_info_to_db() function, Arista platform not supported here. + if self._is_arista_platform(): + logger.log_warning('Arista platform does not support this yet') + return ERR_ARISTA_PLATFORM + + state_db = daemon_base.db_connect(swsscommon.STATE_DB) eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) keys = eeprom_tbl.getKeys() for key in keys: eeprom_tbl._del(key) + + +def main(): + parser = argparse.ArgumentParser(description='post-syseeprom tool') + parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') + parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') + args = parser.parse_args() + + post_syseeprom = PostSyseeprom() + if args.w: + post_syseeprom.write_info_to_db() + elif args.c: + post_syseeprom.clear_db() else: - Daemon_Base.log_error("Invalid parameter") + logger.log_error("Invalid parameter") if __name__ == '__main__': main() From aaf358f4c03da7483aaf82d31fd42160897882b4 Mon Sep 17 00:00:00 2001 From: keboliu Date: Fri, 19 Apr 2019 09:00:10 +0300 Subject: [PATCH 06/15] Revert "refactor the scripts with adding class" This reverts commit 674b981083944c51119e67e276dd7f11e453ff45. --- sonic-post-syseeprom/scripts/post-syseeprom | 68 +++++++-------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index c93ed3978..17c26dd60 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -12,79 +12,53 @@ try: from sonic_daemon_base.daemon_base import DaemonBase import argparse 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") PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' -EEPROM_TABLE_NAME = 'EEPROM_INFO' ERR_ARISTA_PLATFORM = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 -SYSLOG_IDENTIFIER = "post-syseeprom" - -# Global logger class instance -logger = Logger(SYSLOG_IDENTIFIER) +EEPROM_TABLE_NAME = 'EEPROM_INFO' -class PostSyseeprom(DaemonBase): - def __init__(self): - DaemonBase.__init__(self) +def main(): + parser = argparse.ArgumentParser(description='post-syseeprom tool') + parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') + parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') + args = parser.parse_args() - def _is_arista_platform(self): - (platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku() - - return 'arista' in platform_path + Daemon_Base = DaemonBase() + (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() - def write_info_to_db(self): - if self._is_arista_platform(): - # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. - # So for now this tool can not support Arista platform, but this will not cause any issue on them. - # decode-syseeprom scripts will cover Arista case, it will still read from HW/cache instead from DB on Arista platform. - logger.log_warning('Arista platform does not support this yet') - return ERR_ARISTA_PLATFORM + # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. + # So for now this tool can not support Arista platform, but this will not cause anny issue on them. + # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. + if 'arista' in platform_path: + Daemon_Base.log_warning('Arista platform does not support this yet') + return ERR_ARISTA_PLATFORM - eeprom_util = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) + if args.w: + eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) eeprom = eeprom_util.read_eeprom() if eeprom is None : - logger.log_error('Failed to read eeprom') + Daemon_Base.log_error('Failed to read eeprom') return ERR_FAILED_EEPROM err = eeprom_util.update_eeprom_db(eeprom) if err: - logger.log_error('Failed to update eeprom info to database') + Daemon_Base.log_error('Failed to update eeprom info to database') return ERR_FAILED_UPDATE_DB - - def clear_db(self): - # Same case as in above write_info_to_db() function, Arista platform not supported here. - if self._is_arista_platform(): - logger.log_warning('Arista platform does not support this yet') - return ERR_ARISTA_PLATFORM - - state_db = daemon_base.db_connect(swsscommon.STATE_DB) + elif args.c: + state_db = Daemon_Base.db_connect(swsscommon.STATE_DB) eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) keys = eeprom_tbl.getKeys() for key in keys: eeprom_tbl._del(key) - - -def main(): - parser = argparse.ArgumentParser(description='post-syseeprom tool') - parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') - parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') - args = parser.parse_args() - - post_syseeprom = PostSyseeprom() - if args.w: - post_syseeprom.write_info_to_db() - elif args.c: - post_syseeprom.clear_db() else: - logger.log_error("Invalid parameter") + Daemon_Base.log_error("Invalid parameter") if __name__ == '__main__': main() From ea1a8ceac637e2ded83d443f19e2516ca6855a92 Mon Sep 17 00:00:00 2001 From: keboliu Date: Fri, 19 Apr 2019 09:18:24 +0300 Subject: [PATCH 07/15] fix logger --- sonic-post-syseeprom/scripts/post-syseeprom | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index 17c26dd60..2d830b815 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -10,6 +10,8 @@ try: from sonic_daemon_base.daemon_base import DaemonBase + from sonic_daemon_base.daemon_base import Logger + from sonic_daemon_base import daemon_base import argparse from swsscommon import swsscommon except ImportError, e: @@ -23,6 +25,10 @@ ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 EEPROM_TABLE_NAME = 'EEPROM_INFO' +SYSLOG_IDENTIFIER = 'post-syseeprom' + +# Global logger class instance +logger = Logger(SYSLOG_IDENTIFIER) def main(): parser = argparse.ArgumentParser(description='post-syseeprom tool') @@ -34,31 +40,31 @@ def main(): (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. - # So for now this tool can not support Arista platform, but this will not cause anny issue on them. - # decode-syseeprom scripts will cover Arista case, it will still read from hw/cache instead from DB on Arista platform. + # So for now this tool can not support Arista platform, but this will not cause any issue on them. + # decode-syseeprom scripts will cover Arista case, it will still read from HW/cache instead from DB on Arista platform. if 'arista' in platform_path: - Daemon_Base.log_warning('Arista platform does not support this yet') + logger.log_warning("Arista platform does not support this yet") return ERR_ARISTA_PLATFORM if args.w: eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) eeprom = eeprom_util.read_eeprom() if eeprom is None : - Daemon_Base.log_error('Failed to read eeprom') + logger.log_error("Failed to read eeprom") return ERR_FAILED_EEPROM err = eeprom_util.update_eeprom_db(eeprom) if err: - Daemon_Base.log_error('Failed to update eeprom info to database') + logger.log_error("Failed to update eeprom info to database") return ERR_FAILED_UPDATE_DB elif args.c: - state_db = Daemon_Base.db_connect(swsscommon.STATE_DB) + state_db = daemon_base.db_connect(swsscommon.STATE_DB) eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) keys = eeprom_tbl.getKeys() for key in keys: eeprom_tbl._del(key) else: - Daemon_Base.log_error("Invalid parameter") + logger.log_error("Invalid parameter") if __name__ == '__main__': main() From 401856091f4354e91d129d62242c212ae8360f4d Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 15 May 2019 06:19:42 +0300 Subject: [PATCH 08/15] add long arguments and rename one error code --- sonic-post-syseeprom/scripts/post-syseeprom | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index 2d830b815..5fb1742a2 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -20,7 +20,7 @@ except ImportError, e: PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' -ERR_ARISTA_PLATFORM = 1 +ERR_PLATFORM_NOT_SUPPORT = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 @@ -32,8 +32,8 @@ logger = Logger(SYSLOG_IDENTIFIER) def main(): parser = argparse.ArgumentParser(description='post-syseeprom tool') - parser.add_argument("-w", help="write syseeprom info into state DB", action='store_true') - parser.add_argument("-c", help="remove syseeprom info from state DB", action='store_true') + parser.add_argument("-w", "--write", help="write syseeprom info into state DB", action='store_true') + parser.add_argument("-c", "--clear", help="remove syseeprom info from state DB", action='store_true') args = parser.parse_args() Daemon_Base = DaemonBase() @@ -44,9 +44,9 @@ def main(): # decode-syseeprom scripts will cover Arista case, it will still read from HW/cache instead from DB on Arista platform. if 'arista' in platform_path: logger.log_warning("Arista platform does not support this yet") - return ERR_ARISTA_PLATFORM + return ERR_PLATFORM_NOT_SUPPORT - if args.w: + if args.write: eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) eeprom = eeprom_util.read_eeprom() if eeprom is None : @@ -57,7 +57,7 @@ def main(): if err: logger.log_error("Failed to update eeprom info to database") return ERR_FAILED_UPDATE_DB - elif args.c: + elif args.clear: state_db = daemon_base.db_connect(swsscommon.STATE_DB) eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) keys = eeprom_tbl.getKeys() From ae372c41677c65b265f1cf1e1619010933e64ff8 Mon Sep 17 00:00:00 2001 From: keboliu Date: Mon, 20 May 2019 09:42:17 +0300 Subject: [PATCH 09/15] add return code for some error case --- sonic-post-syseeprom/scripts/post-syseeprom | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index 5fb1742a2..78714680b 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -23,6 +23,7 @@ PLATFORM_SPECIFIC_CLASS_NAME = 'board' ERR_PLATFORM_NOT_SUPPORT = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 +ERR_INVALID_PARAMETER = 4 EEPROM_TABLE_NAME = 'EEPROM_INFO' SYSLOG_IDENTIFIER = 'post-syseeprom' @@ -65,6 +66,7 @@ def main(): eeprom_tbl._del(key) else: logger.log_error("Invalid parameter") + return ERR_INVALID_PARAMETER if __name__ == '__main__': main() From 9edaa9cc66170023d6de8b21a89acf3b32948034 Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 22 May 2019 05:44:20 +0300 Subject: [PATCH 10/15] add return code --- sonic-post-syseeprom/scripts/post-syseeprom | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom index 78714680b..e44a72389 100644 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ b/sonic-post-syseeprom/scripts/post-syseeprom @@ -20,6 +20,7 @@ except ImportError, e: PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' +POST_EEPROM_SUCCESS = 0 ERR_PLATFORM_NOT_SUPPORT = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 @@ -68,5 +69,7 @@ def main(): logger.log_error("Invalid parameter") return ERR_INVALID_PARAMETER + return POST_EEPROM_SUCCESS + if __name__ == '__main__': main() From 6070223ad5b46ed1a88cb0d4a1bc02f0d2aed5a6 Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 5 Jun 2019 13:17:40 +0300 Subject: [PATCH 11/15] change one shot task to a daemon --- sonic-post-syseeprom/scripts/post-syseeprom | 75 --------- sonic-syseepromd/scripts/syseepromd | 143 ++++++++++++++++++ .../setup.py | 8 +- 3 files changed, 147 insertions(+), 79 deletions(-) delete mode 100644 sonic-post-syseeprom/scripts/post-syseeprom create mode 100644 sonic-syseepromd/scripts/syseepromd rename {sonic-post-syseeprom => sonic-syseepromd}/setup.py (79%) diff --git a/sonic-post-syseeprom/scripts/post-syseeprom b/sonic-post-syseeprom/scripts/post-syseeprom deleted file mode 100644 index e44a72389..000000000 --- a/sonic-post-syseeprom/scripts/post-syseeprom +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python2 - -''' - post-syseeprom - Syseeprom information gathering tool for SONiC - This tool will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. - It's an one-shot task since syseeprom info are static. - With this tool, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. -''' - -try: - from sonic_daemon_base.daemon_base import DaemonBase - from sonic_daemon_base.daemon_base import Logger - from sonic_daemon_base import daemon_base - import argparse - from swsscommon import swsscommon -except ImportError, e: - raise ImportError (str(e) + " - required module not found") - -PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' -PLATFORM_SPECIFIC_CLASS_NAME = 'board' - -POST_EEPROM_SUCCESS = 0 -ERR_PLATFORM_NOT_SUPPORT = 1 -ERR_FAILED_EEPROM = 2 -ERR_FAILED_UPDATE_DB = 3 -ERR_INVALID_PARAMETER = 4 - -EEPROM_TABLE_NAME = 'EEPROM_INFO' -SYSLOG_IDENTIFIER = 'post-syseeprom' - -# Global logger class instance -logger = Logger(SYSLOG_IDENTIFIER) - -def main(): - parser = argparse.ArgumentParser(description='post-syseeprom tool') - parser.add_argument("-w", "--write", help="write syseeprom info into state DB", action='store_true') - parser.add_argument("-c", "--clear", help="remove syseeprom info from state DB", action='store_true') - args = parser.parse_args() - - Daemon_Base = DaemonBase() - (platform_path, hwsku_path) = Daemon_Base.get_path_to_platform_and_hwsku() - - # Arista platform have their own implementation for eeprom tlv parsing, it's not inherited from eeprom base class. - # So for now this tool can not support Arista platform, but this will not cause any issue on them. - # decode-syseeprom scripts will cover Arista case, it will still read from HW/cache instead from DB on Arista platform. - if 'arista' in platform_path: - logger.log_warning("Arista platform does not support this yet") - return ERR_PLATFORM_NOT_SUPPORT - - if args.write: - eeprom_util = Daemon_Base.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) - eeprom = eeprom_util.read_eeprom() - if eeprom is None : - logger.log_error("Failed to read eeprom") - return ERR_FAILED_EEPROM - - err = eeprom_util.update_eeprom_db(eeprom) - if err: - logger.log_error("Failed to update eeprom info to database") - return ERR_FAILED_UPDATE_DB - elif args.clear: - state_db = daemon_base.db_connect(swsscommon.STATE_DB) - eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) - keys = eeprom_tbl.getKeys() - for key in keys: - eeprom_tbl._del(key) - else: - logger.log_error("Invalid parameter") - return ERR_INVALID_PARAMETER - - return POST_EEPROM_SUCCESS - -if __name__ == '__main__': - main() diff --git a/sonic-syseepromd/scripts/syseepromd b/sonic-syseepromd/scripts/syseepromd new file mode 100644 index 000000000..925101f26 --- /dev/null +++ b/sonic-syseepromd/scripts/syseepromd @@ -0,0 +1,143 @@ +#!/usr/bin/env python2 + +''' + syseepromd + Syseeprom information gathering daemon for SONiC + This daemon will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. + It will continue monitoring the state DB for the syseeprom table, it table deleted, will write again. + With this daemon, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. +''' + +try: + import signal + 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 swsscommon import swsscommon +except ImportError, e: + raise ImportError (str(e) + " - required module not found") + +PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' +PLATFORM_SPECIFIC_CLASS_NAME = 'board' + +EEPROM_INFO_UPDATE_PERIOD_SECS = 10 + +POST_EEPROM_SUCCESS = 0 +ERR_PLATFORM_NOT_SUPPORT = 1 +ERR_FAILED_EEPROM = 2 +ERR_FAILED_UPDATE_DB = 3 +ERR_INVALID_PARAMETER = 4 +ERR_PSUUTIL_LOAD = 4 + +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) + + self.stop = threading.Event() + self.eeprom_util = None + + state_db = daemon_base.db_connect(swsscommon.STATE_DB) + self.eeprom_tbl = swsscommon.Table(state_db, EEPROM_TABLE_NAME) + self.eepromtbl_keys = [] + + def load_eeprom_util(self): + try: + self.eeprom_util = 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) + sys.exit(ERR_PSUUTIL_LOAD) + + def post_eeprom_to_db(self): + eeprom = self.eeprom_util.read_eeprom() + if eeprom is None : + logger.log_error("Failed to read eeprom") + return ERR_FAILED_EEPROM + + err = self.eeprom_util.update_eeprom_db(eeprom) + if err: + logger.log_error("Failed to update eeprom info to database") + return ERR_FAILED_UPDATE_DB + + self.eepromtbl_keys = self.eeprom_tbl.getKeys() + + return POST_EEPROM_SUCCESS + + def clear_db(self): + keys = self.eeprom_tbl.getKeys() + for key in keys: + self.eeprom_tbl._del(key) + + def detect_eeprom_table_integrity(self): + keys = self.eeprom_tbl.getKeys() + + if len(keys) != len(self.eepromtbl_keys): + return False + + for key in self.eepromtbl_keys: + if key not in keys: + return False + + return True + + # Signal handler + def signal_handler(self, sig, frame): + if sig == signal.SIGHUP: + logger.log_info("Caught SIGHUP - ignoring...") + elif sig == signal.SIGINT: + logger.log_info("Caught SIGINT - exiting...") + self.stop.set() + elif sig == signal.SIGTERM: + logger.log_info("Caught SIGTERM - exiting...") + self.stop.set() + else: + logger.log_warning("Caught unhandled signal '" + sig + "'") + + # Run daemon + def run(self): + logger.log_info("Starting up...") + + # Load platform-specific eepromutil class + self.load_eeprom_util() + + # Connect to STATE_DB and post syseeprom info to state DB + rc = self.post_eeprom_to_db() + if rc != POST_EEPROM_SUCCESS: + return rc + + # Start main loop + logger.log_info("Start daemon main loop") + + while not self.stop.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.clear_db() + rcs = self.post_eeprom_to_db() + if rcs != POST_EEPROM_SUCCESS: + self.stop.set() + + logger.log_info("Stop daemon main loop") + + # Delete all the information from DB and then exit + self.clear_db() + + logger.log_info("Shutting down...") + +# +# Main ========================================================================= +# + +def main(): + syseepromd = DaemonSyseeprom() + syseepromd.run() + +if __name__ == '__main__': + main() + diff --git a/sonic-post-syseeprom/setup.py b/sonic-syseepromd/setup.py similarity index 79% rename from sonic-post-syseeprom/setup.py rename to sonic-syseepromd/setup.py index b24e08aee..274369c32 100644 --- a/sonic-post-syseeprom/setup.py +++ b/sonic-syseepromd/setup.py @@ -1,9 +1,9 @@ from setuptools import setup setup( - name='sonic-post-syseeprom', + name='sonic-syseepromd', version='1.0', - description='Tool which posts syseeprom to DB in SONiC', + description='Syseeprom gathering daemon for SONiC', license='Apache 2.0', author='SONiC Team', author_email='linuxnetdev@microsoft.com', @@ -11,7 +11,7 @@ maintainer='Kebo Liu', maintainer_email='kebol@mellanox.com', scripts=[ - 'scripts/post-syseeprom', + 'scripts/syseepromd', ], classifiers=[ 'Development Status :: 4 - Beta', @@ -25,5 +25,5 @@ 'Programming Language :: Python :: 2.7', 'Topic :: System :: Hardware', ], - keywords='sonic SONiC SYSEEPROM syseeprom POST-SYSEEPROM post-syseeprom', + keywords='sonic SONiC SYSEEPROM syseeprom SYSEEPROMD syseepromd', ) From f56bb5a4bec864ee1ac7671c95ce62d4dbe36023 Mon Sep 17 00:00:00 2001 From: keboliu Date: Thu, 6 Jun 2019 13:56:49 +0300 Subject: [PATCH 12/15] fix typo --- sonic-syseepromd/scripts/syseepromd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonic-syseepromd/scripts/syseepromd b/sonic-syseepromd/scripts/syseepromd index 925101f26..3822537e5 100644 --- a/sonic-syseepromd/scripts/syseepromd +++ b/sonic-syseepromd/scripts/syseepromd @@ -4,7 +4,7 @@ syseepromd Syseeprom information gathering daemon for SONiC This daemon will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. - It will continue monitoring the state DB for the syseeprom table, it table deleted, will write again. + It will continue monitoring the state DB for the syseeprom table, if table been deleted, it will write again. With this daemon, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. ''' From 1fc9549198507af92912de10f6c855f087b890b1 Mon Sep 17 00:00:00 2001 From: keboliu Date: Tue, 11 Jun 2019 04:05:15 +0300 Subject: [PATCH 13/15] fix typo --- sonic-syseepromd/scripts/syseepromd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sonic-syseepromd/scripts/syseepromd b/sonic-syseepromd/scripts/syseepromd index 3822537e5..7aea03aba 100644 --- a/sonic-syseepromd/scripts/syseepromd +++ b/sonic-syseepromd/scripts/syseepromd @@ -28,7 +28,7 @@ ERR_PLATFORM_NOT_SUPPORT = 1 ERR_FAILED_EEPROM = 2 ERR_FAILED_UPDATE_DB = 3 ERR_INVALID_PARAMETER = 4 -ERR_PSUUTIL_LOAD = 4 +ERR_EEPROMUTIL_LOAD = 5 EEPROM_TABLE_NAME = 'EEPROM_INFO' SYSLOG_IDENTIFIER = 'syseepromd' @@ -51,8 +51,8 @@ class DaemonSyseeprom(DaemonBase): try: self.eeprom_util = 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) - sys.exit(ERR_PSUUTIL_LOAD) + logger.log_error("Failed to load eeprom utility: %s" % (str(e)), True) + sys.exit(ERR_EEPROMUTIL_LOAD) def post_eeprom_to_db(self): eeprom = self.eeprom_util.read_eeprom() From 5e82cae281a7f08838eecd09e536f0cc6e2a6090 Mon Sep 17 00:00:00 2001 From: keboliu Date: Tue, 11 Jun 2019 05:07:20 +0300 Subject: [PATCH 14/15] rewording the comments and rename a variable --- sonic-syseepromd/scripts/syseepromd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sonic-syseepromd/scripts/syseepromd b/sonic-syseepromd/scripts/syseepromd index 7aea03aba..bade3f986 100644 --- a/sonic-syseepromd/scripts/syseepromd +++ b/sonic-syseepromd/scripts/syseepromd @@ -4,7 +4,7 @@ syseepromd Syseeprom information gathering daemon for SONiC This daemon will be started during the start phase of pmon container, gathering syseeprom info and write to state DB. - It will continue monitoring the state DB for the syseeprom table, if table been deleted, it will write again. + It will continue monitoring the state DB for the syseeprom table, if table was deleted, it will write again. With this daemon, show syseeprom CLI will be able to get data from state DB instead of access hw or cache. ''' @@ -40,7 +40,7 @@ class DaemonSyseeprom(DaemonBase): def __init__(self): DaemonBase.__init__(self) - self.stop = threading.Event() + self.stop_event = threading.Event() self.eeprom_util = None state_db = daemon_base.db_connect(swsscommon.STATE_DB) @@ -92,10 +92,10 @@ class DaemonSyseeprom(DaemonBase): logger.log_info("Caught SIGHUP - ignoring...") elif sig == signal.SIGINT: logger.log_info("Caught SIGINT - exiting...") - self.stop.set() + self.stop_event.set() elif sig == signal.SIGTERM: logger.log_info("Caught SIGTERM - exiting...") - self.stop.set() + self.stop_event.set() else: logger.log_warning("Caught unhandled signal '" + sig + "'") @@ -114,14 +114,14 @@ class DaemonSyseeprom(DaemonBase): # Start main loop logger.log_info("Start daemon main loop") - while not self.stop.wait(EEPROM_INFO_UPDATE_PERIOD_SECS): + 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.clear_db() rcs = self.post_eeprom_to_db() if rcs != POST_EEPROM_SUCCESS: - self.stop.set() + self.stop_event.set() logger.log_info("Stop daemon main loop") From 181bd9f0e9d3f1db08e79df51d27afe4f907d53a Mon Sep 17 00:00:00 2001 From: keboliu Date: Sat, 15 Jun 2019 03:56:29 +0300 Subject: [PATCH 15/15] adjust the polling period according to the review comments --- sonic-syseepromd/scripts/syseepromd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonic-syseepromd/scripts/syseepromd b/sonic-syseepromd/scripts/syseepromd index bade3f986..7d3bfd515 100644 --- a/sonic-syseepromd/scripts/syseepromd +++ b/sonic-syseepromd/scripts/syseepromd @@ -21,7 +21,7 @@ except ImportError, e: PLATFORM_SPECIFIC_MODULE_NAME = 'eeprom' PLATFORM_SPECIFIC_CLASS_NAME = 'board' -EEPROM_INFO_UPDATE_PERIOD_SECS = 10 +EEPROM_INFO_UPDATE_PERIOD_SECS = 60 POST_EEPROM_SUCCESS = 0 ERR_PLATFORM_NOT_SUPPORT = 1