Skip to content

Commit

Permalink
Avoid running galaxy on offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Nov 1, 2022
1 parent 309713b commit db90d9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
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
3 changes: 1 addition & 2 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,7 @@ 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):
runtime.prepare_environment(install_local=True, offline=True)
runtime.prepare_environment(install_local=True, offline=True)


def test_runtime_run(runtime: Runtime) -> None:
Expand Down

0 comments on commit db90d9f

Please sign in to comment.