Skip to content

Commit

Permalink
Fix #1298: Use ipaddress instead of deprecated ipaddr
Browse files Browse the repository at this point in the history
Signed-off-by: Aline Manera <aline.manera@gmail.com>
  • Loading branch information
alinefm committed Mar 28, 2020
1 parent 990b3dc commit 5e06451
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 30 deletions.
1 change: 0 additions & 1 deletion dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ runtime-deps:
- python3-ethtool
- python3-Pillow
- python3-CherryPy
- python3-ipaddr
- python3-libguestfs
- parted-devel
- libvirt
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ before starting up the wokd service.

**Runtime Dependencies**

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
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
sudo -H pip3 install -r requirements-OPENSUSE-LEAP.txt

## Build and Install
Expand Down
18 changes: 8 additions & 10 deletions model/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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}}}
Expand Down Expand Up @@ -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')
Expand Down
18 changes: 9 additions & 9 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion requirements-FEDORA.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
ipaddr
1 change: 0 additions & 1 deletion requirements-UBUNTU.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ethtool
ipaddr
8 changes: 5 additions & 3 deletions tests/test_networkxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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 = """<network>\
Expand Down
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions xmlutils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5e06451

Please sign in to comment.