util: Move default_* network bits to deviceinterface

This commit is contained in:
Cole Robinson 2016-08-24 16:14:33 -04:00
parent 927be95b95
commit 4c759ad23d
3 changed files with 70 additions and 70 deletions

View File

@ -127,14 +127,13 @@ class TestXMLMisc(unittest.TestCase):
def testDefaultBridge(self):
# Test our handling of the default bridge routines
origfunc = None
util = None
try:
util = getattr(virtinst, "util")
origfunc = util.default_bridge
from virtinst import deviceinterface
origfunc = getattr(deviceinterface, "_default_bridge")
def newbridge(ignore_conn):
return "bzz0"
util.default_bridge = newbridge
setattr(deviceinterface, "_default_bridge", newbridge)
dev1 = virtinst.VirtualNetworkInterface(_default_conn)
dev1.macaddr = "22:22:33:44:55:66"
@ -165,8 +164,9 @@ class TestXMLMisc(unittest.TestCase):
" <mac address=\"22:22:33:44:55:68\"/>\n"
"</interface>\n")
finally:
if util and origfunc:
util.default_bridge = origfunc
if origfunc:
from virtinst import deviceinterface
setattr(deviceinterface, "_default_bridge", origfunc)
def testCpustrToTuple(self):
# Various testing our cpustr handling

View File

@ -18,6 +18,7 @@
# MA 02110-1301 USA.
import logging
import os
import random
from . import util
@ -52,6 +53,67 @@ def _random_mac(conn):
return ':'.join(["%02x" % x for x in mac])
def _default_route():
route_file = "/proc/net/route"
if not os.path.exists(route_file):
logging.debug("route_file=%s does not exist", route_file)
return None
for line in file(route_file):
info = line.split()
if len(info) != 11:
logging.debug("Unexpected field count=%s when parsing %s",
len(info), route_file)
break
try:
route = int(info[1], 16)
if route == 0:
return info[0]
except ValueError:
continue
return None
def _default_bridge(conn):
if "VIRTINST_TEST_SUITE" in os.environ:
return "eth0"
if conn.is_remote():
return None
dev = _default_route()
if not dev:
return None
# New style peth0 == phys dev, eth0 == bridge, eth0 == default route
if os.path.exists("/sys/class/net/%s/bridge" % dev):
return dev
# Old style, peth0 == phys dev, eth0 == netloop, xenbr0 == bridge,
# vif0.0 == netloop enslaved, eth0 == default route
try:
defn = int(dev[-1])
except:
defn = -1
if (defn >= 0 and
os.path.exists("/sys/class/net/peth%d/brport" % defn) and
os.path.exists("/sys/class/net/xenbr%d/bridge" % defn)):
return "xenbr%d"
return None
def _default_network(conn):
ret = _default_bridge(conn)
if ret:
return ["bridge", ret]
# FIXME: Check that this exists
return ["network", "default"]
class VirtualPort(XMLBuilder):
_XML_ROOT_NAME = "virtualport"
@ -148,7 +210,7 @@ class VirtualNetworkInterface(VirtualDevice):
ret = self._default_bridge
if ret is None:
ret = False
default = util.default_bridge(self.conn)
default = _default_bridge(self.conn)
if default:
ret = default
@ -262,7 +324,7 @@ class VirtualNetworkInterface(VirtualDevice):
if (self.conn.is_qemu_session() or self.conn.is_test()):
self.type = self.TYPE_USER
else:
self.type, self.source = util.default_network(self.conn)
self.type, self.source = _default_network(self.conn)
VirtualNetworkInterface.register_type()

View File

@ -160,34 +160,6 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True,
raise ValueError(_("Name generation range exceeded."))
def default_bridge(conn):
if "VIRTINST_TEST_SUITE" in os.environ:
return "eth0"
if conn.is_remote():
return None
dev = default_route()
if not dev:
return None
# New style peth0 == phys dev, eth0 == bridge, eth0 == default route
if os.path.exists("/sys/class/net/%s/bridge" % dev):
return dev
# Old style, peth0 == phys dev, eth0 == netloop, xenbr0 == bridge,
# vif0.0 == netloop enslaved, eth0 == default route
try:
defn = int(dev[-1])
except:
defn = -1
if (defn >= 0 and
os.path.exists("/sys/class/net/peth%d/brport" % defn) and
os.path.exists("/sys/class/net/xenbr%d/bridge" % defn)):
return "xenbr%d"
return None
def generate_uuid(conn):
for ignore in range(256):
@ -199,40 +171,6 @@ def generate_uuid(conn):
def default_route():
route_file = "/proc/net/route"
if not os.path.exists(route_file):
logging.debug("route_file=%s does not exist", route_file)
return None
d = file(route_file)
defn = 0
for line in d.xreadlines():
info = line.split()
if (len(info) != 11): # 11 = typical num of fields in the file
logging.warn(_("Invalid line length while parsing %s."),
route_file)
logging.warn(_("Defaulting bridge to xenbr%d"), defn)
break
try:
route = int(info[1], 16)
if route == 0:
return info[0]
except ValueError:
continue
return None
def default_network(conn):
ret = default_bridge(conn)
if ret:
return ["bridge", ret]
# FIXME: Check that this exists
return ["network", "default"]
def randomUUID(conn):
if conn.fake_conn_predictable():
# Testing hack