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 duplicating molecule requirements.yml files #1521

Merged
merged 3 commits into from Apr 19, 2021
Merged
Changes from 2 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
87 changes: 48 additions & 39 deletions src/ansiblelint/prerun.py
Expand Up @@ -101,26 +101,44 @@ def _get_ver_err() -> Tuple[str, str]:
stop=tenacity.stop_after_attempt(3), # type: ignore
before_sleep=tenacity.after_log(_logger, logging.WARNING), # type: ignore
)
def prepare_environment() -> None:
"""Make dependencies available if needed."""
if not options.configured:
# Allow method to be used without calling the command line, so we can
# reuse it in other tools, like molecule.
# pylint: disable=import-outside-toplevel,cyclic-import
from ansiblelint.__main__ import initialize_options
def install_requirements(requirement: str) -> None:
"""Install dependencies from a requirements.yml."""
if not os.path.exists(requirement):
return

initialize_options()
cmd = [
"ansible-galaxy",
"role",
"install",
"--roles-path",
f"{options.project_dir}/.cache/roles",
"-vr",
f"{requirement}",
]

_logger.info("Running %s", " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
_logger.error(run.stdout)
raise RuntimeError(run.returncode)

if not options.offline and os.path.exists("requirements.yml"):
# Run galaxy collection install works on v2 requirements.yml
if "collections" in yaml_from_file(requirement):

cmd = [
"ansible-galaxy",
"role",
"collection",
"install",
"--roles-path",
f"{options.project_dir}/.cache/roles",
"-p",
f"{options.project_dir}/.cache/collections",
"-vr",
"requirements.yml",
f"{requirement}",
]

_logger.info("Running %s", " ".join(cmd))
Expand All @@ -133,32 +151,23 @@ def prepare_environment() -> None:
)
if run.returncode != 0:
_logger.error(run.stdout)
sys.exit(run.returncode)

# Run galaxy collection install works on v2 requirements.yml
if "collections" in yaml_from_file("requirements.yml"):

cmd = [
"ansible-galaxy",
"collection",
"install",
"-p",
f"{options.project_dir}/.cache/collections",
"-vr",
"requirements.yml",
]

_logger.info("Running %s", " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
_logger.error(run.stdout)
raise RuntimeError(run.returncode)
raise RuntimeError(run.returncode)


def prepare_environment() -> None:
"""Make dependencies available if needed."""
if not options.configured:
# Allow method to be used without calling the command line, so we can
# reuse it in other tools, like molecule.
# pylint: disable=import-outside-toplevel,cyclic-import
from ansiblelint.__main__ import initialize_options

initialize_options()

if not options.offline:
install_requirements("requirements.yml")
for req in pathlib.Path(".").glob("molecule/*/requirements.yml"):
install_requirements(str(req))

_install_galaxy_role()
_perform_mockings()
Expand Down