Skip to content

Commit

Permalink
Ensure roles have a namespace when created (ansible#3277)
Browse files Browse the repository at this point in the history
As test tools require the presence of a namespace, we now detect when
user is trying to create standalone roles and require him to mention
the namespace.

Related: ansible/ansible-compat#78
  • Loading branch information
ssbarnea authored Nov 25, 2021
1 parent bd2f602 commit c0274bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
34 changes: 32 additions & 2 deletions src/molecule/command/init/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import logging
import os
import re

import click

Expand Down Expand Up @@ -58,8 +59,22 @@ def execute(self):
:return: None
"""
namespace = None
role_name = self._command_args["role_name"]
role_directory = os.getcwd()

# outside collections our tooling needs a namespace.
if not os.path.isfile("../galaxy.yml"):
name_re = re.compile(r"^[a-z][a-z0-9_]+\.[a-z][a-z0-9_]+$")

if not name_re.match(role_name):
util.sysexit_with_message(
"Outside collections you must mention role "
"namespace like: molecule init role 'acme.myrole'. Be sure "
"you use only lowercase characters and underlines. See https://galaxy.ansible.com/docs/contributing/creating_role.html"
)
namespace, role_name = role_name.split(".")

msg = f"Initializing new role {role_name}..."
LOG.info(msg)

Expand All @@ -75,12 +90,27 @@ def execute(self):
f"Galaxy failed to create role, returned {result.returncode!s}"
)

if namespace:
# we need to inject namespace info into meta/main.yml
cmd = [
"ansible",
"localhost",
"-o", # one line output
"-m",
"lineinfile",
"-a",
f'path={role_name}/meta/main.yml line=" namespace: {namespace}" insertafter=" author: your name"',
]
util.run_command(cmd, check=True)

scenario_base_directory = os.path.join(role_directory, role_name)
templates = [
api.drivers()[self._command_args["driver_name"]].template_dir(),
api.verifiers()[self._command_args["verifier_name"]].template_dir(),
]
self._process_templates("molecule", self._command_args, role_directory)
self._process_templates(
"molecule", {**self._command_args, "role_name": role_name}, role_directory
)
for template in templates:
self._process_templates(
template, self._command_args, scenario_base_directory
Expand Down Expand Up @@ -134,7 +164,7 @@ def role(
role_name,
verifier_name,
): # pragma: no cover
"""Initialize a new role for use with Molecule."""
"""Initialize a new role for use with Molecule, namespace is required outside collections, like acme.myrole."""
command_args = {
"dependency_name": dependency_name,
"driver_name": driver_name,
Expand Down
12 changes: 6 additions & 6 deletions src/molecule/test/unit/command/init/test_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _command_args():
"dependency_name": "galaxy",
"driver_name": "delegated",
"provisioner_name": "ansible",
"role_name": "test-role",
"role_name": "acme.test_role",
"scenario_name": "default",
"subcommand": __name__,
"verifier_name": "ansible",
Expand All @@ -46,13 +46,13 @@ def _instance(_command_args):
def test_execute(temp_dir, _instance, patched_logger_info):
_instance.execute()

msg = "Initializing new role test-role..."
msg = "Initializing new role test_role..."
patched_logger_info.assert_any_call(msg)

assert os.path.isdir("./test-role")
assert os.path.isdir("./test-role/molecule/default")
assert os.path.isdir("./test_role")
assert os.path.isdir("./test_role/molecule/default")

role_directory = os.path.join(temp_dir.strpath, "test-role")
role_directory = os.path.join(temp_dir.strpath, "test_role")
msg = f"Initialized role in {role_directory} successfully."
patched_logger_info.assert_any_call(msg)

Expand All @@ -65,5 +65,5 @@ def test_execute_role_exists(temp_dir, _instance, patched_logger_critical):

assert 1 == e.value.code

msg = "The directory test-role exists. Cannot create new role."
msg = "The directory test_role exists. Cannot create new role."
patched_logger_critical.assert_called_once_with(msg)

0 comments on commit c0274bb

Please sign in to comment.