From 9c7914fc3d9fff77863c9c2599e6c0a63072503a Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 9 Dec 2019 19:23:44 -0700 Subject: [PATCH] Added mount_backup_shares & mount_network_share --- scripts/wk/cfg/net.py | 4 +- scripts/wk/net.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/scripts/wk/cfg/net.py b/scripts/wk/cfg/net.py index cdcf7082..5e42dd84 100644 --- a/scripts/wk/cfg/net.py +++ b/scripts/wk/cfg/net.py @@ -6,7 +6,7 @@ # Servers BACKUP_SERVERS = { 'Server One': { - 'IP': '10.0.0.10', + 'Address': '10.0.0.10', 'Share': 'Backups', 'RO-User': 'restore', 'RO-Pass': 'Abracadabra', @@ -14,7 +14,7 @@ BACKUP_SERVERS = { 'RW-Pass': 'Abracadabra', }, 'Server Two': { - 'IP': '10.0.0.11', + 'Address': 'servertwo.example.com', 'Share': 'Backups', 'RO-User': 'restore', 'RO-Pass': 'Abracadabra', diff --git a/scripts/wk/net.py b/scripts/wk/net.py index 69e9a345..b03644d9 100644 --- a/scripts/wk/net.py +++ b/scripts/wk/net.py @@ -1,6 +1,9 @@ """WizardKit: Net Functions""" # vim: sts=2 sw=2 ts=2 +import os +import pathlib +import platform import re import psutil @@ -8,6 +11,8 @@ import psutil from wk.exe import run_program from wk.std import GenericError, show_data +from wk.cfg.net import BACKUP_SERVERS + # REGEX REGEX_VALID_IP = re.compile( @@ -35,6 +40,93 @@ def connected_to_private_network(): raise GenericError('Not connected to a network') +def is_mounted(details): + """Check if dev/share/etc is mounted, returns bool.""" + #TODO: Make real + if not details: + return False + return False + + +def mount_backup_shares(read_write=False): + """Mount backup shares using OS specific methods.""" + report = [] + for name, details in BACKUP_SERVERS.items(): + mount_point = None + mount_str = f'{name}/{details["Share"]}' + + # Prep mount point + if platform.system() in ('Darwin', 'Linux'): + mount_point = pathlib.Path(f'/Backups/{name}') + if not mount_point.exists(): + # Script should be run as user so sudo is required + run_program(['sudo', 'mkdir', mount_point]) + + # Check if already mounted + if is_mounted(details): + report.append(f'(Already) Mounted {mount_str}') + # Skip to next share + continue + + # Mount share + proc = mount_network_share(details, mount_point, read_write=read_write) + if proc.returncode: + report.append(f'Failed to Mount {mount_str}') + else: + report.append(f'Mounted {mount_str}') + + # Done + return report + + +def mount_network_share(details, mount_point=None, read_write=False): + """Mount network share using OS specific methods.""" + cmd = None + address = details['Address'] + share = details['Share'] + username = details['RO-User'] + password = details['RO-Pass'] + if read_write: + username = details['RW-User'] + password = details['RW-Pass'] + + # Build OS-specific command + if platform.system() == 'Darwin': + cmd = [ + 'sudo', + 'mount', + '-t', 'smbfs', + '-o', f'{"rw" if read_write else "ro"}', + f'//{username}:{password}@{address}/{share}', + mount_point, + ] + elif platform.system() == 'Linux': + cmd = [ + 'sudo', + 'mount', + '-t', 'cifs', + '-o', ( + f'{"rw" if read_write else "ro"}' + f',uid={os.getuid()}' + f',gid={os.getgid()}' + f',username={username}' + f',{"password=" if password else "guest"}{password}' + ), + f'//{address}/{share}', + mount_point + ] + elif platform.system() == 'Windows': + cmd = ['net', 'use'] + if mount_point: + cmd.append(f'{mount_point}:') + cmd.append(f'/user:{username}') + cmd.append(fr'\\{address}\{share}') + cmd.append(password) + + # Mount share + return run_program(cmd, check=False) + + def ping(addr='google.com'): """Attempt to ping addr.""" cmd = (