Skip to content

Commit

Permalink
Filter out inapplicable preinstalled collections
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hertel committed Jan 29, 2021
1 parent d31f655 commit a98f169
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
Expand Up @@ -2,3 +2,7 @@ major_changes:
- >-
It became possible to upgrade Ansible collections from Galaxy servers
using the ``--upgrade`` option with ``ansible-galaxy collection install``.
- >-
A collection can be reinstalled with new version requirements without using
the ``--force`` flag. The collection's dependencies will also be updated
with the new requirements.
47 changes: 9 additions & 38 deletions lib/ansible/galaxy/collection/__init__.py
Expand Up @@ -25,7 +25,6 @@
from hashlib import sha256
from io import BytesIO
from itertools import chain
from resolvelib.resolvers import InconsistentCandidate
from yaml.error import YAMLError

# NOTE: Adding type ignores is a hack for mypy to shut up wrt bug #1153
Expand Down Expand Up @@ -478,43 +477,15 @@ def install_collections(
for coll in preferred_requirements
}
with _display_progress("Process install dependency map"):
try:
dependency_map = _resolve_depenency_map(
collections,
galaxy_apis=apis,
preferred_candidates=preferred_collections,
concrete_artifacts_manager=artifacts_manager,
no_deps=no_deps,
allow_pre_release=allow_pre_release,
upgrade=upgrade,
)
except InconsistentCandidate as inconsistent_candidate_exc:
# FIXME: Processing this error is hacky and should be removed along
# FIXME: with implementing the automatic replacement for installed
# FIXME: collections.
if not all(
inconsistent_candidate_exc.candidate.fqcn == r.fqcn
for r in inconsistent_candidate_exc.criterion.iter_requirement()
):
raise

req_info = inconsistent_candidate_exc.criterion.information[0]
force_flag = (
'--force' if req_info.parent is None
else '--force-with-deps'
)
raise_from(
AnsibleError(
'Cannot meet requirement {collection!s} as it is already '
"installed at version '{installed_ver!s}'. "
'Use {force_flag!s} to overwrite'.format(
collection=req_info.requirement,
force_flag=force_flag,
installed_ver=inconsistent_candidate_exc.candidate.ver,
)
),
inconsistent_candidate_exc,
)
dependency_map = _resolve_depenency_map(
collections,
galaxy_apis=apis,
preferred_candidates=preferred_collections,
concrete_artifacts_manager=artifacts_manager,
no_deps=no_deps,
allow_pre_release=allow_pre_release,
upgrade=upgrade,
)

with _display_progress("Starting collection install process"):
for fqcn, concrete_coll_pin in dependency_map.items():
Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/galaxy/dependency_resolution/providers.py
Expand Up @@ -179,10 +179,10 @@ def find_matches(self, requirements):
for version, _none_src_server in coll_versions
]

preinstalled_candidates = {
candidate for candidate in self._preferred_candidates
if candidate.fqcn == fqcn
}
preinstalled_candidates = set()
for candidate in self._preferred_candidates:
if candidate.fqcn == fqcn and all(self.is_satisfied_by(requirement, candidate) for requirement in requirements):
preinstalled_candidates.add(candidate)

latest_matches = sorted(
{
Expand Down
Expand Up @@ -38,6 +38,37 @@
- "\"Skipping 'namespace1.collection1:2.0.0' as it is already installed\" in result.stdout_lines"
- "\"Skipping 'namespace1.collection2:2.0.0' as it is already installed\" in result.stdout_lines"

- name: test installing a new version without force or explicit upgrade flag
block:
- name: set up an upgradable collection and dependency
command: ansible-galaxy collection install namespace1.collection1:1.0.0 namespace1.collection2:1.0.0 --no-deps --force -s pulp_v3 {{ galaxy_verbosity }}
register: result
failed_when:
- '"namespace1.collection1:1.0.0 was installed successfully" not in result.stdout_lines'
- '"namespace1.collection2:1.0.0 was installed successfully" not in result.stdout_lines'

- name: upgrade the collection and any dependencies
command: ansible-galaxy collection install "namespace1.collection1:2.0.0" -s pulp_v3 {{ galaxy_verbosity }}
register: result

- assert:
that:
- "collection_line.endswith('was installed successfully')"
- "collection_version == '2.0.0'"
- "\"Skipping 'namespace1.collection2:1.0.0' as it is already installed\" in result.stdout_lines"
vars:
# Hack to replace unicode characters for JMESPath filter https://github.com/ansible/ansible/issues/20379
collection_line: "{{ result.stdout_lines | to_json | from_json | json_query(\"[?starts_with(@, 'namespace1.collection1')]\") | first }}"
collection_version: "{{ collection_line | regex_search(version_re) }}"

- name: upgrading again should make no changes
command: ansible-galaxy collection install "namespace1.collection1:2.0.0" -s pulp_v3 {{ galaxy_verbosity }}
register: result

- assert:
that:
- "\"Nothing to do. All requested collections are already installed.\" in result.stdout"

- name: test upgrading a collection with an inexact requirement and --no-deps
block:
- name: set up an upgradable collection and dependency
Expand All @@ -47,6 +78,19 @@
- '"namespace1.collection1:1.0.0 was installed successfully" not in result.stdout_lines'
- '"namespace1.collection2:1.0.0 was installed successfully" not in result.stdout_lines'

- name: test new version constraint that matches the preinstalled version does nothing
command: ansible-galaxy collection install namespace1.collection1:<=2.0.0 --no-deps -s pulp_v3 {{ galaxy_verbosity }}
register: result

- assert:
that:
- '"namespace1.collection1:2.0.0 was installed successfully" not in result.stdout_lines'
# shouldn't have even been examined
- "\"Skipping 'namespace1.collection1:1.0.0' as it is already installed\" not in result.stdout_lines"
- '"namespace1.collection2:2.0.0 was installed successfully" not in result.stdout_lines'
# shouldn't have even been examined
- "\"Skipping 'namespace1.collection2:1.0.0' as it is already installed\" not in result.stdout_lines"

- name: upgrade the collection with an inexact version and --no-deps
command: ansible-galaxy collection install namespace1.collection1:<=2.0.0 --no-deps --upgrade -s pulp_v3 {{ galaxy_verbosity }}
register: result
Expand All @@ -58,6 +102,14 @@
# shouldn't have even been examined
- "\"Skipping 'namespace1.collection2:1.0.0' as it is already installed\" not in result.stdout_lines"

- name: test implicitly upgrading with an inexact flag
command: ansible-galaxy collection install namespace1.collection2:>1.0.0 -s pulp_v3 {{ galaxy_verbosity }}
register: result

- assert:
that:
- '"namespace1.collection2:2.0.0 was installed successfully" in result.stdout_lines'

- name: test upgrading a dep of a collection matching the latest version
block:
- name: set up an upgradable collection and dependency
Expand Down

0 comments on commit a98f169

Please sign in to comment.