← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/cloud-init-net-moves into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/cloud-init-net-moves into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/cloud-init-net-moves/+merge/293807

Moves around the net_util file into the net module, removes some relative imports.

Tiny cleanups.
-- 
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/cloud-init-net-moves into lp:cloud-init.
=== modified file 'cloudinit/distros/arch.py'
--- cloudinit/distros/arch.py	2016-03-03 22:20:10 +0000
+++ cloudinit/distros/arch.py	2016-05-04 17:26:47 +0000
@@ -21,7 +21,7 @@
 from cloudinit import log as logging
 from cloudinit import util
 
-from cloudinit.distros import net_util
+from cloudinit.net import converter
 from cloudinit.distros.parsers.hostname import HostnameConf
 
 from cloudinit.settings import PER_INSTANCE
@@ -61,7 +61,7 @@
         self.package_command('', pkgs=pkglist)
 
     def _write_network(self, settings):
-        entries = net_util.translate_network(settings)
+        entries = converter.translate_network(settings)
         LOG.debug("Translated ubuntu style network settings %s into %s",
                   settings, entries)
         dev_names = entries.keys()

=== modified file 'cloudinit/distros/freebsd.py'
--- cloudinit/distros/freebsd.py	2016-04-04 16:07:19 +0000
+++ cloudinit/distros/freebsd.py	2016-05-04 17:26:47 +0000
@@ -28,7 +28,7 @@
 from cloudinit import ssh_util
 from cloudinit import util
 
-from cloudinit.distros import net_util
+from cloudinit.net import converter
 from cloudinit.distros.parsers.resolv_conf import ResolvConf
 
 from cloudinit.settings import PER_INSTANCE
@@ -284,7 +284,7 @@
             ssh_util.setup_user_keys(keys, name, options=None)
 
     def _write_network(self, settings):
-        entries = net_util.translate_network(settings)
+        entries = converter.translate_network(settings)
         nameservers = []
         searchdomains = []
         dev_names = entries.keys()

=== removed file 'cloudinit/distros/net_util.py'
--- cloudinit/distros/net_util.py	2015-01-21 22:56:53 +0000
+++ cloudinit/distros/net_util.py	1970-01-01 00:00:00 +0000
@@ -1,182 +0,0 @@
-# vi: ts=4 expandtab
-#
-#    Copyright (C) 2012 Canonical Ltd.
-#    Copyright (C) 2012, 2013 Hewlett-Packard Development Company, L.P.
-#    Copyright (C) 2012 Yahoo! Inc.
-#
-#    Author: Scott Moser <scott.moser@xxxxxxxxxxxxx>
-#    Author: Juerg Haefliger <juerg.haefliger@xxxxxx>
-#    Author: Joshua Harlow <harlowja@xxxxxxxxxxxxx>
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License version 3, as
-#    published by the Free Software Foundation.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-
-# This is a util function to translate debian based distro interface blobs as
-# given in /etc/network/interfaces to an *somewhat* agnostic format for
-# distributions that use other formats.
-#
-# TODO(harlowja) remove when we have python-netcf active...
-#
-# The format is the following:
-# {
-#    <device-name>: {
-#        # All optional (if not existent in original format)
-#        "netmask": <ip>,
-#        "broadcast": <ip>,
-#        "gateway": <ip>,
-#        "address": <ip>,
-#        "bootproto": "static"|"dhcp",
-#        "dns-search": <hostname>,
-#        "hwaddress": <mac-address>,
-#        "auto": True (or non-existent),
-#        "dns-nameservers": [<ip/hostname>, ...],
-#    }
-# }
-#
-# Things to note, comments are removed, if a ubuntu/debian interface is
-# marked as auto then only then first segment (?) is retained, ie
-# 'auto eth0 eth0:1' just marks eth0 as auto (not eth0:1).
-#
-# Example input:
-#
-# auto lo
-# iface lo inet loopback
-#
-# auto eth0
-# iface eth0 inet static
-#         address 10.0.0.1
-#         netmask 255.255.252.0
-#         broadcast 10.0.0.255
-#         gateway 10.0.0.2
-#         dns-nameservers 98.0.0.1 98.0.0.2
-#
-# Example output:
-# {
-#     "lo": {
-#         "auto": true
-#     },
-#     "eth0": {
-#         "auto": true,
-#         "dns-nameservers": [
-#             "98.0.0.1",
-#             "98.0.0.2"
-#         ],
-#         "broadcast": "10.0.0.255",
-#         "netmask": "255.255.252.0",
-#         "bootproto": "static",
-#         "address": "10.0.0.1",
-#         "gateway": "10.0.0.2"
-#     }
-# }
-
-def translate_network(settings):
-    # Get the standard cmd, args from the ubuntu format
-    entries = []
-    for line in settings.splitlines():
-        line = line.strip()
-        if not line or line.startswith("#"):
-            continue
-        split_up = line.split(None, 1)
-        if len(split_up) <= 1:
-            continue
-        entries.append(split_up)
-    # Figure out where each iface section is
-    ifaces = []
-    consume = {}
-    for (cmd, args) in entries:
-        if cmd == 'iface':
-            if consume:
-                ifaces.append(consume)
-                consume = {}
-            consume[cmd] = args
-        else:
-            consume[cmd] = args
-    # Check if anything left over to consume
-    absorb = False
-    for (cmd, args) in consume.items():
-        if cmd == 'iface':
-            absorb = True
-    if absorb:
-        ifaces.append(consume)
-    # Now translate
-    real_ifaces = {}
-    for info in ifaces:
-        if 'iface' not in info:
-            continue
-        iface_details = info['iface'].split(None)
-        # Check if current device *may* have an ipv6 IP
-        use_ipv6 = False
-        if 'inet6' in iface_details:
-            use_ipv6 = True
-        dev_name = None
-        if len(iface_details) >= 1:
-            dev = iface_details[0].strip().lower()
-            if dev:
-                dev_name = dev
-        if not dev_name:
-            continue
-        iface_info = {}
-        iface_info['ipv6'] = {}
-        if len(iface_details) >= 3:
-            proto_type = iface_details[2].strip().lower()
-            # Seems like this can be 'loopback' which we don't
-            # really care about
-            if proto_type in ['dhcp', 'static']:
-                iface_info['bootproto'] = proto_type
-        # These can just be copied over
-        if use_ipv6:
-            for k in ['address', 'gateway']:
-                if k in info:
-                    val = info[k].strip().lower()
-                    if val:
-                        iface_info['ipv6'][k] = val
-        else:
-            for k in ['netmask', 'address', 'gateway', 'broadcast']:
-                if k in info:
-                    val = info[k].strip().lower()
-                    if val:
-                        iface_info[k] = val
-            # Name server info provided??
-            if 'dns-nameservers' in info:
-                iface_info['dns-nameservers'] = info['dns-nameservers'].split()
-            # Name server search info provided??
-            if 'dns-search' in info:
-                iface_info['dns-search'] = info['dns-search'].split()
-            # Is any mac address spoofing going on??
-            if 'hwaddress' in info:
-                hw_info = info['hwaddress'].lower().strip()
-                hw_split = hw_info.split(None, 1)
-                if len(hw_split) == 2 and hw_split[0].startswith('ether'):
-                    hw_addr = hw_split[1]
-                    if hw_addr:
-                        iface_info['hwaddress'] = hw_addr
-        # If ipv6 is enabled, device will have multiple IPs, so we need to
-        # update the dictionary instead of overwriting it...
-        if dev_name in real_ifaces:
-            real_ifaces[dev_name].update(iface_info)
-        else:
-            real_ifaces[dev_name] = iface_info
-    # Check for those that should be started on boot via 'auto'
-    for (cmd, args) in entries:
-        args = args.split(None)
-        if not args:
-            continue
-        dev_name = args[0].strip().lower()
-        if cmd == 'auto':
-            # Seems like auto can be like 'auto eth0 eth0:1' so just get the
-            # first part out as the device name
-            if dev_name in real_ifaces:
-                real_ifaces[dev_name]['auto'] = True
-        if cmd == 'iface' and 'inet6' in args:
-            real_ifaces[dev_name]['inet6'] = True
-    return real_ifaces

=== modified file 'cloudinit/distros/rhel.py'
--- cloudinit/distros/rhel.py	2015-06-02 20:27:57 +0000
+++ cloudinit/distros/rhel.py	2016-05-04 17:26:47 +0000
@@ -25,8 +25,8 @@
 from cloudinit import log as logging
 from cloudinit import util
 
-from cloudinit.distros import net_util
 from cloudinit.distros import rhel_util
+from cloudinit.net import converter
 from cloudinit.settings import PER_INSTANCE
 
 LOG = logging.getLogger(__name__)
@@ -65,7 +65,7 @@
 
     def _write_network(self, settings):
         # TODO(harlowja) fix this... since this is the ubuntu format
-        entries = net_util.translate_network(settings)
+        entries = converter.translate_network(settings)
         LOG.debug("Translated ubuntu style network settings %s into %s",
                   settings, entries)
         # Make the intermediate format as the rhel format...

=== modified file 'cloudinit/distros/sles.py'
--- cloudinit/distros/sles.py	2015-01-23 02:21:04 +0000
+++ cloudinit/distros/sles.py	2016-05-04 17:26:47 +0000
@@ -26,8 +26,8 @@
 from cloudinit import log as logging
 from cloudinit import util
 
-from cloudinit.distros import net_util
 from cloudinit.distros import rhel_util
+from cloudinit.net import converter
 from cloudinit.settings import PER_INSTANCE
 
 LOG = logging.getLogger(__name__)
@@ -55,7 +55,7 @@
 
     def _write_network(self, settings):
         # Convert debian settings to ifcfg format
-        entries = net_util.translate_network(settings)
+        entries = converter.translate_network(settings)
         LOG.debug("Translated ubuntu style network settings %s into %s",
                   settings, entries)
         # Make the intermediate format as the suse format...

=== modified file 'cloudinit/net/__init__.py'
--- cloudinit/net/__init__.py	2016-04-15 20:21:05 +0000
+++ cloudinit/net/__init__.py	2016-05-04 17:26:47 +0000
@@ -27,8 +27,8 @@
 
 from cloudinit import log as logging
 from cloudinit import util
-from .udev import generate_udev_rule
-from . import network_state
+from cloudinit.net.udev import generate_udev_rule
+from cloudinit.net import network_state
 
 LOG = logging.getLogger(__name__)
 

=== added file 'cloudinit/net/converter.py'
--- cloudinit/net/converter.py	1970-01-01 00:00:00 +0000
+++ cloudinit/net/converter.py	2016-05-04 17:26:47 +0000
@@ -0,0 +1,183 @@
+# vi: ts=4 expandtab
+#
+#    Copyright (C) 2012 Canonical Ltd.
+#    Copyright (C) 2012, 2013 Hewlett-Packard Development Company, L.P.
+#    Copyright (C) 2012 Yahoo! Inc.
+#
+#    Author: Scott Moser <scott.moser@xxxxxxxxxxxxx>
+#    Author: Juerg Haefliger <juerg.haefliger@xxxxxx>
+#    Author: Joshua Harlow <harlowja@xxxxxxxxxxxxx>
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License version 3, as
+#    published by the Free Software Foundation.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# This is a util function to translate debian based distro interface blobs as
+# given in /etc/network/interfaces to an *somewhat* agnostic format for
+# distributions that use other formats.
+#
+# TODO(harlowja) remove when we have python-netcf active...
+#
+# The format is the following:
+# {
+#    <device-name>: {
+#        # All optional (if not existent in original format)
+#        "netmask": <ip>,
+#        "broadcast": <ip>,
+#        "gateway": <ip>,
+#        "address": <ip>,
+#        "bootproto": "static"|"dhcp",
+#        "dns-search": <hostname>,
+#        "hwaddress": <mac-address>,
+#        "auto": True (or non-existent),
+#        "dns-nameservers": [<ip/hostname>, ...],
+#    }
+# }
+#
+# Things to note, comments are removed, if a ubuntu/debian interface is
+# marked as auto then only then first segment (?) is retained, ie
+# 'auto eth0 eth0:1' just marks eth0 as auto (not eth0:1).
+#
+# Example input:
+#
+# auto lo
+# iface lo inet loopback
+#
+# auto eth0
+# iface eth0 inet static
+#         address 10.0.0.1
+#         netmask 255.255.252.0
+#         broadcast 10.0.0.255
+#         gateway 10.0.0.2
+#         dns-nameservers 98.0.0.1 98.0.0.2
+#
+# Example output:
+# {
+#     "lo": {
+#         "auto": true
+#     },
+#     "eth0": {
+#         "auto": true,
+#         "dns-nameservers": [
+#             "98.0.0.1",
+#             "98.0.0.2"
+#         ],
+#         "broadcast": "10.0.0.255",
+#         "netmask": "255.255.252.0",
+#         "bootproto": "static",
+#         "address": "10.0.0.1",
+#         "gateway": "10.0.0.2"
+#     }
+# }
+
+
+def translate_network(blob):
+    """Translates a debian network style blob into a normalized format."""
+    entries = []
+    for line in blob.splitlines():
+        line = line.strip()
+        if not line or line.startswith("#"):
+            continue
+        split_up = line.split(None, 1)
+        if len(split_up) <= 1:
+            continue
+        entries.append(split_up)
+    # Figure out where each iface section is
+    ifaces = []
+    consume = {}
+    for (cmd, args) in entries:
+        if cmd == 'iface':
+            if consume:
+                ifaces.append(consume)
+                consume = {}
+            consume[cmd] = args
+        else:
+            consume[cmd] = args
+    # Check if anything left over to consume
+    absorb = False
+    for (cmd, args) in consume.items():
+        if cmd == 'iface':
+            absorb = True
+    if absorb:
+        ifaces.append(consume)
+    # Now translate
+    real_ifaces = {}
+    for info in ifaces:
+        if 'iface' not in info:
+            continue
+        iface_details = info['iface'].split(None)
+        # Check if current device *may* have an ipv6 IP
+        use_ipv6 = False
+        if 'inet6' in iface_details:
+            use_ipv6 = True
+        dev_name = None
+        if len(iface_details) >= 1:
+            dev = iface_details[0].strip().lower()
+            if dev:
+                dev_name = dev
+        if not dev_name:
+            continue
+        iface_info = {}
+        iface_info['ipv6'] = {}
+        if len(iface_details) >= 3:
+            proto_type = iface_details[2].strip().lower()
+            # Seems like this can be 'loopback' which we don't
+            # really care about
+            if proto_type in ['dhcp', 'static']:
+                iface_info['bootproto'] = proto_type
+        # These can just be copied over
+        if use_ipv6:
+            for k in ['address', 'gateway']:
+                if k in info:
+                    val = info[k].strip().lower()
+                    if val:
+                        iface_info['ipv6'][k] = val
+        else:
+            for k in ['netmask', 'address', 'gateway', 'broadcast']:
+                if k in info:
+                    val = info[k].strip().lower()
+                    if val:
+                        iface_info[k] = val
+            # Name server info provided??
+            if 'dns-nameservers' in info:
+                iface_info['dns-nameservers'] = info['dns-nameservers'].split()
+            # Name server search info provided??
+            if 'dns-search' in info:
+                iface_info['dns-search'] = info['dns-search'].split()
+            # Is any mac address spoofing going on??
+            if 'hwaddress' in info:
+                hw_info = info['hwaddress'].lower().strip()
+                hw_split = hw_info.split(None, 1)
+                if len(hw_split) == 2 and hw_split[0].startswith('ether'):
+                    hw_addr = hw_split[1]
+                    if hw_addr:
+                        iface_info['hwaddress'] = hw_addr
+        # If ipv6 is enabled, device will have multiple IPs, so we need to
+        # update the dictionary instead of overwriting it...
+        if dev_name in real_ifaces:
+            real_ifaces[dev_name].update(iface_info)
+        else:
+            real_ifaces[dev_name] = iface_info
+    # Check for those that should be started on boot via 'auto'
+    for (cmd, args) in entries:
+        args = args.split(None)
+        if not args:
+            continue
+        dev_name = args[0].strip().lower()
+        if cmd == 'auto':
+            # Seems like auto can be like 'auto eth0 eth0:1' so just get the
+            # first part out as the device name
+            if dev_name in real_ifaces:
+                real_ifaces[dev_name]['auto'] = True
+        if cmd == 'iface' and 'inet6' in args:
+            real_ifaces[dev_name]['inet6'] = True
+    return real_ifaces


Follow ups