Ubuntu Pastebin

Paste from smoser at Mon, 22 Aug 2016 14:32:31 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Add netifrc renderer for gentoo

This adds a netifrc renderer infrastructure and some unit tests
to test you code try running 

you can run the unit tests with:

nosetests tests/unittests/test_net.py
nosetests tests/unittests/test_net.py:TestNetifrc
nosetests tests/unittests/test_net.py:TestNetifrc.testsimple_render_small

I'd get testsimple_convert_and_render working first.
diff --git a/cloudinit/net/netifrc.py b/cloudinit/net/netifrc.py
new file mode 100644
index 0000000..7deade3
--- /dev/null
+++ b/cloudinit/net/netifrc.py
@@ -0,0 +1,59 @@
+# vi: ts=4 expandtab
+#
+#    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 supports rendering output format for netifrc that is used in gentoo.
+
+https://wiki.gentoo.org/wiki/Netifrc
+"""
+
+import copy
+import glob
+import os
+import re
+
+from . import renderer
+
+from cloudinit import util
+
+
+class Renderer(renderer.Renderer):
+    """Renders network information in a /etc/network/interfaces format."""
+
+    def __init__(self, config=None):
+        if not config:
+            config = {}
+        # config is used as in debian to affect some of the paths
+        # that would be written.
+
+    def render_network_state(self, target, network_state):
+        # this actually renders the NetworkState object state into the
+        # correct format used by the target OS
+        
+        # ...
+
+        # create symlinks
+        for iface in network_state.iter_interfaces():
+            _create_network_symlink(iface['name'], target)
+
+
+def _create_network_symlink(interface_name, target):
+    file_path = os.path.join(
+        target, 'etc/init.d/net.{name}'.format(name=interface_name))
+    if not os.path.islink(file_path):
+        # attempt to do the links as relative
+        log.debug('ln -s ../../etc/init.d/net.lo {file_path}'.
+                  format(file_path=file_path))
+        os.symlink('../..//etc/init.d/net.lo',
+                   '{file_path}'.format(file_path=file_path))
+
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 41b9a6d..65d5f2e 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -1,6 +1,7 @@
 from cloudinit import net
 from cloudinit.net import cmdline
 from cloudinit.net import eni
+from cloudinit.net import netifrc
 from cloudinit.net import network_state
 from cloudinit.net import sysconfig
 from cloudinit.sources.helpers import openstack
@@ -681,6 +682,51 @@ class TestEniRoundTrip(TestCase):
             files['/etc/network/interfaces'].splitlines())
 
 
+class TestNetifrc(TestCase):
+    def setUp(self):
+        super(TestCase, self).setUp()
+        self.tmp_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, self.tmp_dir)
+
+    def _render_and_read(self, network_config=None):
+        ns = network_state.parse_net_config_data(network_config)
+        renderer = netifrc.Renderer()
+        renderer.render_network_state(self.tmp_dir, ns)
+        return dir2dict(self.tmp_dir)
+
+    def testsimple_convert_and_render(self):
+        network_config = eni.convert_eni_data(EXAMPLE_ENI)
+        files = self._render_and_read(network_config=network_config)
+
+        # files now is a dictionary of files under tmpdir
+        # (dir2dict does not support symlinks... fixes welcome)
+        # so just go through and look at the files that they are what
+        # you think.
+
+        # the Eni test case above has static expected results for
+        # some files, or you can do whatever you want.
+        self.assertEqual(
+            RENDERED_ENI.splitlines(),
+            files['/etc/network/interfaces'].splitlines())
+
+    def testsimple_render_all(self):
+
+        entry = NETWORK_CONFIGS['all']
+        files = self._render_and_read(network_config=yaml.load(entry['yaml']))
+
+        # check this looks as expected
+        print("files look like: %s" % files)
+        self.assertTrue(1 == 0)
+
+    def testsimple_render_small(self):
+        entry = NETWORK_CONFIGS['small']
+
+        # check this looks as expected
+        print("files look like: %s" % files)
+        self.assertTrue(1 == 0)
+
+
+
 def _gzip_data(data):
     with io.BytesIO() as iobuf:
         gzfp = gzip.GzipFile(mode="wb", fileobj=iobuf)
Download as text