Skip to content

Commit

Permalink
[RHELC-750, RHELC-1005] Rewrite the log messages in download_pkg() to…
Browse files Browse the repository at this point in the history
… not use "unsupported" (oamg#683)

* fix for merge commit

* When I rebased from the github website I made a merge commit that
  messed up my changes this commit goes back and fixes that

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* removed duplicate function

* test_convert2rhel_latest_action_outdated_version() was duplicated twice
  in the code the copy was removed

* New tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Draft for test_bad_NEVEA_to_parse_pkg_string

* added the side_effect paramerter to the monkeypatch

* Fixed typos

* formatted_conver2rhel_version was spelled wrong  and this commit fixes that
* removed the duplication of some of the code that was moved to a function

Co-authored-by: E Gustavsson <[email protected]>

* Fixed a typo with the formmated_convert2rhel_versions var

* Changed some wording for some comments

* Renamed some var names in the _extract_convert2rhel_versions()
* edited the if statment structure in the
  test_convert2rhel_latest_rpm_V_check and the
  test_convert2rhel_latest_rpm_qf_check tests

* Added in a import for mock

* Added in the correct import for mock

* Changed the output showing with run_subprocess -V

* changed the format for the version

* The format for the version was failing an intergration tests
  to fix this the version was changed to be more like the test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Toshio Kuratomi <[email protected]>
Co-authored-by: E Gustavsson <[email protected]>
  • Loading branch information
4 people authored Jan 12, 2024
1 parent 06d3ea0 commit c24bdb6
Show file tree
Hide file tree
Showing 2 changed files with 757 additions and 140 deletions.
128 changes: 96 additions & 32 deletions convert2rhel/actions/system_checks/convert2rhel_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

import rpm

from convert2rhel import __version__ as installed_convert2rhel_version
from convert2rhel import __file__ as convert2rhel_file
from convert2rhel import __version__ as running_convert2rhel_version
from convert2rhel import actions, utils
from convert2rhel.pkghandler import parse_pkg_string
from convert2rhel.systeminfo import system_info
Expand Down Expand Up @@ -108,26 +109,7 @@ def run(self):
)
return

# convert the raw output of convert2rhel version strings into a list
raw_output_convert2rhel_versions = raw_output_convert2rhel_versions.splitlines()

temp_raw_output = []

# We are expecting an repoquery output to be similar to this:
# C2R convert2rhel-0:0.17-1.el7.noarch
# We need the `C2R` identifier to be present on the line so we can know for
# sure that the line we are working with is the a line that contains
# relevant repoquery information to our check, otherwise, we just log the
# information as debug and do nothing with it.
for raw_version in raw_output_convert2rhel_versions:
if "C2R" in raw_version:
temp_raw_output.append(raw_version.lstrip("C2R "))
else:
# Mainly for debugging purposes to see what is happening if we got
# anything else that does not have the C2R identifier at the start
# of the line.
logger.debug("Got a line without the C2R identifier: %s" % raw_version)
raw_output_convert2rhel_versions = temp_raw_output
raw_output_convert2rhel_versions = _extract_convert2rhel_versions(raw_output_convert2rhel_versions)

latest_available_version = ("0", "0.00", "0")
convert2rhel_versions = []
Expand Down Expand Up @@ -161,14 +143,67 @@ def run(self):
latest_available_version = (package_version[1], package_version[2], package_version[3])

logger.debug("Found %s to be latest available version" % (latest_available_version[1]))
# After the for loop, the latest_available_version variable will gain the epoch, version, and release
# (e.g. ("0" "0.26" "1.el7")) information from the convert2rhel yum repo
# when the versions are the same the latest_available_version's release field will cause it to evaluate as a later version.
# Therefore we need to hardcode "0" for both the epoch and release below for installed_convert2rhel_version
# and latest_available_version respectively, to compare **just** the version field.
ver_compare = rpm.labelCompare(
("0", installed_convert2rhel_version, "0"), ("0", latest_available_version[1], "0")
precise_available_version = ("0", latest_available_version[1], "0")
precise_convert2rhel_version = ("0", running_convert2rhel_version, "0")
# Get source files that we're running with import convert2rhel ; convert2rhel.__file__
running_convert2rhel_init_file = convert2rhel_file

# Run `rpm -qf <source file>` to get the installed convert2rhel package NEVRA
running_convert2rhel_NEVRA, return_code = utils.run_subprocess(
(
"rpm",
"-qf",
running_convert2rhel_init_file,
"--qf",
"C2R %{NAME}-%|EPOCH?{%{EPOCH}}:{0}|:%{VERSION}-%{RELEASE}.%{ARCH}\n",
),
print_output=False,
)

running_convert2rhel_NEVRA = _extract_convert2rhel_versions(running_convert2rhel_NEVRA)

# If we couldn't get a NEVRA above, then print a warning that we could not determine the rpm release and use convert2rhel.__version__ to compare with the latest packaged version
if return_code != 0 or len(running_convert2rhel_NEVRA) != 1:
logger.warning(
"Couldn't determine the rpm release; We will check that the version of convert2rhel (%s) is the latest but ignore the rpm release."
% running_convert2rhel_version
)

else:
running_convert2rhel_NEVRA = running_convert2rhel_NEVRA[0]
# Run `rpm -V <convert2rhel pkg NEVRA>` to make sure the user hasn't installed a different convert2rhel version on top of a previously installed rpm package through other means than rpm (e.g. pip install from GitHub)
rpm_convert2rhel_verify, return_code = utils.run_subprocess(
["rpm", "-V", running_convert2rhel_NEVRA],
print_output=False,
)

# If the files aren't what shipped in the rpm, we print a warning that we could not determine the rpm release and use convert2rhel.__version__ to compare with the latest packaged version
if return_code != 0:
logger.warning(
"Some files in the convert2rhel package have changed so the installed convert2rhel is not what was packaged."
" We will check that the version of convert2rhel (%s) is the latest but ignore the rpm release."
% running_convert2rhel_version
)

# Otherwise use the NEVRA from above to compare with the latest packaged version
else:
parsed_convert2rhel_version = parse_pkg_string(running_convert2rhel_NEVRA)

precise_convert2rhel_version = (
parsed_convert2rhel_version[1],
parsed_convert2rhel_version[2],
parsed_convert2rhel_version[3],
)

precise_available_version = latest_available_version

ver_compare = rpm.labelCompare(precise_convert2rhel_version, precise_available_version)

formatted_convert2rhel_version = _format_EVR(*precise_convert2rhel_version)
formatted_available_version = _format_EVR(*precise_available_version)
print("Formatted convert2rhel version: %s" % formatted_convert2rhel_version)
print("Formatted avaliable version: %s" % formatted_available_version)

if ver_compare < 0:
# Current and deprecated env var names
allow_older_envvar_names = ("CONVERT2RHEL_ALLOW_OLDER_VERSION", "CONVERT2RHEL_UNSUPPORTED_VERSION")
Expand All @@ -191,7 +226,7 @@ def run(self):
diagnosis = (
"You are currently running %s and the latest version of convert2rhel is %s.\n"
"'CONVERT2RHEL_ALLOW_OLDER_VERSION' environment variable detected, continuing conversion"
% (installed_convert2rhel_version, latest_available_version[1])
% (formatted_convert2rhel_version, formatted_available_version)
)
logger.warning(diagnosis)
self.add_message(
Expand All @@ -206,7 +241,7 @@ def run(self):
logger.warning(
"You are currently running %s and the latest version of convert2rhel is %s.\n"
"We encourage you to update to the latest version."
% (installed_convert2rhel_version, latest_available_version[1])
% (formatted_convert2rhel_version, formatted_available_version)
)
self.add_message(
level="WARNING",
Expand All @@ -216,7 +251,7 @@ def run(self):
diagnosis=(
"You are currently running %s and the latest version of convert2rhel is %s.\n"
"We encourage you to update to the latest version."
% (installed_convert2rhel_version, latest_available_version[1])
% (formatted_convert2rhel_version, formatted_available_version)
),
)

Expand All @@ -229,10 +264,39 @@ def run(self):
diagnosis=(
"You are currently running %s and the latest version of convert2rhel is %s.\n"
"Only the latest version is supported for conversion."
% (installed_convert2rhel_version, latest_available_version[1])
% (formatted_convert2rhel_version, formatted_available_version)
),
remediation="If you want to ignore this check, then set the environment variable 'CONVERT2RHEL_ALLOW_OLDER_VERSION=1' to continue.",
)
return

logger.info("Latest available convert2rhel version is installed.")


def _format_EVR(epoch, version, release):
return "%s" % (version)


def _extract_convert2rhel_versions(raw_versions):
parsed_versions = []

# convert the raw output of convert2rhel version strings into a list
precise_raw_version = raw_versions.splitlines()

# We are expecting an repoquery output to be similar to this:
# C2R convert2rhel-0:0.17-1.el7.noarch
# We need the `C2R` identifier to be present on the line so we can know for
# sure that the line we are working with is the a line that contains
# relevant repoquery information to our check, otherwise, we just log the
# information as debug and do nothing with it.
for raw_version in precise_raw_version:
if "C2R" in raw_version:
parsed_versions.append(raw_version.lstrip("C2R "))
else:
# Mainly for debugging purposes to see what is happening if we got
# anything else that does not have the C2R identifier at the start
# of the line.
logger.debug("Got a line without the C2R identifier: %s" % raw_version)
precise_raw_version = parsed_versions

return precise_raw_version
Loading

0 comments on commit c24bdb6

Please sign in to comment.