Skip to content

Commit

Permalink
cli:status: don't crash if NetworkManager is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
slyon committed Jan 3, 2023
1 parent 180ad49 commit add5849
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions netplan/cli/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, ip: JSON, nd_data: JSON = [], nm_data: JSON = [],
self.macaddress: str = self.__extract_mac(ip)

# Filter networkd/NetworkManager data
nm_data = nm_data or [] # avoid 'None' value on systems without NM
self.nd: JSON = next((x for x in nd_data if x['Index'] == self.idx), None)
self.nm: JSON = next((x for x in nm_data if x['device'] == self.name), None)

Expand Down Expand Up @@ -356,11 +357,11 @@ def process_generic(self, cmd_output: str) -> JSON:
return yaml.safe_load(cmd_output)

def query_iproute2(self) -> JSON:
data = None
data: JSON = None
try:
output: str = subprocess.check_output(['ip', '-d', '-j', 'addr'],
universal_newlines=True)
data: JSON = self.process_generic(output)
data = self.process_generic(output)
except Exception as e:
logging.critical('Cannot query iproute2 interface data: {}'.format(str(e)))
return data
Expand All @@ -369,11 +370,11 @@ def process_networkd(self, cmd_output) -> JSON:
return yaml.safe_load(cmd_output)['Interfaces']

def query_networkd(self) -> JSON:
data = None
data: JSON = None
try:
output: str = subprocess.check_output(['networkctl', '--json=short'],
universal_newlines=True)
data: JSON = self.process_networkd(output)
data = self.process_networkd(output)
except Exception as e:
logging.critical('Cannot query networkd interface data: {}'.format(str(e)))
return data
Expand All @@ -395,13 +396,13 @@ def process_nm(self, cmd_output) -> JSON:
return data

def query_nm(self) -> JSON:
data = None
data: JSON = None
try:
output: str = subprocess.check_output(['nmcli', '-t', '-f',
'DEVICE,NAME,UUID,FILENAME,TYPE,AUTOCONNECT',
'con', 'show'],
universal_newlines=True)
data: JSON = self.process_nm(output)
data = self.process_nm(output)
except Exception as e:
logging.debug('Cannot query NetworkManager interface data: {}'.format(str(e)))
return data
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_pretty_print(self, networkctl_mock, nm_ssid_mock):
Interface(self._get_itf('wlan0'), nd, nm, dns, routes),
Interface(self._get_itf('wg0'), nd, nm, dns, routes),
Interface(self._get_itf('tun0'), nd, nm, dns, routes),
Interface(FAKE_DEV, [], [], (None, None), ([fakeroute], None)),
Interface(FAKE_DEV, [], None, (None, None), ([fakeroute], None)),
]
data = {'netplan-global-state': {
'online': True,
Expand Down

0 comments on commit add5849

Please sign in to comment.