| 1 | Patch-Source: https://github.com/kimchi-project/kimchi/commit/5e0645184a3b3e3dac1bc40792c09bd81b9892cc
|
| 2 | From: Aline Manera <[email protected]>
|
| 3 | Date: Sat, 28 Mar 2020 15:27:34 -0400
|
| 4 | Subject: [PATCH] Fix #1298: Use ipaddress instead of deprecated ipaddr
|
| 5 |
|
| 6 | Signed-off-by: Aline Manera <[email protected]>
|
| 7 | ---
|
| 8 | dependencies.yaml | 1 -
|
| 9 | docs/README.md | 2 +-
|
| 10 | model/networks.py | 18 ++++++++----------
|
| 11 | network.py | 18 +++++++++---------
|
| 12 | requirements-FEDORA.txt | 1 -
|
| 13 | requirements-UBUNTU.txt | 1 -
|
| 14 | tests/test_networkxml.py | 8 +++++---
|
| 15 | utils.py | 2 +-
|
| 16 | xmlutils/network.py | 7 ++++---
|
| 17 | 9 files changed, 28 insertions(+), 30 deletions(-)
|
| 18 |
|
| 19 | diff --git a/model/networks.py b/model/networks.py
|
| 20 | index a38498166..9d4600355 100644
|
| 21 | |
| 22 | |
| 23 | @@ -17,9 +17,9 @@
|
| 24 | # License along with this library; if not, write to the Free Software
|
| 25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 26 | import copy
|
| 27 | +import ipaddress
|
| 28 | import time
|
| 29 |
|
| 30 | -import ipaddr
|
| 31 | import libvirt
|
| 32 | from libvirt import VIR_INTERFACE_XML_INACTIVE
|
| 33 | from wok.exception import InvalidOperation
|
| 34 | @@ -130,7 +130,7 @@ def _get_available_address(self, addr_pools=None):
|
| 35 | network = NetworkModel.get_network(self.conn.get(), net_name)
|
| 36 | xml = network.XMLDesc(0)
|
| 37 | subnet = NetworkModel.get_network_from_xml(xml)['subnet']
|
| 38 | - subnet and invalid_addrs.append(ipaddr.IPNetwork(subnet))
|
| 39 | + subnet and invalid_addrs.append(ipaddress.IPv4Network(subnet, False))
|
| 40 | addr_pools = addr_pools if addr_pools else netinfo.PrivateNets
|
| 41 | return netinfo.get_one_free_network(invalid_addrs, addr_pools)
|
| 42 |
|
| 43 | @@ -143,17 +143,15 @@ def _set_network_subnet(self, params):
|
| 44 | raise OperationFailed('KCHNET0009E', {'name': params['name']})
|
| 45 |
|
| 46 | try:
|
| 47 | - ip = ipaddr.IPNetwork(netaddr)
|
| 48 | + ip = ipaddress.IPv4Network(netaddr, False)
|
| 49 | except ValueError:
|
| 50 | raise InvalidParameter(
|
| 51 | 'KCHNET0003E', {'subnet': netaddr, 'network': params['name']}
|
| 52 | )
|
| 53 |
|
| 54 | - if ip.ip == ip.network:
|
| 55 | - ip.ip = ip.ip + 1
|
| 56 | -
|
| 57 | - dhcp_start = str(ip.network + int(ip.numhosts / 2))
|
| 58 | - dhcp_end = str(ip.network + int(ip.numhosts - 3))
|
| 59 | + ip.network_address = ip.network_address + 1
|
| 60 | + dhcp_start = str(ip.network_address + int(ip.num_addresses / 2))
|
| 61 | + dhcp_end = str(ip.network_address + int(ip.num_addresses - 3))
|
| 62 | params.update(
|
| 63 | {'net': str(ip), 'dhcp': {
|
| 64 | 'range': {'start': dhcp_start, 'end': dhcp_end}}}
|
| 65 | @@ -384,8 +382,8 @@ def lookup(self, name):
|
| 66 | # libvirt use format 192.168.0.1/24, standard should be 192.168.0.0/24
|
| 67 | # http://www.ovirt.org/File:Issue3.png
|
| 68 | if subnet:
|
| 69 | - subnet = ipaddr.IPNetwork(subnet)
|
| 70 | - subnet = '%s/%s' % (subnet.network, subnet.prefixlen)
|
| 71 | + subnet = ipaddress.IPv4Network(subnet, False)
|
| 72 | + subnet = '%s/%s' % (subnet.network_address, subnet.prefixlen)
|
| 73 |
|
| 74 | if connection in ['passthrough', 'vepa']:
|
| 75 | interfaces = xpath_get_text(xml, '/network/forward/interface/@dev')
|
| 76 | diff --git a/network.py b/network.py
|
| 77 | index eedaaff2e..d5efab334 100644
|
| 78 | |
| 79 | |
| 80 | @@ -18,22 +18,22 @@
|
| 81 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 82 | #
|
| 83 | import glob
|
| 84 | +import ipaddress
|
| 85 | import os
|
| 86 | from distutils.spawn import find_executable
|
| 87 |
|
| 88 | import ethtool
|
| 89 | -import ipaddr
|
| 90 | from wok.stringutils import encode_value
|
| 91 | from wok.utils import run_command
|
| 92 |
|
| 93 |
|
| 94 | -APrivateNets = ipaddr.IPNetwork('10.0.0.0/8')
|
| 95 | -BPrivateNets = ipaddr.IPNetwork('172.16.0.0/12')
|
| 96 | -CPrivateNets = ipaddr.IPNetwork('192.168.0.0/16')
|
| 97 | +APrivateNets = ipaddress.IPv4Network('10.0.0.0/8', False)
|
| 98 | +BPrivateNets = ipaddress.IPv4Network('172.16.0.0/12', False)
|
| 99 | +CPrivateNets = ipaddress.IPv4Network('192.168.0.0/16', False)
|
| 100 | PrivateNets = [CPrivateNets, BPrivateNets, APrivateNets]
|
| 101 | -DefaultNetsPool = [ipaddr.IPNetwork('192.168.122.0/23'),
|
| 102 | - ipaddr.IPNetwork('192.168.124.0/22'),
|
| 103 | - ipaddr.IPNetwork('192.168.128.0/17')]
|
| 104 | +DefaultNetsPool = [ipaddress.IPv4Network('192.168.122.0/23', False),
|
| 105 | + ipaddress.IPv4Network('192.168.124.0/22', False),
|
| 106 | + ipaddress.IPv4Network('192.168.128.0/17', False)]
|
| 107 |
|
| 108 | NET_PATH = '/sys/class/net'
|
| 109 | NIC_PATH = '/sys/class/net/*/device'
|
| 110 | @@ -477,7 +477,7 @@ def get_dev_netaddrs():
|
| 111 | nets = []
|
| 112 | for dev in ethtool.get_devices():
|
| 113 | devnet = get_dev_netaddr(dev)
|
| 114 | - devnet and nets.append(ipaddr.IPNetwork(devnet))
|
| 115 | + devnet and nets.append(ipaddress.IPv4Network(devnet, False))
|
| 116 | return nets
|
| 117 |
|
| 118 |
|
| 119 | @@ -488,7 +488,7 @@ def get_one_free_network(used_nets, nets_pool=None):
|
| 120 | nets_pool = PrivateNets
|
| 121 |
|
| 122 | def _get_free_network(nets, used_nets):
|
| 123 | - for net in nets.subnet(new_prefix=24):
|
| 124 | + for net in nets.subnets(new_prefix=24):
|
| 125 | if not any(net.overlaps(used) for used in used_nets):
|
| 126 | return str(net)
|
| 127 | return None
|
| 128 | diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py
|
| 129 | index 7ec027b69..718b2f9ce 100644
|
| 130 | |
| 131 | |
| 132 | @@ -16,9 +16,9 @@
|
| 133 | # You should have received a copy of the GNU Lesser General Public
|
| 134 | # License along with this library; if not, write to the Free Software
|
| 135 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 136 | +import ipaddress
|
| 137 | import unittest
|
| 138 |
|
| 139 | -import ipaddr
|
| 140 | import lxml.etree as ET
|
| 141 | from wok.plugins.kimchi.xmlutils import network as nxml
|
| 142 | from wok.xmlutils.utils import xpath_get_text
|
| 143 | @@ -90,7 +90,8 @@ def test_ip_xml(self):
|
| 144 | params['net'] = '192.168.122.0/24'
|
| 145 | xml = ET.tostring(nxml._get_ip_elem(**params), encoding='unicode')
|
| 146 | netmask = xpath_get_text(xml, '/ip/@netmask')[0]
|
| 147 | - self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask))
|
| 148 | + self.assertEqual(netmask, str(
|
| 149 | + ipaddress.IPv4Network(params['net'], False).netmask))
|
| 150 |
|
| 151 | def test_forward_xml(self):
|
| 152 | """
|
| 153 | @@ -155,7 +156,8 @@ def test_network_xml(self):
|
| 154 | params['net'] = '192.168.0.0/24'
|
| 155 | xml = nxml.to_network_xml(**params)
|
| 156 | netmask = xpath_get_text(xml, '/network/ip/@netmask')[0]
|
| 157 | - self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask))
|
| 158 | + self.assertEqual(netmask, str(
|
| 159 | + ipaddress.IPv4Network(params['net'], False).netmask))
|
| 160 |
|
| 161 | def test_vepa_network_singledev_xml(self):
|
| 162 | expected_xml = """<network>\
|
| 163 | diff --git a/utils.py b/utils.py
|
| 164 | index d6698ae72..de56b9959 100644
|
| 165 | |
| 166 | |
| 167 | @@ -270,7 +270,7 @@ def is_libvirtd_up():
|
| 168 | mode = os.stat(path).st_mode
|
| 169 | if stat.S_ISSOCK(mode):
|
| 170 | return True
|
| 171 | - except:
|
| 172 | + except Exception:
|
| 173 | pass
|
| 174 |
|
| 175 | return False
|
| 176 | diff --git a/xmlutils/network.py b/xmlutils/network.py
|
| 177 | index bf40c4231..5ad2219f0 100644
|
| 178 | |
| 179 | |
| 180 | @@ -16,7 +16,8 @@
|
| 181 | # You should have received a copy of the GNU Lesser General Public
|
| 182 | # License along with this library; if not, write to the Free Software
|
| 183 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 184 | -import ipaddr
|
| 185 | +import ipaddress
|
| 186 | +
|
| 187 | import lxml.etree as ET
|
| 188 | from lxml.builder import E
|
| 189 |
|
| 190 | @@ -55,8 +56,8 @@ def _get_ip_elem(**kwargs):
|
| 191 | if 'net' not in kwargs.keys():
|
| 192 | return None
|
| 193 |
|
| 194 | - net = ipaddr.IPNetwork(kwargs['net'])
|
| 195 | - ip = E.ip(address=str(net.ip), netmask=str(net.netmask))
|
| 196 | + net = ipaddress.IPv4Network(kwargs['net'], False)
|
| 197 | + ip = E.ip(address=str(net.network_address), netmask=str(net.netmask))
|
| 198 |
|
| 199 | dhcp_params = kwargs.get('dhcp', {})
|
| 200 | dhcp = _get_dhcp_elem(**dhcp_params)
|