Skip to content

Commit 899eab6

Browse files
widorohityadavcloud
authored andcommitted
kvm/security_group: Make Security Group Python 3 compatible (apache#3589)
* kvm/security_group: Make Security Group Python 3 compatible This script only runs on the KVM Hypervisors and these all support Python 3. As Python 2 is deprecated at the end of 2019 we need to fix these scripts to work under Python 3. CentOS 7, 8 and Ubuntu 16.04 and 18.04 all have Python 3 installed by default. Ubuntu 20.04 will no longer have Python 2 installed and therefor this script needs to be modified to work with Python 3. Signed-off-by: Wido den Hollander <[email protected]> * Add dependency of python3 in packaging/centos7/cloud.spec
1 parent 6cec7c7 commit 899eab6

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

debian/control

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ Section: libs
33
Priority: extra
44
Maintainer: Wido den Hollander <[email protected]>
55
Build-Depends: debhelper (>= 9), openjdk-8-jdk | java8-sdk | java8-jdk | openjdk-9-jdk, genisoimage,
6-
python-mysql.connector, maven (>= 3) | maven3, python (>= 2.7), lsb-release, dh-systemd, python-setuptools
6+
python-mysql.connector, maven (>= 3) | maven3, python (>= 2.7), python3 (>= 3), lsb-release, dh-systemd, python-setuptools
77
Standards-Version: 3.8.1
88
Homepage: http://www.cloudstack.org/
99

1010
Package: cloudstack-common
1111
Architecture: all
12-
Depends: ${misc:Depends}, ${python:Depends}, genisoimage, nfs-common, python-netaddr
12+
Depends: ${misc:Depends}, ${python:Depends}, genisoimage, nfs-common
1313
Conflicts: cloud-scripts, cloud-utils, cloud-system-iso, cloud-console-proxy, cloud-daemonize, cloud-deps, cloud-python, cloud-setup
1414
Description: A common package which contains files which are shared by several CloudStack packages
1515

@@ -22,7 +22,7 @@ Description: CloudStack server library
2222

2323
Package: cloudstack-agent
2424
Architecture: all
25-
Depends: ${python:Depends}, openjdk-8-jre-headless | java8-runtime-headless | java8-runtime | openjdk-9-jre-headless, cloudstack-common (= ${source:Version}), lsb-base (>= 9), openssh-client, qemu-kvm (>= 2.5), libvirt-bin (>= 1.3) | libvirt-daemon-system (>= 3.0), uuid-runtime, iproute2, ebtables, vlan, ipset, python-libvirt, ethtool, iptables, lsb-release, aria2
25+
Depends: ${python:Depends}, openjdk-8-jre-headless | java8-runtime-headless | java8-runtime | openjdk-9-jre-headless, cloudstack-common (= ${source:Version}), lsb-base (>= 9), openssh-client, qemu-kvm (>= 2.5), libvirt-bin (>= 1.3) | libvirt-daemon-system (>= 3.0), uuid-runtime, iproute2, ebtables, vlan, ipset, python3-libvirt, ethtool, iptables, lsb-release, aria2
2626
Recommends: init-system-helpers
2727
Conflicts: cloud-agent, cloud-agent-libs, cloud-agent-deps, cloud-agent-scripts
2828
Description: CloudStack agent

packaging/centos7/cloud.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ intelligent IaaS cloud implementation.
5555
Summary: CloudStack management server UI
5656
Requires: java-1.8.0-openjdk
5757
Requires: python
58+
Requires: python3
5859
Requires: bash
5960
Requires: bzip2
6061
Requires: gzip
@@ -82,6 +83,7 @@ management, and intelligence in CloudStack.
8283
%package common
8384
Summary: Apache CloudStack common files and scripts
8485
Requires: python
86+
Requires: python3
8587
Requires: python-argparse
8688
Requires: python-netaddr
8789
Group: System Environment/Libraries

scripts/vm/network/security_group.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22
# Licensed to the Apache Software Foundation (ASF) under one
33
# or more contributor license agreements. See the NOTICE file
44
# distributed with this work for additional information
@@ -26,9 +26,7 @@
2626
import libvirt
2727
import fcntl
2828
import time
29-
from netaddr import IPAddress, IPNetwork
30-
from netaddr.core import AddrFormatError
31-
29+
import ipaddress
3230

3331
logpath = "/var/run/cloud/" # FIXME: Logs should reside in /var/log/cloud
3432
lock_file = "/var/lock/cloudstack_security_group.lock"
@@ -52,7 +50,7 @@ def obtain_file_lock(path):
5250
def execute(cmd):
5351
logging.debug(cmd)
5452
try:
55-
return check_output(cmd, shell=True)
53+
return check_output(cmd, shell=True).decode()
5654
except CalledProcessError as e:
5755
logging.exception('Command exited non-zero: %s', cmd)
5856
raise
@@ -103,8 +101,8 @@ def virshlist(states):
103101

104102
conn = get_libvirt_connection()
105103

106-
alldomains = map(conn.lookupByID, conn.listDomainsID())
107-
alldomains += map(conn.lookupByName, conn.listDefinedDomains())
104+
alldomains = [d for domain in map(conn.lookupByID, conn.listDomainsID())]
105+
alldomains += [d for domain in map(conn.lookupByName, conn.listDefinedDomains())]
108106

109107
domains = []
110108
for domain in alldomains:
@@ -130,7 +128,7 @@ def ipv6_link_local_addr(mac=None):
130128
eui64 = re.sub(r'[.:-]', '', mac).lower()
131129
eui64 = eui64[0:6] + 'fffe' + eui64[6:]
132130
eui64 = hex(int(eui64[0:2], 16) ^ 2)[2:].zfill(2) + eui64[2:]
133-
return IPAddress('fe80::' + ':'.join(re.findall(r'.{4}', eui64)))
131+
return ipaddress.ip_address('fe80::' + ':'.join(re.findall(r'.{4}', eui64)))
134132

135133

136134
def split_ips_by_family(ips):
@@ -140,10 +138,10 @@ def split_ips_by_family(ips):
140138
ip4s = []
141139
ip6s = []
142140
for ip in ips:
143-
version = IPNetwork(ip).version
144-
if version == 4:
141+
network = ipaddress.ip_network(ip)
142+
if network.version == 4:
145143
ip4s.append(ip)
146-
elif version == 6:
144+
elif network.version == 6:
147145
ip6s.append(ip)
148146
return ip4s, ip6s
149147

@@ -516,10 +514,10 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
516514

517515
vm_ip6_addr = [ipv6_link_local]
518516
try:
519-
ip6 = IPAddress(vm_ip6)
517+
ip6 = ipaddress.ip_address(vm_ip6)
520518
if ip6.version == 6:
521519
vm_ip6_addr.append(ip6)
522-
except AddrFormatError:
520+
except (ipaddress.AddressValueError, ValueError):
523521
pass
524522

525523
add_to_ipset(vmipsetName6, vm_ip6_addr, action)
@@ -969,7 +967,7 @@ def parse_network_rules(rules):
969967
ipv6 = []
970968
for ip in cidrs.split(","):
971969
try:
972-
network = IPNetwork(ip)
970+
network = ipaddress.ip_network(ip)
973971
if network.version == 4:
974972
ipv4.append(ip)
975973
else:

0 commit comments

Comments
 (0)