Skip to content

Commit

Permalink
Download c2r repofile instead of generating it
Browse files Browse the repository at this point in the history
It's safer to use the official convert2rhel repofile that is to be used
by customers instead of generating it as that way any changes done it
don't need further convert2rhel code updates.
  • Loading branch information
bocekm committed May 21, 2024
1 parent e5f4820 commit fe9f4ff
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 108 deletions.
74 changes: 43 additions & 31 deletions convert2rhel/actions/system_checks/convert2rhel_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,22 @@

import logging
import os.path
import shutil
import tempfile

import rpm

from convert2rhel import __file__ as convert2rhel_file
from convert2rhel import __version__ as running_convert2rhel_version
from convert2rhel import actions, utils
from convert2rhel import actions, exceptions, repo, utils
from convert2rhel.pkghandler import parse_pkg_string
from convert2rhel.systeminfo import system_info
from convert2rhel.utils import files


logger = logging.getLogger(__name__)

CDN_URL_7 = "https://cdn-public.redhat.com/content/public/addon/dist/convert2rhel/server/7/7Server/x86_64/os/"
CDN_URL_8 = "https://cdn-public.redhat.com/content/public/addon/dist/convert2rhel8/8/x86_64/os/"
RPM_GPG_KEY_PATH = os.path.join(utils.DATA_DIR, "gpg-keys", "RPM-GPG-KEY-redhat-release")

CONVERT2RHEL_REPO_CONTENT = (
"""\
[convert2rhel]
name=Convert2RHEL Repository
baseurl={}
gpgcheck=1
enabled=1
gpgkey=file://%s"""
% RPM_GPG_KEY_PATH
)
C2R_REPOFILE_URLS = {
7: "https://cdn-public.redhat.com/content/public/addon/dist/convert2rhel/server/7/7Server/x86_64/files/repofile.repo",
8: "https://cdn-public.redhat.com/content/public/addon/dist/convert2rhel8/8/x86_64/files/repofile.repo",
}


class Convert2rhelLatest(actions.Action):
Expand All @@ -57,30 +44,22 @@ def run(self):

super(Convert2rhelLatest, self).run()

repo_dir = tempfile.mkdtemp(prefix="convert2rhel_repo.", dir=utils.TMP_DIR)
repo_path = os.path.join(repo_dir, "convert2rhel.repo")
repo_content = CONVERT2RHEL_REPO_CONTENT.format(CDN_URL_7 if system_info.version.major == 7 else CDN_URL_8)
utils.store_content_to_file(filename=repo_path, content=repo_content)
repofile_path = self._get_convert2rhel_repofile_path()
if not repofile_path:
return

cmd = [
"repoquery",
"--disablerepo=*",
"--enablerepo=convert2rhel",
"--releasever=%s" % system_info.version.major,
"--setopt=reposdir=%s" % repo_dir,
"--setopt=reposdir=%s" % os.path.dirname(repofile_path),
"--qf",
"C2R %{NAME}-%{EPOCH}:%{VERSION}-%{RELEASE}.%{ARCH}",
"convert2rhel",
]

# Note: This is safe because we're creating in utils.TMP_DIR which is hardcoded to
# /var/lib/convert2rhel which does not have any world-writable directory components.
files.mkdir_p(repo_dir)

try:
raw_output_convert2rhel_versions, return_code = utils.run_subprocess(cmd, print_output=False)
finally:
shutil.rmtree(repo_dir)
raw_output_convert2rhel_versions, return_code = utils.run_subprocess(cmd, print_output=False)

if return_code != 0:
diagnosis = (
Expand Down Expand Up @@ -241,6 +220,39 @@ def run(self):

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

def _get_convert2rhel_repofile_path(self):
"""Download the official downstream convert2rhel repofile to a temporary directory.
:return: Path of the downloaded downstream convert2rhel repofile
:rtype: str
"""
if system_info.version.major not in C2R_REPOFILE_URLS:
self.add_message(
level="WARNING",
id="CONVERT2RHEL_LATEST_CHECK_UNEXPECTED_SYS_VERSION",
title="Did not perform convert2rhel latest version check",
description="Checking whether the installed convert2rhel package is of the latest available version was"
" skipped due to an unexpected system version",
diagnosis="Expected system versions: %s. Detected major version: %s"
% (", ".join(str(x) for x in C2R_REPOFILE_URLS), system_info.version.major),
)
return None

repofile_url = C2R_REPOFILE_URLS[system_info.version.major]
try:
client_tools_repofile_path = repo.write_temporary_repofile(repo.download_repofile(repofile_url))
return client_tools_repofile_path
except exceptions.CriticalError as err:
self.add_message(
level="WARNING",
id="CONVERT2RHEL_LATEST_CHECK_REPO_DOWNLOAD_FAILED",
title="Did not perform convert2rhel latest version check",
description="Checking whether the installed convert2rhel package is of the latest available version was"
" skipped due to not being able to download the convert2rhel repository file",
diagnosis=err.description,
)
return None


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

0 comments on commit fe9f4ff

Please sign in to comment.