mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
backups: Minor simplification in running of action script
- No need to override run method in derived class if known_credentials is a class property exposed by base class. - Move a method for cosmetic. - Minor formatting. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
357e9a412a
commit
77a93d967f
@ -82,6 +82,7 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
"""Base class for all kinds of Borg repositories."""
|
"""Base class for all kinds of Borg repositories."""
|
||||||
flags = {}
|
flags = {}
|
||||||
is_mounted = True
|
is_mounted = True
|
||||||
|
known_credentials = []
|
||||||
|
|
||||||
def __init__(self, path, credentials=None, uuid=None, **kwargs):
|
def __init__(self, path, credentials=None, uuid=None, **kwargs):
|
||||||
"""Instantiate a new repository.
|
"""Instantiate a new repository.
|
||||||
@ -130,15 +131,6 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
"""Return the repository that the backups action script should use."""
|
"""Return the repository that the backups action script should use."""
|
||||||
return self._path
|
return self._path
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_encryption_data(credentials):
|
|
||||||
"""Return additional dictionary data to send to backups call."""
|
|
||||||
passphrase = credentials.get('encryption_passphrase', None)
|
|
||||||
if passphrase:
|
|
||||||
return {'encryption_passphrase': passphrase}
|
|
||||||
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self):
|
||||||
"""Return Borg information about a repository."""
|
"""Return Borg information about a repository."""
|
||||||
output = self.run(['info', '--path', self.borg_path])
|
output = self.run(['info', '--path', self.borg_path])
|
||||||
@ -188,9 +180,19 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
"""Initialize / create a borg repository."""
|
"""Initialize / create a borg repository."""
|
||||||
if encryption not in SUPPORTED_BORG_ENCRYPTION:
|
if encryption not in SUPPORTED_BORG_ENCRYPTION:
|
||||||
raise ValueError('Unsupported encryption: %s' % encryption)
|
raise ValueError('Unsupported encryption: %s' % encryption)
|
||||||
|
|
||||||
self.run(
|
self.run(
|
||||||
['init', '--path', self.borg_path, '--encryption', encryption])
|
['init', '--path', self.borg_path, '--encryption', encryption])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_encryption_data(credentials):
|
||||||
|
"""Return additional dictionary data to send to backups call."""
|
||||||
|
passphrase = credentials.get('encryption_passphrase', None)
|
||||||
|
if passphrase:
|
||||||
|
return {'encryption_passphrase': passphrase}
|
||||||
|
|
||||||
|
return {}
|
||||||
|
|
||||||
def _run(self, cmd, arguments, superuser=True, **kwargs):
|
def _run(self, cmd, arguments, superuser=True, **kwargs):
|
||||||
"""Run a backups or sshfs action script command."""
|
"""Run a backups or sshfs action script command."""
|
||||||
try:
|
try:
|
||||||
@ -204,7 +206,7 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
def run(self, arguments, superuser=True):
|
def run(self, arguments, superuser=True):
|
||||||
"""Add credentials and run a backups action script command."""
|
"""Add credentials and run a backups action script command."""
|
||||||
for key in self.credentials.keys():
|
for key in self.credentials.keys():
|
||||||
if key not in self.KNOWN_CREDENTIALS:
|
if key not in self.known_credentials:
|
||||||
raise ValueError('Unknown credentials entry: %s' % key)
|
raise ValueError('Unknown credentials entry: %s' % key)
|
||||||
|
|
||||||
input_data = json.dumps(self._get_encryption_data(self.credentials))
|
input_data = json.dumps(self._get_encryption_data(self.credentials))
|
||||||
@ -320,13 +322,10 @@ class RootBorgRepository(BaseBorgRepository):
|
|||||||
"""Initialize the repository object."""
|
"""Initialize the repository object."""
|
||||||
super().__init__(self.PATH, credentials, self.UUID)
|
super().__init__(self.PATH, credentials, self.UUID)
|
||||||
|
|
||||||
def run(self, arguments):
|
|
||||||
return self._run('backups', arguments)
|
|
||||||
|
|
||||||
|
|
||||||
class BorgRepository(BaseBorgRepository):
|
class BorgRepository(BaseBorgRepository):
|
||||||
"""General Borg repository implementation."""
|
"""General Borg repository implementation."""
|
||||||
KNOWN_CREDENTIALS = ['encryption_passphrase']
|
known_credentials = ['encryption_passphrase']
|
||||||
storage_type = 'disk'
|
storage_type = 'disk'
|
||||||
sort_order = 20
|
sort_order = 20
|
||||||
flags = {'removable': True}
|
flags = {'removable': True}
|
||||||
@ -345,7 +344,7 @@ class BorgRepository(BaseBorgRepository):
|
|||||||
class SshBorgRepository(BaseBorgRepository):
|
class SshBorgRepository(BaseBorgRepository):
|
||||||
"""Borg repository that is accessed via SSH"""
|
"""Borg repository that is accessed via SSH"""
|
||||||
SSHFS_MOUNTPOINT = '/media/'
|
SSHFS_MOUNTPOINT = '/media/'
|
||||||
KNOWN_CREDENTIALS = [
|
known_credentials = [
|
||||||
'ssh_keyfile', 'ssh_password', 'encryption_passphrase'
|
'ssh_keyfile', 'ssh_password', 'encryption_passphrase'
|
||||||
]
|
]
|
||||||
storage_type = 'ssh'
|
storage_type = 'ssh'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user