Ubuntu Pastebin

Paste from mgagne at Mon, 15 Aug 2016 22:28:39 +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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
From e6fa8d45ed64b3ac3d3fb0ab13f43d1a7a292108 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/6] 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.
---
 cloudinit/sources/helpers/openstack.py             | 10 +++
 .../unittests/test_datasource/test_configdrive.py  | 99 ++++++++++++++++++++++
 2 files changed, 109 insertions(+)

diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
index 2e7a1d4..3dc57d1 100644
--- a/cloudinit/sources/helpers/openstack.py
+++ b/cloudinit/sources/helpers/openstack.py
@@ -539,6 +539,8 @@ def convert_net_json(network_json=None, known_macs=None):
     networks = network_json.get('networks', [])
     services = network_json.get('services', [])
 
+    bond_name_fmt = "bond%d"
+    bond_number = 0
     config = []
     for link in links:
         subnets = []
@@ -582,9 +584,17 @@ def convert_net_json(network_json=None, known_macs=None):
                     continue
                 elif k.startswith('bond'):
                     params.update({k: v})
+
+            # 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
+
             cfg.update({
                 'bond_interfaces': copy.deepcopy(link['bond_links']),
                 'params': params,
+                'name': link_name,
             })
         elif link['type'] in ['vlan']:
             cfg.update({
diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py
index 18551b9..feafa0f 100644
--- a/tests/unittests/test_datasource/test_configdrive.py
+++ b/tests/unittests/test_datasource/test_configdrive.py
@@ -101,6 +101,94 @@ NETWORK_DATA_2 = {
          "type": "vif", "id": "eth1", "vif_id": "vif-foo2"}]
 }
 
+NETWORK_DATA_3 = {
+  "services": [
+    {
+      "type": "dns",
+      "address": "1.1.1.191"
+    },
+    {
+      "type": "dns",
+      "address": "1.1.1.4"
+    }
+  ],
+  "networks": [
+    {
+      "network_id": "4daf5ce8-38cf-4240-9f1a-04e86d7c6117",
+      "type": "ipv4",
+      "netmask": "255.255.255.248",
+      "link": "vlan2",
+      "routes": [
+        {
+          "netmask": "0.0.0.0",
+          "network": "0.0.0.0",
+          "gateway": "2.2.2.9"
+        }
+      ],
+      "ip_address": "2.2.2.13",
+      "id": "network2-ipv4"
+    },
+    {
+      "network_id": "a9e2f47c-3c43-4782-94d0-e1eeef1c8c9d",
+      "type": "ipv4",
+      "netmask": "255.255.255.248",
+      "link": "vlan3",
+      "routes": [
+        {
+          "netmask": "255.255.255.255",
+          "network": "192.168.1.0",
+          "gateway": "10.0.1.1"
+        }
+      ],
+      "ip_address": "10.0.1.5",
+      "id": "network3-ipv4"
+    }
+  ],
+  "links": [
+    {
+      "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
+      "type": "phy",
+      "id": "eth0",
+      "mtu": 1500
+    },
+    {
+      "ethernet_mac_address": "0c:c4:7a:34:6e:3d",
+      "type": "phy",
+      "id": "eth1",
+      "mtu": 1500
+    },
+    {
+      "bond_mode": "4",
+      "bond_miimon": 100,
+      "type": "bond",
+      "bond_links": [
+        "eth0",
+        "eth1"
+      ],
+      "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
+      "id": "bond0",
+      "bond_xmit_hash_policy": "layer3+4"
+    },
+    {
+      "ethernet_mac_address": "fa:16:3e:b3:72:30",
+      "vlan_link": "bond0",
+      "vlan_id": 602,
+      "type": "vlan",
+      "id": "vlan2",
+      "vlan_mac_address": "fa:16:3e:b3:72:30"
+    },
+    {
+      "ethernet_mac_address": "fa:16:3e:66:ab:a6",
+      "vlan_link": "bond0",
+      "vlan_id": 612,
+      "type": "vlan",
+      "id": "vlan3",
+      "vlan_mac_address": "fa:16:3e:66:ab:a6"
+    }
+  ]
+}
+
+
 
 KNOWN_MACS = {
     'fa:16:3e:69:b0:58': 'enp0s1',
@@ -555,6 +643,17 @@ class TestConvertNetworkData(TestCase):
             eni_rendering = f.read()
             self.assertIn("route add default gw 2.2.2.9", eni_rendering)
 
+    def test_bond_conversion(self):
+        ncfg = openstack.convert_net_json(NETWORK_DATA_3,
+                                          known_macs=KNOWN_MACS)
+        eni_renderer = eni.Renderer()
+        eni_renderer.render_network_state(
+            self.tmp, network_state.parse_net_config_data(ncfg))
+        with open(os.path.join(self.tmp, "etc",
+                               "network", "interfaces"), 'r') as f:
+            eni_rendering = f.read()
+        # TODO: do some check on sanity of the bond interfaces
+
 
 def cfg_ds_from_dir(seed_d):
     cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None,
-- 
1.7.12.3


From 0c06e3c4351c9cd306214db20ecfc0ff8ae0d3d5 Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@ubuntu.com>
Date: Wed, 10 Aug 2016 13:19:17 -0600
Subject: [PATCH 2/6] udpate test a bit. mention a FIXME

---
 .../unittests/test_datasource/test_configdrive.py  | 168 +++++++++++----------
 1 file changed, 86 insertions(+), 82 deletions(-)

diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py
index feafa0f..790ea67 100644
--- a/tests/unittests/test_datasource/test_configdrive.py
+++ b/tests/unittests/test_datasource/test_configdrive.py
@@ -102,100 +102,101 @@ NETWORK_DATA_2 = {
 }
 
 NETWORK_DATA_3 = {
-  "services": [
-    {
-      "type": "dns",
-      "address": "1.1.1.191"
-    },
-    {
-      "type": "dns",
-      "address": "1.1.1.4"
-    }
-  ],
-  "networks": [
-    {
-      "network_id": "4daf5ce8-38cf-4240-9f1a-04e86d7c6117",
-      "type": "ipv4",
-      "netmask": "255.255.255.248",
-      "link": "vlan2",
-      "routes": [
+    "services": [
+        {
+            "type": "dns",
+            "address": "1.1.1.191"
+        },
         {
-          "netmask": "0.0.0.0",
-          "network": "0.0.0.0",
-          "gateway": "2.2.2.9"
+            "type": "dns",
+            "address": "1.1.1.4"
         }
-      ],
-      "ip_address": "2.2.2.13",
-      "id": "network2-ipv4"
-    },
-    {
-      "network_id": "a9e2f47c-3c43-4782-94d0-e1eeef1c8c9d",
-      "type": "ipv4",
-      "netmask": "255.255.255.248",
-      "link": "vlan3",
-      "routes": [
+    ],
+    "networks": [
         {
-          "netmask": "255.255.255.255",
-          "network": "192.168.1.0",
-          "gateway": "10.0.1.1"
+            "network_id": "4daf5ce8-38cf-4240-9f1a-04e86d7c6117",
+            "type": "ipv4",
+            "netmask": "255.255.255.248",
+            "link": "vlan2",
+            "routes": [
+                {
+                    "netmask": "0.0.0.0",
+                    "network": "0.0.0.0",
+                    "gateway": "2.2.2.9"
+                }
+            ],
+            "ip_address": "2.2.2.13",
+            "id": "network2-ipv4"
+        },
+        {
+            "network_id": "a9e2f47c-3c43-4782-94d0-e1eeef1c8c9d",
+            "type": "ipv4",
+            "netmask": "255.255.255.248",
+            "link": "vlan3",
+            "routes": [
+                {
+                    "netmask": "255.255.255.255",
+                    "network": "192.168.1.0",
+                    "gateway": "10.0.1.1"
+                }
+            ],
+            "ip_address": "10.0.1.5",
+            "id": "network3-ipv4"
         }
-      ],
-      "ip_address": "10.0.1.5",
-      "id": "network3-ipv4"
-    }
-  ],
-  "links": [
-    {
-      "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
-      "type": "phy",
-      "id": "eth0",
-      "mtu": 1500
-    },
-    {
-      "ethernet_mac_address": "0c:c4:7a:34:6e:3d",
-      "type": "phy",
-      "id": "eth1",
-      "mtu": 1500
-    },
-    {
-      "bond_mode": "4",
-      "bond_miimon": 100,
-      "type": "bond",
-      "bond_links": [
-        "eth0",
-        "eth1"
-      ],
-      "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
-      "id": "bond0",
-      "bond_xmit_hash_policy": "layer3+4"
-    },
-    {
-      "ethernet_mac_address": "fa:16:3e:b3:72:30",
-      "vlan_link": "bond0",
-      "vlan_id": 602,
-      "type": "vlan",
-      "id": "vlan2",
-      "vlan_mac_address": "fa:16:3e:b3:72:30"
-    },
-    {
-      "ethernet_mac_address": "fa:16:3e:66:ab:a6",
-      "vlan_link": "bond0",
-      "vlan_id": 612,
-      "type": "vlan",
-      "id": "vlan3",
-      "vlan_mac_address": "fa:16:3e:66:ab:a6"
-    }
-  ]
+    ],
+    "links": [
+        {
+            "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
+            "type": "phy",
+            "id": "eth0",
+            "mtu": 1500
+        },
+        {
+            "ethernet_mac_address": "0c:c4:7a:34:6e:3d",
+            "type": "phy",
+            "id": "eth1",
+            "mtu": 1500
+        },
+        {
+            "bond_mode": "4",
+            "bond_miimon": 100,
+            "type": "bond",
+            "bond_links": [
+                "eth0",
+                "eth1"
+            ],
+            "ethernet_mac_address": "0c:c4:7a:34:6e:3c",
+            "id": "bond0",
+            "bond_xmit_hash_policy": "layer3+4"
+        },
+        {
+            "ethernet_mac_address": "fa:16:3e:b3:72:30",
+            "vlan_link": "bond0",
+            "vlan_id": 602,
+            "type": "vlan",
+            "id": "vlan2",
+            "vlan_mac_address": "fa:16:3e:b3:72:30"
+        },
+        {
+            "ethernet_mac_address": "fa:16:3e:66:ab:a6",
+            "vlan_link": "bond0",
+            "vlan_id": 612,
+            "type": "vlan",
+            "id": "vlan3",
+            "vlan_mac_address": "fa:16:3e:66:ab:a6"
+        }
+    ]
 }
 
 
-
 KNOWN_MACS = {
     'fa:16:3e:69:b0:58': 'enp0s1',
     'fa:16:3e:d4:57:ad': 'enp0s2',
     'fa:16:3e:dd:50:9a': 'foo1',
     'fa:16:3e:a8:14:69': 'foo2',
     'fa:16:3e:ed:9a:59': 'foo3',
+    '0c:c4:7a:34:6e:3d': 'oeth1',
+    '0c:c4:7a:34:6e:3c': 'oeth0',
 }
 
 CFG_DRIVE_FILES_V2 = {
@@ -652,7 +653,10 @@ class TestConvertNetworkData(TestCase):
         with open(os.path.join(self.tmp, "etc",
                                "network", "interfaces"), 'r') as f:
             eni_rendering = f.read()
-        # TODO: do some check on sanity of the bond interfaces
+        print("eni_rendering=%s" % eni_rendering)
+        # FIXME: rendered eni ends up with 'eth0' and 'eth1' references
+        # (auto eth0) where it should have oeth0 and oeth1.
+        raise Exception("FOO")
 
 
 def cfg_ds_from_dir(seed_d):
-- 
1.7.12.3


From 640bf9a596c75dde341d7f71e4e290af78cc5748 Mon Sep 17 00:00:00 2001
From: Scott Moser <smoser@ubuntu.com>
Date: Fri, 12 Aug 2016 21:09:46 -0400
Subject: [PATCH 3/6] write the correct link name for bonds

this is not the prettiest, but maintain a list of info
by ids so we can get the correct name for the link-id that is
referenced.
---
 cloudinit/sources/helpers/openstack.py | 48 ++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
index 3dc57d1..ee7b4b6 100644
--- a/cloudinit/sources/helpers/openstack.py
+++ b/cloudinit/sources/helpers/openstack.py
@@ -539,6 +539,8 @@ 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 = []
@@ -553,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()
@@ -591,32 +600,51 @@ def convert_net_json(network_json=None, known_macs=None):
             link_name = bond_name_fmt % bond_number
             bond_number += 1
 
-            cfg.update({
-                'bond_interfaces': copy.deepcopy(link['bond_links']),
-                'params': params,
-                'name': link_name,
-            })
+            # 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:
@@ -625,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 b137cd1e17e23b93037b90f4f1349f839f759796 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= <mgagne@iweb.com>
Date: Tue, 9 Aug 2016 19:56:09 -0400
Subject: [PATCH 4/6] Bond interfaces need to be auto

---
 cloudinit/net/eni.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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


From d3c1b9347a45e1d34083253faae09aa98b5fce22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= <mgagne@iweb.com>
Date: Mon, 15 Aug 2016 17:03:26 -0400
Subject: [PATCH 5/6] Catch ENOTDIR error

---
 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 5b6da2f4be0e219f9c232fd4d81bb3901946db2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= <mgagne@iweb.com>
Date: Mon, 15 Aug 2016 17:49:12 -0400
Subject: [PATCH 6/6] Get real mac address if interface is bonding slave

---
 cloudinit/net/__init__.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
index 126d83f..fa24bec 100644
--- a/cloudinit/net/__init__.py
+++ b/cloudinit/net/__init__.py
@@ -91,6 +91,10 @@ def is_physical(devname):
     return os.path.exists(sys_dev_path(devname, "device"))
 
 
+def is_bonding_slave(devname):
+    return os.path.exists(sys_dev_path(devname, "bonding_slave"))
+
+
 def is_present(devname):
     return os.path.exists(sys_dev_path(devname))
 
@@ -347,7 +351,10 @@ 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)
+    if is_bonding_slave(ifname):
+        return read_sys_net(ifname, "bonding_slave/perm_hwaddr", enoent=False)
+    else:
+        return read_sys_net(ifname, "address", enoent=False)
 
 
 def get_interfaces_by_mac(devs=None):
-- 
1.7.12.3
Download as text