From 65638b4d012400357b08382b068be29751835d5c Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Tue, 14 May 2024 11:09:55 -0400 Subject: [PATCH] remove old install info when installing collections (#83194) * Fix cleaning up galaxy server info from previous installations * changelog --- .../cleanup-outdated-galaxy-install-info.yml | 2 ++ lib/ansible/galaxy/collection/__init__.py | 18 ++++++++++++++++++ .../tasks/install.yml | 7 +++++++ 3 files changed, 27 insertions(+) create mode 100644 changelogs/fragments/cleanup-outdated-galaxy-install-info.yml diff --git a/changelogs/fragments/cleanup-outdated-galaxy-install-info.yml b/changelogs/fragments/cleanup-outdated-galaxy-install-info.yml new file mode 100644 index 00000000000..7fde75f13e7 --- /dev/null +++ b/changelogs/fragments/cleanup-outdated-galaxy-install-info.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-galaxy collection install - remove old installation info when installing collections (https://github.com/ansible/ansible/issues/83182). diff --git a/lib/ansible/galaxy/collection/__init__.py b/lib/ansible/galaxy/collection/__init__.py index d27328cd0fe..47b9cb53af7 100644 --- a/lib/ansible/galaxy/collection/__init__.py +++ b/lib/ansible/galaxy/collection/__init__.py @@ -8,6 +8,7 @@ from __future__ import annotations import errno import fnmatch import functools +import glob import inspect import json import os @@ -1525,6 +1526,7 @@ def install(collection, path, artifacts_manager): # FIXME: mv to dataclasses? artifacts_manager.required_successful_signature_count, artifacts_manager.ignore_signature_errors, ) + remove_source_metadata(collection, b_collection_path) if (collection.is_online_index_pointer and isinstance(collection.src, GalaxyAPI)): write_source_metadata( collection, @@ -1561,6 +1563,22 @@ def write_source_metadata(collection, b_collection_path, artifacts_manager): raise +def remove_source_metadata(collection, b_collection_path): + pattern = f"{collection.namespace}.{collection.name}-*.info" + info_path = os.path.join( + b_collection_path, + b'../../', + to_bytes(pattern, errors='surrogate_or_strict') + ) + if (outdated_info := glob.glob(info_path)): + display.vvvv(f"Removing {pattern} metadata from previous installations") + for info_dir in outdated_info: + try: + shutil.rmtree(info_dir) + except Exception: + pass + + def verify_artifact_manifest(manifest_file, signatures, keyring, required_signature_count, ignore_signature_errors): # type: (str, list[str], str, str, list[str]) -> None failed_verify = False diff --git a/test/integration/targets/ansible-galaxy-collection/tasks/install.yml b/test/integration/targets/ansible-galaxy-collection/tasks/install.yml index 595911086e9..4cc3985c6aa 100644 --- a/test/integration/targets/ansible-galaxy-collection/tasks/install.yml +++ b/test/integration/targets/ansible-galaxy-collection/tasks/install.yml @@ -31,6 +31,9 @@ - install_normal_files.files[2].path | basename in ['MANIFEST.json', 'FILES.json', 'README.md'] - (install_normal_manifest.content | b64decode | from_json).collection_info.version == '1.0.9' - 'from_first_good_server.stdout|regex_findall("has not signed namespace1\.name1")|length == 1' + - "info_dir is is_dir" + vars: + info_dir: "{{ galaxy_dir }}/ansible_collections/namespace1.name1-1.0.9.info" - name: Remove the collection file: @@ -105,6 +108,10 @@ that: - '"Installing ''namespace1.name1:1.1.0-beta.1'' to" in install_prerelease.stdout' - (install_prerelease_actual.content | b64decode | from_json).collection_info.version == '1.1.0-beta.1' + - not ([info_dir, "namespace1.name1-1.0.9.info"] | path_join) is is_dir + - ([info_dir, "namespace1.name1-1.1.0-beta.1.info"] | path_join) is is_dir + vars: + info_dir: "{{ galaxy_dir }}/ansible_collections" - name: Remove beta file: