summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSloane Hertel <19572925+s-hertel@users.noreply.github.com>2022-09-28 10:12:30 -0400
committerGitHub <noreply@github.com>2022-09-28 09:12:30 -0500
commit551ddebeab451c1f158e4ef8b656d60ff57689be (patch)
tree40057217688f195761edf3e62110be4adbdb0c21 /lib
parent00d60a45fa7cd61b25d622735ceb4c8f6a125a76 (diff)
downloadansible-551ddebeab451c1f158e4ef8b656d60ff57689be.tar.gz
[2.13] Limit Galaxy API calls during ansible-galaxy dependency resolution (#78722)
* Limit Galaxy API calls during ansible-galaxy collection dependency resolution when possible Installing a tarfile with a dependency from a Galaxy server (e.g. dependencies: {'ns.coll': '>=1.0.0'}) does not get the available versions of the dependency from the galaxy server if a sufficient version is already installed. Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua> (cherry picked from commit 41b62f7db459b7c3a1d97302da3b9f2cbf302670)
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/galaxy/dependency_resolution/providers.py36
1 files changed, 24 insertions, 12 deletions
diff --git a/lib/ansible/galaxy/dependency_resolution/providers.py b/lib/ansible/galaxy/dependency_resolution/providers.py
index ccb56a9d79..817a1eb227 100644
--- a/lib/ansible/galaxy/dependency_resolution/providers.py
+++ b/lib/ansible/galaxy/dependency_resolution/providers.py
@@ -14,6 +14,7 @@ if t.TYPE_CHECKING:
ConcreteArtifactsManager,
)
from ansible.galaxy.collection.galaxy_api_proxy import MultiGalaxyAPIProxy
+ from ansible.galaxy.api import GalaxyAPI
from ansible.galaxy.collection.gpg import get_signature_from_source
from ansible.galaxy.dependency_resolution.dataclasses import (
@@ -316,8 +317,18 @@ class CollectionDependencyProviderBase(AbstractProvider):
# The fqcn is guaranteed to be the same
version_req = "A SemVer-compliant version or '*' is required. See https://semver.org to learn how to compose it correctly. "
version_req += "This is an issue with the collection."
+
+ # If we're upgrading collections, we can't calculate preinstalled_candidates until the latest matches are found.
+ # Otherwise, we can potentially avoid a Galaxy API call by doing this first.
+ preinstalled_candidates = set()
+ if not self._upgrade and first_req.type == 'galaxy':
+ preinstalled_candidates = {
+ candidate for candidate in self._preferred_candidates
+ if candidate.fqcn == fqcn and
+ all(self.is_satisfied_by(requirement, candidate) for requirement in requirements)
+ }
try:
- coll_versions = self._api_proxy.get_collection_versions(first_req)
+ coll_versions = [] if preinstalled_candidates else self._api_proxy.get_collection_versions(first_req) # type: t.Iterable[t.Tuple[str, GalaxyAPI]]
except TypeError as exc:
if first_req.is_concrete_artifact:
# Non hashable versions will cause a TypeError
@@ -395,19 +406,20 @@ class CollectionDependencyProviderBase(AbstractProvider):
reverse=True, # prefer newer versions over older ones
)
- preinstalled_candidates = {
- candidate for candidate in self._preferred_candidates
- if candidate.fqcn == fqcn and
- (
- # check if an upgrade is necessary
- all(self.is_satisfied_by(requirement, candidate) for requirement in requirements) and
+ if not preinstalled_candidates:
+ preinstalled_candidates = {
+ candidate for candidate in self._preferred_candidates
+ if candidate.fqcn == fqcn and
(
- not self._upgrade or
- # check if an upgrade is preferred
- all(SemanticVersion(latest.ver) <= SemanticVersion(candidate.ver) for latest in latest_matches)
+ # check if an upgrade is necessary
+ all(self.is_satisfied_by(requirement, candidate) for requirement in requirements) and
+ (
+ not self._upgrade or
+ # check if an upgrade is preferred
+ all(SemanticVersion(latest.ver) <= SemanticVersion(candidate.ver) for latest in latest_matches)
+ )
)
- )
- }
+ }
return list(preinstalled_candidates) + latest_matches