summaryrefslogtreecommitdiff
path: root/cloudinit/netinfo.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-05-01 14:18:18 -0600
committerChad Smith <chad.smith@canonical.com>2018-05-01 14:18:18 -0600
commit14cb4924a6cf191107f9c04698ace2753eb44d2b (patch)
treeffa46f32fb324c4218d1e7c5ce1ec04e850a7ba2 /cloudinit/netinfo.py
parent9f5907e1a14e3a4890fa25e0b1910a902e098d58 (diff)
downloadvyos-cloud-init-14cb4924a6cf191107f9c04698ace2753eb44d2b.tar.gz
vyos-cloud-init-14cb4924a6cf191107f9c04698ace2753eb44d2b.zip
netinfo: fix netdev_pformat when a nic does not have an address assigned.
The last set of changes to netdev_pformat ended up dropping the output of devices that were not up. This adds back the 'down' interfaces to the rendered output. LP: #1766302
Diffstat (limited to 'cloudinit/netinfo.py')
-rw-r--r--cloudinit/netinfo.py40
1 files changed, 30 insertions, 10 deletions
diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
index f0906160..1be76fe7 100644
--- a/cloudinit/netinfo.py
+++ b/cloudinit/netinfo.py
@@ -158,12 +158,28 @@ def netdev_info(empty=""):
LOG.warning(
"Could not print networks: missing 'ip' and 'ifconfig' commands")
- if empty != "":
- for (_devname, dev) in devs.items():
- for field in dev:
- if dev[field] == "":
- dev[field] = empty
+ if empty == "":
+ return devs
+
+ recurse_types = (dict, tuple, list)
+
+ def fill(data, new_val="", empty_vals=("", b"")):
+ """Recursively replace 'empty_vals' in data (dict, tuple, list)
+ with new_val"""
+ if isinstance(data, dict):
+ myiter = data.items()
+ elif isinstance(data, (tuple, list)):
+ myiter = enumerate(data)
+ else:
+ raise TypeError("Unexpected input to fill")
+
+ for key, val in myiter:
+ if val in empty_vals:
+ data[key] = new_val
+ elif isinstance(val, recurse_types):
+ fill(val, new_val)
+ fill(devs, new_val=empty)
return devs
@@ -353,8 +369,9 @@ def getgateway():
def netdev_pformat():
lines = []
+ empty = "."
try:
- netdev = netdev_info(empty=".")
+ netdev = netdev_info(empty=empty)
except Exception as e:
lines.append(
util.center(
@@ -368,12 +385,15 @@ def netdev_pformat():
for (dev, data) in sorted(netdev.items()):
for addr in data.get('ipv4'):
tbl.add_row(
- [dev, data["up"], addr["ip"], addr["mask"],
- addr.get('scope', '.'), data["hwaddr"]])
+ (dev, data["up"], addr["ip"], addr["mask"],
+ addr.get('scope', empty), data["hwaddr"]))
for addr in data.get('ipv6'):
tbl.add_row(
- [dev, data["up"], addr["ip"], ".", addr["scope6"],
- data["hwaddr"]])
+ (dev, data["up"], addr["ip"], empty, addr["scope6"],
+ data["hwaddr"]))
+ if len(data.get('ipv6')) + len(data.get('ipv4')) == 0:
+ tbl.add_row((dev, data["up"], empty, empty, empty,
+ data["hwaddr"]))
netdev_s = tbl.get_string()
max_len = len(max(netdev_s.splitlines(), key=len))
header = util.center("Net device info", "+", max_len)