diff --git a/acl_loader/main.py b/acl_loader/main.py index f23aefc2e4..97c5afa46c 100644 --- a/acl_loader/main.py +++ b/acl_loader/main.py @@ -4,15 +4,13 @@ import ipaddr import json import syslog -import tabulate -from natsort import natsorted -import sonic_device_util import openconfig_acl +import tabulate import pyangbind.lib.pybindJSON as pybindJSON -from swsssdk import ConfigDBConnector -from swsssdk import SonicV2Connector -from swsssdk import SonicDBConfig +from natsort import natsorted +from sonic_py_common import device_info +from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig def info(msg): @@ -142,7 +140,7 @@ def __init__(self): # Getting all front asic namespace and correspding config and state DB connector - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() for front_asic_namespaces in namespaces['front_ns']: self.per_npu_configdb[front_asic_namespaces] = ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespaces) self.per_npu_configdb[front_asic_namespaces].connect() diff --git a/config/main.py b/config/main.py index 6bfd4da9b6..43f07a0bd2 100755 --- a/config/main.py +++ b/config/main.py @@ -1,30 +1,28 @@ #!/usr/sbin/env python -import sys -import os import click -import subprocess +import ipaddress +import json import netaddr -import re -import syslog -import time import netifaces +import os +import re +import subprocess +import sys import threading -import json +import time -import sonic_device_util -import ipaddress -from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig from minigraph import parse_device_desc_xml -from config_mgmt import ConfigMgmtDPB -from utilities_common.intf_filter import parse_interface_in_filter -from utilities_common.util_base import UtilHelper -from utilities_common.db import Db from portconfig import get_child_ports, get_port_config_file_name +from sonic_py_common import device_info, logger +from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig +from utilities_common.db import Db +from utilities_common.intf_filter import parse_interface_in_filter import aaa import mlnx import nat +from config_mgmt import ConfigMgmtDPB CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?']) @@ -50,32 +48,11 @@ CFG_LOOPBACK_ID_MAX_VAL = 999 CFG_LOOPBACK_NO="<0-999>" -asic_type = None - -# ========================== Syslog wrappers ========================== - -def log_debug(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_DEBUG, msg) - syslog.closelog() +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) -def log_info(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - -def log_warning(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - -def log_error(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() +asic_type = None class AbbreviationGroup(click.Group): @@ -116,14 +93,13 @@ def get_command(self, ctx, cmd_name): ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) + # # Load breakout config file for Dynamic Port Breakout # try: - # Load the helper class - helper = UtilHelper() - (platform, hwsku) = helper.get_platform_and_hwsku() + (platform, hwsku) = device_info.get_platform_and_hwsku() except Exception as e: click.secho("Failed to get platform and hwsku with error:{}".format(str(e)), fg='red') raise click.Abort() @@ -279,17 +255,17 @@ def execute_systemctl_per_asic_instance(inst, event, service, action): click.echo("Executing {} of service {}@{}...".format(action, service, inst)) run_command("systemctl {} {}@{}.service".format(action, service, inst)) except SystemExit as e: - log_error("Failed to execute {} of service {}@{} with error {}".format(action, service, inst, e)) + log.log_error("Failed to execute {} of service {}@{} with error {}".format(action, service, inst, e)) # Set the event object if there is a failure and exception was raised. event.set() # Execute action on list of systemd services def execute_systemctl(list_of_services, action): - num_asic = sonic_device_util.get_num_npus() + num_asic = device_info.get_num_npus() generated_services_list, generated_multi_instance_services = _get_sonic_generated_services(num_asic) if ((generated_services_list == []) and (generated_multi_instance_services == [])): - log_error("Failed to get generated services") + log.log_error("Failed to get generated services") return for service in list_of_services: @@ -298,12 +274,12 @@ def execute_systemctl(list_of_services, action): click.echo("Executing {} of service {}...".format(action, service)) run_command("systemctl {} {}".format(action, service)) except SystemExit as e: - log_error("Failed to execute {} of service {} with error {}".format(action, service, e)) + log.log_error("Failed to execute {} of service {} with error {}".format(action, service, e)) raise if (service + '.service' in generated_multi_instance_services): # With Multi NPU, Start a thread per instance to do the "action" on multi instance services. - if sonic_device_util.is_multi_npu(): + if device_info.is_multi_npu(): threads = [] # Use this event object to co-ordinate if any threads raised exception e = threading.Event() @@ -357,10 +333,10 @@ def _get_device_type(): # Validate whether a given namespace name is valid in the device. def validate_namespace(namespace): - if not sonic_device_util.is_multi_npu(): + if not device_info.is_multi_npu(): return True - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() if namespace in namespaces['front_ns'] + namespaces['back_ns']: return True else: @@ -665,8 +641,8 @@ def _clear_qos(): 'BUFFER_QUEUE'] namespace_list = [DEFAULT_NAMESPACE] - if sonic_device_util.get_num_npus() > 1: - namespace_list = sonic_device_util.get_namespaces() + if device_info.get_num_npus() > 1: + namespace_list = device_info.get_namespaces() for ns in namespace_list: if ns is DEFAULT_NAMESPACE: @@ -709,18 +685,18 @@ def _get_disabled_services_list(config_db): if feature_table is not None: for feature_name in feature_table.keys(): if not feature_name: - log_warning("Feature is None") + log.log_warning("Feature is None") continue state = feature_table[feature_name]['state'] if not state: - log_warning("Enable state of feature '{}' is None".format(feature_name)) + log.log_warning("Enable state of feature '{}' is None".format(feature_name)) continue if state == "disabled": disabled_services_list.append(feature_name) else: - log_warning("Unable to retreive FEATURE table") + log.log_warning("Unable to retreive FEATURE table") return disabled_services_list @@ -928,7 +904,7 @@ def config(ctx): global asic_type try: - version_info = sonic_device_util.get_sonic_version_info() + version_info = device_info.get_sonic_version_info() asic_type = version_info['asic_type'] except KeyError, TypeError: raise click.Abort() @@ -961,11 +937,11 @@ def save(filename): """Export current config DB to a file on disk.\n : Names of configuration file(s) to save, separated by comma with no spaces in between """ - num_asic = sonic_device_util.get_num_npus() + num_asic = device_info.get_num_npus() cfg_files = [] num_cfg_file = 1 - if sonic_device_util.is_multi_npu(): + if device_info.is_multi_npu(): num_cfg_file += num_asic # If the user give the filename[s], extract the file names. @@ -1000,7 +976,7 @@ def save(filename): else: command = "{} -n {} -d --print-data > {}".format(SONIC_CFGGEN_PATH, namespace, file) - log_info("'save' executing...") + log.log_info("'save' executing...") run_command(command, display_cmd=True) @config.command() @@ -1018,11 +994,11 @@ def load(filename, yes): if not yes: click.confirm(message, abort=True) - num_asic = sonic_device_util.get_num_npus() + num_asic = device_info.get_num_npus() cfg_files = [] num_cfg_file = 1 - if sonic_device_util.is_multi_npu(): + if device_info.is_multi_npu(): num_cfg_file += num_asic # If the user give the filename[s], extract the file names. @@ -1062,7 +1038,7 @@ def load(filename, yes): else: command = "{} -n {} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, namespace, file) - log_info("'load' executing...") + log.log_info("'load' executing...") run_command(command, display_cmd=True) @@ -1084,13 +1060,13 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart): if not yes: click.confirm(message, abort=True) - log_info("'reload' executing...") + log.log_info("'reload' executing...") - num_asic = sonic_device_util.get_num_npus() + num_asic = device_info.get_num_npus() cfg_files = [] num_cfg_file = 1 - if sonic_device_util.is_multi_npu(): + if device_info.is_multi_npu(): num_cfg_file += num_asic # If the user give the filename[s], extract the file names. @@ -1113,7 +1089,7 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart): #Stop services before config push if not no_service_restart: - log_info("'reload' stopping services...") + log.log_info("'reload' stopping services...") _stop_services(db.cfgdb) """ In Single AISC platforms we have single DB service. In multi-ASIC platforms we have a global DB @@ -1188,7 +1164,7 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart): # status from all services before we attempt to restart them if not no_service_restart: _reset_failed_services(db.cfgdb) - log_info("'reload' restarting services...") + log.log_info("'reload' restarting services...") _restart_services(db.cfgdb) @config.command("load_mgmt_config") @@ -1197,7 +1173,7 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart): @click.argument('filename', default='/etc/sonic/device_desc.xml', type=click.Path(exists=True)) def load_mgmt_config(filename): """Reconfigure hostname and mgmt interface based on device description file.""" - log_info("'load_mgmt_config' executing...") + log.log_info("'load_mgmt_config' executing...") command = "{} -M {} --write-to-db".format(SONIC_CFGGEN_PATH, filename) run_command(command, display_cmd=True) #FIXME: After config DB daemon for hostname and mgmt interface is implemented, we'll no longer need to do manual configuration here @@ -1223,20 +1199,20 @@ def load_mgmt_config(filename): @pass_db def load_minigraph(db, no_service_restart): """Reconfigure based on minigraph.""" - log_info("'load_minigraph' executing...") + log.log_info("'load_minigraph' executing...") #Stop services before config push if not no_service_restart: - log_info("'load_minigraph' stopping services...") + log.log_info("'load_minigraph' stopping services...") _stop_services(db.cfgdb) # For Single Asic platform the namespace list has the empty string # for mulit Asic platform the empty string to generate the config # for host namespace_list = [DEFAULT_NAMESPACE] - num_npus = sonic_device_util.get_num_npus() + num_npus = device_info.get_num_npus() if num_npus > 1: - namespace_list += sonic_device_util.get_namespaces() + namespace_list += device_info.get_namespaces() for namespace in namespace_list: if namespace is DEFAULT_NAMESPACE: @@ -1286,7 +1262,7 @@ def load_minigraph(db, no_service_restart): if not no_service_restart: _reset_failed_services(db.cfgdb) #FIXME: After config DB daemon is implemented, we'll no longer need to restart every service. - log_info("'load_minigraph' restarting services...") + log.log_info("'load_minigraph' restarting services...") _restart_services(db.cfgdb) click.echo("Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`.") @@ -1460,7 +1436,7 @@ def add_erspan(session_name, src_ip, dst_ip, dscp, ttl, gre_type, queue, policer """ For multi-npu platforms we need to program all front asic namespaces """ - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() if not namespaces['front_ns']: config_db = ConfigDBConnector() config_db.connect() @@ -1510,7 +1486,7 @@ def add_span(session_name, dst_port, src_port, direction, queue, policer): """ For multi-npu platforms we need to program all front asic namespaces """ - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() if not namespaces['front_ns']: config_db = ConfigDBConnector() config_db.connect() @@ -1535,7 +1511,7 @@ def remove(session_name): """ For multi-npu platforms we need to program all front asic namespaces """ - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() if not namespaces['front_ns']: config_db = ConfigDBConnector() config_db.connect() @@ -1645,25 +1621,26 @@ def qos(ctx): @qos.command('clear') def clear(): """Clear QoS configuration""" - log_info("'qos clear' executing...") + log.log_info("'qos clear' executing...") _clear_qos() @qos.command('reload') def reload(): """Reload QoS configuration""" - log_info("'qos reload' executing...") + log.log_info("'qos reload' executing...") _clear_qos() - platform = sonic_device_util.get_platform() - hwsku = sonic_device_util.get_hwsku() + + _, hwsku_path = device_info.get_paths_to_platform_and_hwsku_dirs() + namespace_list = [DEFAULT_NAMESPACE] - if sonic_device_util.get_num_npus() > 1: - namespace_list = sonic_device_util.get_namespaces() + if device_info.get_num_npus() > 1: + namespace_list = device_info.get_namespaces() for ns in namespace_list: if ns is DEFAULT_NAMESPACE: asic_id_suffix = "" else: - asic_id = sonic_device_util.get_npu_id_from_name(ns) + asic_id = device_info.get_npu_id_from_name(ns) if asic_id is None: click.secho( "Command 'qos reload' failed with invalid namespace '{}'". @@ -1674,9 +1651,7 @@ def reload(): asic_id_suffix = str(asic_id) buffer_template_file = os.path.join( - '/usr/share/sonic/device/', - platform, - hwsku, + hwsku_path, asic_id_suffix, 'buffers.json.j2' ) @@ -1693,9 +1668,7 @@ def reload(): ) run_command(command, display_cmd=True) qos_template_file = os.path.join( - '/usr/share/sonic/device/', - platform, - hwsku, + hwsku_path, asic_id_suffix, 'qos.json.j2' ) @@ -1838,7 +1811,7 @@ def add_vlan(ctx, vid): @click.pass_context def del_vlan(ctx, vid): """Delete VLAN""" - log_info("'vlan del {}' executing...".format(vid)) + log.log_info("'vlan del {}' executing...".format(vid)) db = ctx.obj['db'] keys = [ (k, v) for k, v in db.get_table('VLAN_MEMBER') if k == 'Vlan{}'.format(vid) ] for k in keys: @@ -1862,7 +1835,7 @@ def vlan_member(ctx): @click.pass_context def add_vlan_member(ctx, vid, interface_name, untagged): """Add VLAN member""" - log_info("'vlan member add {} {}' executing...".format(vid, interface_name)) + log.log_info("'vlan member add {} {}' executing...".format(vid, interface_name)) db = ctx.obj['db'] vlan_name = 'Vlan{}'.format(vid) vlan = db.get_entry('VLAN', vlan_name) @@ -1905,7 +1878,7 @@ def add_vlan_member(ctx, vid, interface_name, untagged): @click.pass_context def del_vlan_member(ctx, vid, interface_name): """Delete VLAN member""" - log_info("'vlan member del {} {}' executing...".format(vid, interface_name)) + log.log_info("'vlan member del {} {}' executing...".format(vid, interface_name)) db = ctx.obj['db'] vlan_name = 'Vlan{}'.format(vid) vlan = db.get_entry('VLAN', vlan_name) @@ -2201,12 +2174,12 @@ def all(verbose): """Shut down all BGP sessions In the case of Multi-Asic platform, we shut only the EBGP sessions with external neighbors. """ - log_info("'bgp shutdown all' executing...") + log.log_info("'bgp shutdown all' executing...") namespaces = [DEFAULT_NAMESPACE] ignore_local_hosts = False - if sonic_device_util.is_multi_npu(): - ns_list = sonic_device_util.get_all_namespaces() + if device_info.is_multi_npu(): + ns_list = device_info.get_all_namespaces() namespaces = ns_list['front_ns'] ignore_local_hosts = True @@ -2227,12 +2200,12 @@ def neighbor(ipaddr_or_hostname, verbose): """Shut down BGP session by neighbor IP address or hostname. User can specify either internal or external BGP neighbor to shutdown """ - log_info("'bgp shutdown neighbor {}' executing...".format(ipaddr_or_hostname)) + log.log_info("'bgp shutdown neighbor {}' executing...".format(ipaddr_or_hostname)) namespaces = [DEFAULT_NAMESPACE] found_neighbor = False - if sonic_device_util.is_multi_npu(): - ns_list = sonic_device_util.get_all_namespaces() + if device_info.is_multi_npu(): + ns_list = device_info.get_all_namespaces() namespaces = ns_list['front_ns'] + ns_list['back_ns'] # Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the @@ -2258,12 +2231,12 @@ def all(verbose): """Start up all BGP sessions In the case of Multi-Asic platform, we startup only the EBGP sessions with external neighbors. """ - log_info("'bgp startup all' executing...") + log.log_info("'bgp startup all' executing...") namespaces = [DEFAULT_NAMESPACE] ignore_local_hosts = False - if sonic_device_util.is_multi_npu(): - ns_list = sonic_device_util.get_all_namespaces() + if device_info.is_multi_npu(): + ns_list = device_info.get_all_namespaces() namespaces = ns_list['front_ns'] ignore_local_hosts = True @@ -2281,15 +2254,15 @@ def all(verbose): @click.argument('ipaddr_or_hostname', metavar='', required=True) @click.option('-v', '--verbose', is_flag=True, help="Enable verbose output") def neighbor(ipaddr_or_hostname, verbose): - log_info("'bgp startup neighbor {}' executing...".format(ipaddr_or_hostname)) + log.log_info("'bgp startup neighbor {}' executing...".format(ipaddr_or_hostname)) """Start up BGP session by neighbor IP address or hostname. User can specify either internal or external BGP neighbor to startup """ namespaces = [DEFAULT_NAMESPACE] found_neighbor = False - if sonic_device_util.is_multi_npu(): - ns_list = sonic_device_util.get_all_namespaces() + if device_info.is_multi_npu(): + ns_list = device_info.get_all_namespaces() namespaces = ns_list['front_ns'] + ns_list['back_ns'] # Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the @@ -2321,8 +2294,8 @@ def remove_neighbor(neighbor_ip_or_hostname): namespaces = [DEFAULT_NAMESPACE] removed_neighbor = False - if sonic_device_util.is_multi_npu(): - ns_list = sonic_device_util.get_all_namespaces() + if device_info.is_multi_npu(): + ns_list = device_info.get_all_namespaces() namespaces = ns_list['front_ns'] + ns_list['back_ns'] # Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the @@ -2369,7 +2342,7 @@ def startup(ctx, interface_name): if len(intf_fs) == 1 and interface_name_is_valid(interface_name) is False: ctx.fail("Interface name is invalid. Please enter a valid interface name!!") - log_info("'interface startup {}' executing...".format(interface_name)) + log.log_info("'interface startup {}' executing...".format(interface_name)) port_dict = config_db.get_table('PORT') for port_name in port_dict.keys(): if port_name in intf_fs: @@ -2394,7 +2367,7 @@ def startup(ctx, interface_name): @click.pass_context def shutdown(ctx, interface_name): """Shut down interface""" - log_info("'interface shutdown {}' executing...".format(interface_name)) + log.log_info("'interface shutdown {}' executing...".format(interface_name)) config_db = ctx.obj['config_db'] if get_interface_naming_mode() == "alias": interface_name = interface_alias_to_name(interface_name) @@ -2436,7 +2409,7 @@ def speed(ctx, interface_name, interface_speed, verbose): if interface_name is None: ctx.fail("'interface_name' is None!") - log_info("'interface speed {} {}' executing...".format(interface_name, interface_speed)) + log.log_info("'interface speed {} {}' executing...".format(interface_name, interface_speed)) command = "portconfig -p {} -s {}".format(interface_name, interface_speed) if verbose: @@ -3131,7 +3104,7 @@ def update(): @click.argument('file_name', required=True) def full(file_name): """Full update of ACL rules configuration.""" - log_info("'acl update full {}' executing...".format(file_name)) + log.log_info("'acl update full {}' executing...".format(file_name)) command = "acl-loader update full {}".format(file_name) run_command(command) @@ -3144,7 +3117,7 @@ def full(file_name): @click.argument('file_name', required=True) def incremental(file_name): """Incremental update of ACL rule configuration.""" - log_info("'acl update incremental {}' executing...".format(file_name)) + log.log_info("'acl update incremental {}' executing...".format(file_name)) command = "acl-loader update incremental {}".format(file_name) run_command(command) @@ -3235,7 +3208,7 @@ def remove_reasons(counter_name, reasons, verbose): @click.option('-v', '--verbose', is_flag=True, help="Enable verbose output") def ecn(profile, rmax, rmin, ymax, ymin, gmax, gmin, verbose): """ECN-related configuration tasks""" - log_info("'ecn -profile {}' executing...".format(profile)) + log.log_info("'ecn -profile {}' executing...".format(profile)) command = "ecnconfig -p %s" % profile if rmax is not None: command += " -rmax %d" % rmax if rmin is not None: command += " -rmin %d" % rmin @@ -3622,7 +3595,7 @@ def enable(ctx): ctx.fail("Unable to check sflow status {}".format(e)) if out != "active": - log_info("sflow service is not enabled. Starting sflow docker...") + log.log_info("sflow service is not enabled. Starting sflow docker...") run_command("sudo systemctl enable sflow") run_command("sudo systemctl start sflow") diff --git a/config/mlnx.py b/config/mlnx.py index 9d7810a6a9..54775312ff 100644 --- a/config/mlnx.py +++ b/config/mlnx.py @@ -6,12 +6,13 @@ # try: - import sys import os import subprocess - import click - import syslog + import sys import time + + import click + from sonic_py_common import logger except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -36,32 +37,9 @@ # Command to restart swss service COMMAND_RESTART_SWSS = 'systemctl restart swss.service' -# ========================== Syslog wrappers ========================== -def log_info(msg, syslog_identifier, also_print_to_console=False): - syslog.openlog(syslog_identifier) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - if also_print_to_console: - print msg - - -def log_warning(msg, syslog_identifier, also_print_to_console=False): - syslog.openlog(syslog_identifier) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - if also_print_to_console: - print msg - - -def log_error(msg, syslog_identifier, also_print_to_console=False): - syslog.openlog(syslog_identifier) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() - if also_print_to_console: - print msg +# Global logger instance +log = logger.Logger(SNIFFER_SYSLOG_IDENTIFIER) # run command @@ -166,7 +144,7 @@ def restart_swss(): try: run_command(COMMAND_RESTART_SWSS) except OSError as e: - log_error("Not able to restart swss service, %s" % str(e), SNIFFER_SYSLOG_IDENTIFIER, True) + log.log_error("Not able to restart swss service, %s" % str(e), True) return 1 return 0 diff --git a/fwutil/lib.py b/fwutil/lib.py index eff432c630..71f4d1ae85 100755 --- a/fwutil/lib.py +++ b/fwutil/lib.py @@ -7,19 +7,19 @@ try: import os - import time import json import socket - import urllib import subprocess + import time + import urllib + from collections import OrderedDict import click - import sonic_device_util - - from collections import OrderedDict - from urlparse import urlparse - from tabulate import tabulate from log import LogHelper + from sonic_py_common import device_info + from tabulate import tabulate + from urlparse import urlparse + from . import Platform except ImportError as e: raise ImportError("Required module not found: {}".format(str(e))) @@ -296,15 +296,10 @@ def __init__(self, is_modular_chassis): self.__chassis_component_map = OrderedDict() self.__module_component_map = OrderedDict() - def __get_platform_type(self): - return sonic_device_util.get_platform_info( - sonic_device_util.get_machine_info() - ) - def __get_platform_components_path(self, root_path): return self.PLATFORM_COMPONENTS_PATH_TEMPLATE.format( root_path, - self.__get_platform_type(), + device_info.get_platform(), self.PLATFORM_COMPONENTS_FILE ) diff --git a/fwutil/log.py b/fwutil/log.py index 69d60a28f5..95973b0ddc 100755 --- a/fwutil/log.py +++ b/fwutil/log.py @@ -6,8 +6,8 @@ # try: - import syslog import click + from sonic_py_common import logger except ImportError as e: raise ImportError("Required module not found: {}".format(str(e))) @@ -15,42 +15,11 @@ SYSLOG_IDENTIFIER = "fwutil" -# ========================= Helper classes ===================================== - -class SyslogLogger(object): - """ - SyslogLogger - """ - def __init__(self, identifier): - self.__syslog = syslog - - self.__syslog.openlog( - ident=identifier, - logoption=self.__syslog.LOG_NDELAY, - facility=self.__syslog.LOG_USER - ) - - def __del__(self): - self.__syslog.closelog() +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) - def log_error(self, msg): - self.__syslog.syslog(self.__syslog.LOG_ERR, msg) - - def log_warning(self, msg): - self.__syslog.syslog(self.__syslog.LOG_WARNING, msg) - - def log_notice(self, msg): - self.__syslog.syslog(self.__syslog.LOG_NOTICE, msg) - - def log_info(self, msg): - self.__syslog.syslog(self.__syslog.LOG_INFO, msg) - - def log_debug(self, msg): - self.__syslog.syslog(self.__syslog.LOG_DEBUG, msg) - - -logger = SyslogLogger(SYSLOG_IDENTIFIER) +# ========================= Helper classes ===================================== class LogHelper(object): """ @@ -67,7 +36,7 @@ def __log_fw_action_start(self, action, component, firmware): caption = "Firmware {} started".format(action) template = "{}: component={}, firmware={}" - logger.log_info( + log.log_info( template.format( caption, component, @@ -82,7 +51,7 @@ def __log_fw_action_end(self, action, component, firmware, status, exception=Non exception_template = "{}: component={}, firmware={}, status={}, exception={}" if status: - logger.log_info( + log.log_info( status_template.format( caption, component, @@ -92,7 +61,7 @@ def __log_fw_action_end(self, action, component, firmware, status, exception=Non ) else: if exception is None: - logger.log_error( + log.log_error( status_template.format( caption, component, @@ -101,7 +70,7 @@ def __log_fw_action_end(self, action, component, firmware, status, exception=Non ) ) else: - logger.log_error( + log.log_error( exception_template.format( caption, component, diff --git a/pcieutil/main.py b/pcieutil/main.py index 8fddce7979..5eedd678f1 100644 --- a/pcieutil/main.py +++ b/pcieutil/main.py @@ -6,14 +6,11 @@ # try: - import sys import os - import subprocess + import sys + import click - import imp - import syslog - import types - import traceback + from sonic_py_common import device_info, logger from tabulate import tabulate except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -23,100 +20,43 @@ SYSLOG_IDENTIFIER = "pcieutil" PLATFORM_SPECIFIC_MODULE_NAME = "pcieutil" -PLATFORM_ROOT_PATH = '/usr/share/sonic/device' -PLATFORM_ROOT_PATH_DOCKER = '/usr/share/sonic/platform' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' -HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' -PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' - -#from pcieutil import PcieUtil - # Global platform-specific psuutil class instance platform_pcieutil = None -hwsku_path = None - -# ========================== Syslog wrappers ========================== - - -def log_info(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) - - -def log_warning(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) +platform_plugins_path = None +log = logger.Logger(SYSLOG_IDENTIFIER) -def log_error(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() - if also_print_to_console: - click.echo(msg) - -def log_out(name, result): +def print_result(name, result): string = "PCI Device: {} ".format(name) length = 105-len(string) - sys.stdout.write(string) + sys.stdout.write(string) for i in xrange(int(length)): sys.stdout.write("-") - print ' [%s]' % result - + print(' [%s]' % result) + # ==================== Methods for initialization ==================== -# Returns platform and HW SKU -def get_platform_and_hwsku(): - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError, e: - raise OSError("Cannot detect platform") - - return (platform, hwsku) +# Loads platform specific psuutil module from source -# Loads platform specific psuutil module from source def load_platform_pcieutil(): global platform_pcieutil - global hwsku_plugins_path - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() + global platform_plugins_path # Load platform module from source try: - hwsku_plugins_path = "/".join([PLATFORM_ROOT_PATH, platform, "plugins"]) - sys.path.append(os.path.abspath(hwsku_plugins_path)) + platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() + platform_plugins_path = os.path.join(platform_path, "plugins") + sys.path.append(os.path.abspath(platform_plugins_path)) from pcieutil import PcieUtil except ImportError as e: - log_warning("Fail to load specific PcieUtil moudle. Falling down to the common implementation") + log.log_warning("Failed to load platform-specific PcieUtil module. Falling back to the common implementation") try: from sonic_platform_base.sonic_pcie.pcie_common import PcieUtil - platform_pcieutil = PcieUtil(hwsku_plugins_path) + platform_pcieutil = PcieUtil(platform_plugins_path) except ImportError as e: - log_error("Fail to load default PcieUtil moudle. Error :{}".format(str(e)), True) + log.log_error("Failed to load default PcieUtil module. Error : {}".format(str(e)), True) raise e @@ -135,18 +75,22 @@ def cli(): load_platform_pcieutil() # 'version' subcommand + + @cli.command() def version(): """Display version info""" click.echo("pcieutil version {0}".format(VERSION)) -#show the platform PCIE info +# show the platform PCIE info def print_test_title(testname): click.echo("{name:=^80s}".format(name=testname)) # Show PCIE lnkSpeed + + @cli.command() def pcie_show(): '''Display PCIe Device ''' @@ -159,10 +103,7 @@ def pcie_show(): Fn = item["fn"] Name = item["name"] Id = item["id"] - print "bus:dev.fn %s:%s.%s - dev_id=0x%s, %s" % (Bus,Dev,Fn,Id,Name) - - - + print "bus:dev.fn %s:%s.%s - dev_id=0x%s, %s" % (Bus, Dev, Fn, Id, Name) # Show PCIE Vender ID and Device ID @@ -175,17 +116,15 @@ def pcie_check(): resultInfo = platform_pcieutil.get_pcie_check() for item in resultInfo: if item["result"] == "Passed": - log_out(item["name"], "Passed") + print_result(item["name"], "Passed") else: - log_out(item["name"], "Failed") - log_warning("PCIe Device: " + item["name"] + " Not Found") - err+=1 + print_result(item["name"], "Failed") + log.log_warning("PCIe Device: " + item["name"] + " Not Found") + err += 1 if err: print "PCIe Device Checking All Test ----------->>> FAILED" else: print "PCIe Device Checking All Test ----------->>> PASSED" - - @cli.command() @@ -193,7 +132,8 @@ def pcie_check(): def pcie_generate(): '''Generate config file with current pci device''' platform_pcieutil.dump_conf_yaml() - print "Generate config file pcie.yaml under path %s" %hwsku_plugins_path + print "Generate config file pcie.yaml under path %s" % platform_plugins_path + if __name__ == '__main__': cli() diff --git a/pddf_fanutil/main.py b/pddf_fanutil/main.py index 04ca4ea66a..1aa90b9424 100644 --- a/pddf_fanutil/main.py +++ b/pddf_fanutil/main.py @@ -24,7 +24,6 @@ platform_fanutil = None platform_chassis = None -#logger = UtilLogger(SYSLOG_IDENTIFIER) def _wrapper_get_num_fans(): if platform_chassis is not None: @@ -98,9 +97,6 @@ def _wrapper_dump_sysfs(idx): return platform_fanutil.dump_sysfs() - - - # This is our main entrypoint - the main 'fanutil' command @click.group() def cli(): diff --git a/pddf_ledutil/main.py b/pddf_ledutil/main.py index 5f8e74c55f..6ff08ffa97 100644 --- a/pddf_ledutil/main.py +++ b/pddf_ledutil/main.py @@ -40,9 +40,6 @@ def _wrapper_setstatusled(device_name, color, color_state): click.echo(outputs) - -#logger = UtilLogger(SYSLOG_IDENTIFIER) - # ==================== CLI commands and groups ==================== diff --git a/pddf_psuutil/main.py b/pddf_psuutil/main.py index 54bd0ccc52..f916883828 100644 --- a/pddf_psuutil/main.py +++ b/pddf_psuutil/main.py @@ -24,7 +24,6 @@ platform_psuutil = None platform_chassis = None -#logger = UtilLogger(SYSLOG_IDENTIFIER) # Wrapper APIs so that this util is suited to both 1.0 and 2.0 platform APIs def _wrapper_get_num_psus(): diff --git a/pddf_thermalutil/main.py b/pddf_thermalutil/main.py index 11b2aea1ff..1be6381eec 100644 --- a/pddf_thermalutil/main.py +++ b/pddf_thermalutil/main.py @@ -24,8 +24,6 @@ platform_thermalutil = None platform_chassis = None -#logger = UtilLogger(SYSLOG_IDENTIFIER) - # Wrapper APIs so that this util is suited to both 1.0 and 2.0 platform APIs def _wrapper_get_num_thermals(): if platform_chassis is not None: diff --git a/psuutil/main.py b/psuutil/main.py index 180cbecfa7..efea550962 100644 --- a/psuutil/main.py +++ b/psuutil/main.py @@ -6,12 +6,12 @@ # try: - import sys + import imp import os - import subprocess + import sys + import click - import imp - import syslog + from sonic_py_common import device_info, logger from tabulate import tabulate except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -22,98 +22,35 @@ PLATFORM_SPECIFIC_MODULE_NAME = "psuutil" PLATFORM_SPECIFIC_CLASS_NAME = "PsuUtil" -PLATFORM_ROOT_PATH = '/usr/share/sonic/device' -PLATFORM_ROOT_PATH_DOCKER = '/usr/share/sonic/platform' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' -HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' -PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' - # Global platform-specific psuutil class instance platform_psuutil = None -# ========================== Syslog wrappers ========================== - - -def log_info(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) - - -def log_warning(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) - - -def log_error(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) # ==================== Methods for initialization ==================== -# Returns platform and HW SKU -def get_platform_and_hwsku(): - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError as e: - raise OSError("Cannot detect platform") - - return (platform, hwsku) - - # Loads platform specific psuutil module from source def load_platform_psuutil(): global platform_psuutil - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() - # Load platform module from source - platform_path = '' - if len(platform) != 0: - platform_path = "/".join([PLATFORM_ROOT_PATH, platform]) - else: - platform_path = PLATFORM_ROOT_PATH_DOCKER + platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() try: - module_file = "/".join([platform_path, "plugins", PLATFORM_SPECIFIC_MODULE_NAME + ".py"]) + module_file = os.path.join(platform_path, "plugins", PLATFORM_SPECIFIC_MODULE_NAME + ".py") module = imp.load_source(PLATFORM_SPECIFIC_MODULE_NAME, module_file) except IOError as e: - log_error("Failed to load platform module '%s': %s" % (PLATFORM_SPECIFIC_MODULE_NAME, str(e)), True) + log.log_error("Failed to load platform module '%s': %s" % (PLATFORM_SPECIFIC_MODULE_NAME, str(e)), True) return -1 try: platform_psuutil_class = getattr(module, PLATFORM_SPECIFIC_CLASS_NAME) platform_psuutil = platform_psuutil_class() except AttributeError as e: - log_error("Failed to instantiate '%s' class: %s" % (PLATFORM_SPECIFIC_CLASS_NAME, str(e)), True) + log.log_error("Failed to instantiate '%s' class: %s" % (PLATFORM_SPECIFIC_CLASS_NAME, str(e)), True) return -2 return 0 @@ -137,18 +74,24 @@ def cli(): sys.exit(2) # 'version' subcommand + + @cli.command() def version(): """Display version info""" click.echo("psuutil version {0}".format(VERSION)) # 'numpsus' subcommand + + @cli.command() def numpsus(): """Display number of supported PSUs on device""" click.echo(str(platform_psuutil.get_num_psus())) # 'status' subcommand + + @cli.command() @click.option('-i', '--index', default=-1, type=int, help="the index of PSU") def status(index): @@ -167,8 +110,8 @@ def status(index): msg = "" psu_name = "PSU {}".format(psu) if psu not in supported_psu: - click.echo("Error! The {} is not available on the platform.\n" \ - "Number of supported PSU - {}.".format(psu_name, platform_psuutil.get_num_psus())) + click.echo("Error! The {} is not available on the platform.\n" + "Number of supported PSU - {}.".format(psu_name, platform_psuutil.get_num_psus())) continue presence = platform_psuutil.get_psu_presence(psu) if presence: @@ -181,5 +124,6 @@ def status(index): if status_table: click.echo(tabulate(status_table, header, tablefmt="simple")) + if __name__ == '__main__': cli() diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index 17ec3ea6e6..82c9982516 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -1,27 +1,18 @@ #!/usr/bin/env python -import traceback -import sys import argparse -import syslog -from swsssdk import ConfigDBConnector, SonicDBConfig -from swsssdk import SonicV2Connector -import sonic_device_util - +import sys +import traceback -SYSLOG_IDENTIFIER = 'db_migrator' +from sonic_py_common import device_info, logger +from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector -def log_info(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() +SYSLOG_IDENTIFIER = 'db_migrator' -def log_error(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) class DBMigrator(): @@ -100,7 +91,7 @@ def migrate_interface_table(self): for key in data.keys(): if not self.is_ip_prefix_in_key(key) or key[0] in if_db: continue - log_info('Migrating interface table for ' + key[0]) + log.log_info('Migrating interface table for ' + key[0]) self.configDB.set_entry(table, key[0], data[key]) if_db.append(key[0]) @@ -126,7 +117,7 @@ def migrate_intf_table(self): if if_name == "lo": self.appDB.delete(self.appDB.APPL_DB, key) key = key.replace(if_name, "Loopback0") - log_info('Migrating lo entry to ' + key) + log.log_info('Migrating lo entry to ' + key) self.appDB.set(self.appDB.APPL_DB, key, 'NULL', 'NULL') if '/' not in key: @@ -138,7 +129,7 @@ def migrate_intf_table(self): if_name = key.split(":")[1] if if_name in if_db: continue - log_info('Migrating intf table for ' + if_name) + log.log_info('Migrating intf table for ' + if_name) table = "INTF_TABLE:" + if_name self.appDB.set(self.appDB.APPL_DB, table, 'NULL', 'NULL') if_db.append(if_name) @@ -193,7 +184,7 @@ def mlnx_migrate_buffer_pool_size(self): hwsku = device_data['localhost']['hwsku'] platform = device_data['localhost']['platform'] else: - log_error("Trying to get DEVICE_METADATA from DB but doesn't exist, skip migration") + log.log_error("Trying to get DEVICE_METADATA from DB but doesn't exist, skip migration") return False buffer_pool_conf = self.configDB.get_table('BUFFER_POOL') @@ -230,12 +221,12 @@ def mlnx_migrate_buffer_pool_size(self): new_buffer_pool_conf = spc2_t1_default_config else: # It's not using default buffer pool configuration, no migration needed. - log_info("buffer pool size is not old default value, no need to migrate") + log.log_info("buffer pool size is not old default value, no need to migrate") return True # Migrate old buffer conf to latest. for pool in buffer_pools: self.configDB.set_entry('BUFFER_POOL', pool, new_buffer_pool_conf[pool]) - log_info("Successfully migrate mlnx buffer pool size to the latest.") + log.log_info("Successfully migrate mlnx buffer pool size to the latest.") return True def version_unknown(self): @@ -248,7 +239,7 @@ def version_unknown(self): before migrating date to the next version. """ - log_info('Handling version_unknown') + log.log_info('Handling version_unknown') # NOTE: Uncomment next 3 lines of code when the migration code is in # place. Note that returning specific string is intentional, @@ -265,7 +256,7 @@ def version_1_0_1(self): """ Version 1_0_1. """ - log_info('Handling version_1_0_1') + log.log_info('Handling version_1_0_1') self.migrate_interface_table() self.migrate_intf_table() @@ -276,9 +267,9 @@ def version_1_0_2(self): """ Version 1_0_2. """ - log_info('Handling version_1_0_2') + log.log_info('Handling version_1_0_2') # Check ASIC type, if Mellanox platform then need DB migration - version_info = sonic_device_util.get_sonic_version_info() + version_info = device_info.get_sonic_version_info() if version_info['asic_type'] == "mellanox": if self.mlnx_migrate_buffer_pool_size(): self.set_version('version_1_0_3') @@ -290,7 +281,7 @@ def version_1_0_3(self): """ Current latest version. Nothing to do here. """ - log_info('Handling version_1_0_3') + log.log_info('Handling version_1_0_3') return None @@ -305,14 +296,14 @@ def get_version(self): def set_version(self, version=None): if not version: version = self.CURRENT_VERSION - log_info('Setting version to ' + version) + log.log_info('Setting version to ' + version) entry = { self.TABLE_FIELD : version } self.configDB.set_entry(self.TABLE_NAME, self.TABLE_KEY, entry) def migrate(self): version = self.get_version() - log_info('Upgrading from version ' + version) + log.log_info('Upgrading from version ' + version) while version: next_version = getattr(self, version)() if next_version == version: @@ -364,7 +355,7 @@ def main(): print(str(result)) except Exception as e: - log_error('Caught exception: ' + str(e)) + log.log_error('Caught exception: ' + str(e)) traceback.print_exc() print(str(e)) parser.print_help() diff --git a/scripts/decode-syseeprom b/scripts/decode-syseeprom index e687f4fb68..bb542fd4a4 100755 --- a/scripts/decode-syseeprom +++ b/scripts/decode-syseeprom @@ -5,14 +5,14 @@ # This is the main script that handles eeprom encoding and decoding # try: + import glob + import imp import optparse - import warnings import os import sys - import imp - import glob - from sonic_device_util import get_machine_info - from sonic_device_util import get_platform_info + import warnings + + from sonic_py_common import device_info except ImportError as e: raise ImportError (str(e) + "- required module not found") @@ -26,7 +26,7 @@ def main(): raise RuntimeError("must be root to run") # Get platform name - platform = get_platform_info(get_machine_info()) + platform = device_info.get_platform() platform_path = '/'.join([PLATFORM_ROOT, platform]) diff --git a/scripts/lldpshow b/scripts/lldpshow index 3d53df13db..ba826a3f57 100755 --- a/scripts/lldpshow +++ b/scripts/lldpshow @@ -20,14 +20,17 @@ """ from __future__ import print_function -import subprocess + +import argparse import re +import subprocess import sys import xml.etree.ElementTree as ET -from tabulate import tabulate -import argparse -import sonic_device_util + +from sonic_py_common import device_info from swsssdk import ConfigDBConnector, SonicDBConfig +from tabulate import tabulate + BACKEND_ASIC_INTERFACE_NAME_PREFIX = 'Ethernet-BP' LLDP_INTERFACE_LIST_IN_HOST_NAMESPACE = '' @@ -35,6 +38,7 @@ LLDP_INSTANCE_IN_HOST_NAMESPACE = '' LLDP_DEFAULT_INTERFACE_LIST_IN_ASIC_NAMESPACE = '' SPACE_TOKEN = ' ' + class Lldpshow(object): def __init__(self): self.lldpraw = [] @@ -42,16 +46,17 @@ class Lldpshow(object): self.lldp_interface = [] self.lldp_instance = [] self.err = None - ### So far only find Router and Bridge two capabilities in lldpctl, so any other capacility types will be read as Other - ### if further capability type is supported like WLAN, can just add the tag definition here + # So far only find Router and Bridge two capabilities in lldpctl, so any other capacility types will be read as Other + # if further capability type is supported like WLAN, can just add the tag definition here self.ctags = {'Router': 'R', 'Bridge': 'B'} SonicDBConfig.load_sonic_global_db_config() # For multi-asic platforms we will get only front-panel interface to display - namespaces = sonic_device_util.get_all_namespaces() + namespaces = device_info.get_all_namespaces() per_asic_configdb = {} for instance_num, front_asic_namespaces in enumerate(namespaces['front_ns']): - per_asic_configdb[front_asic_namespaces] = ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespaces) + per_asic_configdb[front_asic_namespaces] = ConfigDBConnector( + use_unix_socket_path=True, namespace=front_asic_namespaces) per_asic_configdb[front_asic_namespaces].connect() # Initalize Interface list to be ''. We will do string append of the interfaces below. self.lldp_interface.append(LLDP_DEFAULT_INTERFACE_LIST_IN_ASIC_NAMESPACE) @@ -72,14 +77,15 @@ class Lldpshow(object): """ for lldp_instace_num in range(len(self.lldp_instance)): lldp_interface_list = lldp_port if lldp_port is not None else self.lldp_interface[lldp_instace_num] - # In detail mode we will pass interface list (only front ports) and get O/P as plain text + # In detail mode we will pass interface list (only front ports) and get O/P as plain text # and in table format we will get xml output - lldp_cmd = 'sudo docker exec -i lldp{} lldpctl '.format(self.lldp_instance[lldp_instace_num]) + ('-f xml' if not lldp_detail_info else lldp_interface_list) + lldp_cmd = 'sudo docker exec -i lldp{} lldpctl '.format(self.lldp_instance[lldp_instace_num]) + ( + '-f xml' if not lldp_detail_info else lldp_interface_list) p = subprocess.Popen(lldp_cmd, stdout=subprocess.PIPE, shell=True) (output, err) = p.communicate() ## Wait for end of command. Get return returncode ## returncode = p.wait() - ### if no error, get the lldpctl result + # if no error, get the lldpctl result if returncode == 0: # ignore the output if given port is not present if lldp_port is not None and lldp_port not in output: @@ -89,7 +95,7 @@ class Lldpshow(object): break else: self.err = err - + if self.err: self.lldpraw = [] @@ -140,10 +146,10 @@ class Lldpshow(object): def sort_sum(self, summary): """ Sort the summary information in the way that is expected(natural string).""" - alphanum_key = lambda key: [re.findall('[A-Za-z]+',key) + [int(port_num) for port_num in re.findall('\d+',key)]] + def alphanum_key(key): return [re.findall('[A-Za-z]+', key) + [int(port_num) + for port_num in re.findall('\d+', key)]] return sorted(summary, key=alphanum_key) - def display_sum(self, lldp_detail_info): """ print out summary result of lldp neighbors @@ -160,18 +166,20 @@ class Lldpshow(object): header = ['LocalPort', 'RemoteDevice', 'RemotePortID', 'Capability', 'RemotePortDescr'] sortedsum = self.sort_sum(self.lldpsum) for key in sortedsum: - lldpstatus.append([ key, self.lldpsum[key]['r_name'], self.lldpsum[key]['r_portid'], self.lldpsum[key]['capability'], self.lldpsum[key]['r_portname']]) + lldpstatus.append([key, self.lldpsum[key]['r_name'], self.lldpsum[key]['r_portid'], + self.lldpsum[key]['capability'], self.lldpsum[key]['r_portname']]) print (tabulate(lldpstatus, header)) print ('-'.rjust(50, '-')) print ('Total entries displayed: ', len(self.lldpsum)) elif self.err is not None: - print ('Error:',self.err) + print ('Error:', self.err) + def main(): - parser = argparse.ArgumentParser(description='Display the LLDP neighbors', - version='1.0.0', - formatter_class=argparse.RawTextHelpFormatter, - epilog=""" + parser = argparse.ArgumentParser(description='Display the LLDP neighbors', + version='1.0.0', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" Examples: lldpshow lldpshow -d @@ -198,5 +206,6 @@ def main(): print(e.message, file=sys.stderr) sys.exit(1) + if __name__ == "__main__": main() diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index cf36febb16..7805dd9a6b 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -7,16 +7,17 @@ ######################################## -import os -import sys +import argparse import json +import os import requests -import argparse -import syslog -import traceback import subprocess +import sys import time +import traceback import warnings + +from sonic_py_common import logger from swsssdk import ConfigDBConnector from netaddr import IPAddress, IPNetwork @@ -58,25 +59,9 @@ FERRET_NEIGHBOR_ADVERTISER_API_PREFIX = '/Ferret/NeighborAdvertiser/Slices/' # -# Syslog functions +# Global logger instance # - -def log_info(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - -def log_warning(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - -def log_error(msg): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() +log = logger.Logger() # @@ -110,13 +95,13 @@ def is_dip_in_device_vlan(ferret_dip): for vlan_interface in vlan_interface_query.iterkeys(): if not is_ip_prefix_in_key(vlan_interface): - log_info('{} does not have a subnet, skipping...'.format(vlan_interface)) + log.log_info('{} does not have a subnet, skipping...'.format(vlan_interface)) continue vlan_subnet = IPNetwork(vlan_interface[1]) if ferret_dip.version != vlan_subnet.version: - log_info('{} version (IPv{}) does not match provided DIP version (IPv{}), skipping...'.format(vlan_interface[0], vlan_subnet.version, ferret_dip.version)) + log.log_info('{} version (IPv{}) does not match provided DIP version (IPv{}), skipping...'.format(vlan_interface[0], vlan_subnet.version, ferret_dip.version)) continue if ferret_dip in vlan_subnet: @@ -263,7 +248,7 @@ def get_vlan_addresses(vlan_interface): elif keys[0] == 'link/ether': mac_addr = keys[1] except Exception: - log_error('failed to get %s addresses from o.s.' % vlan_interface) + log.log_error('failed to get %s addresses from o.s.' % vlan_interface) if not mac_addr: mac_addr = get_vlan_interface_mac_address(vlan_interface) @@ -300,7 +285,7 @@ def construct_neighbor_advertiser_slice(): vlan_id, vxlan_id, ipv4_addr, ipv6_addr, mac_addr = get_vlan_addresses(vlan_interface) if not mac_addr: - log_warning('Cannot find mac addr of vlan interface {}'.format(vlan_interface)) + log.log_warning('Cannot find mac addr of vlan interface {}'.format(vlan_interface)) continue ipv4_mappings = [] @@ -370,7 +355,7 @@ def post_neighbor_advertiser_slice(ferret_service_vip): try: response = wrapped_ferret_request(request_slice, https_endpoint) except Exception as e: - log_error("The request failed, vip: {}, error: {}".format(ferret_service_vip, e)) + log.log_error("The request failed, vip: {}, error: {}".format(ferret_service_vip, e)) return None neighbor_advertiser_configuration = json.loads(response.content) @@ -378,15 +363,15 @@ def post_neighbor_advertiser_slice(ferret_service_vip): # Retry the request if the provided DIP is in the device VLAN if is_dip_in_device_vlan(ferret_server_ipv4_addr): - log_info("Failed to set up neighbor advertiser slice, vip: {}, dip {} is in device VLAN (attempt {}/{})".format(ferret_service_vip, ferret_server_ipv4_addr, retry + 1, DEFAULT_FERRET_QUERY_RETRIES)) + log.log_info("Failed to set up neighbor advertiser slice, vip: {}, dip {} is in device VLAN (attempt {}/{})".format(ferret_service_vip, ferret_server_ipv4_addr, retry + 1, DEFAULT_FERRET_QUERY_RETRIES)) continue # If all the proceeding checks pass, return the provided DIP save_as_json(neighbor_advertiser_configuration, NEIGHBOR_ADVERTISER_RESPONSE_CONFIG_PATH) - log_info("Successfully set up neighbor advertiser slice, vip: {}, dip: {}".format(ferret_service_vip, ferret_server_ipv4_addr)) + log.log_info("Successfully set up neighbor advertiser slice, vip: {}, dip: {}".format(ferret_service_vip, ferret_server_ipv4_addr)) return ferret_server_ipv4_addr - log_error("Failed to set up neighbor advertiser slice, vip: {}, returned dips were in device VLAN".format(ferret_service_vip)) + log.log_error("Failed to set up neighbor advertiser slice, vip: {}, returned dips were in device VLAN".format(ferret_service_vip)) return None @@ -412,9 +397,9 @@ def find_mirror_table_name(): MIRROR_ACL_TABLE_PREFIX + MIRROR_ACL_TABLEV6_NAME == table: v6_table = table if not v4_table: - log_error(MIRROR_ACL_TABLE_NAME + " table does not exist") + log.log_error(MIRROR_ACL_TABLE_NAME + " table does not exist") if not v6_table: - log_error(MIRROR_ACL_TABLEV6_NAME + " table does not exist") + log.log_error(MIRROR_ACL_TABLEV6_NAME + " table does not exist") return (v4_table, v6_table) @@ -459,7 +444,7 @@ def set_mirror_tunnel(ferret_server_ip): # Ensure the mirror session is created before creating the rules time.sleep(DEFAULT_CONFIG_DB_WAIT_TIME) add_mirror_acl_rule() - log_info('Finish setting mirror tunnel; Ferret: {}'.format(ferret_server_ip)) + log.log_info('Finish setting mirror tunnel; Ferret: {}'.format(ferret_server_ip)) # @@ -485,7 +470,7 @@ def reset_mirror_tunnel(): # Ensure the rules are removed before removing the mirror session time.sleep(DEFAULT_CONFIG_DB_WAIT_TIME) remove_mirror_session() - log_info('Finish resetting mirror tunnel') + log.log_info('Finish resetting mirror tunnel') # @@ -514,7 +499,7 @@ def add_vxlan_tunnel_map(): def set_vxlan_tunnel(ferret_server_ip): add_vxlan_tunnel(ferret_server_ip) add_vxlan_tunnel_map() - log_info('Finish setting vxlan tunnel; Ferret: {}'.format(ferret_server_ip)) + log.log_info('Finish setting vxlan tunnel; Ferret: {}'.format(ferret_server_ip)) # @@ -533,7 +518,7 @@ def remove_vxlan_tunnel_map(): def reset_vxlan_tunnel(): remove_vxlan_tunnel_map() remove_vxlan_tunnel() - log_info('Finish resetting vxlan tunnel') + log.log_info('Finish resetting vxlan tunnel') # @@ -550,7 +535,7 @@ def main(): operation_mode = args.mode if operation_mode == 'set' and ferret_service_vips is None: - log_warning('ferret service vip is required in set mode') + log.log_warning('ferret service vip is required in set mode') sys.exit(1) connect_config_db() @@ -568,7 +553,7 @@ def main(): break if not set_success: - log_error('Failed to set up neighbor advertiser slice, tried all vips in {}'.format(ferret_service_vips)) + log.log_error('Failed to set up neighbor advertiser slice, tried all vips in {}'.format(ferret_service_vips)) sys.exit(1) if operation_mode == 'reset': @@ -582,6 +567,6 @@ if __name__ == '__main__': try: main() except Exception as e: - log_error('! [Failure] {} {}'.format(e, traceback.format_exc())) + log.log_error('! [Failure] {} {}'.format(e, traceback.format_exc())) sys.exit(1) diff --git a/scripts/port2alias b/scripts/port2alias index 6c8be6b093..426971479c 100755 --- a/scripts/port2alias +++ b/scripts/port2alias @@ -1,26 +1,11 @@ #!/usr/bin/env python import sys -import subprocess from cStringIO import StringIO -from portconfig import get_port_config +from portconfig import get_port_config +from sonic_py_common import device_info -def get_platform_hwsku(): - hwsku = None - platform = None - command = "show platform summary" - p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) - output = p.stdout.readlines() - for line in output: - tokens = line.split() - if not tokens: - continue - if tokens[0].lower() == 'hwsku:': - hwsku = tokens[1] - elif tokens[0].lower() == 'platform:': - platform = tokens[1] - return (platform, hwsku) def translate_line(line, ports): allowed_symbols = ['-', '_'] @@ -49,7 +34,7 @@ def translate_line(line, ports): return sb.getvalue() def main(): - (platform, hwsku) = get_platform_hwsku() + (platform, hwsku) = device_info.get_platform_and_hwsku() (ports, _) = get_port_config(hwsku, platform) for line in sys.stdin: sys.stdout.write(translate_line(line, ports)) diff --git a/setup.py b/setup.py index 7da80ef286..af5cf6e251 100644 --- a/setup.py +++ b/setup.py @@ -149,7 +149,8 @@ # therefore all dependencies will be assumed to also be available as .debs. # These unlistable dependencies are as follows: # - sonic-config-engine - # - swsssdk + # - sonic-py-common + # - sonic-py-swsssdk # - tabulate install_requires=[ 'click', diff --git a/sfputil/main.py b/sfputil/main.py index c70c4486b1..75a5f741eb 100644 --- a/sfputil/main.py +++ b/sfputil/main.py @@ -6,12 +6,12 @@ # try: - import sys + import imp import os - import subprocess + import sys + import click - import imp - import syslog + from sonic_py_common import device_info, logger from tabulate import tabulate except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -23,44 +23,14 @@ PLATFORM_SPECIFIC_MODULE_NAME = "sfputil" PLATFORM_SPECIFIC_CLASS_NAME = "SfpUtil" -PLATFORM_ROOT_PATH = '/usr/share/sonic/device' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' -HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' -PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' - # Global platform-specific sfputil class instance platform_sfputil = None PLATFORM_JSON = 'platform.json' PORT_CONFIG_INI = 'port_config.ini' -# ========================== Syslog wrappers ========================== - - -def log_info(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - if also_print_to_console: - print msg - - -def log_warning(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - if also_print_to_console: - print msg - - -def log_error(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() - - if also_print_to_console: - print msg +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) # ========================== Methods for printing ========================== @@ -292,69 +262,25 @@ def port_eeprom_data_raw_string_pretty(logical_port_name): # ==================== Methods for initialization ==================== -# Returns platform and HW SKU -def get_platform_and_hwsku(): - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError as e: - raise OSError("Cannot detect platform") - - return (platform, hwsku) - - -# Returns path to port config file -def get_path_to_port_config_file(): - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() - - # Load platform module from source - platform_path = "/".join([PLATFORM_ROOT_PATH, platform]) - hwsku_path = "/".join([platform_path, hwsku]) - - # First check for the presence of the new 'port_config.ini' file - port_config_file_path = "/".join([platform_path, PLATFORM_JSON]) - if not os.path.isfile(port_config_file_path): - # platform.json doesn't exist. Try loading the legacy 'port_config.ini' file - port_config_file_path = "/".join([hwsku_path, PORT_CONFIG_INI]) - - return port_config_file_path - # Loads platform specific sfputil module from source def load_platform_sfputil(): global platform_sfputil - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() - # Load platform module from source - platform_path = "/".join([PLATFORM_ROOT_PATH, platform]) + platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() try: - module_file = "/".join([platform_path, "plugins", PLATFORM_SPECIFIC_MODULE_NAME + ".py"]) + module_file = os.path.join(platform_path, "plugins", PLATFORM_SPECIFIC_MODULE_NAME + ".py") module = imp.load_source(PLATFORM_SPECIFIC_MODULE_NAME, module_file) except IOError as e: - log_error("Failed to load platform module '%s': %s" % (PLATFORM_SPECIFIC_MODULE_NAME, str(e)), True) + log.log_error("Failed to load platform module '%s': %s" % (PLATFORM_SPECIFIC_MODULE_NAME, str(e)), True) return -1 try: platform_sfputil_class = getattr(module, PLATFORM_SPECIFIC_CLASS_NAME) platform_sfputil = platform_sfputil_class() except AttributeError as e: - log_error("Failed to instantiate '%s' class: %s" % (PLATFORM_SPECIFIC_CLASS_NAME, str(e)), True) + log.log_error("Failed to instantiate '%s' class: %s" % (PLATFORM_SPECIFIC_CLASS_NAME, str(e)), True) return -2 return 0 @@ -379,10 +305,10 @@ def cli(): # Load port info try: - port_config_file_path = get_path_to_port_config_file() + port_config_file_path = device_info.get_path_to_port_config_file() platform_sfputil.read_porttab_mappings(port_config_file_path) except Exception as e: - log_error("Error reading port info (%s)" % str(e), True) + log.log_error("Error reading port info (%s)" % str(e), True) sys.exit(3) diff --git a/show/main.py b/show/main.py index 8193e1f611..0158706cb2 100755 --- a/show/main.py +++ b/show/main.py @@ -8,26 +8,23 @@ import subprocess import sys import ipaddress -from pkg_resources import parse_version from collections import OrderedDict import click from natsort import natsorted -from tabulate import tabulate - -import sonic_device_util +from pkg_resources import parse_version +from portconfig import get_child_ports +from sonic_py_common import device_info from swsssdk import ConfigDBConnector from swsssdk import SonicV2Connector -from portconfig import get_child_ports +from tabulate import tabulate from utilities_common.db import Db import mlnx -# Global Variable -PLATFORM_ROOT_PATH = "/usr/share/sonic/device" +# Global Variables PLATFORM_JSON = 'platform.json' HWSKU_JSON = 'hwsku.json' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' PORT_STR = "Ethernet" VLAN_SUB_INTERFACE_SEPARATOR = '.' @@ -824,41 +821,36 @@ def breakout(ctx): ctx.obj = {'db': config_db} try: - curBrkout_tbl = config_db.get_table('BREAKOUT_CFG') + cur_brkout_tbl = config_db.get_table('BREAKOUT_CFG') except Exception as e: click.echo("Breakout table is not present in Config DB") raise click.Abort() if ctx.invoked_subcommand is None: - - # Get HWSKU and Platform information - hw_info_dict = get_hw_info_dict() - platform = hw_info_dict['platform'] - hwsku = hw_info_dict['hwsku'] - # Get port capability from platform and hwsku related files - platformFile = "{}/{}/{}".format(PLATFORM_ROOT_PATH, platform, PLATFORM_JSON) - platformDict = readJsonFile(platformFile)['interfaces'] - hwskuDict = readJsonFile("{}/{}/{}/{}".format(PLATFORM_ROOT_PATH, platform, hwsku, HWSKU_JSON))['interfaces'] + platform_path, hwsku_path = device_info.get_paths_to_platform_and_hwsku_dirs() + platform_file = os.path.join(platform_path, PLATFORM_JSON) + platform_dict = readJsonFile(platform_file)['interfaces'] + hwsku_dict = readJsonFile(os.path.join(hwsku_path, HWSKU_JSON))['interfaces'] - if not platformDict or not hwskuDict: + if not platform_dict or not hwsku_dict: click.echo("Can not load port config from {} or {} file".format(PLATFORM_JSON, HWSKU_JSON)) raise click.Abort() - for port_name in platformDict.keys(): - curBrkout_mode = curBrkout_tbl[port_name]["brkout_mode"] + for port_name in platform_dict.keys(): + cur_brkout_mode = cur_brkout_tbl[port_name]["brkout_mode"] - # Update deafult breakout mode and current breakout mode to platformDict - platformDict[port_name].update(hwskuDict[port_name]) - platformDict[port_name]["Current Breakout Mode"] = curBrkout_mode + # Update deafult breakout mode and current breakout mode to platform_dict + platform_dict[port_name].update(hwsku_dict[port_name]) + platform_dict[port_name]["Current Breakout Mode"] = cur_brkout_mode # List all the child ports if present - child_portDict = get_child_ports(port_name, curBrkout_mode, platformFile) - if not child_portDict: + child_port_dict = get_child_ports(port_name, cur_brkout_mode, platformFile) + if not child_port_dict: click.echo("Cannot find ports from {} file ".format(PLATFORM_JSON)) raise click.Abort() - child_ports = natsorted(child_portDict.keys()) + child_ports = natsorted(child_port_dict.keys()) children, speeds = [], [] # Update portname and speed of child ports if present @@ -868,11 +860,11 @@ def breakout(ctx): speeds.append(str(int(speed)//1000)+'G') children.append(port) - platformDict[port_name]["child ports"] = ",".join(children) - platformDict[port_name]["child port speeds"] = ",".join(speeds) + platform_dict[port_name]["child ports"] = ",".join(children) + platform_dict[port_name]["child port speeds"] = ",".join(speeds) # Sorted keys by name in natural sort Order for human readability - parsed = OrderedDict((k, platformDict[k]) for k in natsorted(platformDict.keys())) + parsed = OrderedDict((k, platform_dict[k]) for k in natsorted(platform_dict.keys())) click.echo(json.dumps(parsed, indent=4)) # 'breakout current-mode' subcommand ("show interfaces breakout current-mode") @@ -888,20 +880,20 @@ def currrent_mode(ctx, interface): body = [] try: - curBrkout_tbl = config_db.get_table('BREAKOUT_CFG') + cur_brkout_tbl = config_db.get_table('BREAKOUT_CFG') except Exception as e: click.echo("Breakout table is not present in Config DB") raise click.Abort() # Show current Breakout Mode of user prompted interface if interface is not None: - body.append([interface, str(curBrkout_tbl[interface]['brkout_mode'])]) + body.append([interface, str(cur_brkout_tbl[interface]['brkout_mode'])]) click.echo(tabulate(body, header, tablefmt="grid")) return # Show current Breakout Mode for all interfaces - for name in natsorted(curBrkout_tbl.keys()): - body.append([name, str(curBrkout_tbl[name]['brkout_mode'])]) + for name in natsorted(cur_brkout_tbl.keys()): + body.append([name, str(cur_brkout_tbl[name]['brkout_mode'])]) click.echo(tabulate(body, header, tablefmt="grid")) @@ -1779,20 +1771,13 @@ def get_hw_info_dict(): This function is used to get the HW info helper function """ hw_info_dict = {} - machine_info = sonic_device_util.get_machine_info() - platform = sonic_device_util.get_platform_info(machine_info) - config_db = ConfigDBConnector() - config_db.connect() - data = config_db.get_table('DEVICE_METADATA') - try: - hwsku = data['localhost']['hwsku'] - except KeyError: - hwsku = "Unknown" - version_info = sonic_device_util.get_sonic_version_info() - asic_type = version_info['asic_type'] - hw_info_dict['platform'] = platform - hw_info_dict['hwsku'] = hwsku - hw_info_dict['asic_type'] = asic_type + + version_info = device_info.get_sonic_version_info() + + hw_info_dict['platform'] = device_info.get_platform() + hw_info_dict['hwsku'] = device_info.get_hwsku() + hw_info_dict['asic_type'] = version_info['asic_type'] + return hw_info_dict @cli.group(cls=AliasedGroup) @@ -1800,7 +1785,7 @@ def platform(): """Show platform-specific hardware info""" pass -version_info = sonic_device_util.get_sonic_version_info() +version_info = device_info.get_sonic_version_info() if (version_info and version_info.get('asic_type') == 'mellanox'): platform.add_command(mlnx.mlnx) @@ -1927,7 +1912,7 @@ def logging(process, lines, follow, verbose): @click.option("--verbose", is_flag=True, help="Enable verbose output") def version(verbose): """Show version information""" - version_info = sonic_device_util.get_sonic_version_info() + version_info = device_info.get_sonic_version_info() hw_info_dict = get_hw_info_dict() serial_number_cmd = "sudo decode-syseeprom -s" serial_number = subprocess.Popen(serial_number_cmd, shell=True, stdout=subprocess.PIPE) diff --git a/sonic_installer/main.py b/sonic_installer/main.py index cc8db16616..fc5984d031 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -8,21 +8,24 @@ import os import subprocess import sys -import syslog import time import urllib import click +from sonic_py_common import logger from swsssdk import SonicV2Connector from .bootloader import get_bootloader from .common import run_command, run_command_or_raise from .exception import SonicRuntimeException +SYSLOG_IDENTIFIER = "sonic-installer" # Global Config object _config = None +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) # This is from the aliases example: # https://github.com/pallets/click/blob/57c6f09611fc47ca80db0bd010f05998b3c0aa95/examples/aliases/aliases.py @@ -601,11 +604,11 @@ def upgrade_docker(container_name, url, cleanup_image, skip_check, tag, warm): count += 1 time.sleep(2) state = hget_warm_restart_table("STATE_DB", "WARM_RESTART_TABLE", warm_app_name, "state") - syslog.syslog("%s reached %s state" % (warm_app_name, state)) + log.log_notice("%s reached %s state" % (warm_app_name, state)) sys.stdout.write("]\n\r") if state != exp_state: click.echo("%s failed to reach %s state" % (warm_app_name, exp_state)) - syslog.syslog(syslog.LOG_ERR, "%s failed to reach %s state" % (warm_app_name, exp_state)) + log.log_error("%s failed to reach %s state" % (warm_app_name, exp_state)) else: exp_state = "" # this is cold upgrade diff --git a/ssdutil/main.py b/ssdutil/main.py index 7accba63a5..94939d3480 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -6,68 +6,20 @@ # try: - import sys - import os - import subprocess import argparse - import syslog + import os + import sys + + from sonic_py_common import device_info, logger except ImportError as e: raise ImportError("%s - required module not found" % str(e)) DEFAULT_DEVICE="/dev/sda" SYSLOG_IDENTIFIER = "ssdutil" -PLATFORM_ROOT_PATH = '/usr/share/sonic/device' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' -HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' -PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' - -def syslog_msg(severity, msg, stdout=False): - """ - Prints to syslog (and stdout if needed) message with specified severity - - Args: - severity : message severity - msg : message - stdout : also primt message to stdout - - """ - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(severity, msg) - syslog.closelog() +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) - if stdout: - print msg - -def get_platform_and_hwsku(): - """ - Retrieves current platform name and hwsku - Raises an OSError exception when failed to fetch - - Returns: - tuple of strings platform and hwsku - e.g. ("x86_64-mlnx_msn2700-r0", "ACS-MSN2700") - """ - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError as e: - raise OSError("Cannot detect platform") - - return (platform, hwsku) def import_ssd_api(diskdev): """ @@ -78,20 +30,18 @@ def import_ssd_api(diskdev): Instance of the class with SSD API implementation (vendor or generic) """ - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() - # try to load platform specific module try: - hwsku_plugins_path = "/".join([PLATFORM_ROOT_PATH, platform, "plugins"]) - sys.path.append(os.path.abspath(hwsku_plugins_path)) + platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() + platform_plugins_path = os.path.join(platform_path, "plugins") + sys.path.append(os.path.abspath(platform_plugins_path)) from ssd_util import SsdUtil except ImportError as e: - syslog_msg(syslog.LOG_WARNING, "Platform specific SsdUtil module not found. Falling down to the generic implementation") + log.log_warning("Platform specific SsdUtil module not found. Falling down to the generic implementation") try: from sonic_platform_base.sonic_ssd.ssd_generic import SsdUtil except ImportError as e: - syslog_msg(syslog.LOG_ERR, "Failed to import default SsdUtil. Error: {}".format(str(e)), True) + log.log_error("Failed to import default SsdUtil. Error: {}".format(str(e)), True) raise e return SsdUtil(diskdev) diff --git a/tests/conftest.py b/tests/conftest.py index 03dbfdb38e..fae575fdc7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,7 @@ import mock_tables.dbconnector -import sonic_device_util +from sonic_py_common import device_info from swsssdk import ConfigDBConnector test_path = os.path.dirname(os.path.abspath(__file__)) @@ -67,7 +67,7 @@ def setup_single_broacom_asic(): import config.main as config import show.main as show - sonic_device_util.get_num_npus = mock.MagicMock(return_value = 1) + device_info.get_num_npus = mock.MagicMock(return_value = 1) config._get_sonic_generated_services = \ mock.MagicMock(return_value = (generated_services_list, [])) diff --git a/utilities_common/util_base.py b/utilities_common/util_base.py index 64c17e6d52..74646e098c 100644 --- a/utilities_common/util_base.py +++ b/utilities_common/util_base.py @@ -2,136 +2,34 @@ try: import imp - import subprocess import os - import syslog + + from sonic_py_common import device_info except ImportError as e: raise ImportError (str(e) + " - required module not found") # # Constants ==================================================================== # -# Platform root directory -PLATFORM_ROOT_PATH = '/usr/share/sonic/device' -PLATFORM_ROOT_DOCKER = '/usr/share/sonic/platform' -SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' -HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' -PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' PDDF_FILE_PATH = '/usr/share/sonic/platform/pddf_support' -# Port config information -PORT_CONFIG = 'port_config.ini' -PORTMAP = 'portmap.ini' - - EEPROM_MODULE_NAME = 'eeprom' EEPROM_CLASS_NAME = 'board' -class UtilLogger(object): - def __init__(self, syslog_identifier): - self.syslog = syslog - self.syslog.openlog(ident=syslog_identifier, logoption=self.syslog.LOG_NDELAY, facility=self.syslog.LOG_DAEMON) - - def __del__(self): - self.syslog.closelog() - - def log_error(self, msg, print_to_console=False): - self.syslog.syslog(self.syslog.LOG_ERR, msg) - - if print_to_console: - print msg - - def log_warning(self, msg, print_to_console=False): - self.syslog.syslog(self.syslog.LOG_WARNING, msg) - - if print_to_console: - print msg - - def log_notice(self, msg, print_to_console=False): - self.syslog.syslog(self.syslog.LOG_NOTICE, msg) - - if print_to_console: - print msg - - def log_info(self, msg, print_to_console=False): - self.syslog.syslog(self.syslog.LOG_INFO, msg) - - if print_to_console: - print msg - - def log_debug(self, msg, print_to_console=False): - self.syslog.syslog(self.syslog.LOG_DEBUG, msg) - - if print_to_console: - print msg - class UtilHelper(object): def __init__(self): pass - # Returns platform and hwsku - def get_platform_and_hwsku(self): - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError as e: - raise OSError("Failed to detect platform: %s" % (str(e))) - - return (platform, hwsku) - - # Returns path to platform and hwsku - def get_path_to_platform_and_hwsku(self): - # Get platform and hwsku - (platform, hwsku) = self.get_platform_and_hwsku() - - # Load platform module from source - platform_path = '' - if len(platform) != 0: - platform_path = "/".join([PLATFORM_ROOT_PATH, platform]) - else: - platform_path = PLATFORM_ROOT_PATH_DOCKER - hwsku_path = "/".join([platform_path, hwsku]) - - return (platform_path, hwsku_path) - - # Returns path to port config file - def get_path_to_port_config_file(self): - # Get platform and hwsku path - (platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku() - - # First check for the presence of the new 'port_config.ini' file - port_config_file_path = "/".join([hwsku_path, PORT_CONFIG]) - if not os.path.isfile(port_config_file_path): - # port_config.ini doesn't exist. Try loading the legacy 'portmap.ini' file - port_config_file_path = "/".join([hwsku_path, PORTMAP]) - if not os.path.isfile(port_config_file_path): - raise IOError("Failed to detect port config file: %s" % (port_config_file_path)) - - return port_config_file_path - # Loads platform specific psuutil module from source def load_platform_util(self, module_name, class_name): platform_util = None # Get path to platform and hwsku - (platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku() + (platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs() try: - module_file = "/".join([platform_path, "plugins", module_name + ".py"]) + module_file = os.path.join(platform_path, "plugins", module_name + ".py") module = imp.load_source(module_name, module_file) except IOError as e: raise IOError("Failed to load platform module '%s': %s" % (module_name, str(e))) diff --git a/watchdogutil/main.py b/watchdogutil/main.py index df0f72c780..27d61ff768 100644 --- a/watchdogutil/main.py +++ b/watchdogutil/main.py @@ -6,11 +6,12 @@ # try: - import sys import os + import sys + import click - import syslog import sonic_platform + from sonic_py_common import logger except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -24,35 +25,8 @@ # Global platform-specific watchdog class instance platform_watchdog = None - -# ========================== Syslog wrappers ========================== - - -def log_info(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_INFO, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) - - -def log_warning(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_WARNING, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) - - -def log_error(msg, also_print_to_console=False): - syslog.openlog(SYSLOG_IDENTIFIER) - syslog.syslog(syslog.LOG_ERR, msg) - syslog.closelog() - - if also_print_to_console: - click.echo(msg) +# Global logger instance +log = logger.Logger(SYSLOG_IDENTIFIER) # ==================== Methods for initialization ==================== @@ -65,12 +39,12 @@ def load_platform_watchdog(): chassis = platform.get_chassis() if not chassis: - log_error("Failed to get chassis") + log.log_error("Failed to get chassis") return CHASSIS_LOAD_ERROR platform_watchdog = chassis.get_watchdog() if not platform_watchdog: - log_error("Failed to get watchdog module") + log.log_error("Failed to get watchdog module") return WATCHDOG_LOAD_ERROR return 0