[PATCH] [Kimchi 0/2] *** DEPENDS ON GINGERBASE NETINFO PATCH *** migrating to Gingerbase netinfo

From: Daniel Henrique Barboza <dhbarboza82@gmail.com> This patch set adapts Kimchi source code to uses the netinfo module from Gingerbase. Daniel Henrique Barboza (2): Migration to Gingerbase netinfo: import changes Migration to Gingerbase netinfo: removing netinfo.py model/interfaces.py | 2 +- model/networks.py | 6 +- netinfo.py | 265 ---------------------------------------------------- tests/test_model.py | 2 +- 4 files changed, 5 insertions(+), 270 deletions(-) delete mode 100644 netinfo.py -- 2.5.5

From: Daniel Henrique Barboza <dhbarboza82@gmail.com> This patch migrates Kimchi to use the new netinfo module from Gingerbase. No API changes were done at this time. Signed-off-by: Daniel Henrique Barboza <dhbarboza82@gmail.com> --- model/interfaces.py | 2 +- model/networks.py | 6 +++--- tests/test_model.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/model/interfaces.py b/model/interfaces.py index 01fbb93..2d74fd4 100644 --- a/model/interfaces.py +++ b/model/interfaces.py @@ -19,7 +19,7 @@ from wok.exception import NotFoundError -from wok.plugins.kimchi import netinfo +from wok.plugins.gingerbase import netinfo from wok.plugins.kimchi.model.networks import NetworksModel diff --git a/model/networks.py b/model/networks.py index 08b2690..9ae2ea8 100644 --- a/model/networks.py +++ b/model/networks.py @@ -30,12 +30,12 @@ from wok.exception import MissingParameter, NotFoundError, OperationFailed from wok.utils import run_command, wok_log from wok.xmlutils.utils import xpath_get_text -from wok.plugins.kimchi import netinfo +from wok.plugins.gingerbase import netinfo +from wok.plugins.gingerbase.netinfo import get_vlan_device, is_bridge, is_vlan +from wok.plugins.gingerbase.netinfo import ports from wok.plugins.kimchi import network as knetwork from wok.plugins.kimchi.config import kimchiPaths from wok.plugins.kimchi.model.config import CapabilitiesModel -from wok.plugins.kimchi.netinfo import get_vlan_device, is_bridge, is_vlan -from wok.plugins.kimchi.netinfo import ports from wok.plugins.kimchi.osinfo import defaults as tmpl_defaults from wok.plugins.kimchi.xmlutils.interface import get_iface_xml from wok.plugins.kimchi.xmlutils.network import create_linux_bridge_xml diff --git a/tests/test_model.py b/tests/test_model.py index 4af3b2c..c89c269 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -37,7 +37,7 @@ from wok.rollbackcontext import RollbackContext from wok.utils import add_task, get_task_id from wok.xmlutils.utils import xpath_get_text -from wok.plugins.kimchi import netinfo +from wok.plugins.gingerbase import netinfo from wok.plugins.kimchi import osinfo from wok.plugins.kimchi.config import kimchiPaths as paths from wok.plugins.kimchi.model import model -- 2.5.5

From: Daniel Henrique Barboza <dhbarboza82@gmail.com> This patch removes netinfo.py from Kimchi code now that Kimchi uses netinfo.py module from Gingerbase. Signed-off-by: Daniel Henrique Barboza <dhbarboza82@gmail.com> --- netinfo.py | 265 ------------------------------------------------------------- 1 file changed, 265 deletions(-) delete mode 100644 netinfo.py diff --git a/netinfo.py b/netinfo.py deleted file mode 100644 index 445d36c..0000000 --- a/netinfo.py +++ /dev/null @@ -1,265 +0,0 @@ -# Project Kimchi -# -# Copyright IBM Corp, 2015-2016 -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -# - -import ethtool -import glob -import os - -from distutils.spawn import find_executable -from wok.utils import run_command -from wok.utils import wok_log - -NET_PATH = '/sys/class/net' -NIC_PATH = '/sys/class/net/*/device' -BRIDGE_PATH = '/sys/class/net/*/bridge' -BONDING_PATH = '/sys/class/net/*/bonding' -WLAN_PATH = '/sys/class/net/*/wireless' -NET_BRPORT = '/sys/class/net/%s/brport' -NET_MASTER = '/sys/class/net/%s/master' -NET_STATE = '/sys/class/net/%s/carrier' -PROC_NET_VLAN = '/proc/net/vlan/' -BONDING_SLAVES = '/sys/class/net/%s/bonding/slaves' -BRIDGE_PORTS = '/sys/class/net/%s/brif' - - -def wlans(): - return [b.split('/')[-2] for b in glob.glob(WLAN_PATH)] - - -def is_wlan(iface): - return iface in wlans() - - -# FIXME if we do not want to list usb nic -def nics(): - return list(set([b.split('/')[-2] for b in glob.glob(NIC_PATH)]) - - set(wlans())) - - -def is_nic(iface): - return iface in nics() - - -def bondings(): - return [b.split('/')[-2] for b in glob.glob(BONDING_PATH)] - - -def is_bonding(iface): - return iface in bondings() - - -def vlans(): - return list(set([b.split('/')[-1] - for b in glob.glob(NET_PATH + '/*')]) & - set([b.split('/')[-1] - for b in glob.glob(PROC_NET_VLAN + '*')])) - - -def is_vlan(iface): - return iface in vlans() - - -def bridges(): - return list(set([b.split('/')[-2] for b in glob.glob(BRIDGE_PATH)] + - ovs_bridges())) - - -def is_bridge(iface): - return iface in bridges() - - -# In some distributions, like Fedora, the files bridge and brif are not created -# under /sys/class/net/<ovsbridge> for OVS bridges. These specific functions -# allows one to differentiate OVS bridges from other types of bridges. -def ovs_bridges(): - ovs_cmd = find_executable("ovs-vsctl") - - # openvswitch not installed: there is no OVS bridge configured - if ovs_cmd is None: - return [] - - out, error, rc = run_command([ovs_cmd, 'list-br']) - if rc != 0: - wok_log.info("Error listing OVS bridges") - return [] - - return [x.strip() for x in out.rstrip('\n').split('\n') if x.strip()] - - -def is_ovs_bridge(iface): - return iface in ovs_bridges() - - -def ovs_bridge_ports(ovsbr): - ovs_cmd = find_executable("ovs-vsctl") - - # openvswitch not installed: there is no OVS bridge configured - if ovs_cmd is None: - return [] - - out, error, rc = run_command([ovs_cmd, 'list-ports', ovsbr]) - if rc != 0: - wok_log.info("Error listing OVS bridge ports for %s" % str(ovsbr)) - return [] - - return [x.strip() for x in out.rstrip('\n').split('\n') if x.strip()] - - -def all_interfaces(): - return [d.rsplit("/", 1)[-1] for d in glob.glob(NET_PATH + '/*')] - - -def slaves(bonding): - with open(BONDING_SLAVES % bonding) as bonding_file: - res = bonding_file.readline().split() - return res - - -def ports(bridge): - if bridge in ovs_bridges(): - return ovs_bridge_ports(bridge) - - return os.listdir(BRIDGE_PORTS % bridge) - - -def is_brport(nic): - ovs_brports = [] - - for ovsbr in ovs_bridges(): - ovs_brports += ovs_bridge_ports(ovsbr) - - return os.path.exists(NET_BRPORT % nic) or nic in ovs_brports - - -def is_bondlave(nic): - return os.path.exists(NET_MASTER % nic) - - -def operstate(dev): - link_status = link_detected(dev) - return "down" if link_status == "n/a" else "up" - - -def link_detected(dev): - # try to read interface carrier (link) status - try: - with open(NET_STATE % dev) as dev_file: - carrier = dev_file.readline().strip() - # when IOError is raised, interface is down - except IOError: - return "n/a" - - # if value is 1, interface up with cable connected - # 0 corresponds to interface up with cable disconnected - return "yes" if carrier == '1' else "no" - - -def get_vlan_device(vlan): - """ Return the device of the given VLAN. """ - dev = None - - if os.path.exists(PROC_NET_VLAN + vlan): - with open(PROC_NET_VLAN + vlan) as vlan_file: - for line in vlan_file: - if "Device:" in line: - dummy, dev = line.split() - break - return dev - - -def get_bridge_port_device(bridge): - """Return the nics list that belongs to bridge.""" - # br --- v --- bond --- nic1 - if bridge not in bridges(): - raise ValueError('unknown bridge %s' % bridge) - nics = [] - for port in ports(bridge): - if port in vlans(): - device = get_vlan_device(port) - if device in bondings(): - nics.extend(slaves(device)) - else: - nics.append(device) - if port in bondings(): - nics.extend(slaves(port)) - else: - nics.append(port) - return nics - - -def aggregated_bridges(): - return [bridge for bridge in bridges() if - (set(get_bridge_port_device(bridge)) & set(nics()))] - - -def bare_nics(): - "The nic is not a port of a bridge or a slave of bond." - return [nic for nic in nics() if not (is_brport(nic) or is_bondlave(nic))] - - -def is_bare_nic(iface): - return iface in bare_nics() - - -# The nic will not be exposed when it is a port of a bridge or -# a slave of bond. -# The bridge will not be exposed when all it's port are tap. -def all_favored_interfaces(): - return aggregated_bridges() + bare_nics() + bondings() - - -def get_interface_type(iface): - # FIXME if we want to get more device type - # just support nic, bridge, bondings and vlan, for we just - # want to expose this 4 kinds of interface - try: - if is_nic(iface): - return "nic" - if is_bonding(iface): - return "bonding" - if is_bridge(iface): - return "bridge" - if is_vlan(iface): - return "vlan" - return 'unknown' - except IOError: - return 'unknown' - - -def get_interface_info(iface): - if iface not in ethtool.get_devices(): - raise ValueError('unknown interface: %s' % iface) - - ipaddr = '' - netmask = '' - try: - ipaddr = ethtool.get_ipaddr(iface) - netmask = ethtool.get_netmask(iface) - except IOError: - pass - - iface_link_detected = link_detected(iface) - iface_status = 'active' if iface_link_detected != "n/a" else "inactive" - - return {'name': iface, - 'type': get_interface_type(iface), - 'status': iface_status, - 'link_detected': iface_link_detected, - 'ipaddr': ipaddr, - 'netmask': netmask} -- 2.5.5

Reviewed-By: Lucio Correia <luciojhc@linux.vnet.ibm.com> On 04-05-2016 14:42, dhbarboza82@gmail.com wrote:
From: Daniel Henrique Barboza <dhbarboza82@gmail.com>
This patch set adapts Kimchi source code to uses the netinfo module from Gingerbase.
Daniel Henrique Barboza (2): Migration to Gingerbase netinfo: import changes Migration to Gingerbase netinfo: removing netinfo.py
model/interfaces.py | 2 +- model/networks.py | 6 +- netinfo.py | 265 ---------------------------------------------------- tests/test_model.py | 2 +- 4 files changed, 5 insertions(+), 270 deletions(-) delete mode 100644 netinfo.py
-- Lucio Correia Software Engineer IBM LTC Brazil
participants (3)
-
Aline Manera
-
dhbarboza82@gmail.com
-
Lucio Correia