[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-API] [PATCH 07 of 33] interface-reconfigure: Cleanup whitespace
Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx> diff -r 4805599e2732 -r 4850b1b8edc6 scripts/interface-reconfigure --- a/scripts/interface-reconfigure Fri Dec 18 14:16:32 2009 +0000 +++ b/scripts/interface-reconfigure Fri Dec 18 14:16:32 2009 +0000 @@ -30,7 +30,7 @@ --mode=static --ip=<IPADDR> --netmask=<NM> [--gateway=<GW>] Options: - --session A session reference to use to access the xapi DB + --session A session reference to use to access the xapi DB --pif A PIF reference within the session. --pif-uuid The UUID of a PIF. --force An interface name. @@ -81,18 +81,18 @@ __STATE = {"OPEN":"OPEN", "NOT-APPLIED":"NOT-APPLIED", "APPLIED":"APPLIED", "REVERTED":"REVERTED", "COMMITTED": "COMMITTED"} - + def __init__(self, path): dirname,basename = os.path.split(path) - + self.__state = self.__STATE['OPEN'] self.__children = [] - + self.__path = os.path.join(dirname, basename) self.__oldpath = os.path.join(dirname, "." + basename + ".xapi-old") self.__newpath = os.path.join(dirname, "." + basename + ".xapi-new") self.__unlink = False - + self.__f = open(self.__newpath, "w") def attach_child(self, child): @@ -106,7 +106,7 @@ return open(self.path()).readlines() except: return "" - + def write(self, args): if self.__state != self.__STATE['OPEN']: raise Error("Attempt to write to file in state %s" % self.__state) @@ -118,11 +118,11 @@ self.__unlink = True self.__f.close() self.__state = self.__STATE['NOT-APPLIED'] - + def close(self): if self.__state != self.__STATE['OPEN']: raise Error("Attempt to close file in state %s" % self.__state) - + self.__f.close() self.__state = self.__STATE['NOT-APPLIED'] @@ -155,7 +155,7 @@ if not self.__unlink: os.link(self.__newpath, self.__path) else: - pass # implicit unlink of original file + pass # implicit unlink of original file # Remove temporary file. os.unlink(self.__newpath) @@ -186,9 +186,9 @@ os.unlink(self.__oldpath) # Leave .*.xapi-new as an aid to debugging. - + self.__state = self.__STATE['REVERTED'] - + def commit(self): if self.__state != self.__STATE['APPLIED']: raise Error("Attempt to commit configuration from state %s" % self.__state) @@ -197,7 +197,7 @@ child.commit() log("Committing changes to %s configuration" % self.__path) - + if os.access(self.__oldpath, os.F_OK): os.unlink(self.__oldpath) if os.access(self.__newpath, os.F_OK): @@ -231,7 +231,7 @@ if not os.path.exists(operstate): log("No operstate for interface %s, assuming down" % i) return 0 - + f = open(operstate) state = f.read().strip() f.close() @@ -275,7 +275,7 @@ # Options for "ethtool -K" offload = None offload_opts = ["rx", "tx", "sg", "tso", "ufo", "gso"] - + for opt in [opt for opt in setting_opts + offload_opts if oc.has_key("ethtool-" + opt)]: val = oc["ethtool-" + opt] @@ -319,7 +319,7 @@ def configure_mtu(oc, f): if not oc.has_key('mtu'): return - + try: mtu = int(oc['mtu']) f.write("MTU=%d\n" % mtu) @@ -328,7 +328,7 @@ def configure_static_routes(interface, oc, f): """Open a route-<interface> file for static routes. - + Opens the static routes configuration file for interface and writes one line for each route specified in the network's other config "static-routes" value. E.g. if @@ -354,7 +354,7 @@ for l in lines: network, masklen, gateway = l.split('/') child.write("%s/%s via %s dev %s\n" % (network, masklen, gateway, interface)) - + f.attach_child(child) child.close() @@ -368,16 +368,16 @@ common options and returns the file object. """ f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % 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]))) f.write("XEMANAGED=yes\n") f.write("DEVICE=%s\n" % interface) f.write("ONBOOT=no\n") - + return f -def open_network_ifcfg(pif): +def open_network_ifcfg(pif): bridge = bridge_name(pif) interface = interface_name(pif) if bridge: @@ -388,9 +388,9 @@ def open_pif_ifcfg(pif): pifrec = db.get_pif_record(pif) - + log("Configuring %s (%s)" % (interface_name(pif), pifrec['MAC'])) - + f = __open_ifcfg(interface_name(pif)) if pifrec.has_key('other_config'): @@ -761,13 +761,13 @@ return self.__bonds[bond] else: return None - + def get_vlan_record(self, vlan): if self.__vlans.has_key(vlan): return self.__vlans[vlan] else: return None - + def bridge_name(pif): """Return the bridge name associated with pif, or None if network is bridgeless""" pifrec = db.get_pif_record(pif) @@ -803,9 +803,9 @@ def load_bonding_driver(): log("Loading bonding driver") os.spawnl(os.P_WAIT, "/sbin/modprobe", "/sbin/modprobe", "bonding") - try: + try: # bond_device_exists() uses the contents of sysfs_bonding_masters to work out which devices - # have already been created. Unfortunately the driver creates "bond0" automatically at + # have already been created. Unfortunately the driver creates "bond0" automatically at # modprobe init. Get rid of this now or our accounting will go wrong. f = open(sysfs_bonding_masters, "w") f.write("-bond0") @@ -817,7 +817,7 @@ lines = open("/proc/modules").read().split("\n") modules = [line.split(" ")[0] for line in lines] return "bonding" in modules - + def bond_device_exists(name): f = open(sysfs_bonding_masters, "r") bonds = f.readline().split() @@ -825,7 +825,7 @@ return name in bonds def __create_bond_device(name): - + if not bonding_driver_loaded(): load_bonding_driver() @@ -833,7 +833,7 @@ log("bond master %s already exists, not creating" % name) else: log("Creating bond master %s" % name) - try: + try: f = open(sysfs_bonding_masters, "w") f.write("+" + name) f.close() @@ -885,8 +885,8 @@ def configure_network(pif, f): """Write the configuration file for a network. - Writes configuration derived from the network object into the relevant - ifcfg file. The configuration file is passed in, but if the network is + Writes configuration derived from the network object into the relevant + ifcfg file. The configuration file is passed in, but if the network is bridgeless it will be ifcfg-<interface>, otherwise it will be ifcfg-<bridge>. This routine may also write ifcfg files of the networks corresponding to other PIFs @@ -896,7 +896,7 @@ pif: Opaque_ref of pif f : ConfigurationFile(/path/to/ifcfg) to which we append network configuration """ - + pifrec = db.get_pif_record(pif) nw = pifrec['network'] nwrec = db.get_network_record(nw) @@ -913,7 +913,7 @@ configure_mtu(nwrec['other_config'], f) configure_static_routes(device, nwrec['other_config'], f) - + if pifrec.has_key('other_config'): oc = pifrec['other_config'] @@ -927,7 +927,7 @@ f.write("BOOTPROTO=dhcp\n") f.write("PERSISTENT_DHCLIENT=yes\n") elif pifrec['ip_configuration_mode'] == "Static": - f.write("BOOTPROTO=none\n") + f.write("BOOTPROTO=none\n") f.write("NETMASK=%(netmask)s\n" % pifrec) f.write("IPADDR=%(IP)s\n" % pifrec) f.write("GATEWAY=%(gateway)s\n" % pifrec) @@ -957,7 +957,7 @@ peerdns_pif = None defaultroute_pif = None - + # loop through all the pifs on this host looking for one with # other-config:peerdns = true, and one with # other-config:default-route=true @@ -976,13 +976,13 @@ else: log('Warning: multiple pifs with "defaultroute=true" - choosing %s and ignoring %s' % \ (db.get_pif_record(defaultroute_pif)['device'], __pifrec['device'])) - + # If no pif is explicitly specified then use the mgmt pif for peerdns/defaultroute if peerdns_pif == None: peerdns_pif = management_pif if defaultroute_pif == None: defaultroute_pif = management_pif - + # Update all the other network's ifcfg files and ensure consistency for __pif in other_pifs_on_host: __f = open_network_ifcfg(__pif) @@ -1002,10 +1002,10 @@ else: # There is no need to change this ifcfg file. So don't attach_child. pass - + # ... and for this pif too f.write('PEERDNS=%s\n' % ((pif == peerdns_pif) and 'yes' or 'no')) - + # Update gatewaydev fnetwork = ConfigurationFile("/etc/sysconfig/network") for line in fnetwork.readlines(): @@ -1035,7 +1035,7 @@ pifrec = db.get_pif_record(pif) f = open_pif_ifcfg(pif) - + f.write("TYPE=Ethernet\n") f.write("HWADDR=%(MAC)s\n" % pifrec) @@ -1063,16 +1063,16 @@ def get_bond_slaves_of_pif(pif): """Returns a list of PIFs which make up the given bonded pif.""" - + pifrec = db.get_pif_record(pif) bmo = pifrec['bond_master_of'] if len(bmo) > 1: raise Error("Bond-master-of contains too many elements") - + if len(bmo) == 0: return [] - + bondrec = db.get_bond_record(bmo[0]) if not bondrec: raise Error("No bond record for bond master PIF") @@ -1080,7 +1080,7 @@ # build a list of slave's pifs slave_pifs = bondrec['slaves'] - # Ensure any currently attached slaves are listed in the opposite order to the order in + # Ensure any currently attached slaves are listed in the opposite order to the order in # which they were attached. The first slave attached must be the last detached since # the bond is using its MAC address. try: @@ -1091,7 +1091,7 @@ slave_pifs.remove(slave_pif) slave_pifs.insert(0, slave_pif) except IOError: - pass + pass return slave_pifs @@ -1121,7 +1121,7 @@ f.attach_child(s) # The bond option defaults - bond_options = { + bond_options = { "mode": "balance-slb", "miimon": "100", "downdelay": "200", @@ -1145,7 +1145,7 @@ """Find the PIF which is the VLAN slave of pif. Returns the 'physical' PIF underneath the a VLAN PIF @pif.""" - + pifrec = db.get_pif_record(pif) vlan = pifrec['VLAN_master_of'] @@ -1160,7 +1160,7 @@ def get_vlan_masters_of_pif(pif): """Returns a list of PIFs which are VLANs on top of the given pif.""" - + pifrec = db.get_pif_record(pif) vlans = [db.get_vlan_record(v) for v in pifrec['VLAN_slave_of']] return [v['untagged_PIF'] for v in vlans if v and db.pif_exists(v['untagged_PIF'])] @@ -1182,7 +1182,7 @@ f = open_pif_ifcfg(pif) f.write("VLAN=yes\n") f.attach_child(slave) - + return f def configure_pif(pif): @@ -1237,14 +1237,14 @@ which are bond slaves of this one. This is because they will be required when the bond is brought up.""" - rec = db.get_pif_record(pif) + rec = db.get_pif_record(pif) interface = interface_name(pif) bridge = bridge_name(pif) if rec['VLAN'] != '-1': log("bring_down_interface: %s is a VLAN" % interface) ifdown(interface) - + if destroy: log("Destroy vlan device %s" % interface) vconfig_rem(interface) @@ -1257,7 +1257,7 @@ if db.get_pif_record(slave)['currently_attached']: log("bring_down_interface: vlan slave is currently attached") return - + masters = get_vlan_masters_of_pif(slave) masters = [m for m in masters if m != pif and db.get_pif_record(m)['currently_attached']] if len(masters) > 0: @@ -1278,7 +1278,7 @@ interface = interface_name(pif) # Need to bring down bond slaves first since the bond device - # must be up to enslave/unenslave. + # must be up to enslave/unenslave. bond_slaves = get_bond_slaves_of_pif(pif) log("bond slaves of %s - %s" % (rec['device'], [interface_name(s) for s in bond_slaves])) for slave in bond_slaves: @@ -1318,7 +1318,7 @@ """ rec = db.get_pif_record(pif) - + # VLAN on bond seems to need bond brought up explicitly, but VLAN # on normal device does not. Might as well always bring it up. if rec['VLAN'] != '-1': @@ -1329,7 +1329,7 @@ interface = interface_name(pif) create_bond_device(pif) - + log("Bring interface %s up" % interface) ifup(interface) @@ -1338,7 +1338,7 @@ pifrec = db.get_pif_record(pif) f = configure_pif(pif) - + interface = interface_name(pif) bridge = bridge_name(pif) mode = pifrec['ip_configuration_mode'] @@ -1352,7 +1352,7 @@ else: log("Configuring %s using %s configuration" % (interface, mode)) configure_network(pif, f) - + f.close() # if there is a bridge using this pif then bring it down @@ -1383,17 +1383,17 @@ log("Unlinking stale file %s" % removed.path()) removed.unlink() f.attach_child(removed) - + # No masters left - now its safe to reconfigure the slave. bring_down_interface(pif) - + try: f.apply() bring_up_interface(pif) if bridge: bring_up_bridge(bridge) - + # Update /etc/issue (which contains the IP address of the management interface) os.system("/sbin/update-issue") @@ -1408,7 +1408,7 @@ log("failed to apply changes: %s" % e.msg) f.revert() raise - + def action_down(pif): bring_down_interface(pif, destroy=True) @@ -1477,9 +1477,9 @@ log("%s returned %d" % (" ".join(args), rc)) # kill any dhclients previously started run('/bin/sh','-c','/usr/bin/killall -9 dhclient; exit 0') - + # Create list of bridges - bridges = [ itf for itf in os.listdir('/sys/class/net/') + bridges = [ itf for itf in os.listdir('/sys/class/net/') if os.path.exists('/sys/class/net/%s/bridge' % itf) ] # Per bridge bringing down @@ -1487,7 +1487,7 @@ # Detach from IP stack run("/sbin/ip", "route", "flush", "dev", bridge, "table", "all") run("/sbin/ip", "addr", "flush", "dev", bridge) - + # Remove interfaces brifs = [ itf for itf in os.listdir('/sys/class/net/%s/brif/' % bridge) if not itf.startswith('vif') ] @@ -1527,27 +1527,27 @@ if (os.path.exists('/sys/class/net/%s/ifindex' % itf) and (itf != "lo") and (not itf.startswith("vif")) and - (not itf.startswith("xapi")) and + (not itf.startswith("xapi")) and (not itf.startswith("xenbr")) and - (not itf.startswith("sit")) and + (not itf.startswith("sit")) and (not itf.startswith("ppp")) ) ] for phys in physifs: run("/sbin/ip", "route", "flush", "dev", phys, "table", "all") run("/sbin/ip", "addr", "flush", "dev", phys) - run("/sbin/ip", "link", "set", phys, "down") + run("/sbin/ip", "link", "set", phys, "down") def main(argv=None): global management_pif - + session = None pif_uuid = None pif = None force_interface = None force_management = False - + if argv is None: argv = sys.argv @@ -1566,7 +1566,7 @@ raise Usage(msg) force_rewrite_config = {} - + for o,a in arglist: if o == "--pif": pif = a @@ -1586,7 +1586,7 @@ syslog.openlog(os.path.basename(argv[0])) log("Called as " + str.join(" ", argv)) - + if len(args) < 1: raise Usage("Required option <action> not present") if len(args) > 1: @@ -1668,7 +1668,7 @@ # Save cache. db.save(dbcache_file) - + except Usage, err: print >>sys.stderr, err.msg print >>sys.stderr, "For help use --help." @@ -1676,7 +1676,7 @@ except Error, err: log(err.msg) return 1 - + return 0 if __name__ == "__main__": @@ -1690,5 +1690,5 @@ log(exline) syslog.closelog() - + sys.exit(rc) _______________________________________________ xen-api mailing list xen-api@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/mailman/listinfo/xen-api
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |