Be able to define directly in migrations hooks that are called when restoring system/apps prior to the introduction of the migration

This commit is contained in:
Alexandre Aubin 2021-04-02 04:28:52 +02:00
parent df49cc83d5
commit c552b4f006
3 changed files with 70 additions and 21 deletions

View file

@ -61,7 +61,7 @@ from yunohost.hook import (
hook_exec, hook_exec,
CUSTOM_HOOK_FOLDER, CUSTOM_HOOK_FOLDER,
) )
from yunohost.tools import tools_postinstall from yunohost.tools import tools_postinstall, _tools_migrations_run_after_system_restore, _tools_migrations_run_before_app_restore
from yunohost.regenconf import regen_conf from yunohost.regenconf import regen_conf
from yunohost.log import OperationLogger from yunohost.log import OperationLogger
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
@ -1282,16 +1282,15 @@ class RestoreManager:
regen_conf() regen_conf()
# TODO : here, we should have a way to go through all migrations _tools_migrations_run_after_system_restore(backup_version=self.info["from_yunohost_version"])
# and apply stuff if needed
# Remove all permission for all app which is still in the LDAP # Remove all permission for all app still in the LDAP
for permission_name in user_permission_list(ignore_system_perms=True)[ for permission_name in user_permission_list(ignore_system_perms=True)[
"permissions" "permissions"
].keys(): ].keys():
permission_delete(permission_name, force=True, sync_perm=False) permission_delete(permission_name, force=True, sync_perm=False)
# Restore permission for the app which is installed # Restore permission for apps installed
for permission_name, permission_infos in old_apps_permission.items(): for permission_name, permission_infos in old_apps_permission.items():
app_name, perm_name = permission_name.split(".") app_name, perm_name = permission_name.split(".")
if _is_installed(app_name): if _is_installed(app_name):
@ -1456,22 +1455,7 @@ class RestoreManager:
os.remove("%s/permissions.yml" % app_settings_new_path) os.remove("%s/permissions.yml" % app_settings_new_path)
# Migrate old settings _tools_migrations_run_before_app_restore(backup_version=self.info["from_yunohost_version"], app_id=app_instance_name)
legacy_permission_settings = [
"skipped_uris",
"unprotected_uris",
"protected_uris",
"skipped_regex",
"unprotected_regex",
"protected_regex",
]
if any(
app_setting(app_instance_name, setting) is not None
for setting in legacy_permission_settings
):
from yunohost.utils.legacy import migrate_legacy_permission_settings
migrate_legacy_permission_settings(app=app_instance_name)
# Prepare env. var. to pass to script # Prepare env. var. to pass to script
env_dict = _make_environment_for_app_script(app_instance_name) env_dict = _make_environment_for_app_script(app_instance_name)

View file

@ -78,6 +78,32 @@ class MyMigration(Migration):
ldap.update("cn=%s,ou=permission" % permission, update) ldap.update("cn=%s,ou=permission" % permission, update)
introduced_in_version = "4.1"
def run_after_system_restore(self):
# Update LDAP database
self.add_new_ldap_attributes()
def run_before_system_restore(self, app_id):
from yunohost.app import app_setting
from yunohost.utils.legacy import migrate_legacy_permission_settings
# Migrate old settings
legacy_permission_settings = [
"skipped_uris",
"unprotected_uris",
"protected_uris",
"skipped_regex",
"unprotected_regex",
"protected_regex",
]
if any(
app_setting(app_id, setting) is not None
for setting in legacy_permission_settings
):
migrate_legacy_permission_settings(app=app_id)
def run(self): def run(self):
# FIXME : what do we really want to do here ... # FIXME : what do we really want to do here ...

View file

@ -29,6 +29,7 @@ import yaml
import subprocess import subprocess
import pwd import pwd
from importlib import import_module from importlib import import_module
from packaging import version
from moulinette import msignals, m18n from moulinette import msignals, m18n
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
@ -1101,6 +1102,44 @@ def _skip_all_migrations():
write_to_yaml(MIGRATIONS_STATE_PATH, new_states) write_to_yaml(MIGRATIONS_STATE_PATH, new_states)
def _tools_migrations_run_after_system_restore(backup_version):
all_migrations = _get_migrations_list()
for migration in all_migrations:
if hasattr(migration, "introduced_in_version") \
and version.parse(migration.introduced_in_version) > version.parse(backup_version) \
and hasattr(migration, "run_after_system_restore"):
try:
logger.info(m18n.n("migrations_running_forward", id=migration.id))
migration.run_after_system_restore()
except Exception as e:
msg = m18n.n(
"migrations_migration_has_failed", exception=e, id=migration.id
)
logger.error(msg, exc_info=1)
raise
def _tools_migrations_run_after_system_restore(backup_version, app_id):
all_migrations = _get_migrations_list()
for migration in all_migrations:
if hasattr(migration, "introduced_in_version") \
and version.parse(migration.introduced_in_version) > version.parse(backup_version) \
and hasattr(migration, "run_before_app_restore"):
try:
logger.info(m18n.n("migrations_running_forward", id=migration.id))
migration.run_before_app_restore(app_id)
except Exception as e:
msg = m18n.n(
"migrations_migration_has_failed", exception=e, id=migration.id
)
logger.error(msg, exc_info=1)
raise
class Migration(object): class Migration(object):
# Those are to be implemented by daughter classes # Those are to be implemented by daughter classes