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:
parent
3262888024
commit
07cb287eb0
3 changed files with 29 additions and 6 deletions
1
scripts/hw-diags.py
Normal file → Executable file
1
scripts/hw-diags.py
Normal file → Executable file
|
|
@ -1,3 +1,4 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
"""Wizard Kit: Hardware Diagnostics"""
|
"""Wizard Kit: Hardware Diagnostics"""
|
||||||
# vim: sts=2 sw=2 ts=2
|
# vim: sts=2 sw=2 ts=2
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1231,7 +1231,11 @@ def network_test():
|
||||||
LOG.info('Network Test')
|
LOG.info('Network Test')
|
||||||
try_and_print = std.TryAndPrint()
|
try_and_print = std.TryAndPrint()
|
||||||
result = try_and_print.run(
|
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
|
# Bail if not connected
|
||||||
if result['Failed']:
|
if result['Failed']:
|
||||||
|
|
|
||||||
|
|
@ -23,22 +23,36 @@ REGEX_VALID_IP = re.compile(
|
||||||
|
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
def connected_to_private_network():
|
def connected_to_private_network(raise_on_error=False):
|
||||||
"""Check if connected to a private network.
|
"""Check if connected to a private network, returns bool.
|
||||||
|
|
||||||
This checks for a valid private IP assigned to this system.
|
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()
|
devs = psutil.net_if_addrs()
|
||||||
for dev in devs.values():
|
for dev in devs.values():
|
||||||
for family in dev:
|
for family in dev:
|
||||||
if REGEX_VALID_IP.search(family.address):
|
if REGEX_VALID_IP.search(family.address):
|
||||||
# Valid IP found
|
# Valid IP found
|
||||||
return
|
connected = True
|
||||||
|
break
|
||||||
|
if connected:
|
||||||
|
break
|
||||||
|
|
||||||
# No valid IP found
|
# No valid IP found
|
||||||
|
if not connected and raise_on_error:
|
||||||
raise GenericError('Not connected to a network')
|
raise GenericError('Not connected to a network')
|
||||||
|
|
||||||
|
# Done
|
||||||
|
if raise_on_error:
|
||||||
|
connected = None
|
||||||
|
return connected
|
||||||
|
|
||||||
|
|
||||||
def mount_backup_shares(read_write=False):
|
def mount_backup_shares(read_write=False):
|
||||||
"""Mount backup shares using OS specific methods."""
|
"""Mount backup shares using OS specific methods."""
|
||||||
|
|
@ -88,6 +102,10 @@ def mount_network_share(details, mount_point=None, read_write=False):
|
||||||
username = details['RW-User']
|
username = details['RW-User']
|
||||||
password = details['RW-Pass']
|
password = details['RW-Pass']
|
||||||
|
|
||||||
|
# Network check
|
||||||
|
if not connected_to_private_network():
|
||||||
|
raise RuntimeError('Not connected to a network')
|
||||||
|
|
||||||
# Build OS-specific command
|
# Build OS-specific command
|
||||||
if platform.system() == 'Darwin':
|
if platform.system() == 'Darwin':
|
||||||
cmd = [
|
cmd = [
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue