Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve install_collection implementation #1637

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@


options = Namespace(
cache_dir=".cache",
cache_dir=None,
colored=True,
configured=False,
cwd=".",
Expand Down
23 changes: 17 additions & 6 deletions src/ansiblelint/prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down