def main():
    """
    DistMigration post mount actions

    Preserve custom data file(s) e.g udev rules from the system
    to be migrated to the live migration system and activate
    those file changes to become effective
    """
    Logger.setup()
    log = logging.getLogger(Defaults.get_migration_log_name())
    root_path = Defaults.get_system_root_path()

    migration_config = MigrationConfig()
    preserve_info = migration_config.get_preserve_info()
    if preserve_info:
        for _, preserve_files in preserve_info.items():
            for preserve_file in preserve_files:
                target_dir = os.path.dirname(preserve_file)
                source_file = os.path.normpath(
                    os.sep.join([root_path, preserve_file]))
                log.info('Copy file: {0} to: {1}'.format(
                    source_file, target_dir))
                if not os.path.exists(target_dir):
                    Command.run(['mkdir', '-p', target_dir])
                shutil.copy(source_file, target_dir)
        if 'rules' in preserve_info.keys():
            Command.run(['udevadm', 'control', '--reload'])
            Command.run(
                ['udevadm', 'trigger', '--type=subsystems', '--action=add'])
            Command.run(
                ['udevadm', 'trigger', '--type=devices', '--action=add'])
示例#2
0
def main():
    """
    DistMigration run zypper based migration

    Call zypper migration plugin and migrate the system.
    The output of the call is logged on the system to migrate
    """
    Logger.setup()
    log = logging.getLogger(Defaults.get_migration_log_name())
    root_path = Defaults.get_system_root_path()

    try:
        log.info('Running migrate service')
        migration_config = MigrationConfig()
        if migration_config.get_preserve_info():
            # set potential missing settings in env
            log.info('Update env variables')
            update_env(migration_config.get_preserve_info())
            log_env(log)
        verbose_migration = '--verbose' if migration_config.is_verbosity_requested() else '--no-verbose'
        if migration_config.is_zypper_migration_plugin_requested():
            bash_command = ' '.join(
                [
                    'zypper', 'migration',
                    verbose_migration,
                    '--non-interactive',
                    '--gpg-auto-import-keys',
                    '--no-selfupdate',
                    '--auto-agree-with-licenses',
                    '--allow-vendor-change',
                    '--strict-errors-dist-migration',
                    '--replacefiles',
                    '--product', migration_config.get_migration_product(),
                    '--root', root_path,
                    '&>>', Defaults.get_migration_log_file()
                ]
            )
            Command.run(
                ['bash', '-c', bash_command]
            )
        else:
            bash_command = ' '.join(
                [
                    'zypper',
                    '--no-cd',
                    '--non-interactive',
                    '--gpg-auto-import-keys',
                    '--root', root_path,
                    'dup',
                    '--auto-agree-with-licenses',
                    '--allow-vendor-change',
                    '--download', 'in-advance',
                    '--replacefiles',
                    '--allow-downgrade',
                    '&>>', Defaults.get_migration_log_file()
                ]
            )
            zypper_call = Command.run(
                ['bash', '-c', bash_command], raise_on_error=False
            )
            if zypper_has_failed(zypper_call.returncode):
                raise DistMigrationCommandException(
                    '{0} failed with: {1}: {2}'.format(
                        bash_command, zypper_call.output, zypper_call.error
                    )
                )
    except Exception as issue:
        etc_issue_path = os.sep.join(
            [root_path, 'etc/issue']
        )
        log_path_migrated_system = os.sep + os.path.relpath(
            Defaults.get_migration_log_file(), root_path
        )
        with open(etc_issue_path, 'w') as issue_file:
            issue_file.write(
                'Migration has failed, for further details see {0}'.format(
                    log_path_migrated_system
                )
            )
        log.error('migrate service failed with {0}'.format(issue))
        raise DistMigrationZypperException(
            'Migration failed with {0}'.format(issue)
        )