From 34810df7ea484a327cdfb23c013677e48f11d60b Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Fri, 5 Oct 2018 18:53:55 -0700 Subject: [PATCH] [show] Eliminate need for writing template files for 'show platform summary' and 'show version' (#335) - Eliminate the need to write template files to disk in both "show platform summary" and "show version" - Now catching exception if we cannot retrieve HwSKU from ConfigDB and display "Unknown" as result --- show/main.py | 66 +++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/show/main.py b/show/main.py index 97e8eb4b4ea2..e1dbc035df57 100755 --- a/show/main.py +++ b/show/main.py @@ -1,16 +1,18 @@ #! /usr/bin/python -u -import click import errno -import getpass import json import os import re import subprocess import sys + +import click from click_default_group import DefaultGroup from natsort import natsorted from tabulate import tabulate + +import sonic_platform from swsssdk import ConfigDBConnector from swsssdk import SonicV2Connector @@ -878,25 +880,24 @@ def platform(): @platform.command() def summary(): """Show hardware platform information""" - username = getpass.getuser() + machine_info = sonic_platform.get_machine_info() + platform = sonic_platform.get_platform_info(machine_info) - PLATFORM_TEMPLATE_FILE = "/tmp/cli_platform_{0}.j2".format(username) - PLATFORM_TEMPLATE_CONTENTS = "Platform: {{ DEVICE_METADATA.localhost.platform }}\n" \ - "HwSKU: {{ DEVICE_METADATA.localhost.hwsku }}\n" \ - "ASIC: {{ asic_type }}" - - # Create a temporary Jinja2 template file to use with sonic-cfggen - f = open(PLATFORM_TEMPLATE_FILE, 'w') - f.write(PLATFORM_TEMPLATE_CONTENTS) - f.close() + config_db = ConfigDBConnector() + config_db.connect() + data = config_db.get_table('DEVICE_METADATA') - cmd = "sonic-cfggen -d -y /etc/sonic/sonic_version.yml -t {0}".format(PLATFORM_TEMPLATE_FILE) - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - click.echo(p.stdout.read()) + try: + hwsku = data['localhost']['hwsku'] + except KeyError: + hwsku = "Unknown" - # Clean up - os.remove(PLATFORM_TEMPLATE_FILE) + version_info = sonic_platform.get_sonic_version_info() + asic_type = version_info['asic_type'] + click.echo("Platform: {}".format(platform)) + click.echo("HwSKU: {}".format(hwsku)) + click.echo("ASIC: {}".format(asic_type)) # 'syseeprom' subcommand ("show platform syseeprom") @platform.command() @@ -955,33 +956,20 @@ def logging(process, lines, follow, verbose): @cli.command() def version(): """Show version information""" - username = getpass.getuser() - - VERSION_TEMPLATE_FILE = "/tmp/cli_version_{0}.j2".format(username) - VERSION_TEMPLATE_CONTENTS = "SONiC Software Version: SONiC.{{ build_version }}\n" \ - "Distribution: Debian {{ debian_version }}\n" \ - "Kernel: {{ kernel_version }}\n" \ - "Build commit: {{ commit_id }}\n" \ - "Build date: {{ build_date }}\n" \ - "Built by: {{ built_by }}" - - # Create a temporary Jinja2 template file to use with sonic-cfggen - f = open(VERSION_TEMPLATE_FILE, 'w') - f.write(VERSION_TEMPLATE_CONTENTS) - f.close() - - cmd = "sonic-cfggen -y /etc/sonic/sonic_version.yml -t {0}".format(VERSION_TEMPLATE_FILE) - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - click.echo(p.stdout.read()) + version_info = sonic_platform.get_sonic_version_info() + + click.echo("SONiC Software Version: SONiC.{}".format(version_info['build_version'])) + click.echo("Distribution: Debian {}".format(version_info['debian_version'])) + click.echo("Kernel: {}".format(version_info['kernel_version'])) + click.echo("Build commit: {}".format(version_info['commit_id'])) + click.echo("Build date: {}".format(version_info['build_date'])) + click.echo("Built by: {}".format(version_info['built_by'])) - click.echo("Docker images:") + click.echo("\nDocker images:") cmd = 'sudo docker images --format "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.Size}}"' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) click.echo(p.stdout.read()) - # Clean up - os.remove(VERSION_TEMPLATE_FILE) - # # 'environment' command ("show environment") #