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

Avoid running galaxy on offline mode #180

Merged
merged 1 commit into from
Nov 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ repos:
additional_dependencies:
- cached_property
- flaky
- jinja2
- packaging
- pytest
- pytest-mock
- subprocess-tee>=0.3.5
- types-PyYAML
- types-pkg_resources
- types-jsonschema>=4.4.9
- types-pkg_resources
- repo: https://github.com/pycqa/pylint
rev: v2.15.3
hooks:
Expand Down
37 changes: 21 additions & 16 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def install_collection_from_disk(
force=True,
)

# pylint: disable=too-many-branches
def install_requirements(
self, requirement: str, retry: bool = False, offline: bool = False
) -> None:
Expand All @@ -294,13 +295,14 @@ def install_requirements(

if offline:
_logger.warning(
"Offline mode ignored because `ansible-galaxy role install` command does not support it."
"Role installation skipped because `ansible-galaxy role install` command does not support an offline mode."
)
_logger.info("Running %s", " ".join(cmd))
result = self.exec(cmd, retry=retry)
if result.returncode != 0:
_logger.error(result.stdout)
raise AnsibleCommandError(result)
else:
_logger.info("Running %s", " ".join(cmd))
result = self.exec(cmd, retry=retry)
if result.returncode != 0:
_logger.error(result.stdout)
raise AnsibleCommandError(result)

# Run galaxy collection install works on v2 requirements.yml
if "collections" in reqs_yaml:
Expand All @@ -311,22 +313,25 @@ def install_requirements(
"install",
"-v",
]
skip = False
if offline:
if self.version_in_range(upper="2.14"):
_logger.warning(
"Offline mode ignored because it is not supported by ansible versions before 2.14."
"Collection install skipped because ansible versions before 2.14 do not support an offline mode."
)
skip = True
else:
cmd.append("--offline")
cmd.extend(["-r", requirement])
if self.cache_dir:
cmd.extend(["-p", f"{self.cache_dir}/collections"])
_logger.info("Running %s", " ".join(cmd))
result = self.exec(cmd, retry=retry)
if result.returncode != 0:
_logger.error(result.stdout)
_logger.error(result.stderr)
raise AnsibleCommandError(result)
if not skip:
cmd.extend(["-r", requirement])
if self.cache_dir:
cmd.extend(["-p", f"{self.cache_dir}/collections"])
_logger.info("Running %s", " ".join(cmd))
result = self.exec(cmd, retry=retry)
if result.returncode != 0:
_logger.error(result.stdout)
_logger.error(result.stderr)
raise AnsibleCommandError(result)

def prepare_environment( # noqa: C901
self,
Expand Down
9 changes: 8 additions & 1 deletion test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,14 @@ def test_prepare_environment_offline_role() -> None:
"""Ensure that we can make use of offline roles."""
with remember_cwd("test/roles/acme.missing_deps"):
runtime = Runtime(isolated=True)
with pytest.raises(AnsibleCommandError):
if runtime.version_in_range(lower="2.14"):
# starting with 2.14 we can properly fail because this role has
# some missing collections in its requirements. We pass the offline
# but install will fail because there are not really offline
# requirements.
with pytest.raises(AnsibleCommandError):
runtime.prepare_environment(install_local=True, offline=True)
else:
runtime.prepare_environment(install_local=True, offline=True)


Expand Down