Implement migration while restore backup

This commit is contained in:
Josué Tille 2018-12-12 17:40:29 +01:00
parent 0d3d33fdce
commit f22e7144b4
No known key found for this signature in database
GPG key ID: D5E068C6DFA8681D
3 changed files with 42 additions and 14 deletions

View file

@ -1085,7 +1085,7 @@ backup:
api: POST /backup/restore/<name>
configuration:
authenticate: all
authenticator: ldap-anonymous
authenticator: as-root
arguments:
name:
help: Name of the local backup archive

View file

@ -800,14 +800,14 @@ class RestoreManager():
Public methods:
set_targets(self, system_parts=[], apps=[])
restore(self)
restore(self, auth)
Usage:
restore_manager = RestoreManager(name)
restore_manager.set_targets(None, ['wordpress__3'])
restore_manager.restore()
restore_manager.restore(auth)
if restore_manager.success:
logger.success(m18n.n('restore_complete'))
@ -893,6 +893,23 @@ class RestoreManager():
logger.debug("executing the post-install...")
tools_postinstall(domain, 'yunohost', True)
def _migrate_system_if_needed(self, auth):
"""
Do some migration if needed
"""
# Check if we need to do the migration 0009 : setup group and permission
result = auth.search('ou=groups,dc=yunohost,dc=org',
'(&(objectclass=groupOfNamesYnh)(cn=all_users))',
['cn'])
if not result:
from importlib import import_module
migration_0009 = import_module('yunohost.data_migrations.0009_setup_group_permission')
# Update LDAP schema restart slapd
logger.info(m18n.n("migration_0009_update_LDAP_schema"))
service_regen_conf(names=['slapd'], force=True)
migration_0009.migrate_LDAP_db(auth)
def clean(self):
"""
End a restore operations by cleaning the working directory and
@ -1100,7 +1117,7 @@ class RestoreManager():
# "Actual restore" (reverse step of the backup collect part) #
#
def restore(self):
def restore(self, auth):
"""
Restore the archive
@ -1114,8 +1131,9 @@ class RestoreManager():
# Apply dirty patch to redirect php5 file on php7
self._patch_backup_csv_file()
self._restore_system()
self._restore_apps()
self._restore_system(auth)
self._migrate_system_if_needed(auth)
self._restore_apps(auth)
finally:
self.clean()
@ -1159,7 +1177,7 @@ class RestoreManager():
logger.warning(m18n.n('backup_php5_to_php7_migration_may_fail',
error=str(e)))
def _restore_system(self):
def _restore_system(self, auth):
""" Restore user and system parts """
system_targets = self.targets.list("system", exclude=["Skipped"])
@ -1199,15 +1217,15 @@ class RestoreManager():
service_regen_conf()
def _restore_apps(self):
def _restore_apps(self, auth):
"""Restore all apps targeted"""
apps_targets = self.targets.list("apps", exclude=["Skipped"])
for app in apps_targets:
self._restore_app(app)
self._restore_app(auth, app)
def _restore_app(self, app_instance_name):
def _restore_app(self, auth, app_instance_name):
"""
Restore an app
@ -1286,7 +1304,12 @@ class RestoreManager():
filesystem.chown(app_scripts_new_path, 'admin', None, True)
# Restore permissions
os.system("slapadd -l '%s/permission.ldif'" % app_settings_in_archive)
if os.path.isfile(app_restore_script_in_archive):
os.system("slapadd -l '%s/permission.ldif'" % app_settings_in_archive)
else:
from importlib import import_module
migration_0009 = import_module('yunohost.data_migrations.0009_setup_group_permission')
migration_0009.migrate_app_permission(auth, app=app_instance_name)
# Copy the app scripts to a writable temporary folder
# FIXME : use 'install -Dm555' or something similar to what's done
@ -2137,7 +2160,7 @@ def backup_restore(auth, name, system=[], apps=[], force=False):
#
restore_manager.mount()
restore_manager.restore()
restore_manager.restore(auth)
# Check if something has been restored
if restore_manager.success:

View file

@ -54,10 +54,15 @@ def migrate_LDAP_db(auth):
user_group_update(auth, 'all_users', add_user=username, force=True, sync_perm=False)
def migrate_app_permission(auth):
def migrate_app_permission(auth, app=None):
logger.info(m18n.n("migration_0009_migrate_permission"))
for app_info in app_list(installed=True)['apps']:
if app:
apps = app_list(installed=True, filter=app)['apps']
else:
apps = app_list(installed=True)['apps']
for app_info in apps:
app = app_info['id']
permission = app_setting(app, 'allowed_users')
path = app_setting(app, 'path')