From 531eabfbaf56ffa3ec777b208a9a578e4c79ce41 Mon Sep 17 00:00:00 2001 From: Nikos Triantafillis Date: Mon, 6 Nov 2017 18:05:28 -0800 Subject: [PATCH 1/5] config reload needs to restart services --- config/main.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/config/main.py b/config/main.py index ed2f131298..57ed0a0e2f 100644 --- a/config/main.py +++ b/config/main.py @@ -131,6 +131,21 @@ def reload(filename): command = "{} -j {} --write-to-db".format(SONIC_CFGGEN_PATH, filename) run_command(command, display_cmd=True) client.set(config_db.INIT_INDICATOR, True) + command = "{} -j {} -v \"DEVICE_METADATA['localhost']['hostname']\"".format(SONIC_CFGGEN_PATH, filename) + p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + p.wait() + hostname = p.communicate()[0].strip() + _change_hostname(hostname) + run_command("service interfaces-config restart", display_cmd=True) + run_command("service ntp-config restart", display_cmd=True) + run_command("service rsyslog-config restart", display_cmd=True) + run_command("service swss restart", display_cmd=True) + run_command("service bgp restart", display_cmd=True) + run_command("service teamd restart", display_cmd=True) + run_command("service pmon restart", display_cmd=True) + run_command("service lldp restart", display_cmd=True) + run_command("service snmp restart", display_cmd=True) + run_command("service dhcp_relay restart", display_cmd=True) @cli.command() @click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, From caca60ea8b0950c9838962420d7fd9ee2b0a92fc Mon Sep 17 00:00:00 2001 From: Nikos Triantafillis Date: Wed, 8 Nov 2017 15:13:45 -0800 Subject: [PATCH 2/5] config reload needs to restart services --- config/main.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/config/main.py b/config/main.py index 57ed0a0e2f..ace8643b99 100644 --- a/config/main.py +++ b/config/main.py @@ -93,6 +93,18 @@ def _abort_if_false(ctx, param, value): if not value: ctx.abort() +def _restart_services(): + run_command("service interfaces-config restart", display_cmd=True) + run_command("service ntp-config restart", display_cmd=True) + run_command("service rsyslog-config restart", display_cmd=True) + run_command("service swss restart", display_cmd=True) + run_command("service bgp restart", display_cmd=True) + run_command("service teamd restart", display_cmd=True) + run_command("service pmon restart", display_cmd=True) + run_command("service lldp restart", display_cmd=True) + run_command("service snmp restart", display_cmd=True) + run_command("service dhcp_relay restart", display_cmd=True) + # This is our main entrypoint - the main 'config' command @click.group() def cli(): @@ -136,16 +148,7 @@ def reload(filename): p.wait() hostname = p.communicate()[0].strip() _change_hostname(hostname) - run_command("service interfaces-config restart", display_cmd=True) - run_command("service ntp-config restart", display_cmd=True) - run_command("service rsyslog-config restart", display_cmd=True) - run_command("service swss restart", display_cmd=True) - run_command("service bgp restart", display_cmd=True) - run_command("service teamd restart", display_cmd=True) - run_command("service pmon restart", display_cmd=True) - run_command("service lldp restart", display_cmd=True) - run_command("service snmp restart", display_cmd=True) - run_command("service dhcp_relay restart", display_cmd=True) + _restart_services() @cli.command() @click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, @@ -192,16 +195,7 @@ def load_minigraph(): hostname = p.communicate()[0].strip() _change_hostname(hostname) #FIXME: After config DB daemon is implemented, we'll no longer need to restart every service. - run_command("service interfaces-config restart", display_cmd=True) - run_command("service ntp-config restart", display_cmd=True) - run_command("service rsyslog-config restart", display_cmd=True) - run_command("service swss restart", display_cmd=True) - run_command("service bgp restart", display_cmd=True) - run_command("service teamd restart", display_cmd=True) - run_command("service pmon restart", display_cmd=True) - run_command("service lldp restart", display_cmd=True) - run_command("service snmp restart", display_cmd=True) - run_command("service dhcp_relay restart", display_cmd=True) + _restart_services() print "Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`." # # 'bgp' group From 504ca6b7295ec0d9f35f2f69c8f07774fe354f39 Mon Sep 17 00:00:00 2001 From: Nikos Triantafillis Date: Fri, 26 Jan 2018 17:44:45 -0800 Subject: [PATCH 3/5] show [ip|ipv6] interfaces cmds --- setup.py | 3 +- show/main.py | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 61e42fd438..6cbc107f94 100644 --- a/setup.py +++ b/setup.py @@ -73,7 +73,8 @@ def get_test_suite(): 'click-default-group', 'click', 'natsort', - 'tabulate' + 'tabulate', + 'netifaces==0.10.6', ], classifiers=[ 'Development Status :: 3 - Alpha', diff --git a/show/main.py b/show/main.py index a0fdfd3cf8..6ec926dbe0 100755 --- a/show/main.py +++ b/show/main.py @@ -7,6 +7,8 @@ import os import subprocess import sys +import netifaces +from netaddr import * from click_default_group import DefaultGroup from natsort import natsorted from tabulate import tabulate @@ -349,12 +351,95 @@ def mac(vlan, port): # # This group houses IP (i.e., IPv4) commands and subgroups -@cli.group() +@cli.group(cls=AliasedGroup, default_if_no_args=False) def ip(): """Show IP (IPv4) commands""" pass +# +# get_if_admin_state +# +# Given an interface name, return its admin state reported by the kernel. +# +def get_if_admin_state(iface): + admin_file = "/sys/class/net/{0}/flags" + + try: + state_file = open(admin_file.format(iface), "r") + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "error" + + content = state_file.readline().rstrip() + flags = int(content, 16) + + if flags & 0x1: + return "up" + else: + return "down" + + +# +# get_if_oper_state +# +# Given an interface name, return its oper state reported by the kernel. +# +def get_if_oper_state(iface): + oper_file = "/sys/class/net/{0}/carrier" + + try: + state_file = open(oper_file.format(iface), "r") + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "error" + + oper_state = state_file.readline().rstrip() + if oper_state == "1": + return "up" + else: + return "down" + + +# +# 'show ip interfaces' command +# +# Display all interfaces with an IPv4 address and their admin/oper states. +# Addresses from all scopes are included. Interfaces with no addresses are +# excluded. +# +@ip.command() +def interfaces(): + """Show interfaces IPv4 address""" + header = ['Interface', 'IPv4 address/mask', 'Admin/Oper'] + data = [] + + interfaces = natsorted(netifaces.interfaces()) + + for iface in interfaces: + + ipaddresses = netifaces.ifaddresses(iface) + + if netifaces.AF_INET in ipaddresses: + + ifaddresses = [] + for ipaddr in ipaddresses[netifaces.AF_INET]: + netmask = IPAddress(ipaddr['netmask']).netmask_bits() + ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)]) + + if len(ifaddresses) > 0: + admin = get_if_admin_state(iface) + if admin == "up": + oper = get_if_oper_state(iface) + else: + oper = "down" + data.append([iface, ifaddresses[0][1], admin + "/" + oper]) + for ifaddr in ifaddresses[1:]: + data.append(["", ifaddr[1], ""]) + + print tabulate(data, header, tablefmt="simple", stralign='left', missingval="") + + # # 'route' subcommand ("show ip route") # @@ -382,12 +467,51 @@ def protocol(): # # This group houses IPv6-related commands and subgroups -@cli.group() +@cli.group(cls=AliasedGroup, default_if_no_args=False) def ipv6(): """Show IPv6 commands""" pass +# +# 'show ipv6 interfaces' command +# +# Display all interfaces with an IPv6 address and their admin/oper states. +# Addresses from all scopes are included. Interfaces with no addresses are +# excluded. +# +@ipv6.command() +def interfaces(): + """Show interfaces IPv6 address""" + header = ['Interface', 'IPv6 address/mask', 'Admin/Oper'] + data = [] + + interfaces = natsorted(netifaces.interfaces()) + + for iface in interfaces: + + ipaddresses = netifaces.ifaddresses(iface) + + if netifaces.AF_INET6 in ipaddresses: + + ifaddresses = [] + for ipaddr in ipaddresses[netifaces.AF_INET6]: + netmask = ipaddr['netmask'].split('/', 1)[-1] + ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)]) + + if len(ifaddresses) > 0: + admin = get_if_admin_state(iface) + if admin == "up": + oper = get_if_oper_state(iface) + else: + oper = "down" + data.append([iface, ifaddresses[0][1], admin + "/" + oper]) + for ifaddr in ifaddresses[1:]: + data.append(["", ifaddr[1], ""]) + + print tabulate(data, header, tablefmt="simple", stralign='left', missingval="") + + # # 'route' subcommand ("show ipv6 route") # From 0c525873333c6a4e68fe636dde167dc5c09fa9a1 Mon Sep 17 00:00:00 2001 From: Nikos Triantafillis Date: Fri, 26 Jan 2018 18:14:17 -0800 Subject: [PATCH 4/5] show [ip|ipv6] interfaces --- show/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/show/main.py b/show/main.py index 6ec926dbe0..13cec80af7 100755 --- a/show/main.py +++ b/show/main.py @@ -496,7 +496,11 @@ def interfaces(): ifaddresses = [] for ipaddr in ipaddresses[netifaces.AF_INET6]: - netmask = ipaddr['netmask'].split('/', 1)[-1] + mask = ipaddr['netmask'] + if isinstance(mask, str): + netmask = IPAddress(mask).netmask_bits() + elif isinstance(mask, unicode): + netmask = mask.split('/', 1)[-1] ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)]) if len(ifaddresses) > 0: From bd6700d45f789cb1ae864246d44b1b77b20a3bab Mon Sep 17 00:00:00 2001 From: Nikos Triantafillis Date: Fri, 26 Jan 2018 18:14:17 -0800 Subject: [PATCH 5/5] show [ip|ipv6] interfaces cmds --- setup.py | 2 +- show/main.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 6cbc107f94..1c1abb76b6 100644 --- a/setup.py +++ b/setup.py @@ -74,7 +74,7 @@ def get_test_suite(): 'click', 'natsort', 'tabulate', - 'netifaces==0.10.6', + 'netifaces', ], classifiers=[ 'Development Status :: 3 - Alpha', diff --git a/show/main.py b/show/main.py index 6ec926dbe0..13cec80af7 100755 --- a/show/main.py +++ b/show/main.py @@ -496,7 +496,11 @@ def interfaces(): ifaddresses = [] for ipaddr in ipaddresses[netifaces.AF_INET6]: - netmask = ipaddr['netmask'].split('/', 1)[-1] + mask = ipaddr['netmask'] + if isinstance(mask, str): + netmask = IPAddress(mask).netmask_bits() + elif isinstance(mask, unicode): + netmask = mask.split('/', 1)[-1] ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)]) if len(ifaddresses) > 0: