mirror of
https://github.com/retspen/webvirtcloud
synced 2025-07-31 12:41:08 +00:00
Migrate source code to python 3.7 and refine code style
This commit is contained in:
parent
0e231a930d
commit
a4d74a09a6
55 changed files with 192 additions and 306 deletions
|
|
@ -6,12 +6,11 @@ Further Information might be available at:
|
|||
https://github.com/haypo/python-ipy
|
||||
"""
|
||||
|
||||
__version__ = '0.83'
|
||||
__version__ = '1.00'
|
||||
|
||||
import bisect
|
||||
import collections
|
||||
import sys
|
||||
import types
|
||||
|
||||
|
||||
# Definition of the Ranges for IPv4 IPs
|
||||
# this should include www.iana.org/assignments/ipv4-address-space
|
||||
|
|
@ -21,7 +20,7 @@ IPv4ranges = {
|
|||
'00000000': 'PRIVATE', # 0/8
|
||||
'00001010': 'PRIVATE', # 10/8
|
||||
'0110010001': 'CARRIER_GRADE_NAT', # 100.64/10
|
||||
'01111111': 'PRIVATE', # 127.0/8
|
||||
'01111111': 'LOOPBACK', # 127.0/8
|
||||
'1': 'PUBLIC', # fall back
|
||||
'1010100111111110': 'PRIVATE', # 169.254/16
|
||||
'101011000001': 'PRIVATE', # 172.16/12
|
||||
|
|
@ -121,14 +120,6 @@ MAX_IPV6_ADDRESS = 0xffffffffffffffffffffffffffffffff
|
|||
IPV6_TEST_MAP = 0xffffffffffffffffffffffff00000000
|
||||
IPV6_MAP_MASK = 0x00000000000000000000ffff00000000
|
||||
|
||||
if sys.version_info >= (3,):
|
||||
INT_TYPES = (int,)
|
||||
STR_TYPES = (str,)
|
||||
xrange = range
|
||||
else:
|
||||
INT_TYPES = (int, long)
|
||||
STR_TYPES = (str, unicode)
|
||||
|
||||
|
||||
class IPint(object):
|
||||
"""Handling of IP addresses returning integers.
|
||||
|
|
@ -175,7 +166,7 @@ class IPint(object):
|
|||
prefixlen = -1
|
||||
|
||||
# handling of non string values in constructor
|
||||
if isinstance(data, INT_TYPES):
|
||||
if isinstance(data, int):
|
||||
self.ip = int(data)
|
||||
if ipversion == 0:
|
||||
if self.ip <= MAX_IPV4_ADDRESS:
|
||||
|
|
@ -199,7 +190,7 @@ class IPint(object):
|
|||
self._ipversion = data._ipversion
|
||||
self._prefixlen = data._prefixlen
|
||||
self.ip = data.ip
|
||||
elif isinstance(data, STR_TYPES):
|
||||
elif isinstance(data, str):
|
||||
# TODO: refactor me!
|
||||
# splitting of a string into IP and prefixlen et. al.
|
||||
x = data.split('-')
|
||||
|
|
@ -219,7 +210,7 @@ class IPint(object):
|
|||
# make sure the broadcast is the same as the last ip
|
||||
# otherwise it will return /16 for something like:
|
||||
# 192.168.0.0-192.168.191.255
|
||||
if IP('%s/%s' % (ip, 32 - netbits)).broadcast().int() != last:
|
||||
if IP('%s/%s' % (ip, 32-netbits)).broadcast().int() != last:
|
||||
raise ValueError("the range %s is not on a network boundary." % data)
|
||||
elif len(x) == 1:
|
||||
x = data.split('/')
|
||||
|
|
@ -243,7 +234,7 @@ class IPint(object):
|
|||
else:
|
||||
raise ValueError("can't parse")
|
||||
|
||||
(self.ip, parsedVersion) = parseAddress(ip)
|
||||
(self.ip, parsedVersion) = parseAddress(ip, ipversion)
|
||||
if ipversion == 0:
|
||||
ipversion = parsedVersion
|
||||
if prefixlen == -1:
|
||||
|
|
@ -318,15 +309,15 @@ class IPint(object):
|
|||
(self._ipversion == 6 and self._prefixlen == 128):
|
||||
if self.NoPrefixForSingleIp:
|
||||
want = 0
|
||||
if want == None:
|
||||
if want is None:
|
||||
want = self.WantPrefixLen
|
||||
if want == None:
|
||||
if want is None:
|
||||
want = 1
|
||||
if want:
|
||||
if want == 2:
|
||||
# this should work with IP and IPint
|
||||
netmask = self.netmask()
|
||||
if not isinstance(netmask, INT_TYPES):
|
||||
if not isinstance(netmask, int):
|
||||
netmask = netmask.int()
|
||||
return "/%s" % (intToIp(netmask, self._ipversion))
|
||||
elif want == 3:
|
||||
|
|
@ -354,7 +345,7 @@ class IPint(object):
|
|||
"""
|
||||
|
||||
bits = _ipVersionToLen(self._ipversion)
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 0
|
||||
ret = _intToBin(self.ip)
|
||||
return '0' * (bits - len(ret)) + ret + self._printPrefix(wantprefixlen)
|
||||
|
|
@ -370,7 +361,7 @@ class IPint(object):
|
|||
'ffff:ffff:ffff:ffff:ffff:f:f:fffc/127'
|
||||
"""
|
||||
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 1
|
||||
|
||||
if self._ipversion == 4:
|
||||
|
|
@ -385,7 +376,7 @@ class IPint(object):
|
|||
# every element of followingzeros will contain the number of zeros
|
||||
# following the corresponding element of hextets
|
||||
followingzeros = [0] * 8
|
||||
for i in xrange(len(hextets)):
|
||||
for i in range(len(hextets)):
|
||||
followingzeros[i] = _countFollowingZeros(hextets[i:])
|
||||
# compressionpos is the position where we can start removing zeros
|
||||
compressionpos = followingzeros.index(max(followingzeros))
|
||||
|
|
@ -413,7 +404,7 @@ class IPint(object):
|
|||
2001:658:22a:cafe:200:0:0:1
|
||||
"""
|
||||
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 1
|
||||
|
||||
if self._ipversion == 4:
|
||||
|
|
@ -434,7 +425,7 @@ class IPint(object):
|
|||
2001:0658:022a:cafe:0200:0000:0000:0001
|
||||
"""
|
||||
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 1
|
||||
|
||||
return intToIp(self.ip, self._ipversion) + self._printPrefix(wantprefixlen)
|
||||
|
|
@ -448,7 +439,7 @@ class IPint(object):
|
|||
0x20010658022acafe0200000000000001
|
||||
"""
|
||||
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 0
|
||||
|
||||
x = '0x%x' % self.ip
|
||||
|
|
@ -463,7 +454,7 @@ class IPint(object):
|
|||
42540616829182469433547762482097946625
|
||||
"""
|
||||
|
||||
if self.WantPrefixLen == None and wantprefixlen == None:
|
||||
if self.WantPrefixLen is None and wantprefixlen is None:
|
||||
wantprefixlen = 0
|
||||
|
||||
x = '%d' % self.ip
|
||||
|
|
@ -473,7 +464,7 @@ class IPint(object):
|
|||
"""Return a description of the IP type ('PRIVATE', 'RESERVED', etc).
|
||||
|
||||
>>> print(IP('127.0.0.1').iptype())
|
||||
PRIVATE
|
||||
LOOPBACK
|
||||
>>> print(IP('192.168.1.1').iptype())
|
||||
PRIVATE
|
||||
>>> print(IP('195.185.1.2').iptype())
|
||||
|
|
@ -496,7 +487,7 @@ class IPint(object):
|
|||
raise ValueError("only IPv4 and IPv6 supported")
|
||||
|
||||
bits = self.strBin()
|
||||
for i in xrange(len(bits), 0, -1):
|
||||
for i in range(len(bits), 0, -1):
|
||||
if bits[:i] in iprange:
|
||||
return iprange[bits[:i]]
|
||||
return "unknown"
|
||||
|
|
@ -553,6 +544,9 @@ class IPint(object):
|
|||
"""
|
||||
return True
|
||||
|
||||
def __bool__(self):
|
||||
return self.__nonzero__()
|
||||
|
||||
def __len__(self):
|
||||
"""
|
||||
Return the length of a subnet.
|
||||
|
|
@ -606,8 +600,8 @@ class IPint(object):
|
|||
"""
|
||||
|
||||
if isinstance(key, slice):
|
||||
return [self.ip + int(x) for x in xrange(*key.indices(len(self)))]
|
||||
if not isinstance(key, INT_TYPES):
|
||||
return [self.ip + int(x) for x in range(*key.indices(len(self)))]
|
||||
if not isinstance(key, int):
|
||||
raise TypeError
|
||||
if key < 0:
|
||||
if abs(key) <= self.len():
|
||||
|
|
@ -848,13 +842,13 @@ class IP(IPint):
|
|||
for x in self:
|
||||
ret.append(x.reverseName())
|
||||
elif self.len() < 2 ** 16:
|
||||
for i in xrange(0, self.len(), 2 ** 8):
|
||||
for i in range(0, self.len(), 2 ** 8):
|
||||
ret.append(self[i].reverseName()[2:])
|
||||
elif self.len() < 2 ** 24:
|
||||
for i in xrange(0, self.len(), 2 ** 16):
|
||||
for i in range(0, self.len(), 2 ** 16):
|
||||
ret.append(self[i].reverseName()[4:])
|
||||
else:
|
||||
for i in xrange(0, self.len(), 2 ** 24):
|
||||
for i in range(0, self.len(), 2 ** 24):
|
||||
ret.append(self[i].reverseName()[6:])
|
||||
return ret
|
||||
elif self._ipversion == 6:
|
||||
|
|
@ -952,7 +946,7 @@ class IP(IPint):
|
|||
127.0.0.3
|
||||
"""
|
||||
if isinstance(key, slice):
|
||||
return [IP(IPint.__getitem__(self, x), ipversion=self._ipversion) for x in xrange(*key.indices(len(self)))]
|
||||
return [IP(IPint.__getitem__(self, x), ipversion=self._ipversion) for x in range(*key.indices(len(self)))]
|
||||
return IP(IPint.__getitem__(self, key), ipversion=self._ipversion)
|
||||
|
||||
def __repr__(self):
|
||||
|
|
@ -1183,7 +1177,7 @@ class IPSet(collections.MutableSet):
|
|||
# prefix length and differ only on the last bit of the prefix
|
||||
addrlen = len(self.prefixes)
|
||||
i = 0
|
||||
while i < addrlen - 1:
|
||||
while i < addrlen-1:
|
||||
j = i + 1
|
||||
|
||||
try:
|
||||
|
|
@ -1328,7 +1322,7 @@ def _parseAddressIPv6(ipstr):
|
|||
return value
|
||||
|
||||
|
||||
def parseAddress(ipstr):
|
||||
def parseAddress(ipstr, ipversion=0):
|
||||
"""
|
||||
Parse a string and return the corresponding IP address (as integer)
|
||||
and a guess of the IP version.
|
||||
|
|
@ -1397,7 +1391,7 @@ def parseAddress(ipstr):
|
|||
# assume IPv6 in pure hexadecimal notation
|
||||
return (hexval, 6)
|
||||
|
||||
elif ipstr.find('.') != -1 or (intval is not None and intval < 256):
|
||||
elif ipstr.find('.') != -1 or (intval is not None and intval < 256 and ipversion != 6):
|
||||
# assume IPv4 ('127' gets interpreted as '127.0.0.0')
|
||||
bytes = ipstr.split('.')
|
||||
if len(bytes) > 4:
|
||||
|
|
@ -1415,7 +1409,7 @@ def parseAddress(ipstr):
|
|||
# will be interpreted as IPv4 first byte
|
||||
if intval > MAX_IPV6_ADDRESS:
|
||||
raise ValueError("IP Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, intval))
|
||||
if intval <= MAX_IPV4_ADDRESS:
|
||||
if intval <= MAX_IPV4_ADDRESS and ipversion != 6:
|
||||
return (intval, 4)
|
||||
else:
|
||||
return (intval, 6)
|
||||
|
|
@ -1436,7 +1430,7 @@ def intToIp(ip, version):
|
|||
if version == 4:
|
||||
if ip > MAX_IPV4_ADDRESS:
|
||||
raise ValueError("IPv4 Address can't be larger than %x: %x" % (MAX_IPV4_ADDRESS, ip))
|
||||
for l in xrange(4):
|
||||
for l in range(4):
|
||||
ret = str(ip & 0xff) + '.' + ret
|
||||
ip = ip >> 8
|
||||
ret = ret[:-1]
|
||||
|
|
@ -1444,7 +1438,7 @@ def intToIp(ip, version):
|
|||
if ip > MAX_IPV6_ADDRESS:
|
||||
raise ValueError("IPv6 Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, ip))
|
||||
l = "%032x" % ip
|
||||
for x in xrange(1, 33):
|
||||
for x in range(1, 33):
|
||||
ret = l[-x] + ret
|
||||
if x % 4 == 0:
|
||||
ret = ':' + ret
|
||||
|
|
@ -1617,7 +1611,7 @@ def _prefixlenToNetmask(prefixlen, version):
|
|||
return 0
|
||||
elif prefixlen < 0:
|
||||
raise ValueError("Prefixlen must be > 0")
|
||||
return ((2 << prefixlen - 1) - 1) << (_ipVersionToLen(version) - prefixlen)
|
||||
return ((2 << prefixlen-1) - 1) << (_ipVersionToLen(version) - prefixlen)
|
||||
|
||||
|
||||
def _remove_subprefix(prefix, subprefix):
|
||||
|
|
@ -1631,8 +1625,8 @@ def _remove_subprefix(prefix, subprefix):
|
|||
|
||||
# Start cutting in half, recursively
|
||||
prefixes = [
|
||||
IP('%s/%d' % (prefix[0], prefix._prefixlen + 1)),
|
||||
IP('%s/%d' % (prefix[int(prefix.len() / 2)], prefix._prefixlen + 1)),
|
||||
IP('%s/%d' % (prefix[0], prefix._prefixlen+1)),
|
||||
IP('%s/%d' % (prefix[int(prefix.len()/2)], prefix._prefixlen+1)),
|
||||
]
|
||||
if subprefix in prefixes[0]:
|
||||
return _remove_subprefix(prefixes[0], subprefix) + IPSet([prefixes[1]])
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import libvirt
|
|||
import threading
|
||||
import socket
|
||||
from vrtManager import util
|
||||
from rwlock import ReadWriteLock
|
||||
from vrtManager.rwlock import ReadWriteLock
|
||||
from django.conf import settings
|
||||
from libvirt import libvirtError
|
||||
|
||||
|
|
@ -216,18 +216,18 @@ class wvmConnection(object):
|
|||
|
||||
def __unicode__(self):
|
||||
if self.type == CONN_TCP:
|
||||
type_str = u'tcp'
|
||||
type_str = 'tcp'
|
||||
elif self.type == CONN_SSH:
|
||||
type_str = u'ssh'
|
||||
type_str = 'ssh'
|
||||
elif self.type == CONN_TLS:
|
||||
type_str = u'tls'
|
||||
type_str = 'tls'
|
||||
else:
|
||||
type_str = u'invalid_type'
|
||||
type_str = 'invalid_type'
|
||||
|
||||
return u'qemu+{type}://{user}@{host}/system'.format(type=type_str, user=self.login, host=self.host)
|
||||
return f'qemu+{type_str}://{self.login}@{self.host}/system'
|
||||
|
||||
def __repr__(self):
|
||||
return '<wvmConnection {connection_str}>'.format(connection_str=unicode(self))
|
||||
return '<wvmConnection {connection_str}>'.format(connection_str=str(self))
|
||||
|
||||
|
||||
class wvmConnectionManager(object):
|
||||
|
|
@ -272,19 +272,19 @@ class wvmConnectionManager(object):
|
|||
raises libvirtError if (re)connecting fails
|
||||
"""
|
||||
# force all string values to unicode
|
||||
host = unicode(host)
|
||||
login = unicode(login)
|
||||
passwd = unicode(passwd) if passwd is not None else None
|
||||
host = str(host)
|
||||
login = str(login)
|
||||
passwd = str(passwd) if passwd is not None else None
|
||||
|
||||
connection = self._search_connection(host, login, passwd, conn)
|
||||
|
||||
if (connection is None):
|
||||
if connection is None:
|
||||
self._connections_lock.acquireWrite()
|
||||
try:
|
||||
# we have to search for the connection again after aquireing the write lock
|
||||
# as the thread previously holding the write lock may have already added our connection
|
||||
connection = self._search_connection(host, login, passwd, conn)
|
||||
if (connection is None):
|
||||
if connection is None:
|
||||
# create a new connection if a matching connection does not already exist
|
||||
connection = wvmConnection(host, login, passwd, conn)
|
||||
|
||||
|
|
@ -332,6 +332,7 @@ class wvmConnectionManager(object):
|
|||
except Exception as err:
|
||||
return err
|
||||
|
||||
|
||||
connection_manager = wvmConnectionManager(
|
||||
settings.LIBVIRT_KEEPALIVE_INTERVAL if hasattr(settings, 'LIBVIRT_KEEPALIVE_INTERVAL') else 5,
|
||||
settings.LIBVIRT_KEEPALIVE_COUNT if hasattr(settings, 'LIBVIRT_KEEPALIVE_COUNT') else 5
|
||||
|
|
@ -368,7 +369,7 @@ class wvmConnect(object):
|
|||
minor = ver / 1000
|
||||
ver = ver % 1000
|
||||
release = ver
|
||||
return "%s.%s.%s" % (major,minor,release)
|
||||
return f"{major}.{minor}.{release}"
|
||||
|
||||
def get_lib_version(self):
|
||||
ver = self.wvm.getLibVersion()
|
||||
|
|
@ -377,7 +378,7 @@ class wvmConnect(object):
|
|||
minor = ver / 1000
|
||||
ver %= 1000
|
||||
release = ver
|
||||
return "%s.%s.%s" % (major,minor,release)
|
||||
return f"{major}.{minor}.{release}"
|
||||
|
||||
def is_kvm_supported(self):
|
||||
"""Return KVM capabilities."""
|
||||
|
|
@ -432,7 +433,7 @@ class wvmConnect(object):
|
|||
for arch in ctx.xpath('/capabilities/guest/arch'):
|
||||
domain_types = arch.xpath('domain/@type')
|
||||
arch_name = arch.xpath('@name')[0]
|
||||
result[arch_name]= domain_types
|
||||
result[arch_name] = domain_types
|
||||
return result
|
||||
return util.get_xml_path(self.get_cap_xml(), func=hypervisors)
|
||||
|
||||
|
|
@ -446,7 +447,7 @@ class wvmConnect(object):
|
|||
for arch in ctx.xpath('/capabilities/guest/arch'):
|
||||
emulator = arch.xpath('emulator')
|
||||
arch_name = arch.xpath('@name')[0]
|
||||
result[arch_name]= emulator
|
||||
result[arch_name] = emulator
|
||||
return result
|
||||
return util.get_xml_path(self.get_cap_xml(), func=emulators)
|
||||
|
||||
|
|
@ -460,8 +461,9 @@ class wvmConnect(object):
|
|||
def get_bus_list(ctx):
|
||||
result = []
|
||||
for disk_enum in ctx.xpath('/domainCapabilities/devices/disk/enum'):
|
||||
if disk_enum.xpath("@name")[0] == "bus":
|
||||
for values in disk_enum: result.append(values.text)
|
||||
if disk_enum.xpath("@name")[0] == "bus":
|
||||
for values in disk_enum:
|
||||
result.append(values.text)
|
||||
return result
|
||||
|
||||
# return [ 'ide', 'scsi', 'usb', 'virtio' ]
|
||||
|
|
@ -474,7 +476,8 @@ class wvmConnect(object):
|
|||
result = []
|
||||
for disk_enum in ctx.xpath('/domainCapabilities/devices/disk/enum'):
|
||||
if disk_enum.xpath("@name")[0] == "diskDevice":
|
||||
for values in disk_enum: result.append(values.text)
|
||||
for values in disk_enum:
|
||||
result.append(values.text)
|
||||
return result
|
||||
|
||||
# return [ 'disk', 'cdrom', 'floppy', 'lun' ]
|
||||
|
|
@ -482,11 +485,11 @@ class wvmConnect(object):
|
|||
|
||||
def get_image_formats(self):
|
||||
"""Get available image formats"""
|
||||
return [ 'raw', 'qcow', 'qcow2' ]
|
||||
return ['raw', 'qcow', 'qcow2']
|
||||
|
||||
def get_file_extensions(self):
|
||||
"""Get available image filename extensions"""
|
||||
return [ 'img', 'qcow', 'qcow2' ]
|
||||
return ['img', 'qcow', 'qcow2']
|
||||
|
||||
def get_video(self):
|
||||
""" Get available graphics video types """
|
||||
|
|
@ -494,9 +497,10 @@ class wvmConnect(object):
|
|||
result = []
|
||||
for video_enum in ctx.xpath('/domainCapabilities/devices/video/enum'):
|
||||
if video_enum.xpath("@name")[0] == "modelType":
|
||||
for values in video_enum: result.append(values.text)
|
||||
for values in video_enum:
|
||||
result.append(values.text)
|
||||
return result
|
||||
return util.get_xml_path(self.get_dom_cap_xml(),func=get_video_list)
|
||||
return util.get_xml_path(self.get_dom_cap_xml(), func=get_video_list)
|
||||
|
||||
def get_iface(self, name):
|
||||
return self.wvm.interfaceLookupByName(name)
|
||||
|
|
@ -560,6 +564,7 @@ class wvmConnect(object):
|
|||
|
||||
def get_host_instances(self, raw_mem_size=False):
|
||||
vname = {}
|
||||
|
||||
def get_info(doc):
|
||||
mem = util.get_xpath(doc, "/domain/currentMemory")
|
||||
mem = int(mem) / 1024
|
||||
|
|
@ -592,6 +597,7 @@ class wvmConnect(object):
|
|||
def get_user_instances(self, name):
|
||||
dom = self.get_instance(name)
|
||||
xml = dom.XMLDesc(0)
|
||||
|
||||
def get_info(ctx):
|
||||
mem = util.get_xpath(ctx, "/domain/currentMemory")
|
||||
mem = int(mem) / 1024
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import string
|
|||
from vrtManager import util
|
||||
from vrtManager.connection import wvmConnect
|
||||
from webvirtcloud.settings import QEMU_CONSOLE_DEFAULT_TYPE
|
||||
from webvirtcloud.settings import QEMU_CONSOLE_LISTEN_ADDRESSES
|
||||
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_OWNER as default_owner
|
||||
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_FORMAT
|
||||
|
||||
|
|
@ -107,7 +106,7 @@ class wvmCreate(wvmConnect):
|
|||
if not pool:
|
||||
storages = self.get_storages(only_actives=True)
|
||||
else:
|
||||
storages = [pool,]
|
||||
storages = [pool]
|
||||
for storage in storages:
|
||||
stg = self.get_storage(storage)
|
||||
if stg.info()[0] != 0:
|
||||
|
|
@ -204,12 +203,13 @@ class wvmCreate(wvmConnect):
|
|||
hd_disk_letters = list(string.lowercase)
|
||||
sd_disk_letters = list(string.lowercase)
|
||||
add_cd = True
|
||||
#for image, img_type in images.items():
|
||||
# for image, img_type in images.items():
|
||||
for volume in images:
|
||||
stg = self.get_storage_by_vol_path(volume['path'])
|
||||
stg_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type")
|
||||
|
||||
if volume['device'] == 'cdrom': add_cd = False
|
||||
if volume['device'] == 'cdrom':
|
||||
add_cd = False
|
||||
|
||||
if stg_type == 'rbd':
|
||||
ceph_user, secret_uuid, ceph_hosts = get_rbd_storage_data(stg)
|
||||
|
|
@ -284,7 +284,4 @@ class wvmCreate(wvmConnect):
|
|||
</devices>
|
||||
</domain>""" % video
|
||||
|
||||
|
||||
|
||||
|
||||
self._defineXML(xml)
|
||||
|
|
|
|||
|
|
@ -19,9 +19,7 @@ class wvmHostDetails(wvmConnect):
|
|||
all_mem = self.wvm.getInfo()[1] * 1048576
|
||||
freemem = self.wvm.getMemoryStats(-1, 0)
|
||||
if type(freemem) == dict:
|
||||
free = (freemem.values()[0] +
|
||||
freemem.values()[2] +
|
||||
freemem.values()[3]) * 1024
|
||||
free = (freemem.values()[0] + freemem.values()[2] + freemem.values()[3]) * 1024
|
||||
percent = (100 - ((free * 100) / all_mem))
|
||||
usage = (all_mem - free)
|
||||
mem_usage = {'total': all_mem, 'usage': usage, 'percent': percent}
|
||||
|
|
@ -59,14 +57,10 @@ class wvmHostDetails(wvmConnect):
|
|||
Function return host server information: hostname, cpu, memory, ...
|
||||
"""
|
||||
info = []
|
||||
info.append(self.wvm.getHostname()) # hostname
|
||||
info.append(self.wvm.getInfo()[0]) # architecture
|
||||
info.append(self.wvm.getInfo()[1] * 1048576) # memory
|
||||
info.append(self.wvm.getInfo()[2]) # cpu core count
|
||||
info.append(get_xml_path(self.wvm.getSysinfo(0), func=cpu_version)) # cpu version
|
||||
info.append(self.wvm.getURI()) #uri
|
||||
info.append(self.wvm.getHostname()) # hostname
|
||||
info.append(self.wvm.getInfo()[0]) # architecture
|
||||
info.append(self.wvm.getInfo()[1] * 1048576) # memory
|
||||
info.append(self.wvm.getInfo()[2]) # cpu core count
|
||||
info.append(get_xml_path(self.wvm.getSysinfo(0), func=cpu_version)) # cpu version
|
||||
info.append(self.wvm.getURI()) # uri
|
||||
return info
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ class wvmInstance(wvmConnect):
|
|||
"""Get number of physical CPUs."""
|
||||
hostinfo = self.wvm.getInfo()
|
||||
pcpus = hostinfo[4] * hostinfo[5] * hostinfo[6] * hostinfo[7]
|
||||
range_pcpus = xrange(1, int(pcpus + 1))
|
||||
range_pcpus = range(1, int(pcpus + 1))
|
||||
return range_pcpus
|
||||
|
||||
def get_net_device(self):
|
||||
|
|
@ -249,7 +249,7 @@ class wvmInstance(wvmConnect):
|
|||
result = []
|
||||
dev = volume = storage = src_file = None
|
||||
disk_format = used_size = disk_size = disk_cache = None
|
||||
|
||||
|
||||
for disk in doc.xpath('/domain/devices/disk'):
|
||||
device = disk.xpath('@device')[0]
|
||||
if device == 'disk':
|
||||
|
|
@ -322,7 +322,7 @@ class wvmInstance(wvmConnect):
|
|||
os = tree.find('os')
|
||||
menu = os.find("bootmenu")
|
||||
|
||||
if menu == None:
|
||||
if menu is None:
|
||||
bootmenu = ElementTree.fromstring("<bootmenu enable='yes'/>")
|
||||
os.append(bootmenu)
|
||||
menu = os.find("bootmenu")
|
||||
|
|
@ -365,7 +365,7 @@ class wvmInstance(wvmConnect):
|
|||
for dev in devices:
|
||||
dev_target = dev_type = dev_device = dev_alias = None
|
||||
boot_dev = dev.find('boot')
|
||||
if boot_dev != None:
|
||||
if boot_dev is not None:
|
||||
idx = boot_dev.get('order')
|
||||
dev_type = dev.get('type')
|
||||
dev_device = dev.get('device')
|
||||
|
|
@ -398,7 +398,7 @@ class wvmInstance(wvmConnect):
|
|||
# Remove rest of them
|
||||
for dev in tree.find('devices'):
|
||||
boot_dev = dev.find('boot')
|
||||
if boot_dev != None:
|
||||
if boot_dev is not None:
|
||||
dev.remove(boot_dev)
|
||||
return tree
|
||||
|
||||
|
|
@ -410,19 +410,19 @@ class wvmInstance(wvmConnect):
|
|||
devices = tree.findall("./devices/disk[@device='disk']")
|
||||
for d in devices:
|
||||
device = d.find("./target[@dev='{}']".format(dev['dev']))
|
||||
if device != None:
|
||||
if device is not None:
|
||||
d.append(order)
|
||||
elif dev['type'] == 'cdrom':
|
||||
devices = tree.findall("./devices/disk[@device='cdrom']")
|
||||
for d in devices:
|
||||
device = d.find("./target[@dev='{}']".format(dev['dev']))
|
||||
if device != None:
|
||||
if device is not None:
|
||||
d.append(order)
|
||||
elif dev['type'] == 'network':
|
||||
devices = tree.findall("./devices/interface[@type='network']")
|
||||
for d in devices:
|
||||
device = d.find("mac[@address='{}']".format(dev['dev']))
|
||||
if device != None:
|
||||
if device is not None:
|
||||
d.append(order)
|
||||
else:
|
||||
raise Exception('Invalid Device Type for boot order')
|
||||
|
|
@ -520,7 +520,7 @@ class wvmInstance(wvmConnect):
|
|||
time.sleep(1)
|
||||
cpu_use_now = self.instance.info()[4]
|
||||
diff_usage = cpu_use_now - cpu_use_ago
|
||||
cpu_usage['cpu'] = 100 * diff_usage / (1 * nbcore * 10 ** 9L)
|
||||
cpu_usage['cpu'] = 100 * diff_usage / (1 * nbcore * 10 ** 9)
|
||||
else:
|
||||
cpu_usage['cpu'] = 0
|
||||
return cpu_usage
|
||||
|
|
@ -532,7 +532,8 @@ class wvmInstance(wvmConnect):
|
|||
rss = mem_stats['rss'] if mem_stats['rss'] else 0
|
||||
total = mem_stats['actual'] if mem_stats['actual'] else 0
|
||||
available = total - rss
|
||||
if available < 0: available = 0
|
||||
if available < 0:
|
||||
available = 0
|
||||
|
||||
mem_usage['used'] = rss
|
||||
mem_usage['total'] = total
|
||||
|
|
@ -622,7 +623,7 @@ class wvmInstance(wvmConnect):
|
|||
if listen_addr is None:
|
||||
listen_addr = util.get_xml_path(self._XMLDesc(0), "/domain/devices/graphics/listen/@address")
|
||||
if listen_addr is None:
|
||||
return "127.0.0.1"
|
||||
return "127.0.0.1"
|
||||
return listen_addr
|
||||
|
||||
def set_console_listen_addr(self, listen_addr):
|
||||
|
|
@ -650,13 +651,13 @@ class wvmInstance(wvmConnect):
|
|||
pass
|
||||
newxml = ElementTree.tostring(root)
|
||||
return self._defineXML(newxml)
|
||||
|
||||
|
||||
def get_console_socket(self):
|
||||
socket = util.get_xml_path(self._XMLDesc(0), "/domain/devices/graphics/@socket")
|
||||
return socket
|
||||
|
||||
def get_console_type(self):
|
||||
console_type = util.get_xml_path(self._XMLDesc(0),"/domain/devices/graphics/@type")
|
||||
console_type = util.get_xml_path(self._XMLDesc(0), "/domain/devices/graphics/@type")
|
||||
return console_type
|
||||
|
||||
def set_console_type(self, console_type):
|
||||
|
|
@ -765,7 +766,7 @@ class wvmInstance(wvmConnect):
|
|||
source_dev = disk['path']
|
||||
vol = self.get_volume_by_path(source_dev)
|
||||
vol.resize(disk['size_new'])
|
||||
|
||||
|
||||
new_xml = ElementTree.tostring(tree)
|
||||
self._defineXML(new_xml)
|
||||
|
||||
|
|
@ -901,7 +902,7 @@ class wvmInstance(wvmConnect):
|
|||
|
||||
stg = vol.storagePoolLookupByVolume()
|
||||
stg.createXMLFrom(vol_clone_xml, vol, meta_prealloc)
|
||||
|
||||
|
||||
source_protocol = elm.get('protocol')
|
||||
if source_protocol == 'rbd':
|
||||
source_name = elm.get('name')
|
||||
|
|
@ -927,13 +928,13 @@ class wvmInstance(wvmConnect):
|
|||
if source_dev:
|
||||
clone_path = os.path.join(os.path.dirname(source_dev), target_file)
|
||||
elm.set('dev', clone_path)
|
||||
|
||||
|
||||
vol = self.get_volume_by_path(source_dev)
|
||||
stg = vol.storagePoolLookupByVolume()
|
||||
|
||||
|
||||
vol_name = util.get_xml_path(vol.XMLDesc(0), "/volume/name")
|
||||
pool_name = util.get_xml_path(stg.XMLDesc(0), "/pool/name")
|
||||
|
||||
|
||||
storage = self.get_wvmStorage(pool_name)
|
||||
storage.clone_volume(vol_name, target_file)
|
||||
|
||||
|
|
@ -954,7 +955,7 @@ class wvmInstance(wvmConnect):
|
|||
net = self.get_network(source)
|
||||
bridge_name = net.bridgeName()
|
||||
return bridge_name
|
||||
|
||||
|
||||
def add_network(self, mac_address, source, source_type='net', interface_type='bridge', model='virtio', nwfilter=None):
|
||||
tree = ElementTree.fromstring(self._XMLDesc(0))
|
||||
bridge_name = self.get_bridge_name(source, source_type)
|
||||
|
|
@ -1007,13 +1008,15 @@ class wvmInstance(wvmConnect):
|
|||
source = interface.find('filterref')
|
||||
|
||||
if net_filter:
|
||||
if source is not None: source.set('filter', net_filter)
|
||||
if source is not None:
|
||||
source.set('filter', net_filter)
|
||||
else:
|
||||
element = ElementTree.Element("filterref")
|
||||
element.attrib['filter'] = net_filter
|
||||
interface.append(element)
|
||||
else:
|
||||
if source is not None: interface.remove(source)
|
||||
if source is not None:
|
||||
interface.remove(source)
|
||||
elif interface.get('type') == 'network':
|
||||
source = interface.find('mac')
|
||||
source.set('address', net_mac)
|
||||
|
|
@ -1022,13 +1025,15 @@ class wvmInstance(wvmConnect):
|
|||
source = interface.find('filterref')
|
||||
|
||||
if net_filter:
|
||||
if source is not None: source.set('filter', net_filter)
|
||||
if source is not None:
|
||||
source.set('filter', net_filter)
|
||||
else:
|
||||
element = ElementTree.Element("filterref")
|
||||
element.attrib['filter'] = net_filter
|
||||
interface.append(element)
|
||||
else:
|
||||
if source is not None: interface.remove(source)
|
||||
if source is not None:
|
||||
interface.remove(source)
|
||||
|
||||
new_xml = ElementTree.tostring(tree)
|
||||
self._defineXML(new_xml)
|
||||
|
|
@ -1038,7 +1043,7 @@ class wvmInstance(wvmConnect):
|
|||
option = tree.find(o)
|
||||
option_value = options.get(o, '').strip()
|
||||
if not option_value:
|
||||
if not option is None:
|
||||
if option is not None:
|
||||
tree.remove(option)
|
||||
else:
|
||||
if option is None:
|
||||
|
|
@ -1059,4 +1064,3 @@ class wvmInstance(wvmConnect):
|
|||
|
||||
def set_memory(self, size, flags=0):
|
||||
self.instance.setMemoryFlags(size, flags)
|
||||
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ class wvmNetwork(wvmConnect):
|
|||
return result
|
||||
|
||||
return util.get_xml_path(self._XMLDesc(0), func=network)
|
||||
|
||||
|
||||
def modify_fixed_address(self, name, address, mac):
|
||||
util.validate_macaddr(mac)
|
||||
if name:
|
||||
|
|
@ -204,14 +204,14 @@ class wvmNetwork(wvmConnect):
|
|||
break
|
||||
if host is None:
|
||||
self.update(VIR_NETWORK_UPDATE_COMMAND_ADD_LAST, VIR_NETWORK_SECTION_IP_DHCP_HOST, -1, new_xml,
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE|VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
else:
|
||||
# change the host
|
||||
if host.get('name') == new_host_xml.get('name') and host.get('ip') == new_host_xml.get('ip'):
|
||||
return False
|
||||
else:
|
||||
self.update(VIR_NETWORK_UPDATE_COMMAND_MODIFY, VIR_NETWORK_SECTION_IP_DHCP_HOST, -1, new_xml,
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE|VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
|
||||
def delete_fixed_address(self, mac):
|
||||
util.validate_macaddr(mac)
|
||||
|
|
@ -222,7 +222,7 @@ class wvmNetwork(wvmConnect):
|
|||
if h.get('mac') == mac:
|
||||
new_xml = '<host mac="{}" name="{}" ip="{}"/>'.format(mac, h.get('name'), h.get('ip'))
|
||||
self.update(VIR_NETWORK_UPDATE_COMMAND_DELETE, VIR_NETWORK_SECTION_IP_DHCP_HOST, -1, new_xml,
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE|VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
VIR_NETWORK_UPDATE_AFFECT_LIVE | VIR_NETWORK_UPDATE_AFFECT_CONFIG)
|
||||
break
|
||||
|
||||
def modify_dhcp_range(self, range_start, range_end):
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class wvmNWFilters(wvmConnect):
|
|||
def create_nwfilter(self, xml):
|
||||
self.wvm.nwfilterDefineXML(xml)
|
||||
|
||||
def clone_nwfilter(self,name, cln_name):
|
||||
def clone_nwfilter(self, name, cln_name):
|
||||
nwfilter = self.get_nwfilter(name)
|
||||
if nwfilter:
|
||||
tree = ElementTree.fromstring(nwfilter.XMLDesc(0))
|
||||
|
|
|
|||
|
|
@ -143,13 +143,13 @@ class wvmStorage(wvmConnect):
|
|||
return util.get_xml_path(self._XMLDesc(0), "/pool/target/path")
|
||||
|
||||
def get_allocation(self):
|
||||
return long(util.get_xml_path(self._XMLDesc(0), "/pool/allocation"))
|
||||
return int(util.get_xml_path(self._XMLDesc(0), "/pool/allocation"))
|
||||
|
||||
def get_available(self):
|
||||
return long(util.get_xml_path(self._XMLDesc(0), "/pool/available"))
|
||||
return int(util.get_xml_path(self._XMLDesc(0), "/pool/available"))
|
||||
|
||||
def get_capacity(self):
|
||||
return long(util.get_xml_path(self._XMLDesc(0), "/pool/capacity"))
|
||||
return int(util.get_xml_path(self._XMLDesc(0), "/pool/capacity"))
|
||||
|
||||
def get_pretty_allocation(self):
|
||||
return util.pretty_bytes(self.get_allocation())
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ def randomUUID():
|
|||
"%02x" * 6]) % tuple(u)
|
||||
|
||||
|
||||
def randomPasswd(length=12, alphabet=string.letters + string.digits):
|
||||
def randomPasswd(length=12, alphabet=string.ascii_letters + string.digits):
|
||||
"""Generate a random password"""
|
||||
return ''.join([random.choice(alphabet) for i in xrange(length)])
|
||||
return ''.join([random.choice(alphabet) for i in range(length)])
|
||||
|
||||
|
||||
def get_max_vcpus(conn, type=None):
|
||||
|
|
@ -75,7 +75,7 @@ def compareMAC(p, q):
|
|||
else:
|
||||
return -1
|
||||
|
||||
for i in xrange(len(pa)):
|
||||
for i in range(len(pa)):
|
||||
n = int(pa[i], 0x10) - int(qa[i], 0x10)
|
||||
if n > 0:
|
||||
return 1
|
||||
|
|
@ -110,7 +110,7 @@ def get_xpath(doc, path):
|
|||
if ret is not None:
|
||||
if type(ret) == list:
|
||||
if len(ret) >= 1:
|
||||
if hasattr(ret[0],'text'):
|
||||
if hasattr(ret[0], 'text'):
|
||||
result = ret[0].text
|
||||
else:
|
||||
result = ret[0]
|
||||
|
|
@ -119,6 +119,7 @@ def get_xpath(doc, path):
|
|||
|
||||
return result
|
||||
|
||||
|
||||
def pretty_mem(val):
|
||||
val = int(val)
|
||||
if val > (10 * 1024 * 1024):
|
||||
|
|
@ -145,13 +146,11 @@ def validate_uuid(val):
|
|||
form = re.match("[a-fA-F0-9]{32}$", val)
|
||||
if form is None:
|
||||
raise ValueError(
|
||||
"UUID must be a 32-digit hexadecimal number. It may take "
|
||||
"the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or may "
|
||||
"omit hyphens altogether.")
|
||||
"UUID must be a 32-digit hexadecimal number. It may take the form "
|
||||
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or may omit hyphens altogether.")
|
||||
|
||||
else: # UUID had no dashes, so add them in
|
||||
val = (val[0:8] + "-" + val[8:12] + "-" + val[12:16] +
|
||||
"-" + val[16:20] + "-" + val[20:32])
|
||||
val = (val[0:8] + "-" + val[8:12] + "-" + val[12:16] + "-" + val[16:20] + "-" + val[20:32])
|
||||
return val
|
||||
|
||||
|
||||
|
|
@ -159,7 +158,7 @@ def validate_macaddr(val):
|
|||
if val is None:
|
||||
return
|
||||
|
||||
if not (isinstance(val, str) or isinstance(val, basestring)):
|
||||
if not (isinstance(val, str) or isinstance(val, str)):
|
||||
raise ValueError("MAC address must be a string.")
|
||||
|
||||
form = re.match("^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$", val)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue