python: Backport argparse to older platforms.
[sliver-openvswitch.git] / xenserver / opt_xensource_libexec_interface-reconfigure
index a350952..ff4b976 100755 (executable)
@@ -33,6 +33,8 @@
     --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).
+    --no-syslog         Write log messages to stderr instead of system log.
 """
 
 # Notes:
@@ -88,7 +90,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 +123,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 +187,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 +195,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])
 
@@ -202,6 +204,8 @@ def ifup(netdev):
 #
 
 def pif_rename_physical_devices(pif):
+    if pif_is_tunnel(pif):
+        return
 
     if pif_is_vlan(pif):
         pif = pif_get_vlan_slave(pif)
@@ -238,7 +242,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 +262,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])))
@@ -284,7 +288,8 @@ def ipdev_configure_network(pif, dp):
     """
 
     pifrec = db().get_pif_record(pif)
-    nwrec = db().get_network_record(pifrec['network'])
+    nw = pifrec['network']
+    nwrec = db().get_network_record(nw)
 
     ipdev = pif_ipdev_name(pif)
 
@@ -319,12 +324,13 @@ def ipdev_configure_network(pif, dp):
         if len(offload):
             f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % str.join(" ", offload))
 
-        mtu = mtu_setting(nwrec['other_config'])
-        if mtu:
-            f.write("MTU=%s\n" % mtu)
-
         ipdev_configure_static_routes(ipdev, nwrec['other_config'], f)
 
+    mtu = mtu_setting(nw, "Network", nwrec['other_config'])
+    if mtu:
+        f.write("MTU=%s\n" % mtu)
+
+
     if pifrec.has_key('DNS') and pifrec['DNS'] != "":
         ServerList = pifrec['DNS'].split(",")
         for i in range(len(ServerList)): f.write("DNS%d=%s\n" % (i+1, ServerList[i]))
@@ -382,7 +388,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)
@@ -411,7 +417,7 @@ def action_up(pif, force):
     pifrec = db().get_pif_record(pif)
 
     ipdev = pif_ipdev_name(pif)
-    dp = DatapathFactory(pif)
+    dp = DatapathFactory()(pif)
 
     log("action_up: %s" % ipdev)
 
@@ -441,7 +447,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:
@@ -451,7 +457,7 @@ def action_up(pif, force):
 
 def action_down(pif):
     ipdev = pif_ipdev_name(pif)
-    dp = DatapathFactory(pif)
+    dp = DatapathFactory()(pif)
 
     log("action_down: %s" % ipdev)
 
@@ -459,6 +465,9 @@ def action_down(pif):
 
     dp.bring_down()
 
+def action_rewrite():
+    DatapathFactory().rewrite()
+    
 # This is useful for reconfiguring the mgmt interface after having lost connectivity to the pool master
 def action_force_rewrite(bridge, config):
     def getUUID():
@@ -502,7 +511,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()
@@ -518,6 +527,8 @@ def action_force_rewrite(bridge, config):
     f.write('\t\t<VLAN_slave_of/>\n')
     f.write('\t\t<VLAN_master_of>OpaqueRef:NULL</VLAN_master_of>\n')
     f.write('\t\t<VLAN>-1</VLAN>\n')
+    f.write('\t\t<tunnel_access_PIF_of/>\n')
+    f.write('\t\t<tunnel_transport_PIF_of/>\n')
     f.write('\t\t<device>%s</device>\n' % interface)
     f.write('\t\t<MAC>%s</MAC>\n' % mac)
     f.write('\t\t<other_config/>\n')
@@ -580,6 +591,8 @@ def main(argv=None):
                         "force-interface=",
                         "management",
                         "mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
+                        "root-prefix=",
+                        "no-syslog",
                         "help" ]
             arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
         except getopt.GetoptError, msg:
@@ -600,12 +613,17 @@ 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 == "--no-syslog":
+                set_log_destination("stderr")
             elif o == "-h" or o == "--help":
                 print __doc__ % {'command-name': os.path.basename(argv[0])}
                 return 0
 
-        syslog.openlog(os.path.basename(argv[0]))
-        log("Called as " + str.join(" ", argv))
+        if get_log_destination() == "syslog":
+            syslog.openlog(os.path.basename(argv[0]))
+            log("Called as " + str.join(" ", argv))
 
         if len(args) < 1:
             raise Usage("Required option <action> not present")
@@ -655,7 +673,7 @@ def main(argv=None):
                 pif = db().get_pif_by_uuid(pif_uuid)
 
             if action == "rewrite":
-                pass
+                action_rewrite()
             else:
                 if not pif:
                     raise Usage("No PIF given")