summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2014-11-30 21:26:14 -0800
committerJoshua Harlow <harlowja@gmail.com>2014-11-30 21:26:14 -0800
commit411c6457222f9b33d2fa272dc61fb0c56e3a8be3 (patch)
tree300416c809f3ddc7a4f9e6e85f0ac84bd656b9ff
parent26af82a55325c460fdeb7eb6dceef7d433671e47 (diff)
downloadvyos-cloud-init-411c6457222f9b33d2fa272dc61fb0c56e3a8be3.tar.gz
vyos-cloud-init-411c6457222f9b33d2fa272dc61fb0c56e3a8be3.zip
Be more tolerant of missing information
Instead of failing when IPv6 information is not found we should be more tolerant of said information not existing so that we behave like the pre IPv6 addition.
-rw-r--r--cloudinit/netinfo.py98
1 files changed, 50 insertions, 48 deletions
diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
index d891315b..473b5fd6 100644
--- a/cloudinit/netinfo.py
+++ b/cloudinit/netinfo.py
@@ -97,7 +97,6 @@ def netdev_info(empty=""):
def route_info():
(route_out, _err) = util.subp(["netstat", "-rn"])
- (route_out6, _err6) = util.subp(["netstat", "-A inet6", "-n"])
routes = {}
routes['ipv4'] = []
@@ -140,24 +139,29 @@ def route_info():
routes['ipv4'].append(entry)
- entries6 = route_out6.splitlines()[1:]
- for line in entries6:
- if not line:
- continue
- toks = line.split()
-
- if (len(toks) < 6 or toks[0] == "Kernel" or
- toks[0] == "Proto" or toks[0] == "Active"):
- continue
- entry = {
- 'proto': toks[0],
- 'recv-q': toks[1],
- 'send-q': toks[2],
- 'local address': toks[3],
- 'foreign address': toks[4],
- 'state': toks[5],
- }
- routes['ipv6'].append(entry)
+ try:
+ (route_out6, _err6) = util.subp(["netstat", "-A", "inet6", "-n"])
+ except util.ProcessExecutionError:
+ pass
+ else:
+ entries6 = route_out6.splitlines()[1:]
+ for line in entries6:
+ if not line:
+ continue
+ toks = line.split()
+
+ if (len(toks) < 6 or toks[0] == "Kernel" or
+ toks[0] == "Proto" or toks[0] == "Active"):
+ continue
+ entry = {
+ 'proto': toks[0],
+ 'recv-q': toks[1],
+ 'send-q': toks[2],
+ 'local address': toks[3],
+ 'foreign address': toks[4],
+ 'state': toks[5],
+ }
+ routes['ipv6'].append(entry)
return routes
@@ -179,14 +183,14 @@ def netdev_pformat():
netdev = netdev_info(empty=".")
except Exception:
lines.append(util.center("Net device info failed", '!', 80))
- netdev = None
- if netdev is not None:
+ else:
fields = ['Device', 'Up', 'Address', 'Mask', 'Scope', 'Hw-Address']
tbl = PrettyTable(fields)
for (dev, d) in netdev.iteritems():
tbl.add_row([dev, d["up"], d["addr"], d["mask"], ".", d["hwaddr"]])
- if d["addr6"]:
- tbl.add_row([dev, d["up"], d["addr6"], ".", d["scope6"], d["hwaddr"]])
+ if d.get('addr6'):
+ tbl.add_row([dev, d["up"],
+ d["addr6"], ".", d.get("scope6"), d["hwaddr"]])
netdev_s = tbl.get_string()
max_len = len(max(netdev_s.splitlines(), key=len))
header = util.center("Net device info", "+", max_len)
@@ -201,35 +205,33 @@ def route_pformat():
except Exception as e:
lines.append(util.center('Route info failed', '!', 80))
util.logexc(LOG, "Route info failed: %s" % e)
- routes = None
- if routes is not None:
- fields_v4 = ['Route', 'Destination', 'Gateway',
- 'Genmask', 'Interface', 'Flags']
-
- if routes.get('ipv6') is not None:
- fields_v6 = ['Route', 'Proto', 'Recv-Q', 'Send-Q', 'Local Address',
- 'Foreign Address', 'State']
-
- tbl_v4 = PrettyTable(fields_v4)
- for (n, r) in enumerate(routes.get('ipv4')):
- route_id = str(n)
- tbl_v4.add_row([route_id, r['destination'],
- r['gateway'], r['genmask'],
- r['iface'], r['flags']])
- route_s = tbl_v4.get_string()
- if fields_v6:
+ else:
+ if routes.get('ipv4'):
+ fields_v4 = ['Route', 'Destination', 'Gateway',
+ 'Genmask', 'Interface', 'Flags']
+ tbl_v4 = PrettyTable(fields_v4)
+ for (n, r) in enumerate(routes.get('ipv4')):
+ route_id = str(n)
+ tbl_v4.add_row([route_id, r['destination'],
+ r['gateway'], r['genmask'],
+ r['iface'], r['flags']])
+ route_s = tbl_v4.get_string()
+ max_len = len(max(route_s.splitlines(), key=len))
+ header = util.center("Route IPv4 info", "+", max_len)
+ lines.extend([header, route_s])
+ if routes.get('ipv6'):
+ fields_v6 = ['Route', 'Proto', 'Recv-Q', 'Send-Q',
+ 'Local Address', 'Foreign Address', 'State']
tbl_v6 = PrettyTable(fields_v6)
for (n, r) in enumerate(routes.get('ipv6')):
route_id = str(n)
tbl_v6.add_row([route_id, r['proto'],
- r['recv-q'], r['send-q'],
- r['local address'], r['foreign address'],
- r['state']])
- route_s = route_s + tbl_v6.get_string()
-
- max_len = len(max(route_s.splitlines(), key=len))
- header = util.center("Route info", "+", max_len)
- lines.extend([header, route_s])
+ r['recv-q'], r['send-q'],
+ r['local address'], r['foreign address'],
+ r['state']])
+ header = util.center("Route IPv6 info", "+", max_len)
+ route_s = tbl_v6.get_string()
+ lines.extend([header, route_s])
return "\n".join(lines)