Skip to content

Commit

Permalink
[show] Eliminate need for writing template files for 'show platform s…
Browse files Browse the repository at this point in the history
…ummary' 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
  • Loading branch information
jleveque authored Oct 6, 2018
1 parent 4258287 commit 34810df
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions show/main.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
#
Expand Down

0 comments on commit 34810df

Please sign in to comment.