mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
backups: Minor styling fixes
- Run yapf - Fix flake8 errors/warnings. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
c22bec1cf4
commit
de22c79665
@ -65,29 +65,29 @@ def parse_arguments():
|
|||||||
export_tar = subparsers.add_parser('export-tar', help=export_help)
|
export_tar = subparsers.add_parser('export-tar', help=export_help)
|
||||||
|
|
||||||
get_archive_apps = subparsers.add_parser(
|
get_archive_apps = subparsers.add_parser(
|
||||||
'get-archive-apps',
|
'get-archive-apps', help='Get list of apps included in archive')
|
||||||
help='Get list of apps included in archive')
|
|
||||||
|
|
||||||
restore_archive = subparsers.add_parser(
|
restore_archive = subparsers.add_parser(
|
||||||
'restore-archive', help='Restore files from an archive')
|
'restore-archive', help='Restore files from an archive')
|
||||||
restore_archive.add_argument('--destination', help='Destination',
|
restore_archive.add_argument('--destination', help='Destination',
|
||||||
required=True)
|
required=True)
|
||||||
|
|
||||||
for cmd in [info, init, list_repo, create_archive, delete_archive,
|
for cmd in [
|
||||||
export_tar, get_archive_apps, restore_archive, setup]:
|
info, init, list_repo, create_archive, delete_archive, export_tar,
|
||||||
|
get_archive_apps, restore_archive, setup
|
||||||
|
]:
|
||||||
cmd.add_argument('--path', help='Repository or Archive path',
|
cmd.add_argument('--path', help='Repository or Archive path',
|
||||||
required=False)
|
required=False)
|
||||||
cmd.add_argument('--ssh-keyfile', help='Path of private ssh key',
|
cmd.add_argument('--ssh-keyfile', help='Path of private ssh key',
|
||||||
default=None)
|
default=None)
|
||||||
cmd.add_argument('--encryption-passphrase',
|
cmd.add_argument('--encryption-passphrase',
|
||||||
help='Encryption passphrase',
|
help='Encryption passphrase', default=None)
|
||||||
default=None)
|
|
||||||
|
|
||||||
get_exported_archive_apps = subparsers.add_parser(
|
get_exported_archive_apps = subparsers.add_parser(
|
||||||
'get-exported-archive-apps',
|
'get-exported-archive-apps',
|
||||||
help='Get list of apps included in exported archive file')
|
help='Get list of apps included in exported archive file')
|
||||||
get_exported_archive_apps.add_argument(
|
get_exported_archive_apps.add_argument('--path', help='Tarball file path',
|
||||||
'--path', help='Tarball file path', required=True)
|
required=True)
|
||||||
|
|
||||||
restore_exported_archive = subparsers.add_parser(
|
restore_exported_archive = subparsers.add_parser(
|
||||||
'restore-exported-archive',
|
'restore-exported-archive',
|
||||||
@ -108,21 +108,23 @@ def subcommand_setup(arguments):
|
|||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
|
|
||||||
init(arguments, encryption='none')
|
init_repository(arguments, encryption='none')
|
||||||
|
|
||||||
|
|
||||||
def init(arguments, encryption):
|
def init_repository(arguments, encryption):
|
||||||
"""Initialize a local or remote borg repository"""
|
"""Initialize a local or remote borg repository"""
|
||||||
if encryption != 'none':
|
if encryption != 'none':
|
||||||
if not hasattr(arguments, 'encryption_passphrase') or not \
|
if not hasattr(arguments, 'encryption_passphrase') or not \
|
||||||
arguments.encryption_passphrase:
|
arguments.encryption_passphrase:
|
||||||
raise ValueError('No encryption passphrase provided')
|
raise ValueError('No encryption passphrase provided')
|
||||||
|
|
||||||
cmd = ['borg', 'init', '--encryption', encryption, arguments.path]
|
cmd = ['borg', 'init', '--encryption', encryption, arguments.path]
|
||||||
run(cmd, arguments=arguments)
|
run(cmd, arguments=arguments)
|
||||||
|
|
||||||
|
|
||||||
def subcommand_init(arguments):
|
def subcommand_init(arguments):
|
||||||
init(arguments, encryption=arguments.encryption)
|
"""Initialize the borg repository."""
|
||||||
|
init_repository(arguments, encryption=arguments.encryption)
|
||||||
|
|
||||||
|
|
||||||
def subcommand_info(arguments):
|
def subcommand_info(arguments):
|
||||||
@ -162,8 +164,7 @@ def _extract(archive_path, destination, locations=None, env=None):
|
|||||||
os.chdir(os.path.expanduser(destination))
|
os.chdir(os.path.expanduser(destination))
|
||||||
# TODO: with python 3.7 use subprocess.run with the 'capture_output'
|
# TODO: with python 3.7 use subprocess.run with the 'capture_output'
|
||||||
# argument
|
# argument
|
||||||
process = subprocess.run(borg_call, env=env,
|
process = subprocess.run(borg_call, env=env, stdout=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE)
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
error = process.stderr.decode()
|
error = process.stderr.decode()
|
||||||
@ -190,8 +191,10 @@ def subcommand_get_archive_apps(arguments):
|
|||||||
"""Get list of apps included in archive."""
|
"""Get list of apps included in archive."""
|
||||||
env = get_env(arguments)
|
env = get_env(arguments)
|
||||||
manifest_folder = os.path.relpath(MANIFESTS_FOLDER, '/')
|
manifest_folder = os.path.relpath(MANIFESTS_FOLDER, '/')
|
||||||
borg_call = ['borg', 'list', arguments.path, manifest_folder,
|
borg_call = [
|
||||||
'--format', '{path}{NEWLINE}']
|
'borg', 'list', arguments.path, manifest_folder, '--format',
|
||||||
|
'{path}{NEWLINE}'
|
||||||
|
]
|
||||||
timeout = None
|
timeout = None
|
||||||
if 'BORG_RSH' in env and 'SSHPASS' not in env:
|
if 'BORG_RSH' in env and 'SSHPASS' not in env:
|
||||||
timeout = TIMEOUT
|
timeout = TIMEOUT
|
||||||
@ -217,24 +220,25 @@ def _get_apps_of_manifest(manifest):
|
|||||||
Get apps of a manifest.
|
Get apps of a manifest.
|
||||||
Supports both dict format as well as list format of plinth <=0.42
|
Supports both dict format as well as list format of plinth <=0.42
|
||||||
"""
|
"""
|
||||||
if type(manifest) is list:
|
if isinstance(manifest, list):
|
||||||
apps = manifest
|
apps = manifest
|
||||||
elif type(manifest) is dict and 'apps' in manifest:
|
elif isinstance(manifest, dict) and 'apps' in manifest:
|
||||||
apps = manifest['apps']
|
apps = manifest['apps']
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('Unknown manifest format')
|
raise RuntimeError('Unknown manifest format')
|
||||||
|
|
||||||
return apps
|
return apps
|
||||||
|
|
||||||
|
|
||||||
def subcommand_get_exported_archive_apps(arguments):
|
def subcommand_get_exported_archive_apps(arguments):
|
||||||
"""Get list of apps included in an exported archive file."""
|
"""Get list of apps included in an exported archive file."""
|
||||||
manifest = None
|
manifest = None
|
||||||
with tarfile.open(arguments.path) as t:
|
with tarfile.open(arguments.path) as tar_handle:
|
||||||
filenames = t.getnames()
|
filenames = tar_handle.getnames()
|
||||||
for name in filenames:
|
for name in filenames:
|
||||||
if 'var/lib/plinth/backups-manifests/' in name \
|
if 'var/lib/plinth/backups-manifests/' in name \
|
||||||
and name.endswith('.json'):
|
and name.endswith('.json'):
|
||||||
manifest_data = t.extractfile(name).read()
|
manifest_data = tar_handle.extractfile(name).read()
|
||||||
manifest = json.loads(manifest_data)
|
manifest = json.loads(manifest_data)
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -275,8 +279,8 @@ def read_password():
|
|||||||
"""Read the password from stdin."""
|
"""Read the password from stdin."""
|
||||||
if sys.stdin.isatty():
|
if sys.stdin.isatty():
|
||||||
return ''
|
return ''
|
||||||
else:
|
|
||||||
return ''.join(sys.stdin)
|
return ''.join(sys.stdin)
|
||||||
|
|
||||||
|
|
||||||
def get_env(arguments, use_credentials=False):
|
def get_env(arguments, use_credentials=False):
|
||||||
@ -284,12 +288,11 @@ def get_env(arguments, use_credentials=False):
|
|||||||
env = dict(os.environ, BORG_RELOCATED_REPO_ACCESS_IS_OK='yes')
|
env = dict(os.environ, BORG_RELOCATED_REPO_ACCESS_IS_OK='yes')
|
||||||
# always provide BORG_PASSPHRASE (also if empty) so borg does not get stuck
|
# always provide BORG_PASSPHRASE (also if empty) so borg does not get stuck
|
||||||
# while asking for a passphrase.
|
# while asking for a passphrase.
|
||||||
passphrase = arguments.encryption_passphrase if \
|
passphrase = arguments.encryption_passphrase or ''
|
||||||
arguments.encryption_passphrase else ''
|
|
||||||
env['BORG_PASSPHRASE'] = passphrase
|
env['BORG_PASSPHRASE'] = passphrase
|
||||||
if use_credentials:
|
if use_credentials:
|
||||||
if arguments.ssh_keyfile:
|
if arguments.ssh_keyfile:
|
||||||
env['BORG_RSH'] = "ssh -i %s" % arguments.ssh_keyfile
|
env['BORG_RSH'] = 'ssh -i %s' % arguments.ssh_keyfile
|
||||||
else:
|
else:
|
||||||
password = read_password()
|
password = read_password()
|
||||||
if password:
|
if password:
|
||||||
@ -297,6 +300,7 @@ def get_env(arguments, use_credentials=False):
|
|||||||
env['BORG_RSH'] = 'sshpass -e ssh -o StrictHostKeyChecking=no'
|
env['BORG_RSH'] = 'sshpass -e ssh -o StrictHostKeyChecking=no'
|
||||||
else:
|
else:
|
||||||
raise ValueError('could not find credentials')
|
raise ValueError('could not find credentials')
|
||||||
|
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
|
||||||
@ -305,9 +309,10 @@ def run(cmd, arguments, check=True):
|
|||||||
# Set a timeout to not get stuck if the remote server asks for a password.
|
# Set a timeout to not get stuck if the remote server asks for a password.
|
||||||
timeout = None
|
timeout = None
|
||||||
use_credentials = False
|
use_credentials = False
|
||||||
if "@" in arguments.path:
|
if '@' in arguments.path:
|
||||||
timeout = TIMEOUT
|
timeout = TIMEOUT
|
||||||
use_credentials = True
|
use_credentials = True
|
||||||
|
|
||||||
env = get_env(arguments, use_credentials=use_credentials)
|
env = get_env(arguments, use_credentials=use_credentials)
|
||||||
subprocess.run(cmd, check=check, env=env, timeout=timeout)
|
subprocess.run(cmd, check=check, env=env, timeout=timeout)
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,7 @@ TIMEOUT = 5
|
|||||||
|
|
||||||
|
|
||||||
class AlreadyMountedError(Exception):
|
class AlreadyMountedError(Exception):
|
||||||
pass
|
"""Exception raised when mount point is already mounted."""
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
def parse_arguments():
|
||||||
@ -112,6 +112,7 @@ def _is_mounted(mountpoint):
|
|||||||
|
|
||||||
|
|
||||||
def subcommand_is_mounted(arguments):
|
def subcommand_is_mounted(arguments):
|
||||||
|
"""Print whether a path is already mounted."""
|
||||||
print(json.dumps(_is_mounted(arguments.mountpoint)))
|
print(json.dumps(_is_mounted(arguments.mountpoint)))
|
||||||
|
|
||||||
|
|
||||||
@ -119,8 +120,8 @@ def read_password():
|
|||||||
"""Read the password from stdin."""
|
"""Read the password from stdin."""
|
||||||
if sys.stdin.isatty():
|
if sys.stdin.isatty():
|
||||||
return ''
|
return ''
|
||||||
else:
|
|
||||||
return ''.join(sys.stdin)
|
return ''.join(sys.stdin)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
@ -183,15 +183,16 @@ class BorgRepository():
|
|||||||
def run(self, arguments):
|
def run(self, arguments):
|
||||||
return self._run('backups', arguments)
|
return self._run('backups', arguments)
|
||||||
|
|
||||||
def reraise_known_error(self, err):
|
@staticmethod
|
||||||
|
def reraise_known_error(err):
|
||||||
"""Look whether the caught error is known and reraise it accordingly"""
|
"""Look whether the caught error is known and reraise it accordingly"""
|
||||||
caught_error = str(err)
|
caught_error = str(err)
|
||||||
for known_error in KNOWN_ERRORS:
|
for known_error in KNOWN_ERRORS:
|
||||||
for error in known_error["errors"]:
|
for error in known_error["errors"]:
|
||||||
if error in caught_error:
|
if error in caught_error:
|
||||||
raise known_error["raise_as"](known_error["message"])
|
raise known_error["raise_as"](known_error["message"])
|
||||||
else:
|
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
|
|
||||||
class SshBorgRepository(BorgRepository):
|
class SshBorgRepository(BorgRepository):
|
||||||
@ -319,7 +320,8 @@ class SshBorgRepository(BorgRepository):
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
|
|
||||||
def _append_sshfs_arguments(self, arguments, credentials):
|
@staticmethod
|
||||||
|
def _append_sshfs_arguments(arguments, credentials):
|
||||||
"""Add credentials to a run command and kwargs"""
|
"""Add credentials to a run command and kwargs"""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
|
|
||||||
@ -348,6 +350,7 @@ def get_ssh_repositories():
|
|||||||
for storage in network_storage.get_storages().values():
|
for storage in network_storage.get_storages().values():
|
||||||
repository = SshBorgRepository(automount=False, **storage)
|
repository = SshBorgRepository(automount=False, **storage)
|
||||||
repositories[storage['uuid']] = repository.get_view_content()
|
repositories[storage['uuid']] = repository.get_view_content()
|
||||||
|
|
||||||
return repositories
|
return repositories
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user