# HG changeset patch # User Ben Pfaff # Date 1278595551 -3600 # Node ID 78a1d9979fbf6c9e19d78dad452c3d28abaf0a6d # Parent b06691271200bfb9903e3e49b2dd833583b797aa [PATCH] xenserver: Add --root-prefix feature to interface-reconfigure. >From 64ddb6fecfe21a7b50c5907dd88de67a87ec6eca Mon Sep 17 00:00:00 2001 Date: Mon, 22 Feb 2010 16:25:54 -0800 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 Signed-off-by: Ian Campbell --- .../opt_xensource_libexec_InterfaceReconfigure.py | 19 ++++++++++---- ...xensource_libexec_InterfaceReconfigureBridge.py | 10 ++++---- .../opt_xensource_libexec_interface-reconfigure | 26 +++++++++++-------- 3 files changed, 34 insertions(+), 21 deletions(-) diff -r b06691271200 -r 78a1d9979fbf scripts/InterfaceReconfigure.py --- a/scripts/InterfaceReconfigure.py Thu Jul 08 14:25:51 2010 +0100 +++ b/scripts/InterfaceReconfigure.py Thu Jul 08 14:25:51 2010 +0100 @@ -16,6 +16,15 @@ 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 @@ 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 @@ -324,7 +333,7 @@ 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() @@ -441,7 +450,7 @@ else: log("Loading xapi database cache from %s" % cache_file) - xml = parseXML(cache_file) + xml = parseXML(root_prefix() + cache_file) self.__pifs = {} self.__bonds = {} @@ -646,7 +655,7 @@ # 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.""" @@ -827,7 +836,7 @@ # 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: diff -r b06691271200 -r 78a1d9979fbf scripts/InterfaceReconfigureBridge.py --- a/scripts/InterfaceReconfigureBridge.py Thu Jul 08 14:25:51 2010 +0100 +++ b/scripts/InterfaceReconfigureBridge.py Thu Jul 08 14:25:51 2010 +0100 @@ -15,7 +15,7 @@ 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 @@ 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 @@ 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 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 @@ -296,7 +296,7 @@ # 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] diff -r b06691271200 -r 78a1d9979fbf scripts/interface-reconfigure --- a/scripts/interface-reconfigure Thu Jul 08 14:25:51 2010 +0100 +++ b/scripts/interface-reconfigure Thu Jul 08 14:25:51 2010 +0100 @@ -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 @@ 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 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 @@ 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 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 @@ # 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 @@ 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]))) @@ -384,7 +385,7 @@ 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) @@ -443,7 +444,7 @@ 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: @@ -504,7 +505,7 @@ log("Configuring %s using %s configuration" % (bridge, mode)) - f = ConfigurationFile(dbcache_file) + f = ConfigurationFile(root_prefix() + dbcache_file) pif_uuid = getUUID() network_uuid = getUUID() @@ -582,6 +583,7 @@ "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: @@ -602,6 +604,8 @@ 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