2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-21 14:49:41 +00:00

xenserver: Add --root-prefix feature to interface-reconfigure.

This makes it easier to do unit tests (some of which will be added in an
upcoming commit) by allowing fake configuration files and scripts to be
added in a directory other than the real root.

Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Ben Pfaff
2010-02-22 16:25:54 -08:00
parent 86e1bb44da
commit 64ddb6fecf
3 changed files with 34 additions and 21 deletions

View File

@@ -16,6 +16,15 @@ import os
from xml.dom.minidom import getDOMImplementation
from xml.dom.minidom import parse as parseXML
the_root_prefix = ""
def root_prefix():
"""Returns a string to prefix to all file name references, which
is useful for testing."""
return the_root_prefix
def set_root_prefix(prefix):
global the_root_prefix
the_root_prefix = prefix
#
# Logging.
#
@@ -38,7 +47,7 @@ class Error(Exception):
def run_command(command):
log("Running command: " + ' '.join(command))
rc = os.spawnl(os.P_WAIT, command[0], *command)
rc = os.spawnl(os.P_WAIT, root_prefix() + command[0], *command)
if rc != 0:
log("Command failed %d: " % rc + ' '.join(command))
return False
@@ -323,7 +332,7 @@ def db_init_from_xenapi(session):
class DatabaseCache(object):
def __read_xensource_inventory(self):
filename = "/etc/xensource-inventory"
filename = root_prefix() + "/etc/xensource-inventory"
f = open(filename, "r")
lines = [x.strip("\n") for x in f.readlines()]
f.close()
@@ -440,7 +449,7 @@ class DatabaseCache(object):
else:
log("Loading xapi database cache from %s" % cache_file)
xml = parseXML(cache_file)
xml = parseXML(root_prefix() + cache_file)
self.__pifs = {}
self.__bonds = {}
@@ -625,7 +634,7 @@ def pif_ipdev_name(pif):
#
def netdev_exists(netdev):
return os.path.exists("/sys/class/net/" + netdev)
return os.path.exists(root_prefix() + "/sys/class/net/" + netdev)
def pif_netdev_name(pif):
"""Get the netdev name for a PIF."""
@@ -806,7 +815,7 @@ def DatapathFactory(pif):
# XXX Need a datapath object for bridgeless PIFs
try:
network_conf = open("/etc/xensource/network.conf", 'r')
network_conf = open(root_prefix() + "/etc/xensource/network.conf", 'r')
network_backend = network_conf.readline().strip()
network_conf.close()
except Exception, e:

View File

@@ -15,7 +15,7 @@ from InterfaceReconfigure import *
import sys
import time
sysfs_bonding_masters = "/sys/class/net/bonding_masters"
sysfs_bonding_masters = root_prefix() + "/sys/class/net/bonding_masters"
def open_pif_ifcfg(pif):
pifrec = db().get_pif_record(pif)
@@ -23,7 +23,7 @@ def open_pif_ifcfg(pif):
interface = pif_netdev_name(pif)
log("Configuring %s (%s)" % (interface, pifrec['MAC']))
f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % interface)
f = ConfigurationFile("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), interface))
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
@@ -69,7 +69,7 @@ def load_bonding_driver():
log("Failed to load bonding driver: %s" % e)
def bonding_driver_loaded():
lines = open("/proc/modules").read().split("\n")
lines = open(root_prefix() + "/proc/modules").read().split("\n")
modules = [line.split(" ")[0] for line in lines]
return "bonding" in modules
@@ -226,7 +226,7 @@ def bring_down_interface(pif, destroy=False):
def interface_is_up(pif):
try:
interface = pif_netdev_name(pif)
state = open("/sys/class/net/%s/operstate" % interface).read().strip()
state = open("%s/sys/class/net/%s/operstate" % (root_prefix(), interface)).read().strip()
return state == "up"
except:
return False # interface prolly doesn't exist
@@ -294,7 +294,7 @@ def pif_get_bond_slaves_sorted(pif):
# which they were attached. The first slave attached must be the last detached since
# the bond is using its MAC address.
try:
attached_slaves = open("/sys/class/net/%s/bonding/slaves" % pifrec['device']).readline().split()
attached_slaves = open("%s/sys/class/net/%s/bonding/slaves" % (root_prefix(), pifrec['device'])).readline().split()
for slave in attached_slaves:
pifs = [p for p in db().get_pifs_by_device(slave) if not pif_is_vlan(p)]
slave_pif = pifs[0]

View File

@@ -33,6 +33,7 @@
--pif A PIF reference within the session.
--pif-uuid The UUID of a PIF.
--force An interface name.
--root-prefix=DIR Use DIR as alternate root directory (for testing).
"""
# Notes:
@@ -88,7 +89,7 @@ def check_allowed(pif):
pifrec = db().get_pif_record(pif)
try:
f = open("/proc/ardence")
f = open(root_prefix() + "/proc/ardence")
macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines())
f.close()
if len(macline) == 1:
@@ -121,20 +122,20 @@ def netdev_remap_name(pif, already_renamed=[]):
def get_netdev_mac(device):
try:
return read1("/sys/class/net/%s/address" % device)
return read1("%s/sys/class/net/%s/address" % (root_prefix(), device))
except:
# Probably no such device.
return None
def get_netdev_tx_queue_len(device):
try:
return int(read1("/sys/class/net/%s/tx_queue_len" % device))
return int(read1("%s/sys/class/net/%s/tx_queue_len" % (root_prefix(), device)))
except:
# Probably no such device.
return None
def get_netdev_by_mac(mac):
for device in os.listdir("/sys/class/net"):
for device in os.listdir(root_prefix() + "/sys/class/net"):
dev_mac = get_netdev_mac(device)
if (dev_mac and mac.lower() == dev_mac.lower() and
get_netdev_tx_queue_len(device)):
@@ -185,7 +186,7 @@ def ifdown(netdev):
if not netdev_exists(netdev):
log("ifdown: device %s does not exist, ignoring" % netdev)
return
if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
if not os.path.exists("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), netdev)):
log("ifdown: device %s exists but ifcfg-%s does not" % (netdev,netdev))
run_command(["/sbin/ifconfig", netdev, 'down'])
return
@@ -193,7 +194,7 @@ def ifdown(netdev):
def ifup(netdev):
"""Bring up a network interface"""
if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
if not os.path.exists(root_prefix() + "/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
raise Error("ifup: device %s exists but ifcfg-%s does not" % (netdev,netdev))
run_command(["/sbin/ifup", netdev])
@@ -238,7 +239,7 @@ def ipdev_configure_static_routes(interface, oc, f):
# The key is not present, i.e. there are no static routes
lines = []
child = ConfigurationFile("/etc/sysconfig/network-scripts/route-%s" % interface)
child = ConfigurationFile("%s/etc/sysconfig/network-scripts/route-%s" % (root_prefix(), interface))
child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(child.path()), os.path.basename(sys.argv[0])))
@@ -258,7 +259,7 @@ def ipdev_open_ifcfg(pif):
log("Writing network configuration for %s" % ipdev)
f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % ipdev)
f = ConfigurationFile("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), ipdev))
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
@@ -382,7 +383,7 @@ def ipdev_configure_network(pif, dp):
is_gatewaydev = defaultroute_pif == pif
if is_dnsdev or is_gatewaydev:
fnetwork = ConfigurationFile("/etc/sysconfig/network")
fnetwork = ConfigurationFile(root_prefix() + "/etc/sysconfig/network")
for line in fnetwork.readlines():
if is_dnsdev and line.lstrip().startswith('DNSDEV='):
fnetwork.write('DNSDEV=%s\n' % ipdev)
@@ -441,7 +442,7 @@ def action_up(pif, force):
dp.post()
# Update /etc/issue (which contains the IP address of the management interface)
os.system("/sbin/update-issue")
os.system(root_prefix() + "/sbin/update-issue")
f.commit()
except Error, e:
@@ -502,7 +503,7 @@ def action_force_rewrite(bridge, config):
log("Configuring %s using %s configuration" % (bridge, mode))
f = ConfigurationFile(dbcache_file)
f = ConfigurationFile(root_prefix() + dbcache_file)
pif_uuid = getUUID()
network_uuid = getUUID()
@@ -580,6 +581,7 @@ def main(argv=None):
"force-interface=",
"management",
"mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
"root-prefix=",
"help" ]
arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
except getopt.GetoptError, msg:
@@ -600,6 +602,8 @@ def main(argv=None):
force_management = True
elif o in ["--mac", "--device", "--mode", "--ip", "--netmask", "--gateway"]:
force_rewrite_config[o[2:]] = a
elif o == "--root-prefix":
set_root_prefix(a)
elif o == "-h" or o == "--help":
print __doc__ % {'command-name': os.path.basename(argv[0])}
return 0