Patch-Source: https://github.com/kimchi-project/kimchi/commit/5e0645184a3b3e3dac1bc40792c09bd81b9892cc From: Aline Manera Date: Sat, 28 Mar 2020 15:27:34 -0400 Subject: [PATCH] Fix #1298: Use ipaddress instead of deprecated ipaddr Signed-off-by: Aline Manera --- dependencies.yaml | 1 - docs/README.md | 2 +- model/networks.py | 18 ++++++++---------- network.py | 18 +++++++++--------- requirements-FEDORA.txt | 1 - requirements-UBUNTU.txt | 1 - tests/test_networkxml.py | 8 +++++--- utils.py | 2 +- xmlutils/network.py | 7 ++++--- 9 files changed, 28 insertions(+), 30 deletions(-) diff --git a/model/networks.py b/model/networks.py index a38498166..9d4600355 100644 --- a/model/networks.py +++ b/model/networks.py @@ -17,9 +17,9 @@ # 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 copy +import ipaddress import time -import ipaddr import libvirt from libvirt import VIR_INTERFACE_XML_INACTIVE from wok.exception import InvalidOperation @@ -130,7 +130,7 @@ def _get_available_address(self, addr_pools=None): network = NetworkModel.get_network(self.conn.get(), net_name) xml = network.XMLDesc(0) subnet = NetworkModel.get_network_from_xml(xml)['subnet'] - subnet and invalid_addrs.append(ipaddr.IPNetwork(subnet)) + subnet and invalid_addrs.append(ipaddress.IPv4Network(subnet, False)) addr_pools = addr_pools if addr_pools else netinfo.PrivateNets return netinfo.get_one_free_network(invalid_addrs, addr_pools) @@ -143,17 +143,15 @@ def _set_network_subnet(self, params): raise OperationFailed('KCHNET0009E', {'name': params['name']}) try: - ip = ipaddr.IPNetwork(netaddr) + ip = ipaddress.IPv4Network(netaddr, False) except ValueError: raise InvalidParameter( 'KCHNET0003E', {'subnet': netaddr, 'network': params['name']} ) - if ip.ip == ip.network: - ip.ip = ip.ip + 1 - - dhcp_start = str(ip.network + int(ip.numhosts / 2)) - dhcp_end = str(ip.network + int(ip.numhosts - 3)) + ip.network_address = ip.network_address + 1 + dhcp_start = str(ip.network_address + int(ip.num_addresses / 2)) + dhcp_end = str(ip.network_address + int(ip.num_addresses - 3)) params.update( {'net': str(ip), 'dhcp': { 'range': {'start': dhcp_start, 'end': dhcp_end}}} @@ -384,8 +382,8 @@ def lookup(self, name): # libvirt use format 192.168.0.1/24, standard should be 192.168.0.0/24 # http://www.ovirt.org/File:Issue3.png if subnet: - subnet = ipaddr.IPNetwork(subnet) - subnet = '%s/%s' % (subnet.network, subnet.prefixlen) + subnet = ipaddress.IPv4Network(subnet, False) + subnet = '%s/%s' % (subnet.network_address, subnet.prefixlen) if connection in ['passthrough', 'vepa']: interfaces = xpath_get_text(xml, '/network/forward/interface/@dev') diff --git a/network.py b/network.py index eedaaff2e..d5efab334 100644 --- a/network.py +++ b/network.py @@ -18,22 +18,22 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # import glob +import ipaddress import os from distutils.spawn import find_executable import ethtool -import ipaddr from wok.stringutils import encode_value from wok.utils import run_command -APrivateNets = ipaddr.IPNetwork('10.0.0.0/8') -BPrivateNets = ipaddr.IPNetwork('172.16.0.0/12') -CPrivateNets = ipaddr.IPNetwork('192.168.0.0/16') +APrivateNets = ipaddress.IPv4Network('10.0.0.0/8', False) +BPrivateNets = ipaddress.IPv4Network('172.16.0.0/12', False) +CPrivateNets = ipaddress.IPv4Network('192.168.0.0/16', False) PrivateNets = [CPrivateNets, BPrivateNets, APrivateNets] -DefaultNetsPool = [ipaddr.IPNetwork('192.168.122.0/23'), - ipaddr.IPNetwork('192.168.124.0/22'), - ipaddr.IPNetwork('192.168.128.0/17')] +DefaultNetsPool = [ipaddress.IPv4Network('192.168.122.0/23', False), + ipaddress.IPv4Network('192.168.124.0/22', False), + ipaddress.IPv4Network('192.168.128.0/17', False)] NET_PATH = '/sys/class/net' NIC_PATH = '/sys/class/net/*/device' @@ -477,7 +477,7 @@ def get_dev_netaddrs(): nets = [] for dev in ethtool.get_devices(): devnet = get_dev_netaddr(dev) - devnet and nets.append(ipaddr.IPNetwork(devnet)) + devnet and nets.append(ipaddress.IPv4Network(devnet, False)) return nets @@ -488,7 +488,7 @@ def get_one_free_network(used_nets, nets_pool=None): nets_pool = PrivateNets def _get_free_network(nets, used_nets): - for net in nets.subnet(new_prefix=24): + for net in nets.subnets(new_prefix=24): if not any(net.overlaps(used) for used in used_nets): return str(net) return None diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py index 7ec027b69..718b2f9ce 100644 --- a/tests/test_networkxml.py +++ b/tests/test_networkxml.py @@ -16,9 +16,9 @@ # 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 ipaddress import unittest -import ipaddr import lxml.etree as ET from wok.plugins.kimchi.xmlutils import network as nxml from wok.xmlutils.utils import xpath_get_text @@ -90,7 +90,8 @@ def test_ip_xml(self): params['net'] = '192.168.122.0/24' xml = ET.tostring(nxml._get_ip_elem(**params), encoding='unicode') netmask = xpath_get_text(xml, '/ip/@netmask')[0] - self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask)) + self.assertEqual(netmask, str( + ipaddress.IPv4Network(params['net'], False).netmask)) def test_forward_xml(self): """ @@ -155,7 +156,8 @@ def test_network_xml(self): params['net'] = '192.168.0.0/24' xml = nxml.to_network_xml(**params) netmask = xpath_get_text(xml, '/network/ip/@netmask')[0] - self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask)) + self.assertEqual(netmask, str( + ipaddress.IPv4Network(params['net'], False).netmask)) def test_vepa_network_singledev_xml(self): expected_xml = """\ diff --git a/utils.py b/utils.py index d6698ae72..de56b9959 100644 --- a/utils.py +++ b/utils.py @@ -270,7 +270,7 @@ def is_libvirtd_up(): mode = os.stat(path).st_mode if stat.S_ISSOCK(mode): return True - except: + except Exception: pass return False diff --git a/xmlutils/network.py b/xmlutils/network.py index bf40c4231..5ad2219f0 100644 --- a/xmlutils/network.py +++ b/xmlutils/network.py @@ -16,7 +16,8 @@ # 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 ipaddr +import ipaddress + import lxml.etree as ET from lxml.builder import E @@ -55,8 +56,8 @@ def _get_ip_elem(**kwargs): if 'net' not in kwargs.keys(): return None - net = ipaddr.IPNetwork(kwargs['net']) - ip = E.ip(address=str(net.ip), netmask=str(net.netmask)) + net = ipaddress.IPv4Network(kwargs['net'], False) + ip = E.ip(address=str(net.network_address), netmask=str(net.netmask)) dhcp_params = kwargs.get('dhcp', {}) dhcp = _get_dhcp_elem(**dhcp_params)