mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[wip] Pep8 + repo change
This commit is contained in:
parent
10ce900aa6
commit
e96d76814f
2 changed files with 63 additions and 78 deletions
|
@ -71,7 +71,6 @@ class BackupRestoreTargetsManager(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
self.targets = {}
|
self.targets = {}
|
||||||
self.results = {
|
self.results = {
|
||||||
"system": {},
|
"system": {},
|
||||||
|
@ -1969,8 +1968,7 @@ class CustomBackupMethod(BackupMethod):
|
||||||
# "Front-end" #
|
# "Front-end" #
|
||||||
#
|
#
|
||||||
|
|
||||||
def backup_create(name=None, description=None, methods=[],
|
def backup_create(name=None, description=None, repos=[],
|
||||||
output_directory=None, no_compress=False,
|
|
||||||
system=[], apps=[]):
|
system=[], apps=[]):
|
||||||
"""
|
"""
|
||||||
Create a backup local archive
|
Create a backup local archive
|
||||||
|
@ -1995,30 +1993,6 @@ def backup_create(name=None, description=None, methods=[],
|
||||||
if name and name in backup_list()['archives']:
|
if name and name in backup_list()['archives']:
|
||||||
raise YunohostError('backup_archive_name_exists')
|
raise YunohostError('backup_archive_name_exists')
|
||||||
|
|
||||||
# Validate output_directory option
|
|
||||||
if output_directory:
|
|
||||||
output_directory = os.path.abspath(output_directory)
|
|
||||||
|
|
||||||
# Check for forbidden folders
|
|
||||||
if output_directory.startswith(ARCHIVES_PATH) or \
|
|
||||||
re.match(r'^/(|(bin|boot|dev|etc|lib|root|run|sbin|sys|usr|var)(|/.*))$',
|
|
||||||
output_directory):
|
|
||||||
raise YunohostError('backup_output_directory_forbidden')
|
|
||||||
|
|
||||||
# Check that output directory is empty
|
|
||||||
if os.path.isdir(output_directory) and no_compress and \
|
|
||||||
os.listdir(output_directory):
|
|
||||||
raise YunohostError('backup_output_directory_not_empty')
|
|
||||||
elif no_compress:
|
|
||||||
raise YunohostError('backup_output_directory_required')
|
|
||||||
|
|
||||||
# Define methods (retro-compat)
|
|
||||||
if not methods:
|
|
||||||
if no_compress:
|
|
||||||
methods = ['copy']
|
|
||||||
else:
|
|
||||||
methods = ['tar'] # In future, borg will be the default actions
|
|
||||||
|
|
||||||
# If no --system or --apps given, backup everything
|
# If no --system or --apps given, backup everything
|
||||||
if system is None and apps is None:
|
if system is None and apps is None:
|
||||||
system = []
|
system = []
|
||||||
|
@ -2032,20 +2006,15 @@ def backup_create(name=None, description=None, methods=[],
|
||||||
_create_archive_dir()
|
_create_archive_dir()
|
||||||
|
|
||||||
# Prepare files to backup
|
# Prepare files to backup
|
||||||
if no_compress:
|
|
||||||
backup_manager = BackupManager(name, description,
|
|
||||||
work_dir=output_directory)
|
|
||||||
else:
|
|
||||||
backup_manager = BackupManager(name, description)
|
backup_manager = BackupManager(name, description)
|
||||||
|
|
||||||
# Add backup methods
|
# Add backup methods
|
||||||
if output_directory:
|
if repos == []:
|
||||||
methods = BackupMethod.create(methods, output_directory)
|
repos = ['/home/yunohost.backup/archives']
|
||||||
else:
|
|
||||||
methods = BackupMethod.create(methods)
|
|
||||||
|
|
||||||
for method in methods:
|
methods = []
|
||||||
backup_manager.add(method)
|
for repo in repos:
|
||||||
|
backup_manager.add(BackupMethod.create(methods, repo))
|
||||||
|
|
||||||
# Add backup targets (system and apps)
|
# Add backup targets (system and apps)
|
||||||
backup_manager.set_system_targets(system)
|
backup_manager.set_system_targets(system)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
""" License
|
""" License
|
||||||
|
|
||||||
Copyright (C) 2013 YunoHost
|
Copyright (C) 2013 Yunohost
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as published
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
@ -25,36 +25,39 @@
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import json
|
|
||||||
import errno
|
|
||||||
import time
|
import time
|
||||||
import tarfile
|
|
||||||
import shutil
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from moulinette import msignals, m18n
|
from moulinette import msignals, m18n
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
from moulinette.utils import filesystem
|
from moulinette.utils import filesystem
|
||||||
from moulinette.utils.log import getActionLogger
|
from moulinette.utils.log import getActionLogger
|
||||||
from moulinette.utils.filesystem import read_file
|
from moulinette.utils.filesystem import read_file, read_json, write_to_json
|
||||||
|
|
||||||
|
|
||||||
|
from yunohost.utils.error import YunohostError
|
||||||
from yunohost.monitor import binary_to_human
|
from yunohost.monitor import binary_to_human
|
||||||
from yunohost.log import OperationLogger
|
from yunohost.log import OperationLogger, is_unit_operation
|
||||||
|
from yunohost.backup import BackupMethod
|
||||||
|
|
||||||
logger = getActionLogger('yunohost.repository')
|
logger = getActionLogger('yunohost.repository')
|
||||||
REPOSITORIES_PATH = '/etc/yunohost/repositories.yml'
|
REPOSITORIES_PATH = '/etc/yunohost/repositories.yml'
|
||||||
|
|
||||||
|
|
||||||
class BackupRepository(object):
|
class BackupRepository(object):
|
||||||
|
"""
|
||||||
|
BackupRepository manage all repository the admin added to the instance
|
||||||
|
"""
|
||||||
|
repositories = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, name):
|
def get(cls, name):
|
||||||
cls.load()
|
cls.load()
|
||||||
|
|
||||||
if name not in cls.repositories:
|
if name not in cls.repositories:
|
||||||
raise MoulinetteError(errno.EINVAL, m18n.n(
|
raise YunohostError('backup_repository_doesnt_exists', name=name)
|
||||||
'backup_repository_doesnt_exists', name=name))
|
|
||||||
|
|
||||||
return BackupRepository(**repositories[name])
|
return BackupRepository(**cls.repositories[name])
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls):
|
def load(cls):
|
||||||
|
@ -67,10 +70,7 @@ class BackupRepository(object):
|
||||||
try:
|
try:
|
||||||
cls.repositories = read_json(REPOSITORIES_PATH)
|
cls.repositories = read_json(REPOSITORIES_PATH)
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
raise MoulinetteError(1,
|
raise YunohostError('backup_cant_open_repositories_file', reason=e)
|
||||||
m18n.n('backup_cant_open_repositories_file',
|
|
||||||
reason=e),
|
|
||||||
exc_info=1)
|
|
||||||
return cls.repositories
|
return cls.repositories
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -81,34 +81,41 @@ class BackupRepository(object):
|
||||||
try:
|
try:
|
||||||
write_to_json(REPOSITORIES_PATH, cls.repositories)
|
write_to_json(REPOSITORIES_PATH, cls.repositories)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise MoulinetteError(1, m18n.n('backup_cant_save_repositories_file',
|
raise YunohostError('backup_cant_save_repositories_file', reason=e)
|
||||||
reason=e),
|
|
||||||
exc_info=1)
|
|
||||||
|
|
||||||
|
def __init__(self, location, name=None, description=None, method=None,
|
||||||
def __init__(self, location, name, description=None, method=None,
|
|
||||||
encryption=None, quota=None):
|
encryption=None, quota=None):
|
||||||
|
|
||||||
self.location = None
|
self.location = location
|
||||||
self._split_location()
|
self._split_location()
|
||||||
|
|
||||||
self.name = location if name is None else name
|
self.name = location if name is None else name
|
||||||
if self.name in repositories:
|
if self.name in BackupMethod.repositories:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('backup_repository_already_exists', repositories=name))
|
raise YunohostError('backup_repository_already_exists', repositories=name)
|
||||||
|
|
||||||
self.description = None
|
self.description = description
|
||||||
self.encryption = None
|
self.encryption = encryption
|
||||||
self.quota = None
|
self.quota = quota
|
||||||
|
|
||||||
if method is None:
|
if method is None:
|
||||||
method = 'tar' if self.domain is None else 'borg'
|
method = 'tar' if self.domain is None else 'borg'
|
||||||
self.method = BackupMethod.create(method, self)
|
self.method = BackupMethod.create(method, self)
|
||||||
|
|
||||||
|
# Check for forbidden folders
|
||||||
|
if self.path.startswith(ARCHIVES_PATH) or \
|
||||||
|
re.match(r'^/(|(bin|boot|dev|etc|lib|root|run|sbin|sys|usr|var)(|/.*))$',
|
||||||
|
self.path):
|
||||||
|
raise YunohostError('backup_output_directory_forbidden')
|
||||||
|
|
||||||
|
# Check that output directory is empty
|
||||||
|
if os.path.isdir(location) and os.listdir(location):
|
||||||
|
raise YunohostError('backup_output_directory_not_empty')
|
||||||
|
|
||||||
def compute_space_used(self):
|
def compute_space_used(self):
|
||||||
if self.used is None:
|
if self.used is None:
|
||||||
try:
|
try:
|
||||||
self.used = self.method.compute_space_used()
|
self.used = self.method.compute_space_used()
|
||||||
except NotYetImplemented as e:
|
except (AttributeError, NotImplementedError):
|
||||||
self.used = 'unknown'
|
self.used = 'unknown'
|
||||||
return self.used
|
return self.used
|
||||||
|
|
||||||
|
@ -116,9 +123,9 @@ class BackupRepository(object):
|
||||||
self.method.purge()
|
self.method.purge()
|
||||||
|
|
||||||
def delete(self, purge=False):
|
def delete(self, purge=False):
|
||||||
repositories = BackupRepositories.repositories
|
repositories = BackupRepository.repositories
|
||||||
|
|
||||||
repository = repositories.pop(name)
|
repositories.pop(self.name)
|
||||||
|
|
||||||
BackupRepository.save()
|
BackupRepository.save()
|
||||||
|
|
||||||
|
@ -137,13 +144,15 @@ class BackupRepository(object):
|
||||||
location_match = re.match(location_regex, self.location)
|
location_match = re.match(location_regex, self.location)
|
||||||
|
|
||||||
if location_match is None:
|
if location_match is None:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('backup_repositories_invalid_location', location=location))
|
raise YunohostError('backup_repositories_invalid_location',
|
||||||
|
location=location)
|
||||||
|
|
||||||
self.protocol = location_match.group('protocol')
|
self.protocol = location_match.group('protocol')
|
||||||
self.user = location_match.group('user')
|
self.user = location_match.group('user')
|
||||||
self.domain = location_match.group('domain')
|
self.domain = location_match.group('domain')
|
||||||
self.path = location_match.group('path')
|
self.path = location_match.group('path')
|
||||||
|
|
||||||
|
|
||||||
def backup_repository_list(name, full=False):
|
def backup_repository_list(name, full=False):
|
||||||
"""
|
"""
|
||||||
List available repositories where put archives
|
List available repositories where put archives
|
||||||
|
@ -155,6 +164,7 @@ def backup_repository_list(name, full=False):
|
||||||
else:
|
else:
|
||||||
return repositories.keys()
|
return repositories.keys()
|
||||||
|
|
||||||
|
|
||||||
def backup_repository_info(name, human_readable=True, space_used=False):
|
def backup_repository_info(name, human_readable=True, space_used=False):
|
||||||
"""
|
"""
|
||||||
Show info about a repository
|
Show info about a repository
|
||||||
|
@ -164,7 +174,6 @@ def backup_repository_info(name, human_readable=True, space_used=False):
|
||||||
"""
|
"""
|
||||||
repository = BackupRepository.get(name)
|
repository = BackupRepository.get(name)
|
||||||
|
|
||||||
|
|
||||||
if space_used:
|
if space_used:
|
||||||
repository.compute_space_used()
|
repository.compute_space_used()
|
||||||
|
|
||||||
|
@ -172,11 +181,12 @@ def backup_repository_info(name, human_readable=True, space_used=False):
|
||||||
if human_readable:
|
if human_readable:
|
||||||
if 'quota' in repository:
|
if 'quota' in repository:
|
||||||
repository['quota'] = binary_to_human(repository['quota'])
|
repository['quota'] = binary_to_human(repository['quota'])
|
||||||
if 'used' in repository and isinstance(repository['used', int):
|
if 'used' in repository and isinstance(repository['used'], int):
|
||||||
repository['used'] = binary_to_human(repository['used'])
|
repository['used'] = binary_to_human(repository['used'])
|
||||||
|
|
||||||
return repository
|
return repository
|
||||||
|
|
||||||
|
|
||||||
@is_unit_operation()
|
@is_unit_operation()
|
||||||
def backup_repository_add(operation_logger, location, name, description=None,
|
def backup_repository_add(operation_logger, location, name, description=None,
|
||||||
methods=None, quota=None, encryption="passphrase"):
|
methods=None, quota=None, encryption="passphrase"):
|
||||||
|
@ -184,17 +194,24 @@ def backup_repository_add(operation_logger, location, name, description=None,
|
||||||
Add a backup repository
|
Add a backup repository
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
|
location -- Location of the repository (could be a remote location)
|
||||||
name -- Name of the backup repository
|
name -- Name of the backup repository
|
||||||
|
description -- An optionnal description
|
||||||
|
quota -- Maximum size quota of the repository
|
||||||
|
encryption -- If available, the kind of encryption to use
|
||||||
"""
|
"""
|
||||||
repository = BackupRepository(location, name, description, methods, quota, encryption)
|
repository = BackupRepository(
|
||||||
|
location, name, description, methods, quota, encryption)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
repository.save()
|
repository.save()
|
||||||
except MoulinetteError as e:
|
except MoulinetteError:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('backup_repository_add_failed',
|
raise YunohostError('backup_repository_add_failed',
|
||||||
|
repository=name, location=location)
|
||||||
|
|
||||||
|
logger.success(m18n.n('backup_repository_added',
|
||||||
repository=name, location=location))
|
repository=name, location=location))
|
||||||
|
|
||||||
logger.success(m18n.n('backup_repository_added', repository=name, location=location))
|
|
||||||
|
|
||||||
@is_unit_operation()
|
@is_unit_operation()
|
||||||
def backup_repository_update(operation_logger, name, description=None,
|
def backup_repository_update(operation_logger, name, description=None,
|
||||||
|
@ -215,12 +232,12 @@ def backup_repository_update(operation_logger, name, description=None,
|
||||||
|
|
||||||
try:
|
try:
|
||||||
repository.save()
|
repository.save()
|
||||||
except MoulinetteError as e:
|
except MoulinetteError:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('backup_repository_update_failed',
|
raise YunohostError('backup_repository_update_failed', repository=name)
|
||||||
repository=name))
|
|
||||||
logger.success(m18n.n('backup_repository_updated', repository=name,
|
logger.success(m18n.n('backup_repository_updated', repository=name,
|
||||||
location=repository['location']))
|
location=repository['location']))
|
||||||
|
|
||||||
|
|
||||||
@is_unit_operation()
|
@is_unit_operation()
|
||||||
def backup_repository_remove(operation_logger, name, purge=False):
|
def backup_repository_remove(operation_logger, name, purge=False):
|
||||||
"""
|
"""
|
||||||
|
@ -234,4 +251,3 @@ def backup_repository_remove(operation_logger, name, purge=False):
|
||||||
repository.delete(purge)
|
repository.delete(purge)
|
||||||
logger.success(m18n.n('backup_repository_removed', repository=name,
|
logger.success(m18n.n('backup_repository_removed', repository=name,
|
||||||
path=repository['path']))
|
path=repository['path']))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue