Ubuntu Pastebin

Paste from smoser at Thu, 7 Sep 2017 20:21:01 +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
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
index 41367a8b..a913993b 100644
--- a/cloudinit/sources/DataSourceEc2.py
+++ b/cloudinit/sources/DataSourceEc2.py
@@ -297,11 +297,12 @@ class DataSourceEc2(sources.DataSource):
 
         result = None
         net_md = self.metadata.get('network')
-        if isinstance(net_md, dict):
+        try:
             result = convert_ec2_metadata_network_config(net_md)
-        else:
-            LOG.warning("unexpected metadata 'network' key not valid: %s",
-                        net_md)
+        except ValueError as e:
+            LOG.warning("Failed converting metadata (%s). network config=%s",
+                        e, net_md)
+
         self._network_config = result
 
         return self._network_config
@@ -470,10 +471,22 @@ def convert_ec2_metadata_network_config(network_md, macs_to_nics=None):
 
     @return A dict of network config version 1 based on the metadata and macs.
     """
-    netcfg = {'version': 1, 'config': []}
+    if not isinstance(network_md, dict):
+        raise ValueError("network_md is not a dict")
+
+    if not isinstance(network_md.get('interfaces'), dict):
+        raise ValueError("network_md had non-dict at 'interfaces'")
+
+    if not isinstance(network_md['interfaces'].get('macs'), dict):
+        raise ValueError("network_md['interfaces'] had non-dict at 'macs'")
+
+    macs_metadata = network_md['interfaces']['macs']
+
     if not macs_to_nics:
         macs_to_nics = net.get_interfaces_by_mac()
-    macs_metadata = network_md['interfaces']['macs']
+
+    valid = []
+    netcfg = {'version': 1, 'config': []}
     for mac, nic_name in macs_to_nics.items():
         nic_metadata = macs_metadata.get(mac)
         if not nic_metadata:
@@ -484,7 +497,14 @@ def convert_ec2_metadata_network_config(network_md, macs_to_nics=None):
             nic_cfg['subnets'].append({'type': 'dhcp4'})
         if nic_metadata.get('ipv6s'):
             nic_cfg['subnets'].append({'type': 'dhcp6'})
+        if len(nic_cfg['subnets']):
+            valid.append(nic_name)
         netcfg['config'].append(nic_cfg)
+
+    # if no subnets were added raise a value error.
+    if not len(valid):
+        raise ValueError("No subnets defined from network_md.")
+
     return netcfg
 
 
Download as text