Added mount_backup_shares & mount_network_share
This commit is contained in:
parent
2770f85e01
commit
9c7914fc3d
2 changed files with 94 additions and 2 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
# Servers
|
# Servers
|
||||||
BACKUP_SERVERS = {
|
BACKUP_SERVERS = {
|
||||||
'Server One': {
|
'Server One': {
|
||||||
'IP': '10.0.0.10',
|
'Address': '10.0.0.10',
|
||||||
'Share': 'Backups',
|
'Share': 'Backups',
|
||||||
'RO-User': 'restore',
|
'RO-User': 'restore',
|
||||||
'RO-Pass': 'Abracadabra',
|
'RO-Pass': 'Abracadabra',
|
||||||
|
|
@ -14,7 +14,7 @@ BACKUP_SERVERS = {
|
||||||
'RW-Pass': 'Abracadabra',
|
'RW-Pass': 'Abracadabra',
|
||||||
},
|
},
|
||||||
'Server Two': {
|
'Server Two': {
|
||||||
'IP': '10.0.0.11',
|
'Address': 'servertwo.example.com',
|
||||||
'Share': 'Backups',
|
'Share': 'Backups',
|
||||||
'RO-User': 'restore',
|
'RO-User': 'restore',
|
||||||
'RO-Pass': 'Abracadabra',
|
'RO-Pass': 'Abracadabra',
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
"""WizardKit: Net Functions"""
|
"""WizardKit: Net Functions"""
|
||||||
# vim: sts=2 sw=2 ts=2
|
# vim: sts=2 sw=2 ts=2
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
import platform
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
|
|
@ -8,6 +11,8 @@ import psutil
|
||||||
from wk.exe import run_program
|
from wk.exe import run_program
|
||||||
from wk.std import GenericError, show_data
|
from wk.std import GenericError, show_data
|
||||||
|
|
||||||
|
from wk.cfg.net import BACKUP_SERVERS
|
||||||
|
|
||||||
|
|
||||||
# REGEX
|
# REGEX
|
||||||
REGEX_VALID_IP = re.compile(
|
REGEX_VALID_IP = re.compile(
|
||||||
|
|
@ -35,6 +40,93 @@ def connected_to_private_network():
|
||||||
raise GenericError('Not connected to a 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'):
|
def ping(addr='google.com'):
|
||||||
"""Attempt to ping addr."""
|
"""Attempt to ping addr."""
|
||||||
cmd = (
|
cmd = (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue