mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +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:
@@ -16,6 +16,15 @@ import os
|
|||||||
from xml.dom.minidom import getDOMImplementation
|
from xml.dom.minidom import getDOMImplementation
|
||||||
from xml.dom.minidom import parse as parseXML
|
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.
|
# Logging.
|
||||||
#
|
#
|
||||||
@@ -38,7 +47,7 @@ class Error(Exception):
|
|||||||
|
|
||||||
def run_command(command):
|
def run_command(command):
|
||||||
log("Running command: " + ' '.join(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:
|
if rc != 0:
|
||||||
log("Command failed %d: " % rc + ' '.join(command))
|
log("Command failed %d: " % rc + ' '.join(command))
|
||||||
return False
|
return False
|
||||||
@@ -323,7 +332,7 @@ def db_init_from_xenapi(session):
|
|||||||
|
|
||||||
class DatabaseCache(object):
|
class DatabaseCache(object):
|
||||||
def __read_xensource_inventory(self):
|
def __read_xensource_inventory(self):
|
||||||
filename = "/etc/xensource-inventory"
|
filename = root_prefix() + "/etc/xensource-inventory"
|
||||||
f = open(filename, "r")
|
f = open(filename, "r")
|
||||||
lines = [x.strip("\n") for x in f.readlines()]
|
lines = [x.strip("\n") for x in f.readlines()]
|
||||||
f.close()
|
f.close()
|
||||||
@@ -440,7 +449,7 @@ class DatabaseCache(object):
|
|||||||
else:
|
else:
|
||||||
log("Loading xapi database cache from %s" % cache_file)
|
log("Loading xapi database cache from %s" % cache_file)
|
||||||
|
|
||||||
xml = parseXML(cache_file)
|
xml = parseXML(root_prefix() + cache_file)
|
||||||
|
|
||||||
self.__pifs = {}
|
self.__pifs = {}
|
||||||
self.__bonds = {}
|
self.__bonds = {}
|
||||||
@@ -625,7 +634,7 @@ def pif_ipdev_name(pif):
|
|||||||
#
|
#
|
||||||
|
|
||||||
def netdev_exists(netdev):
|
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):
|
def pif_netdev_name(pif):
|
||||||
"""Get the netdev name for a PIF."""
|
"""Get the netdev name for a PIF."""
|
||||||
@@ -806,7 +815,7 @@ def DatapathFactory(pif):
|
|||||||
# XXX Need a datapath object for bridgeless PIFs
|
# XXX Need a datapath object for bridgeless PIFs
|
||||||
|
|
||||||
try:
|
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_backend = network_conf.readline().strip()
|
||||||
network_conf.close()
|
network_conf.close()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ from InterfaceReconfigure import *
|
|||||||
import sys
|
import sys
|
||||||
import time
|
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):
|
def open_pif_ifcfg(pif):
|
||||||
pifrec = db().get_pif_record(pif)
|
pifrec = db().get_pif_record(pif)
|
||||||
@@ -23,7 +23,7 @@ def open_pif_ifcfg(pif):
|
|||||||
interface = pif_netdev_name(pif)
|
interface = pif_netdev_name(pif)
|
||||||
log("Configuring %s (%s)" % (interface, pifrec['MAC']))
|
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" % \
|
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
|
||||||
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
|
(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)
|
log("Failed to load bonding driver: %s" % e)
|
||||||
|
|
||||||
def bonding_driver_loaded():
|
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]
|
modules = [line.split(" ")[0] for line in lines]
|
||||||
return "bonding" in modules
|
return "bonding" in modules
|
||||||
|
|
||||||
@@ -226,7 +226,7 @@ def bring_down_interface(pif, destroy=False):
|
|||||||
def interface_is_up(pif):
|
def interface_is_up(pif):
|
||||||
try:
|
try:
|
||||||
interface = pif_netdev_name(pif)
|
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"
|
return state == "up"
|
||||||
except:
|
except:
|
||||||
return False # interface prolly doesn't exist
|
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
|
# which they were attached. The first slave attached must be the last detached since
|
||||||
# the bond is using its MAC address.
|
# the bond is using its MAC address.
|
||||||
try:
|
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:
|
for slave in attached_slaves:
|
||||||
pifs = [p for p in db().get_pifs_by_device(slave) if not pif_is_vlan(p)]
|
pifs = [p for p in db().get_pifs_by_device(slave) if not pif_is_vlan(p)]
|
||||||
slave_pif = pifs[0]
|
slave_pif = pifs[0]
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
--pif A PIF reference within the session.
|
--pif A PIF reference within the session.
|
||||||
--pif-uuid The UUID of a PIF.
|
--pif-uuid The UUID of a PIF.
|
||||||
--force An interface name.
|
--force An interface name.
|
||||||
|
--root-prefix=DIR Use DIR as alternate root directory (for testing).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Notes:
|
# Notes:
|
||||||
@@ -88,7 +89,7 @@ def check_allowed(pif):
|
|||||||
|
|
||||||
pifrec = db().get_pif_record(pif)
|
pifrec = db().get_pif_record(pif)
|
||||||
try:
|
try:
|
||||||
f = open("/proc/ardence")
|
f = open(root_prefix() + "/proc/ardence")
|
||||||
macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines())
|
macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines())
|
||||||
f.close()
|
f.close()
|
||||||
if len(macline) == 1:
|
if len(macline) == 1:
|
||||||
@@ -121,20 +122,20 @@ def netdev_remap_name(pif, already_renamed=[]):
|
|||||||
|
|
||||||
def get_netdev_mac(device):
|
def get_netdev_mac(device):
|
||||||
try:
|
try:
|
||||||
return read1("/sys/class/net/%s/address" % device)
|
return read1("%s/sys/class/net/%s/address" % (root_prefix(), device))
|
||||||
except:
|
except:
|
||||||
# Probably no such device.
|
# Probably no such device.
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_netdev_tx_queue_len(device):
|
def get_netdev_tx_queue_len(device):
|
||||||
try:
|
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:
|
except:
|
||||||
# Probably no such device.
|
# Probably no such device.
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_netdev_by_mac(mac):
|
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)
|
dev_mac = get_netdev_mac(device)
|
||||||
if (dev_mac and mac.lower() == dev_mac.lower() and
|
if (dev_mac and mac.lower() == dev_mac.lower() and
|
||||||
get_netdev_tx_queue_len(device)):
|
get_netdev_tx_queue_len(device)):
|
||||||
@@ -185,7 +186,7 @@ def ifdown(netdev):
|
|||||||
if not netdev_exists(netdev):
|
if not netdev_exists(netdev):
|
||||||
log("ifdown: device %s does not exist, ignoring" % netdev)
|
log("ifdown: device %s does not exist, ignoring" % netdev)
|
||||||
return
|
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))
|
log("ifdown: device %s exists but ifcfg-%s does not" % (netdev,netdev))
|
||||||
run_command(["/sbin/ifconfig", netdev, 'down'])
|
run_command(["/sbin/ifconfig", netdev, 'down'])
|
||||||
return
|
return
|
||||||
@@ -193,7 +194,7 @@ def ifdown(netdev):
|
|||||||
|
|
||||||
def ifup(netdev):
|
def ifup(netdev):
|
||||||
"""Bring up a network interface"""
|
"""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))
|
raise Error("ifup: device %s exists but ifcfg-%s does not" % (netdev,netdev))
|
||||||
run_command(["/sbin/ifup", 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
|
# The key is not present, i.e. there are no static routes
|
||||||
lines = []
|
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" % \
|
child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
|
||||||
(os.path.basename(child.path()), os.path.basename(sys.argv[0])))
|
(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)
|
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" % \
|
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
|
||||||
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
|
(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
|
is_gatewaydev = defaultroute_pif == pif
|
||||||
|
|
||||||
if is_dnsdev or is_gatewaydev:
|
if is_dnsdev or is_gatewaydev:
|
||||||
fnetwork = ConfigurationFile("/etc/sysconfig/network")
|
fnetwork = ConfigurationFile(root_prefix() + "/etc/sysconfig/network")
|
||||||
for line in fnetwork.readlines():
|
for line in fnetwork.readlines():
|
||||||
if is_dnsdev and line.lstrip().startswith('DNSDEV='):
|
if is_dnsdev and line.lstrip().startswith('DNSDEV='):
|
||||||
fnetwork.write('DNSDEV=%s\n' % ipdev)
|
fnetwork.write('DNSDEV=%s\n' % ipdev)
|
||||||
@@ -441,7 +442,7 @@ def action_up(pif, force):
|
|||||||
dp.post()
|
dp.post()
|
||||||
|
|
||||||
# Update /etc/issue (which contains the IP address of the management interface)
|
# 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()
|
f.commit()
|
||||||
except Error, e:
|
except Error, e:
|
||||||
@@ -502,7 +503,7 @@ def action_force_rewrite(bridge, config):
|
|||||||
|
|
||||||
log("Configuring %s using %s configuration" % (bridge, mode))
|
log("Configuring %s using %s configuration" % (bridge, mode))
|
||||||
|
|
||||||
f = ConfigurationFile(dbcache_file)
|
f = ConfigurationFile(root_prefix() + dbcache_file)
|
||||||
|
|
||||||
pif_uuid = getUUID()
|
pif_uuid = getUUID()
|
||||||
network_uuid = getUUID()
|
network_uuid = getUUID()
|
||||||
@@ -580,6 +581,7 @@ def main(argv=None):
|
|||||||
"force-interface=",
|
"force-interface=",
|
||||||
"management",
|
"management",
|
||||||
"mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
|
"mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
|
||||||
|
"root-prefix=",
|
||||||
"help" ]
|
"help" ]
|
||||||
arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
|
arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
|
||||||
except getopt.GetoptError, msg:
|
except getopt.GetoptError, msg:
|
||||||
@@ -600,6 +602,8 @@ def main(argv=None):
|
|||||||
force_management = True
|
force_management = True
|
||||||
elif o in ["--mac", "--device", "--mode", "--ip", "--netmask", "--gateway"]:
|
elif o in ["--mac", "--device", "--mode", "--ip", "--netmask", "--gateway"]:
|
||||||
force_rewrite_config[o[2:]] = a
|
force_rewrite_config[o[2:]] = a
|
||||||
|
elif o == "--root-prefix":
|
||||||
|
set_root_prefix(a)
|
||||||
elif o == "-h" or o == "--help":
|
elif o == "-h" or o == "--help":
|
||||||
print __doc__ % {'command-name': os.path.basename(argv[0])}
|
print __doc__ % {'command-name': os.path.basename(argv[0])}
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user