@@ -80,6 +80,30 @@ def get_major_version_from_a_valid_version(version):
8080 return version .split ('.' )[0 ]
8181
8282
83+ def _get_latest_version_with_matching_major_version (versions , major_version ):
84+ """
85+ Find the latest version from given list of available versions matching the provided major version.
86+
87+ Versioning schema: MAJOR.MINOR
88+
89+ :param list[str] versions: List of versions to choose from.
90+ :param str major_version: The major version for which to find the latest version.
91+ :rtype: str
92+ :returns: Latest version with given major version form the given versions list, or empty string when not found.
93+ """
94+ latest_minor_version = - 1
95+ for version in versions :
96+ version = version .split ('.' )
97+ if len (version ) <= 1 :
98+ continue # skip versions without a minor version
99+ if version [0 ] == major_version :
100+ minor_version = int (version [1 ])
101+ latest_minor_version = max (minor_version , latest_minor_version )
102+ if latest_minor_version == - 1 :
103+ return ''
104+ return f'{ major_version } .{ latest_minor_version } '
105+
106+
83107def detect_sap_hana ():
84108 """
85109 Detect SAP HANA based on existence of /hana/shared/*/exe/linuxx86_64/hdb/sapcontrol
@@ -156,15 +180,6 @@ def get_upgrade_paths_config():
156180 return upgrade_paths_map
157181
158182
159- def get_target_versions_from_config (src_version_id , distro , flavor ):
160- """
161- Retrieve all possible target versions from upgrade_paths_map.
162- If no match is found returns empty list.
163- """
164- upgrade_paths_map = get_upgrade_paths_config ()
165- return upgrade_paths_map .get (distro , {}).get (flavor , {}).get (src_version_id , [])
166-
167-
168183def get_virtual_version_from_config (src_version_id , distro ):
169184 """
170185 Retrieve the virtual version for the given version from upgrade_paths_map.
@@ -179,8 +194,11 @@ def get_supported_target_versions(target_distro, flavour=get_upgrade_flavour()):
179194 """
180195 Return a list of supported target versions for the given `flavour` of upgrade.
181196 The default value for `flavour` is `default`.
182- """
183197
198+ :param str flavour: One of the upgrade flavours.
199+ :rtype: list[str]
200+ :returns: List of supported target versions.
201+ """
184202 os_release_contents = _retrieve_os_release_contents ()
185203 current_version_id = os_release_contents .get ('VERSION_ID' , '' )
186204 source_distro = os_release_contents .get ('ID' , '' )
@@ -193,15 +211,18 @@ def get_supported_target_versions(target_distro, flavour=get_upgrade_flavour()):
193211 # when upconverting from centos, we need to lookup by virtual version
194212 current_version_id = get_virtual_version_from_config (current_version_id , source_distro )
195213
196- target_versions = get_target_versions_from_config (current_version_id , target_distro , flavour )
214+ upgrade_paths_map = get_upgrade_paths_config ()
215+ relevant_paths = upgrade_paths_map .get (source_distro , {}).get (flavour , {})
216+ target_versions = relevant_paths .get (current_version_id , [])
197217 if not target_versions :
198- # If we cannot find a particular major.minor version in the map,
199- # we fallback to pick a target version just based on a major version.
200- # This can happen for example when testing not yet released versions.
201- # But also removes the need to handle virtual versions on X->centos upgrades.
218+ # If we cannot find a particular major.minor version in the map, we treat
219+ # the system as if it was the latest minor version of its major version
220+ # defined in the upgrade paths map. This can happen for example when
221+ # testing not yet released versions.
222+ available_source_versions = relevant_paths .keys ()
202223 major_version = get_major_version_from_a_valid_version (current_version_id )
203- target_versions = get_target_versions_from_config ( major_version , target_distro , flavour )
204-
224+ latest_version = _get_latest_version_with_matching_major_version ( available_source_versions , major_version )
225+ target_versions = relevant_paths . get ( latest_version , [])
205226 return target_versions
206227
207228
0 commit comments