Updated wk.net.connected_to_private_network()

* Can either return True/False or return None/raise Exception
* Added network check to mount_backup_shares()
This commit is contained in:
2Shirt 2019-12-09 20:53:42 -07:00
parent 3262888024
commit 07cb287eb0
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 29 additions and 6 deletions

1
scripts/hw-diags.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Wizard Kit: Hardware Diagnostics"""
# vim: sts=2 sw=2 ts=2

View file

@ -1231,7 +1231,11 @@ def network_test():
LOG.info('Network Test')
try_and_print = std.TryAndPrint()
result = try_and_print.run(
'Network connection...', net.connected_to_private_network, msg_good='OK')
message='Network connection...',
function=net.connected_to_private_network,
msg_good='OK',
raise_on_error=True,
)
# Bail if not connected
if result['Failed']:

View file

@ -23,21 +23,35 @@ REGEX_VALID_IP = re.compile(
# Functions
def connected_to_private_network():
"""Check if connected to a private network.
def connected_to_private_network(raise_on_error=False):
"""Check if connected to a private network, returns bool.
This checks for a valid private IP assigned to this system.
If one isn't found then an exception is raised.
NOTE: If one isn't found and raise_on_error=True then an exception is raised.
NOTE 2: If one is found and raise_on_error=True then None is returned.
"""
connected = False
# Check IPs
devs = psutil.net_if_addrs()
for dev in devs.values():
for family in dev:
if REGEX_VALID_IP.search(family.address):
# Valid IP found
return
connected = True
break
if connected:
break
# No valid IP found
raise GenericError('Not connected to a network')
if not connected and raise_on_error:
raise GenericError('Not connected to a network')
# Done
if raise_on_error:
connected = None
return connected
def mount_backup_shares(read_write=False):
@ -88,6 +102,10 @@ def mount_network_share(details, mount_point=None, read_write=False):
username = details['RW-User']
password = details['RW-Pass']
# Network check
if not connected_to_private_network():
raise RuntimeError('Not connected to a network')
# Build OS-specific command
if platform.system() == 'Darwin':
cmd = [