Skip to content

Commit

Permalink
Remove pager from 'show,' 'config' and 'sonic_installer' commands (so…
Browse files Browse the repository at this point in the history
  • Loading branch information
jleveque authored Aug 18, 2017
1 parent 18bb5ed commit d1ded16
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
18 changes: 7 additions & 11 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
# Helper functions
#

def run_command(command, pager=False, display_cmd=False):
def run_command(command, display_cmd=False):
"""Run bash command and print output to stdout
"""
if display_cmd == True:
click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green'))

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
stdout = p.communicate()[0]
p.wait()
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
(out, err) = proc.communicate()

if len(stdout) > 0:
if pager is True:
click.echo_via_pager(p.stdout.read())
else:
click.echo(p.stdout.read())
if len(out) > 0:
click.echo(out)

if p.returncode != 0:
sys.exit(p.returncode)
if proc.returncode != 0:
sys.exit(proc.returncode)

def _is_neighbor_ipaddress(ipaddress):
"""Returns True if a neighbor has the IP address <ipaddress>, False if not
Expand Down
49 changes: 25 additions & 24 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def read_config(self, filename):


# This aliased group has been modified from click examples to inherit from DefaultGroup instead of click.Group.
# DefaultFroup is a superclass of click.Group which calls a default subcommand instead of showing
# DefaultGroup is a superclass of click.Group which calls a default subcommand instead of showing
# a help message if no subcommand is passed
class AliasedGroup(DefaultGroup):
"""This subclass of a DefaultGroup supports looking up aliases in a config
Expand Down Expand Up @@ -83,17 +83,16 @@ def get_command(self, ctx, cmd_name):
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))


def run_command(command, pager=False):
def run_command(command):
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
(out, err) = proc.communicate()

try:
if pager is True and sys.stdout.isatty():
click.echo_via_pager(p.stdout.read())
else:
click.echo(p.stdout.read())
click.echo(out)
except IOError as e:
# In our version of Click, click.echo() and click.echo_via_pager() do not properly handle
# In our version of Click (v6.6), click.echo() and click.echo_via_pager() do not properly handle
# SIGPIPE, and if a pipe is broken before all output is processed (e.g., pipe output to 'head'),
# it will result in a stack trace. This is apparently fixed upstream, but for now, we silently
# ignore SIGPIPE here.
Expand All @@ -102,6 +101,8 @@ def run_command(command, pager=False):
else:
raise

if proc.returncode != 0:
sys.exit(proc.returncode)

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '?'])

Expand Down Expand Up @@ -158,7 +159,7 @@ def summary(interfacename):
run_command(command)
else:
command = cmd_ifconfig
run_command(command, pager=True)
run_command(command)

# 'counters' subcommand
@interfaces.command()
Expand All @@ -178,13 +179,13 @@ def counters(period, printall, clear):
if period is not None:
cmd += " -p {}".format(period)

run_command(cmd, pager=True)
run_command(cmd)

# 'portchannel' subcommand
@interfaces.command()
def portchannel():
"""Show PortChannel information"""
run_command("teamshow", pager=True)
run_command("teamshow")

# 'sfp' subcommand
@interfaces.command()
Expand All @@ -197,7 +198,7 @@ def sfp(interfacename):
if interfacename is not None:
cmd += " -p {}".format(interfacename)

run_command(cmd, pager=True)
run_command(cmd)

#
# 'lldp' group ####
Expand All @@ -217,13 +218,13 @@ def neighbors(interfacename):
command = "sudo lldpctl {}".format(interfacename)
run_command(command)
else:
run_command("sudo lldpctl", pager=True)
run_command("sudo lldpctl")

# 'tables' subcommand ####
@lldp.command()
def table():
"""Show LLDP neighbors in tabular format"""
run_command("sudo lldpshow", pager=True)
run_command("sudo lldpshow")

#
# 'bgp' group ####
Expand All @@ -249,7 +250,7 @@ def neighbor(ipaddress):
command = 'sudo vtysh -c "show ip bgp neighbor {} "'.format(ipaddress)
run_command(command)
else:
run_command('sudo vtysh -c "show ip bgp neighbor"', pager=True)
run_command('sudo vtysh -c "show ip bgp neighbor"')

# 'summary' subcommand ####
@bgp.command()
Expand Down Expand Up @@ -317,7 +318,7 @@ def logging(process, lines, follow):
if lines is not None:
command += " | tail -{}".format(lines)

run_command(command, pager=True)
run_command(command)


#
Expand Down Expand Up @@ -361,7 +362,7 @@ def version():
@cli.command()
def environment():
"""Show environmentals (voltages, fans, temps)"""
run_command('sudo sensors', pager=True)
run_command('sudo sensors')


#
Expand All @@ -378,7 +379,7 @@ def processes():
def cpu():
"""Show processes CPU info"""
# Run top batch mode to prevent unexpected newline after each newline
run_command('top -bn 1', pager=True)
run_command('top -bn 1')


#
Expand Down Expand Up @@ -415,7 +416,7 @@ def runningconfiguration():
@runningconfiguration.command()
def bgp():
"""Show BGP running configuration"""
run_command('sudo vtysh -c "show running-config"', pager=True)
run_command('sudo vtysh -c "show running-config"')


# 'interfaces' subcommand
Expand All @@ -427,22 +428,22 @@ def interfaces(interfacename):
command = "cat /etc/network/interfaces | grep {} -A 4".format(interfacename)
run_command(command)
else:
run_command('cat /etc/network/interfaces', pager=True)
run_command('cat /etc/network/interfaces')


# 'snmp' subcommand
@runningconfiguration.command()
def snmp():
"""Show SNMP running configuration"""
command = 'sudo docker exec -it snmp cat /etc/snmp/snmpd.conf'
run_command(command, pager=True)
run_command(command)


# 'ntp' subcommand
@runningconfiguration.command()
def ntp():
"""Show NTP running configuration"""
run_command('cat /etc/ntp.conf', pager=True)
run_command('cat /etc/ntp.conf')


# 'startupconfiguration' group ###
Expand Down Expand Up @@ -474,7 +475,7 @@ def arp(ipaddress):
command = '{} {}'.format(cmd, ipaddress)
run_command(command)
else:
run_command(cmd, pager=True)
run_command(cmd)

# Add 'arp' command to both the root 'cli' group and the 'ip' subgroup
cli.add_command(arp)
Expand All @@ -493,7 +494,7 @@ def route(ipaddress):
command = 'sudo vtysh -c "show ip route {}"'.format(ipaddress)
run_command(command)
else:
run_command('sudo vtysh -c "show ip route"', pager=True)
run_command('sudo vtysh -c "show ip route"')

# Add 'route' command to both the root 'cli' group and the 'ip' subgroup
cli.add_command(route)
Expand Down
18 changes: 9 additions & 9 deletions sonic_installer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ def get_image_type():
return IMAGE_TYPE_ONIE

# Run bash command and print output to stdout
def run_command(command, pager=False):
def run_command(command):
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
if pager is True:
click.echo_via_pager(p.stdout.read())
else:
click.echo(p.stdout.read())
p.wait()
if p.returncode != 0:
sys.exit(p.returncode)

proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
(out, err) = proc.communicate()

click.echo(out)

if proc.returncode != 0:
sys.exit(proc.returncode)

# Returns list of installed images
def get_installed_images():
Expand Down

0 comments on commit d1ded16

Please sign in to comment.