|
| 1 | + |
| 2 | +# Copyright (c) 2025 SUSE Linux LLC. All rights reserved. |
| 3 | +# |
| 4 | +# This file is part of suse-migration-services. |
| 5 | +# |
| 6 | +# suse-migration-services is free software: you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# suse-migration-services is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with suse-migration-services. If not, see <http://www.gnu.org/licenses/> |
| 18 | +# |
| 19 | +import logging |
| 20 | +import yaml |
| 21 | +import os |
| 22 | +import platform |
| 23 | + |
| 24 | +from suse_migration_services.defaults import Defaults |
| 25 | +from suse_migration_services.command import Command |
| 26 | + |
| 27 | +log = logging.getLogger(Defaults.get_migration_log_name()) |
| 28 | + |
| 29 | + |
| 30 | +class MigrationTarget: |
| 31 | + """Implements the detection of migration target""" |
| 32 | + @staticmethod |
| 33 | + def _parse_custom_config(): |
| 34 | + migration_config = '/etc/sle-migration-service.yml' |
| 35 | + if os.path.isfile(migration_config): |
| 36 | + try: |
| 37 | + with open(migration_config, 'r') as config: |
| 38 | + return yaml.safe_load(config) or {} |
| 39 | + except Exception as issue: |
| 40 | + message = 'Loading {0} failed: {1}: {2}'.format( |
| 41 | + migration_config, type(issue).__name__, issue |
| 42 | + ) |
| 43 | + log.error(message) |
| 44 | + |
| 45 | + return {} |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def get_migration_target(): |
| 49 | + config_data = MigrationTarget._parse_custom_config() |
| 50 | + migration_product = config_data.get('migration_product') |
| 51 | + if migration_product: |
| 52 | + product = migration_product.split('/') |
| 53 | + return { |
| 54 | + 'identifier': product[0], |
| 55 | + 'version': product[1], |
| 56 | + 'arch': product[2] |
| 57 | + } |
| 58 | + sles15_migration = Command.run( |
| 59 | + ['rpm', '-q', 'SLES15-Migration'], raise_on_error=False |
| 60 | + ) |
| 61 | + if sles15_migration.returncode == 0: |
| 62 | + # return default migration target |
| 63 | + return { |
| 64 | + 'identifier': 'SLES', |
| 65 | + 'version': '15.3', |
| 66 | + 'arch': platform.machine() |
| 67 | + } |
| 68 | + sles16_migration = Command.run( |
| 69 | + ['rpm', '-q', 'SLES16-Migration'], raise_on_error=False |
| 70 | + ) |
| 71 | + if sles16_migration.returncode == 0: |
| 72 | + # return default migration target |
| 73 | + return { |
| 74 | + 'identifier': 'SLES', |
| 75 | + 'version': '16.0', |
| 76 | + 'arch': platform.machine() |
| 77 | + } |
| 78 | + return {} |
0 commit comments