diff --git a/src/pylorax/api/projects.py b/src/pylorax/api/projects.py index 44e6e902..6830e350 100644 --- a/src/pylorax/api/projects.py +++ b/src/pylorax/api/projects.py @@ -19,7 +19,6 @@ log = logging.getLogger("lorax-composer") import os from ConfigParser import ConfigParser -import yum from glob import glob import time @@ -321,34 +320,34 @@ def modules_info(yb, module_names): return modules 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 - :type repo: yum.yumRepo.YumRepository + :param repo: Yum Repository represented as a dict + :type repo: dict :returns: A string :rtype: str 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. """ - repo_str = "[%s]\n" % repo.id - if repo.metalink: - repo_str += "metalink = %s\n" % repo.metalink - elif repo.mirrorlist: - repo_str += "mirrorlist = %s\n" % repo.mirrorlist - elif repo.baseurl: - repo_str += "baseurl = %s\n" % repo.baseurl[0] + repo_str = "[%s]\n" % repo["id"] + if repo["metalink"]: + repo_str += "metalink = %s\n" % repo["metalink"] + elif repo["mirrorlist"]: + repo_str += "mirrorlist = %s\n" % repo["mirrorlist"] + elif repo["baseurl"]: + repo_str += "baseurl = %s\n" % repo["baseurl"][0] else: raise RuntimeError("Repo has no baseurl, metalink, or mirrorlist") # proxy is optional - if repo.proxy: - repo_str += "proxy = %s\n" % repo.proxy + if "proxy" in repo: + repo_str += "proxy = %s\n" % repo["proxy"] - repo_str += "sslverify = %s\n" % repo.sslverify - repo_str += "gpgcheck = %s\n" % repo.gpgcheck - if repo.gpgkey: - repo_str += "gpgkey = %s\n" % ",".join(repo.gpgkey) + repo_str += "sslverify = %s\n" % repo["sslverify"] + repo_str += "gpgcheck = %s\n" % repo["gpgcheck"] + if "gpgkey" in repo: + repo_str += "gpgkey = %s\n" % ",".join(repo["gpgkey"]) return repo_str @@ -411,58 +410,64 @@ def repo_to_source(repo, system_source): return 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 :type source: dict :returns: A yum YumRepository object :rtype: yum.yumRepo.YumRepository + The dict it suitable for passing to yum's add_enable_repo function + after popping off baseurl and mirrorlist. + Example:: { - "check_gpg": True, - "check_ssl": True, - "gpgkey_urls": [ - "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64" - ], - "name": "fedora", + "gpgcheck": True, + "sslverify": True, + "gpgkey": ["file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64"], + "id": "fedora", "proxy": "http://proxy.brianlane.com:8123", - "system": True - "type": "yum-metalink", - "url": "https://mirrors.fedoraproject.org/metalink?repo=fedora-28&arch=x86_64" + "baseurl": "https://mirrors.fedoraproject.org/metalink?repo=fedora-28&arch=x86_64", + "metalink": None, + "mirrorlist": None } """ - repo = yum.yumRepo.YumRepository(source["name"]) + repo = {"id": source["name"]} + # This will allow errors to be raised so we can catch them # 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": - repo.baseurl = source["url"] + repo["baseurl"] = [source["url"]] + repo["metalink"] = None + repo["mirrorlist"] = None elif source["type"] == "yum-metalink": - repo.metalink = source["url"] + repo["metalink"] = source["url"] + repo["baseurl"] = [] + repo["mirrorlist"] = None elif source["type"] == "yum-mirrorlist": - repo.mirrorlist = source["url"] + repo["mirrorlist"] = source["url"] + repo["baseurl"] = [] + repo["metalink"] = None if "proxy" in source: - repo.proxy = source["proxy"] + repo["proxy"] = source["proxy"] if source["check_ssl"]: - repo.sslverify = True + repo["sslverify"] = True else: - repo.sslverify = False + repo["sslverify"] = False if source["check_gpg"]: - repo.gpgcheck = True + repo["gpgcheck"] = True else: - repo.gpgcheck = False + repo["gpgcheck"] = False if "gpgkey_urls" in source: - repo.gpgkey = ",".join(source["gpgkey_urls"]) - - repo.enable() + repo["gpgkey"] = source["gpgkey_urls"] return repo diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 1e481641..75dd8965 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1454,8 +1454,11 @@ def v0_api(api): if source["name"] in repos: yb.repos.delete(source["name"]) - repo = source_to_repo(source) - yb.repos.add(repo) + repo_dict = source_to_repo(source) + 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"]) 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 source_path = joinpaths(repo_dir, os.path.basename("%s.repo" % source["name"])) 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: 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: yb = api.config["YUMLOCK"].yb repos = list(r.id for r in yb.repos.listEnabled()) - log.info("BCL: repos = %s", repos) if source["name"] in repos: yb.repos.delete(source["name"]) # delete doesn't remove it from the cache used by listEnabled so we have to force it diff --git a/tests/pylorax/test_projects.py b/tests/pylorax/test_projects.py index a77c0145..428bc661 100644 --- a/tests/pylorax/test_projects.py +++ b/tests/pylorax/test_projects.py @@ -412,6 +412,10 @@ gpgcheck = True 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): @classmethod def setUpClass(self): @@ -479,44 +483,44 @@ class SourceTest(unittest.TestCase): def test_source_to_repo_baseurl(self): """Test creating a yum.yumRepo.YumRepository with a 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): """Test creating a yum.yumRepo.YumRepository with a 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): """Test creating a yum.yumRepo.YumRepository with a 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): """Test creating a yum.yumRepo.YumRepository with a 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): """Test creating a yum.yumRepo.YumRepository with a proxy""" 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): """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): """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): """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): """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): """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())