Skip to content

Commit 5e06451

Browse files
committed
Fix #1298: Use ipaddress instead of deprecated ipaddr
Signed-off-by: Aline Manera <[email protected]>
1 parent 990b3dc commit 5e06451

9 files changed

+28
-30
lines changed

dependencies.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ runtime-deps:
7979
- python3-ethtool
8080
- python3-Pillow
8181
- python3-CherryPy
82-
- python3-ipaddr
8382
- python3-libguestfs
8483
- parted-devel
8584
- libvirt

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ before starting up the wokd service.
6969

7070
**Runtime Dependencies**
7171

72-
sudo zypper install -y python3-configobj python3-lxml python3-magic python3-paramiko python3-ldap spice-html5 novnc qemu-kvm python3-libvirt-python python3-ethtool python3-Pillow python3-CherryPy python3-ipaddr python3-libguestfs parted-devel libvirt libvirt-daemon-config-network open-iscsi guestfs-tools nfs-client gcc python3-devel
72+
sudo zypper install -y python3-configobj python3-lxml python3-magic python3-paramiko python3-ldap spice-html5 novnc qemu-kvm python3-libvirt-python python3-ethtool python3-Pillow python3-CherryPy python3-libguestfs parted-devel libvirt libvirt-daemon-config-network open-iscsi guestfs-tools nfs-client gcc python3-devel
7373
sudo -H pip3 install -r requirements-OPENSUSE-LEAP.txt
7474

7575
## Build and Install

model/networks.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
# License along with this library; if not, write to the Free Software
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919
import copy
20+
import ipaddress
2021
import time
2122

22-
import ipaddr
2323
import libvirt
2424
from libvirt import VIR_INTERFACE_XML_INACTIVE
2525
from wok.exception import InvalidOperation
@@ -130,7 +130,7 @@ def _get_available_address(self, addr_pools=None):
130130
network = NetworkModel.get_network(self.conn.get(), net_name)
131131
xml = network.XMLDesc(0)
132132
subnet = NetworkModel.get_network_from_xml(xml)['subnet']
133-
subnet and invalid_addrs.append(ipaddr.IPNetwork(subnet))
133+
subnet and invalid_addrs.append(ipaddress.IPv4Network(subnet, False))
134134
addr_pools = addr_pools if addr_pools else netinfo.PrivateNets
135135
return netinfo.get_one_free_network(invalid_addrs, addr_pools)
136136

@@ -143,17 +143,15 @@ def _set_network_subnet(self, params):
143143
raise OperationFailed('KCHNET0009E', {'name': params['name']})
144144

145145
try:
146-
ip = ipaddr.IPNetwork(netaddr)
146+
ip = ipaddress.IPv4Network(netaddr, False)
147147
except ValueError:
148148
raise InvalidParameter(
149149
'KCHNET0003E', {'subnet': netaddr, 'network': params['name']}
150150
)
151151

152-
if ip.ip == ip.network:
153-
ip.ip = ip.ip + 1
154-
155-
dhcp_start = str(ip.network + int(ip.numhosts / 2))
156-
dhcp_end = str(ip.network + int(ip.numhosts - 3))
152+
ip.network_address = ip.network_address + 1
153+
dhcp_start = str(ip.network_address + int(ip.num_addresses / 2))
154+
dhcp_end = str(ip.network_address + int(ip.num_addresses - 3))
157155
params.update(
158156
{'net': str(ip), 'dhcp': {
159157
'range': {'start': dhcp_start, 'end': dhcp_end}}}
@@ -384,8 +382,8 @@ def lookup(self, name):
384382
# libvirt use format 192.168.0.1/24, standard should be 192.168.0.0/24
385383
# http://www.ovirt.org/File:Issue3.png
386384
if subnet:
387-
subnet = ipaddr.IPNetwork(subnet)
388-
subnet = '%s/%s' % (subnet.network, subnet.prefixlen)
385+
subnet = ipaddress.IPv4Network(subnet, False)
386+
subnet = '%s/%s' % (subnet.network_address, subnet.prefixlen)
389387

390388
if connection in ['passthrough', 'vepa']:
391389
interfaces = xpath_get_text(xml, '/network/forward/interface/@dev')

network.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919
#
2020
import glob
21+
import ipaddress
2122
import os
2223
from distutils.spawn import find_executable
2324

2425
import ethtool
25-
import ipaddr
2626
from wok.stringutils import encode_value
2727
from wok.utils import run_command
2828

2929

30-
APrivateNets = ipaddr.IPNetwork('10.0.0.0/8')
31-
BPrivateNets = ipaddr.IPNetwork('172.16.0.0/12')
32-
CPrivateNets = ipaddr.IPNetwork('192.168.0.0/16')
30+
APrivateNets = ipaddress.IPv4Network('10.0.0.0/8', False)
31+
BPrivateNets = ipaddress.IPv4Network('172.16.0.0/12', False)
32+
CPrivateNets = ipaddress.IPv4Network('192.168.0.0/16', False)
3333
PrivateNets = [CPrivateNets, BPrivateNets, APrivateNets]
34-
DefaultNetsPool = [ipaddr.IPNetwork('192.168.122.0/23'),
35-
ipaddr.IPNetwork('192.168.124.0/22'),
36-
ipaddr.IPNetwork('192.168.128.0/17')]
34+
DefaultNetsPool = [ipaddress.IPv4Network('192.168.122.0/23', False),
35+
ipaddress.IPv4Network('192.168.124.0/22', False),
36+
ipaddress.IPv4Network('192.168.128.0/17', False)]
3737

3838
NET_PATH = '/sys/class/net'
3939
NIC_PATH = '/sys/class/net/*/device'
@@ -477,7 +477,7 @@ def get_dev_netaddrs():
477477
nets = []
478478
for dev in ethtool.get_devices():
479479
devnet = get_dev_netaddr(dev)
480-
devnet and nets.append(ipaddr.IPNetwork(devnet))
480+
devnet and nets.append(ipaddress.IPv4Network(devnet, False))
481481
return nets
482482

483483

@@ -488,7 +488,7 @@ def get_one_free_network(used_nets, nets_pool=None):
488488
nets_pool = PrivateNets
489489

490490
def _get_free_network(nets, used_nets):
491-
for net in nets.subnet(new_prefix=24):
491+
for net in nets.subnets(new_prefix=24):
492492
if not any(net.overlaps(used) for used in used_nets):
493493
return str(net)
494494
return None

requirements-FEDORA.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
ipaddr

requirements-UBUNTU.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
ethtool
2-
ipaddr

tests/test_networkxml.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
# You should have received a copy of the GNU Lesser General Public
1717
# License along with this library; if not, write to the Free Software
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
import ipaddress
1920
import unittest
2021

21-
import ipaddr
2222
import lxml.etree as ET
2323
from wok.plugins.kimchi.xmlutils import network as nxml
2424
from wok.xmlutils.utils import xpath_get_text
@@ -90,7 +90,8 @@ def test_ip_xml(self):
9090
params['net'] = '192.168.122.0/24'
9191
xml = ET.tostring(nxml._get_ip_elem(**params), encoding='unicode')
9292
netmask = xpath_get_text(xml, '/ip/@netmask')[0]
93-
self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask))
93+
self.assertEqual(netmask, str(
94+
ipaddress.IPv4Network(params['net'], False).netmask))
9495

9596
def test_forward_xml(self):
9697
"""
@@ -155,7 +156,8 @@ def test_network_xml(self):
155156
params['net'] = '192.168.0.0/24'
156157
xml = nxml.to_network_xml(**params)
157158
netmask = xpath_get_text(xml, '/network/ip/@netmask')[0]
158-
self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask))
159+
self.assertEqual(netmask, str(
160+
ipaddress.IPv4Network(params['net'], False).netmask))
159161

160162
def test_vepa_network_singledev_xml(self):
161163
expected_xml = """<network>\

utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def is_libvirtd_up():
270270
mode = os.stat(path).st_mode
271271
if stat.S_ISSOCK(mode):
272272
return True
273-
except:
273+
except Exception:
274274
pass
275275

276276
return False

xmlutils/network.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public
1717
# License along with this library; if not, write to the Free Software
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19-
import ipaddr
19+
import ipaddress
20+
2021
import lxml.etree as ET
2122
from lxml.builder import E
2223

@@ -55,8 +56,8 @@ def _get_ip_elem(**kwargs):
5556
if 'net' not in kwargs.keys():
5657
return None
5758

58-
net = ipaddr.IPNetwork(kwargs['net'])
59-
ip = E.ip(address=str(net.ip), netmask=str(net.netmask))
59+
net = ipaddress.IPv4Network(kwargs['net'], False)
60+
ip = E.ip(address=str(net.network_address), netmask=str(net.netmask))
6061

6162
dhcp_params = kwargs.get('dhcp', {})
6263
dhcp = _get_dhcp_elem(**dhcp_params)

0 commit comments

Comments
 (0)