From 57a6d5bd1cb5fb85fdda040d0165d8a75c8f43f1 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 24 Jun 2021 15:43:18 +0100 Subject: [PATCH] Improve install_collection implementation (#1637) - assure at default verbosity we do log ansible-galaxy command ran - add ability to specify destination for install_collection - assure we inject our own cache to top of collection path - allow use of require_collection without a cache folder. --- src/ansiblelint/config.py | 2 +- src/ansiblelint/prerun.py | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ansiblelint/config.py b/src/ansiblelint/config.py index b0c98c4718..4159f89ac8 100644 --- a/src/ansiblelint/config.py +++ b/src/ansiblelint/config.py @@ -60,7 +60,7 @@ options = Namespace( - cache_dir=".cache", + cache_dir=None, colored=True, configured=False, cwd=".", diff --git a/src/ansiblelint/prerun.py b/src/ansiblelint/prerun.py index 4aec87cfd6..1471ae6427 100644 --- a/src/ansiblelint/prerun.py +++ b/src/ansiblelint/prerun.py @@ -97,7 +97,7 @@ def _get_ver_err() -> Tuple[str, str]: return ver, err -def install_collection(collection: str) -> None: +def install_collection(collection: str, destination: Optional[str] = None) -> None: """Install an Ansible collection. Can accept version constraints like 'foo.bar:>=1.2.3' @@ -106,11 +106,11 @@ def install_collection(collection: str) -> None: "ansible-galaxy", "collection", "install", - "-p", - f"{options.cache_dir}/collections", "-v", - f"{collection}", ] + if destination: + cmd.extend(["-p", destination]) + cmd.append(f"{collection}") _logger.info("Running %s", " ".join(cmd)) run = subprocess.run( @@ -201,7 +201,12 @@ def prepare_environment(required_collections: Optional[Dict[str, str]] = None) - if required_collections: for name, min_version in required_collections.items(): - install_collection(f"{name}:>={min_version}") + install_collection( + f"{name}:>={min_version}", + destination=f"{options.cache_dir}/collections" + if options.cache_dir + else None, + ) _install_galaxy_role() _perform_mockings() @@ -437,7 +442,7 @@ def ansible_config_get(key: str, kind: Type[Any] = str) -> Union[str, List[str], return None -def require_collection( +def require_collection( # noqa: C901 name: str, version: Optional[str] = None, install: bool = True ) -> None: """Check if a minimal collection version is present or exits. @@ -453,6 +458,12 @@ def require_collection( paths = ansible_config_get('COLLECTIONS_PATHS', list) if not paths or not isinstance(paths, list): sys.exit(f"Unable to determine ansible collection paths. ({paths})") + + if options.cache_dir: + # if we have a cache dir, we want to be use that would be preferred + # destination when installing a missing collection + paths.insert(0, f"{options.cache_dir}/collections") + for path in paths: collpath = os.path.join(path, 'ansible_collections', ns, coll) if os.path.exists(collpath):