Setting tag pyplnet-7.0-0
[pyplnet.git] / plnet.py
index c776d1a..782494a 100755 (executable)
--- a/plnet.py
+++ b/plnet.py
@@ -1,4 +1,6 @@
-#!/usr/bin/python /usr/bin/plcsh
+#!/usr/bin/python3 /usr/bin/plcsh
+
+# pylint: disable=c0111, c0103, r0912, r0913, r0914, r0915
 
 import os
 import socket
@@ -11,69 +13,56 @@ import re
 import sioc
 import modprobe
 
-global version
-version = 4.3
-
 def ovs_check(logger):
     """ Return True if openvswitch is running, False otherwise. Try restarting
         it once.
     """
-    rc = os.system("service openvswitch status")
-    if rc == 0:
+    retcod = os.system("service openvswitch status")
+    if retcod == 0:
         return True
     logger.log("net: restarting openvswitch")
-    rc = os.system("service openvswitch restart")
-    rc = os.system("service openvswitch status")
-    if rc == 0:
+    retcod = os.system("service openvswitch restart")
+    retcod = os.system("service openvswitch status")
+    if retcod == 0:
         return True
     logger.log("net: failed to restart openvswitch")
     return False
 
-def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeManager"):
-    global version
+def InitInterfaces(logger, plc, data, root="",
+                   files_only=False, program="NodeManager"):
 
     sysconfig = "{}/etc/sysconfig/network-scripts".format(root)
     try:
         os.makedirs(sysconfig)
-    except OSError, e:
+    except OSError as e:
         if e.errno != errno.EEXIST:
             raise e
 
     # query running network interfaces
     devs = sioc.gifconf()
-    ips = dict(zip(devs.values(), devs.keys()))
+    ips = {ip: interface for (ip, interface) in devs.items()}
     macs = {}
     for dev in devs:
         macs[sioc.gifhwaddr(dev).lower()] = dev
 
     devices_map = {}
     device_id = 1
-    hostname = data.get('hostname',socket.gethostname())
+    hostname = data.get('hostname', socket.gethostname())
     gateway = None
     # assume data['interfaces'] contains this node's Interfaces
-    # can cope with 4.3 ('networks') or 5.0 ('interfaces')
-    try:
-        interfaces = data['interfaces']
-    except:
-        interfaces = data['networks']
+    interfaces = data['interfaces']
     failedToGetSettings = False
 
-    # NOTE: GetInterfaces/NodeNetworks does not necessarily order the interfaces
-    # returned.  Because 'interface' is decremented as each interface is processed,
+    # NOTE: GetInterfaces does not necessarily order the interfaces returned.
+    # Because 'interface' is decremented as each interface is processed,
     # by the time is_primary=True (primary) interface is reached, the device
     # "eth<interface>" is not eth0.  But, something like eth-4, or eth-12.
-    # This code sorts the interfaces, placing is_primary=True interfaces first.  
+    # This code sorts the interfaces, placing is_primary=True interfaces first.
     # There is a lot of room for improvement to how this
     # script handles interfaces and how it chooses the primary interface.
-    def compare_by (fieldname):
-        def compare_two_dicts (a, b):
-            return cmp(a[fieldname], b[fieldname])
-        return compare_two_dicts
-
     # NOTE: by sorting on 'is_primary' and then reversing (since False is sorted
     # before True) all 'is_primary' interfaces are at the beginning of the list.
-    interfaces.sort( compare_by('is_primary') )
-    interfaces.reverse()
+    interfaces.sort(key=lambda d: d['is_primary'], reverse=True)
 
     # The names of the bridge devices
     bridgeDevices = []
@@ -84,8 +73,9 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
         logger.verbose('net:InitInterfaces ips = {}'.format(ips))
         # Get interface name preferably from MAC address, falling back
         # on IP address.
-        hwaddr=interface['mac']
-        if hwaddr <> None: hwaddr=hwaddr.lower()
+        hwaddr = interface['mac']
+        if hwaddr is not None:
+            hwaddr = hwaddr.lower()
         if hwaddr in macs:
             orig_ifname = macs[hwaddr]
         elif interface['ip'] in ips:
@@ -102,51 +92,44 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             gateway = interface['gateway']
 
         if 'interface_tag_ids' in interface:
-            version = 4.3
             interface_tag_ids = "interface_tag_ids"
             interface_tag_id = "interface_tag_id"
             name_key = "tagname"
-        else:
-            version = 4.2
-            interface_tag_ids = "nodenetwork_setting_ids"
-            interface_tag_id = "nodenetwork_setting_id"
-            name_key = "name"
 
-        if len(interface[interface_tag_ids]) > 0:
+        if interface[interface_tag_ids]:
+            filter = {interface_tag_id : interface[interface_tag_ids]}
             try:
-                filter = { interface_tag_id : interface[interface_tag_ids] }
-                if version == 4.3:
-                    settings = plc.GetInterfaceTags(filter)
-                else:
-                    settings = plc.GetNodeNetworkSettings(filter)
-            except:
-                logger.log("net:InitInterfaces FATAL: failed call GetInterfaceTags({})"
-                           .format(filter))
+                settings = plc.GetInterfaceTags(filter)
+            except Exception:
+                logger.log("net:InitInterfaces FATAL: failed call "
+                           "GetInterfaceTags({})".format(filter))
                 failedToGetSettings = True
                 continue # on to the next interface
 
             for setting in settings:
                 settingname = setting[name_key].upper()
-                if ((settingname in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER','VLAN','TYPE','DEVICETYPE')) or \
-                    (re.search('^IPADDR[0-9]+$|^NETMASK[0-9]+$', settingname))):
+                if (settingname in ('IFNAME', 'ALIAS', 'CFGOPTIONS', 'DRIVER',
+                                    'VLAN', 'TYPE', 'DEVICETYPE') or
+                        re.search('^IPADDR[0-9]+$|^NETMASK[0-9]+$', settingname)):
                     # TD: Added match for secondary IPv4 configuration.
-                    details[settingname]=setting['value']
+                    details[settingname] = setting['value']
                 # IPv6 support on IPv4 interface
-                elif settingname in ('IPV6ADDR','IPV6_DEFAULTGW','IPV6ADDR_SECONDARIES', 'IPV6_AUTOCONF'):
+                elif settingname in ('IPV6ADDR', 'IPV6_DEFAULTGW',
+                                     'IPV6ADDR_SECONDARIES', 'IPV6_AUTOCONF'):
                     # TD: Added IPV6_AUTOCONF.
-                    details[settingname]=setting['value']
-                    details['IPV6INIT']='yes'
+                    details[settingname] = setting['value']
+                    details['IPV6INIT'] = 'yes'
                 # wireless settings
                 elif settingname in \
-                        [  "MODE", "ESSID", "NW", "FREQ", "CHANNEL", "SENS", "RATE",
-                           "KEY", "KEY1", "KEY2", "KEY3", "KEY4", "SECURITYMODE", 
-                           "IWCONFIG", "IWPRIV" ] :
-                    details [settingname] = setting['value']
-                    details ['TYPE']='Wireless'
+                        ("MODE", "ESSID", "NW", "FREQ", "CHANNEL", "SENS",
+                         "RATE", "KEY", "KEY1", "KEY2", "KEY3", "KEY4",
+                         "SECURITYMODE", "IWCONFIG", "IWPRIV"):
+                    details[settingname] = setting['value']
+                    details['TYPE'] = 'Wireless'
                 # Bridge setting
-                elif settingname in [ 'BRIDGE' ]:
+                elif settingname in ('BRIDGE',):
                     details['BRIDGE'] = setting['value']
-                elif settingname in [ 'OVS_BRIDGE' ]:
+                elif settingname in ('OVS_BRIDGE',):
                     # If openvswitch isn't running, then we'll lose network
                     # connectivity when we reconfigure eth0.
                     if ovs_check(logger):
@@ -154,7 +137,8 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                         details['TYPE'] = "OVSPort"
                         details['DEVICETYPE'] = "ovs"
                     else:
-                        logger.log("net:InitInterfaces ERROR: OVS_BRIDGE specified, yet ovs is not running")
+                        logger.log("net:InitInterfaces ERROR: OVS_BRIDGE specified, "
+                                   "yet ovs is not running")
                 else:
                     logger.log("net:InitInterfaces WARNING: ignored setting named {}"
                                .format(setting[name_key]))
@@ -166,7 +150,7 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                 del details['HWADDR']
                 if hwaddr in macs:
                     hwifname = macs[hwaddr]
-                    if ('IFNAME' in details) and details['IFNAME'] <> hwifname:
+                    if ('IFNAME' in details) and details['IFNAME'] != hwifname:
                         logger.log("net:InitInterfaces WARNING: alias ifname ({}) and hwaddr ifname ({}) do not match"
                                    .format(details['IFNAME'], hwifname))
                         details['IFNAME'] = hwifname
@@ -183,12 +167,14 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                     isValid=isValid and part.isalnum()
 
                 if isValid:
-                    devices_map["{}:{}".format(details['IFNAME'], details['ALIAS'])] = details 
+                    devices_map["{}:{}".format(details['IFNAME'], details['ALIAS'])] = details
                 else:
-                    logger.log("net:InitInterfaces WARNING: interface alias ({}) not a valid string for RH ifup-aliases"
+                    logger.log("net:InitInterfaces WARNING: interface alias ({}) "
+                               "is not a valid string for RH ifup-aliases"
                                .format(details['ALIAS']))
             else:
-                logger.log("net:InitInterfaces WARNING: interface alias ({}) not matched to an interface"
+                logger.log("net:InitInterfaces WARNING: interface alias ({}) "
+                           " not matched to an interface"
                            .format(details['ALIAS']))
             device_id -= 1
         elif ('BRIDGE' in details or 'OVS_BRIDGE' in details) and 'IFNAME' in details:
@@ -208,15 +194,12 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
 
             logger.log('net:InitInterfaces: Adding {} {}'.format(bridgeType, bridgeName))
             bridgeDetails = prepDetails(interface)
-            
+
             # TD: Add configuration for secondary IPv4 and IPv6 addresses to the bridge.
-            if len(interface[interface_tag_ids]) > 0:
-                filter = { interface_tag_id : interface[interface_tag_ids] }
+            if interface[interface_tag_ids]:
+                filter = {interface_tag_id : interface[interface_tag_ids]}
                 try:
-                    if version == 4.3:
-                        settings = plc.GetInterfaceTags(filter)
-                    else:
-                        settings = plc.GetNodeNetworkSettings(filter)
+                    settings = plc.GetInterfaceTags(filter)
                 except:
                     logger.log("net:InitInterfaces FATAL: failed call GetInterfaceTags({})"
                                .format(filter))
@@ -229,10 +212,11 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                         # TD: Added match for secondary IPv4 configuration.
                         bridgeDetails[settingname]=setting['value']
                     # IPv6 support on IPv4 interface
-                    elif settingname in ('IPV6ADDR','IPV6_DEFAULTGW','IPV6ADDR_SECONDARIES', 'IPV6_AUTOCONF'):
+                    elif settingname in ('IPV6ADDR', 'IPV6_DEFAULTGW',
+                                         'IPV6ADDR_SECONDARIES', 'IPV6_AUTOCONF'):
                         # TD: Added IPV6_AUTOCONF.
-                        bridgeDetails[settingname]=setting['value']
-                        bridgeDetails['IPV6INIT']='yes'
+                        bridgeDetails[settingname] = setting['value']
+                        bridgeDetails['IPV6INIT'] = 'yes'
 
             bridgeDevices.append(bridgeName)
             bridgeDetails['TYPE'] = bridgeType
@@ -252,7 +236,7 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                 device_id -= 1
             else:
                 while True:
-                    ifname="eth{}".format(device_id - 1)
+                    ifname = "eth{}".format(device_id - 1)
                     if ifname not in devices_map:
                         break
                     device_id += 1
@@ -260,23 +244,23 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                     logger.log("net:InitInterfaces WARNING: possibly blowing away {} configuration"
                                .format(ifname))
             devices_map[ifname] = details
-        device_id += 1 
+        device_id += 1
     logger.log('net:InitInterfaces: Device map: {}'.format(devices_map))
     m = modprobe.Modprobe()
     try:
         m.input("{}/etc/modprobe.conf".format(root))
     except:
         pass
-    for (dev, details) in devices_map.iteritems():
+    for (dev, details) in devices_map.items():
         # get the driver string "moduleName option1=a option2=b"
-        driver=details.get('DRIVER','')
-        if driver <> '':
-            driver=driver.split()
-            kernelmodule=driver[0]
-            m.aliasset(dev,kernelmodule)
-            options=" ".join(driver[1:])
-            if options <> '':
-                m.optionsset(dev,options)
+        driver = details.get('DRIVER', '')
+        if driver != '':
+            driver = driver.split()
+            kernelmodule = driver[0]
+            m.aliasset(dev, kernelmodule)
+            options = " ".join(driver[1:])
+            if options != '':
+                m.optionsset(dev, options)
     m.output("{}/etc/modprobe.conf".format(root), program)
 
     # clean up after any ifcfg-$dev script that's no longer listed as
@@ -286,21 +270,23 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
     files = os.listdir(sysconfig)
 
     # filter out the ifcfg-* files
-    ifcfgs=[]
+    ifcfgs = []
     for f in files:
         if f.find("ifcfg-") == 0:
             ifcfgs.append(f)
 
     # remove loopback (lo) from ifcfgs list
     lo = "ifcfg-lo"
-    if lo in ifcfgs: ifcfgs.remove(lo)
+    if lo in ifcfgs:
+        ifcfgs.remove(lo)
 
     # remove known devices from ifcfgs list
-    for (dev, details) in devices_map.iteritems():
+    for (dev, details) in devices_map.items():
         ifcfg = 'ifcfg-'+dev
-        if ifcfg in ifcfgs: ifcfgs.remove(ifcfg)
+        if ifcfg in ifcfgs:
+            ifcfgs.remove(ifcfg)
 
-    # delete the remaining ifcfgs from 
+    # delete the remaining ifcfgs from
     deletedSomething = False
 
     if not failedToGetSettings:
@@ -318,26 +304,25 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
         time.sleep(2)
 
     # Write network configuration file
-    networkconf = file("{}/etc/sysconfig/network".format(root), "w")
-    networkconf.write("NETWORKING=yes\nHOSTNAME={}\n".format(hostname))
-    if gateway is not None:
-        networkconf.write("GATEWAY={}\n".format(gateway))
-    networkconf.close()
+    with open("{}/etc/sysconfig/network".format(root), "w") as networkconf:
+        networkconf.write("NETWORKING=yes\nHOSTNAME={}\n".format(hostname))
+        if gateway is not None:
+            networkconf.write("GATEWAY={}\n".format(gateway))
 
     # Process ifcfg-$dev changes / additions
     newdevs = []
     table = 10
-    for (dev, details) in devices_map.iteritems():
+    for (dev, details) in devices_map.items():
         (fd, tmpnam) = tempfile.mkstemp(dir=sysconfig)
         f = os.fdopen(fd, "w")
         f.write("# Autogenerated by pyplnet... do not edit!\n")
         if 'DRIVER' in details:
             f.write("# using {} driver for device {}\n".format(details['DRIVER'], dev))
         f.write('DEVICE={}\n'.format(dev))
-        
+
         # print the configuration values
-        for (key, val) in details.iteritems():
-            if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER','GATEWAY'):
+        for (key, val) in details.items():
+            if key not in ('IFNAME', 'ALIAS', 'CFGOPTIONS', 'DRIVER', 'GATEWAY'):
                 f.write('{}="{}"\n'.format(key, val))
 
         # print the configuration specific option values (if any)
@@ -356,18 +341,16 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
         def comparefiles(a,b):
             try:
                 logger.verbose("net:InitInterfaces comparing {} with {}".format(a, b))
-                if not os.path.exists(a): return False
-                fb = open(a)
-                buf_a = fb.read()
-                fb.close()
+                if not os.path.exists(a) or not os.path.exists(b):
+                    return False
+                with open(a) as fb:
+                    buf_a = fb.read()
 
-                if not os.path.exists(b): return False
-                fb = open(b)
-                buf_b = fb.read()
-                fb.close()
+                with open(b) as fb:
+                    buf_b = fb.read()
 
                 return buf_a == buf_b
-            except IOError, e:
+            except IOError as e:
                 return False
 
         src_route_changed = False
@@ -380,7 +363,7 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             rule_dest = "{}/rule-{}".format(sysconfig, dev)
             if not comparefiles(rule_tmpnam, rule_dest):
                 os.rename(rule_tmpnam, rule_dest)
-                os.chmod(rule_dest, 0644)
+                os.chmod(rule_dest, 0o644)
                 src_route_changed = True
             else:
                 os.unlink(rule_tmpnam)
@@ -399,7 +382,7 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             route_dest = "{}/route-{}".format(sysconfig, dev)
             if not comparefiles(route_tmpnam, route_dest):
                 os.rename(route_tmpnam, route_dest)
-                os.chmod(route_dest, 0644)
+                os.chmod(route_dest, 0o644)
                 src_route_changed = True
             else:
                 os.unlink(route_tmpnam)
@@ -409,9 +392,9 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             logger.verbose('net:InitInterfaces adding configuration for {}'.format(dev))
             # add ifcfg-$dev configuration file
             os.rename(tmpnam,path)
-            os.chmod(path,0644)
+            os.chmod(path,0o644)
             newdevs.append(dev)
-            
+
         elif not comparefiles(tmpnam,path) or src_route_changed:
             logger.verbose('net:InitInterfaces Configuration change for {}'.format(dev))
             if not files_only:
@@ -423,8 +406,8 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
 
             logger.log('replacing configuration for {}'.format(dev))
             # replace ifcfg-$dev configuration file
-            os.rename(tmpnam,path)
-            os.chmod(path,0644)
+            os.rename(tmpnam, path)
+            os.chmod(path, 0o644)
             newdevs.append(dev)
         else:
             # tmpnam & path are identical
@@ -432,23 +415,23 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
 
     for dev in newdevs:
         cfgvariables = {}
-        fb = file("{}/ifcfg-{}".format(sysconfig, dev), "r")
-        for line in fb.readlines():
-            parts = line.split()
-            if parts[0][0]=="#":continue
-            if parts[0].find('='):
-                name,value = parts[0].split('=')
-                # clean up name & value
-                name = name.strip()
-                value = value.strip()
-                value = value.strip("'")
-                value = value.strip('"')
-                cfgvariables[name]=value
-        fb.close()
+        with open("{}/ifcfg-{}".format(sysconfig, dev), "r") as fb:
+            for line in fb.readlines():
+                parts = line.split()
+                if parts[0][0] == "#":
+                    continue
+                if parts[0].find('='):
+                    name, value = parts[0].split('=')
+                    # clean up name & value
+                    name = name.strip()
+                    value = value.strip()
+                    value = value.strip("'")
+                    value = value.strip('"')
+                    cfgvariables[name] = value
 
         def getvar(name):
             if name in cfgvariables:
-                value=cfgvariables[name]
+                value = cfgvariables[name]
                 value = value.lower()
                 return value
             return ''
@@ -480,8 +463,9 @@ def prepDetails(interface, hostname=''):
     details = {}
     details['ONBOOT']  = 'yes'
     details['USERCTL'] = 'no'
+    # starting with f27, it's OK to use NetworkManager
     # attempt to work around issues seen starting with f23
-    details['NM_CONTROLLED'] = 'no'
+    details['NM_CONTROLLED'] = 'no'
     if interface['mac']:
         details['HWADDR'] = interface['mac']
     if interface['is_primary']:
@@ -530,34 +514,34 @@ def removeBridgedIfaceDetails(details):
     # TD: Also turn off IPv6
     details['IPV6INIT']      = 'no'
     details['IPV6_AUTOCONF'] = 'no'
-    
+
     return details
 
-if __name__ == "__main__":
+def main():
     import optparse
     import sys
 
-    parser = optparse.OptionParser(usage="plnet [-v] [-f] [-p <program>] -r root node_id")
-    parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
-    parser.add_option("-r", "--root", action="store", type="string",
-                      dest="root", default=None)
-    parser.add_option("-f", "--files-only", action="store_true",
-                      dest="files_only")
-    parser.add_option("-p", "--program", action="store", type="string",
-                      dest="program", default="plnet")
+    usage = "plnet [-v] [-f] [-p <program>] -r root node_id"
+
+    parser = optparse.OptionParser(usage=usage)
+    parser.add_option(
+        "-r", "--root", action="store", type="string",
+        dest="root", default=None)
+    parser.add_option(
+        "-v", "--verbose", action="store_true", dest="verbose")
+    parser.add_option(
+        "-f", "--files-only", action="store_true",
+        dest="files_only")
+    parser.add_option(
+        "-p", "--program", action="store", type="string",
+        dest="program", default="plnet")
     (options, args) = parser.parse_args()
     if len(args) != 1 or options.root is None:
-        print sys.argv
-        print >>sys.stderr, "Missing root or node_id"
         parser.print_help()
         sys.exit(1)
 
     node = shell.GetNodes({'node_id': [int(args[0])]})
-    try:
-        interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
-    except AttributeError:
-        interfaces = shell.GetNodeNetworks({'nodenetwork_id':node[0]['nodenetwork_ids']})
-        version = 4.2
+    interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
 
 
     data = {'hostname': node[0]['hostname'], 'interfaces': interfaces}
@@ -566,8 +550,11 @@ if __name__ == "__main__":
             self.verbosity = verbose
         def log(self, msg, loglevel=2):
             if self.verbosity:
-                print msg
+                print(msg)
         def verbose(self, msg):
             self.log(msg, 1)
     l = logger(options.verbose)
     InitInterfaces(l, shell, data, options.root, options.files_only)
+
+if __name__ == "__main__":
+    main()