Use yum.add_enable_repo() for new repos

Yum needs to have some other attrs setup on the YumRepository object, so
use the function provided to ensure that everything is correct. Also
switch the related functions to use a dict instead of a YumRepository
object.
This commit is contained in:
Brian C. Lane 2018-06-04 12:54:04 -07:00
parent 957def1264
commit 3cfb895054
3 changed files with 65 additions and 54 deletions

View File

@ -19,7 +19,6 @@ log = logging.getLogger("lorax-composer")
import os import os
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
import yum
from glob import glob from glob import glob
import time import time
@ -321,34 +320,34 @@ def modules_info(yb, module_names):
return modules return modules
def yum_repo_to_file_repo(repo): def yum_repo_to_file_repo(repo):
"""Return a string representation of a YumRepository object suitable for writing to a .repo file """Return a string representation of a repo dict suitable for writing to a .repo file
:param repo: Yum Repository :param repo: Yum Repository represented as a dict
:type repo: yum.yumRepo.YumRepository :type repo: dict
:returns: A string :returns: A string
:rtype: str :rtype: str
The YumRepo.dump() function does not produce a string that can be used as a The YumRepo.dump() function does not produce a string that can be used as a
yum .repo file. So do this manually with only the attributes we care about. yum .repo file. So do this manually with only the attributes we care about.
""" """
repo_str = "[%s]\n" % repo.id repo_str = "[%s]\n" % repo["id"]
if repo.metalink: if repo["metalink"]:
repo_str += "metalink = %s\n" % repo.metalink repo_str += "metalink = %s\n" % repo["metalink"]
elif repo.mirrorlist: elif repo["mirrorlist"]:
repo_str += "mirrorlist = %s\n" % repo.mirrorlist repo_str += "mirrorlist = %s\n" % repo["mirrorlist"]
elif repo.baseurl: elif repo["baseurl"]:
repo_str += "baseurl = %s\n" % repo.baseurl[0] repo_str += "baseurl = %s\n" % repo["baseurl"][0]
else: else:
raise RuntimeError("Repo has no baseurl, metalink, or mirrorlist") raise RuntimeError("Repo has no baseurl, metalink, or mirrorlist")
# proxy is optional # proxy is optional
if repo.proxy: if "proxy" in repo:
repo_str += "proxy = %s\n" % repo.proxy repo_str += "proxy = %s\n" % repo["proxy"]
repo_str += "sslverify = %s\n" % repo.sslverify repo_str += "sslverify = %s\n" % repo["sslverify"]
repo_str += "gpgcheck = %s\n" % repo.gpgcheck repo_str += "gpgcheck = %s\n" % repo["gpgcheck"]
if repo.gpgkey: if "gpgkey" in repo:
repo_str += "gpgkey = %s\n" % ",".join(repo.gpgkey) repo_str += "gpgkey = %s\n" % ",".join(repo["gpgkey"])
return repo_str return repo_str
@ -411,58 +410,64 @@ def repo_to_source(repo, system_source):
return source return source
def source_to_repo(source): def source_to_repo(source):
"""Return a yum YumRepository object created from a source dict """Return an add_enable_repo kwargs dict created from a source dict
:param source: A Weldr source dict :param source: A Weldr source dict
:type source: dict :type source: dict
:returns: A yum YumRepository object :returns: A yum YumRepository object
:rtype: yum.yumRepo.YumRepository :rtype: yum.yumRepo.YumRepository
The dict it suitable for passing to yum's add_enable_repo function
after popping off baseurl and mirrorlist.
Example:: Example::
{ {
"check_gpg": True, "gpgcheck": True,
"check_ssl": True, "sslverify": True,
"gpgkey_urls": [ "gpgkey": ["file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64"],
"file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64" "id": "fedora",
],
"name": "fedora",
"proxy": "http://proxy.brianlane.com:8123", "proxy": "http://proxy.brianlane.com:8123",
"system": True "baseurl": "https://mirrors.fedoraproject.org/metalink?repo=fedora-28&arch=x86_64",
"type": "yum-metalink", "metalink": None,
"url": "https://mirrors.fedoraproject.org/metalink?repo=fedora-28&arch=x86_64" "mirrorlist": None
} }
""" """
repo = yum.yumRepo.YumRepository(source["name"]) repo = {"id": source["name"]}
# This will allow errors to be raised so we can catch them # This will allow errors to be raised so we can catch them
# without this they are logged, but the repo is silently disabled # without this they are logged, but the repo is silently disabled
repo.skip_if_unavailable = False repo["skip_if_unavailable"] = False
if source["type"] == "yum-baseurl": if source["type"] == "yum-baseurl":
repo.baseurl = source["url"] repo["baseurl"] = [source["url"]]
repo["metalink"] = None
repo["mirrorlist"] = None
elif source["type"] == "yum-metalink": elif source["type"] == "yum-metalink":
repo.metalink = source["url"] repo["metalink"] = source["url"]
repo["baseurl"] = []
repo["mirrorlist"] = None
elif source["type"] == "yum-mirrorlist": elif source["type"] == "yum-mirrorlist":
repo.mirrorlist = source["url"] repo["mirrorlist"] = source["url"]
repo["baseurl"] = []
repo["metalink"] = None
if "proxy" in source: if "proxy" in source:
repo.proxy = source["proxy"] repo["proxy"] = source["proxy"]
if source["check_ssl"]: if source["check_ssl"]:
repo.sslverify = True repo["sslverify"] = True
else: else:
repo.sslverify = False repo["sslverify"] = False
if source["check_gpg"]: if source["check_gpg"]:
repo.gpgcheck = True repo["gpgcheck"] = True
else: else:
repo.gpgcheck = False repo["gpgcheck"] = False
if "gpgkey_urls" in source: if "gpgkey_urls" in source:
repo.gpgkey = ",".join(source["gpgkey_urls"]) repo["gpgkey"] = source["gpgkey_urls"]
repo.enable()
return repo return repo

View File

@ -1454,8 +1454,11 @@ def v0_api(api):
if source["name"] in repos: if source["name"] in repos:
yb.repos.delete(source["name"]) yb.repos.delete(source["name"])
repo = source_to_repo(source) repo_dict = source_to_repo(source)
yb.repos.add(repo) repoid = repo_dict.pop("id")
baseurl = repo_dict.pop("baseurl")
mirrorlist = repo_dict.pop("mirrorlist")
yb.add_enable_repo(repoid, baseurl, mirrorlist, **repo_dict)
log.info("Updating repository metadata after adding %s", source["name"]) log.info("Updating repository metadata after adding %s", source["name"])
update_metadata(yb) update_metadata(yb)
@ -1472,7 +1475,7 @@ def v0_api(api):
# Make sure the source name can't contain a path traversal by taking the basename # Make sure the source name can't contain a path traversal by taking the basename
source_path = joinpaths(repo_dir, os.path.basename("%s.repo" % source["name"])) source_path = joinpaths(repo_dir, os.path.basename("%s.repo" % source["name"]))
with open(source_path, "w") as f: with open(source_path, "w") as f:
f.write(yum_repo_to_file_repo(repo)) f.write(yum_repo_to_file_repo(source_to_repo(source)))
except Exception as e: except Exception as e:
log.error("(v0_projects_source_add) adding %s failed: %s", source["name"], str(e)) log.error("(v0_projects_source_add) adding %s failed: %s", source["name"], str(e))
@ -1480,7 +1483,6 @@ def v0_api(api):
with api.config["YUMLOCK"].lock: with api.config["YUMLOCK"].lock:
yb = api.config["YUMLOCK"].yb yb = api.config["YUMLOCK"].yb
repos = list(r.id for r in yb.repos.listEnabled()) repos = list(r.id for r in yb.repos.listEnabled())
log.info("BCL: repos = %s", repos)
if source["name"] in repos: if source["name"] in repos:
yb.repos.delete(source["name"]) yb.repos.delete(source["name"])
# delete doesn't remove it from the cache used by listEnabled so we have to force it # delete doesn't remove it from the cache used by listEnabled so we have to force it

View File

@ -412,6 +412,10 @@ gpgcheck = True
gpgkey = https://fake-repo.gpgkey gpgkey = https://fake-repo.gpgkey
""" """
def yum_to_file(d):
"""Test function to convert a source to a dict and then to a yum repo string"""
return yum_repo_to_file_repo(source_to_repo(d))
class SourceTest(unittest.TestCase): class SourceTest(unittest.TestCase):
@classmethod @classmethod
def setUpClass(self): def setUpClass(self):
@ -479,44 +483,44 @@ class SourceTest(unittest.TestCase):
def test_source_to_repo_baseurl(self): def test_source_to_repo_baseurl(self):
"""Test creating a yum.yumRepo.YumRepository with a baseurl""" """Test creating a yum.yumRepo.YumRepository with a baseurl"""
repo = source_to_repo(fakerepo_baseurl()) repo = source_to_repo(fakerepo_baseurl())
self.assertEqual(repo.baseurl[0], fakerepo_baseurl()["url"]) self.assertEqual(repo["baseurl"][0], fakerepo_baseurl()["url"])
def test_source_to_repo_metalink(self): def test_source_to_repo_metalink(self):
"""Test creating a yum.yumRepo.YumRepository with a metalink""" """Test creating a yum.yumRepo.YumRepository with a metalink"""
repo = source_to_repo(fakerepo_metalink()) repo = source_to_repo(fakerepo_metalink())
self.assertEqual(repo.metalink, fakerepo_metalink()["url"]) self.assertEqual(repo["metalink"], fakerepo_metalink()["url"])
def test_source_to_repo_mirrorlist(self): def test_source_to_repo_mirrorlist(self):
"""Test creating a yum.yumRepo.YumRepository with a mirrorlist""" """Test creating a yum.yumRepo.YumRepository with a mirrorlist"""
repo = source_to_repo(fakerepo_mirrorlist()) repo = source_to_repo(fakerepo_mirrorlist())
self.assertEqual(repo.mirrorlist, fakerepo_mirrorlist()["url"]) self.assertEqual(repo["mirrorlist"], fakerepo_mirrorlist()["url"])
def test_source_to_repo_proxy(self): def test_source_to_repo_proxy(self):
"""Test creating a yum.yumRepo.YumRepository with a proxy""" """Test creating a yum.yumRepo.YumRepository with a proxy"""
repo = source_to_repo(fakerepo_proxy()) repo = source_to_repo(fakerepo_proxy())
self.assertEqual(repo.proxy, fakerepo_proxy()["proxy"]) self.assertEqual(repo["proxy"], fakerepo_proxy()["proxy"])
def test_source_to_repo_gpgkey(self): def test_source_to_repo_gpgkey(self):
"""Test creating a yum.yumRepo.YumRepository with a proxy""" """Test creating a yum.yumRepo.YumRepository with a proxy"""
repo = source_to_repo(fakerepo_gpgkey()) repo = source_to_repo(fakerepo_gpgkey())
self.assertEqual(repo.gpgkey, fakerepo_gpgkey()["gpgkey_urls"]) self.assertEqual(repo["gpgkey"], fakerepo_gpgkey()["gpgkey_urls"])
def test_drtfr_baseurl(self): def test_drtfr_baseurl(self):
"""Test creating a yum .repo file from a baseurl Repo object""" """Test creating a yum .repo file from a baseurl Repo object"""
self.assertEqual(yum_repo_to_file_repo(FakeRepoBaseUrl()), fakerepo_baseurl_str()) self.assertEqual(yum_to_file(fakerepo_baseurl()), fakerepo_baseurl_str())
def test_drtfr_metalink(self): def test_drtfr_metalink(self):
"""Test creating a yum .repo file from a metalink Repo object""" """Test creating a yum .repo file from a metalink Repo object"""
self.assertEqual(yum_repo_to_file_repo(FakeRepoMetalink()), fakerepo_metalink_str()) self.assertEqual(yum_to_file(fakerepo_metalink()), fakerepo_metalink_str())
def test_drtfr_mirrorlist(self): def test_drtfr_mirrorlist(self):
"""Test creating a yum .repo file from a mirrorlist Repo object""" """Test creating a yum .repo file from a mirrorlist Repo object"""
self.assertEqual(yum_repo_to_file_repo(FakeRepoMirrorlist()), fakerepo_mirrorlist_str()) self.assertEqual(yum_to_file(fakerepo_mirrorlist()), fakerepo_mirrorlist_str())
def test_drtfr_proxy(self): def test_drtfr_proxy(self):
"""Test creating a yum .repo file from a baseurl Repo object with proxy""" """Test creating a yum .repo file from a baseurl Repo object with proxy"""
self.assertEqual(yum_repo_to_file_repo(FakeRepoProxy()), fakerepo_proxy_str()) self.assertEqual(yum_to_file(fakerepo_proxy()), fakerepo_proxy_str())
def test_drtfr_gpgkey(self): def test_drtfr_gpgkey(self):
"""Test creating a yum .repo file from a baseurl Repo object with gpgkey""" """Test creating a yum .repo file from a baseurl Repo object with gpgkey"""
self.assertEqual(yum_repo_to_file_repo(FakeRepoGPGKey()), fakerepo_gpgkey_str()) self.assertEqual(yum_to_file(fakerepo_gpgkey()), fakerepo_gpgkey_str())