Ubuntu Pastebin

Paste from mgagne at Mon, 22 Aug 2016 16:13:44 +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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
From dbb8f0a3890fe6977a2bc603fb3d37b6bfc910c4 Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@ubuntu.com>
Date: Wed, 3 Aug 2016 14:55:18 -0400
Subject: [PATCH 1/4] Generate a dummy bond name for OpenStack

The OpenStack network_data.json does not provide a name for bond links.
This change makes it so a dummy one is generated and used instead
to satisfy cloud-init which does require one.

In order to write the correct link (underlying 'link' names)
for the bonds, we maintain a list of info by ids so we can easily
get the right device name.
---
 cloudinit/sources/helpers/openstack.py | 56 +++++++++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 7 deletions(-)

diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
index 84322e0..568b5c2 100644
--- a/cloudinit/sources/helpers/openstack.py
+++ b/cloudinit/sources/helpers/openstack.py
@@ -539,6 +539,10 @@ def convert_net_json(network_json=None, known_macs=None):
     networks = network_json.get('networks', [])
     services = network_json.get('services', [])
 
+    bond_links_needed = []
+    link_id_info = {}
+    bond_name_fmt = "bond%d"
+    bond_number = 0
     config = []
     for link in links:
         subnets = []
@@ -551,6 +555,13 @@ def convert_net_json(network_json=None, known_macs=None):
         if 'name' in link:
             cfg['name'] = link['name']
 
+        if link.get('ethernet_mac_address'):
+            link_id_info[link['id']] = link.get('ethernet_mac_address')
+
+        curinfo = {'name': cfg.get('name'),
+                   'mac': link.get('ethernet_mac_address'),
+                   'id': link['id'], 'type': link['type']}
+
         for network in [n for n in networks
                         if n['link'] == link['id']]:
             subnet = dict((k, v) for k, v in network.items()
@@ -582,31 +593,58 @@ def convert_net_json(network_json=None, known_macs=None):
                     continue
                 elif k.startswith('bond'):
                     params.update({k: v})
-            cfg.update({
-                'bond_interfaces': copy.deepcopy(link['bond_links']),
-                'params': params,
-            })
+
+            # openstack does not provide a name for the bond.
+            # they do provide an 'id', but that is possibly non-sensical.
+            # so we just create our own name.
+            link_name = bond_name_fmt % bond_number
+            bond_number += 1
+
+            # bond_links reference links by their id, but we need to add
+            # to the network config by their nic name.
+            # store that in bond_links_needed, and update these later.
+            bond_links_needed.append(
+                (cfg, copy.deepcopy(link['bond_links']))
+            )
+            #cfg.update({
+            #    'bond_interfaces': copy.deepcopy(link['bond_links']),
+            #    'params': params,
+            #    'name': link_name,
+            #})
+            cfg.update({'params': params, 'name': link_name})
+
+            curinfo['name'] = link_name
         elif link['type'] in ['vlan']:
+            name = "%s.%s" % (link['vlan_link'], link['vlan_id'])
             cfg.update({
-                'name': "%s.%s" % (link['vlan_link'],
-                                   link['vlan_id']),
+                'name': name,
                 'vlan_link': link['vlan_link'],
                 'vlan_id': link['vlan_id'],
                 'mac_address': link['vlan_mac_address'],
             })
+            curinfo.update({'mac': link['vlan_mac_address'],
+                            'name': name})
         else:
             raise ValueError(
                 'Unknown network_data link type: %s' % link['type'])
 
         config.append(cfg)
+        link_id_info[curinfo['id']] = curinfo
 
     need_names = [d for d in config
                   if d.get('type') == 'physical' and 'name' not in d]
 
-    if need_names:
+    if need_names or bond_links_needed:
         if known_macs is None:
             known_macs = net.get_interfaces_by_mac()
 
+        # go through and fill out the link_id_info with names
+        for link_id, info in link_id_info.items():
+            if info.get('name'):
+                continue
+            if info.get('mac') in known_macs:
+                info['name'] = known_macs[info['mac']]
+
         for d in need_names:
             mac = d.get('mac_address')
             if not mac:
@@ -615,6 +653,10 @@ def convert_net_json(network_json=None, known_macs=None):
                 raise ValueError("Unable to find a system nic for %s" % d)
             d['name'] = known_macs[mac]
 
+        for cfg, link_ids in bond_links_needed:
+            cfg['bond_interfaces'] = [link_id_info[l]['name']
+                                      for l in link_ids]
+
     for service in services:
         cfg = service
         cfg.update({'type': 'nameserver'})
-- 
1.7.12.3


From e19b372f409c2596e83905ecec2ab862beb73fef Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@brickies.net>
Date: Mon, 15 Aug 2016 15:43:35 -0400
Subject: [PATCH 2/4] add a vlan test case

this adds a vlan test case that similarly references an id
rather than a name, where the id is not the name.
---
 cloudinit/sources/helpers/openstack.py | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
index 568b5c2..a5a2a1d 100644
--- a/cloudinit/sources/helpers/openstack.py
+++ b/cloudinit/sources/helpers/openstack.py
@@ -539,7 +539,7 @@ def convert_net_json(network_json=None, known_macs=None):
     networks = network_json.get('networks', [])
     services = network_json.get('services', [])
 
-    bond_links_needed = []
+    link_updates = []
     link_id_info = {}
     bond_name_fmt = "bond%d"
     bond_number = 0
@@ -603,14 +603,10 @@ def convert_net_json(network_json=None, known_macs=None):
             # bond_links reference links by their id, but we need to add
             # to the network config by their nic name.
             # store that in bond_links_needed, and update these later.
-            bond_links_needed.append(
-                (cfg, copy.deepcopy(link['bond_links']))
+            link_updates.append(
+                (cfg, 'bond_interfaces', '%s',
+                 copy.deepcopy(link['bond_links']))
             )
-            #cfg.update({
-            #    'bond_interfaces': copy.deepcopy(link['bond_links']),
-            #    'params': params,
-            #    'name': link_name,
-            #})
             cfg.update({'params': params, 'name': link_name})
 
             curinfo['name'] = link_name
@@ -618,10 +614,12 @@ def convert_net_json(network_json=None, known_macs=None):
             name = "%s.%s" % (link['vlan_link'], link['vlan_id'])
             cfg.update({
                 'name': name,
-                'vlan_link': link['vlan_link'],
                 'vlan_id': link['vlan_id'],
                 'mac_address': link['vlan_mac_address'],
             })
+            link_updates.append((cfg, 'vlan_link', '%s', link['vlan_link']))
+            link_updates.append((cfg, 'name', "%%s.%s" % link['vlan_id'],
+                                 link['vlan_link']))
             curinfo.update({'mac': link['vlan_mac_address'],
                             'name': name})
         else:
@@ -634,7 +632,7 @@ def convert_net_json(network_json=None, known_macs=None):
     need_names = [d for d in config
                   if d.get('type') == 'physical' and 'name' not in d]
 
-    if need_names or bond_links_needed:
+    if need_names or link_updates:
         if known_macs is None:
             known_macs = net.get_interfaces_by_mac()
 
@@ -653,9 +651,11 @@ def convert_net_json(network_json=None, known_macs=None):
                 raise ValueError("Unable to find a system nic for %s" % d)
             d['name'] = known_macs[mac]
 
-        for cfg, link_ids in bond_links_needed:
-            cfg['bond_interfaces'] = [link_id_info[l]['name']
-                                      for l in link_ids]
+        for cfg, key, fmt, target in link_updates:
+            if isinstance(target, (list, tuple)):
+                cfg[key] = [fmt % link_id_info[l]['name'] for l in target]
+            else:
+                cfg[key] = fmt % link_id_info[target]['name']
 
     for service in services:
         cfg = service
-- 
1.7.12.3


From 2dee86048c210b534d8a618bcd2994c33d0af2b5 Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@brickies.net>
Date: Mon, 15 Aug 2016 16:55:35 -0400
Subject: [PATCH 3/4] read_sys_net catch a errno.ENOTDIR error as ENOENT

Opening a path as a file that has <existing_file>/anything will return
a will raise ENOTDIR rather than ENOENT.  This handles that case
in read_sys_net as a if the file did not exist.
---
 cloudinit/net/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
index 21cc602..126d83f 100644
--- a/cloudinit/net/__init__.py
+++ b/cloudinit/net/__init__.py
@@ -36,7 +36,7 @@ def read_sys_net(devname, path, translate=None, enoent=None, keyerror=None):
     try:
         contents = util.load_file(sys_dev_path(devname, path))
     except (OSError, IOError) as e:
-        if getattr(e, 'errno', None) == errno.ENOENT:
+        if getattr(e, 'errno', None) in (errno.ENOENT, errno.ENOTDIR):
             if enoent is not None:
                 return enoent
         raise
-- 
1.7.12.3


From dcb9605760319201c11f16aa7d439dd7e386a14a Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@brickies.net>
Date: Tue, 16 Aug 2016 15:00:28 -0400
Subject: [PATCH 4/4] make bond interfaces auto, get hwaddress of nic if part
 of bond.

---
 cloudinit/net/__init__.py | 7 ++++++-
 cloudinit/net/eni.py      | 2 +-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
index 126d83f..7e58bfe 100644
--- a/cloudinit/net/__init__.py
+++ b/cloudinit/net/__init__.py
@@ -347,7 +347,12 @@ def _rename_interfaces(renames, strict_present=True, strict_busy=True,
 
 def get_interface_mac(ifname):
     """Returns the string value of an interface's MAC Address"""
-    return read_sys_net(ifname, "address", enoent=False)
+    path = "address"
+    if os.path.isdir(sys_dev_path(ifname, "bonding_slave")):
+        # for a bond slave, get the nic's hwaddress, not the address it
+        # is using because its part of a bond.
+        path = "bonding_slave/perm_hwaddr"
+    return read_sys_net(ifname, path, enoent=False)
 
 
 def get_interfaces_by_mac(devs=None):
diff --git a/cloudinit/net/eni.py b/cloudinit/net/eni.py
index eff5b92..cd533dd 100644
--- a/cloudinit/net/eni.py
+++ b/cloudinit/net/eni.py
@@ -399,7 +399,7 @@ class Renderer(renderer.Renderer):
         else:
             # ifenslave docs say to auto the slave devices
             lines = []
-            if 'bond-master' in iface:
+            if 'bond-master' in iface or 'bond-slaves' in iface:
                 lines.append("auto {name}".format(**iface))
             lines.append("iface {name} {inet} {mode}".format(**iface))
             lines.extend(_iface_add_attrs(iface, index=0))
-- 
1.7.12.3
Download as text