Added mount_backup_shares & mount_network_share

This commit is contained in:
2Shirt 2019-12-09 19:23:44 -07:00
parent 2770f85e01
commit 9c7914fc3d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 94 additions and 2 deletions

View file

@ -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',

View file

@ -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 = (