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

Handle custom project_dir #1222

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions ansible_runner/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ def __init__(self,

self.artifact_dir = os.path.join(artifact_dir, "{}".format(self.ident))

self.loader = ArtifactLoader(self.private_data_dir)

if not project_dir:
self.project_dir = os.path.join(self.private_data_dir, 'project')
else:
self.project_dir = project_dir
self.project_dir = self.loader.abspath(project_dir)

self.rotate_artifacts = rotate_artifacts
self.fact_cache_type = fact_cache_type
self.fact_cache = os.path.join(self.artifact_dir, fact_cache or 'fact_cache') if self.fact_cache_type == 'jsonfile' else None

self.loader = ArtifactLoader(self.private_data_dir)

if self.host_cwd:
self.host_cwd = os.path.abspath(self.host_cwd)
self.cwd = self.host_cwd
Expand Down Expand Up @@ -469,10 +469,13 @@ def wrap_args_for_containerization(self, args, execution_mode, cmdline_args):
if self.container_workdir:
workdir = self.container_workdir
elif self.host_cwd is not None and os.path.exists(self.host_cwd):
# mount current host working diretory if passed and exist
# mount current host working directory if passed and exist
self._ensure_path_safe_to_mount(self.host_cwd)
self._update_volume_mount_paths(new_args, self.host_cwd)
workdir = self.host_cwd
elif self.project_dir:
workdir = "/runner/project"
self._update_volume_mount_paths(new_args, self.project_dir, workdir)
else:
workdir = "/runner/project"

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/projects/project_dir_test/env/envvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANSIBLE_DEVEL_WARNING: no
1 change: 1 addition & 0 deletions test/fixtures/projects/project_dir_test/inventory/inv_1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
host_1 ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
gather_facts: no

tasks:
- debug:
msg: "In my_project"
6 changes: 6 additions & 0 deletions test/fixtures/projects/project_dir_test/project/debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
gather_facts: no

tasks:
- debug:
msg: "In project"
62 changes: 62 additions & 0 deletions test/integration/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,68 @@ def test_multiple_inventories(project_fixtures):
assert 'host_2' in stdout


def test_default_project_dir(project_fixtures):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
playbook='debug.yml',
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In project' in stdout


def test_project_dir(project_fixtures):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
project_dir='my_project',
playbook='debug.yml',
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In my_project' in stdout


@pytest.mark.test_all_runtimes
def test_default_project_dir_inside_container(project_fixtures, runtime):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
playbook='debug.yml',
process_isolation_executable=runtime,
process_isolation=True,
container_image=defaults.default_container_image,
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In project' in stdout


@pytest.mark.test_all_runtimes
def test_project_dir_inside_container(project_fixtures, runtime):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
project_dir='my_project',
playbook='debug.yml',
process_isolation_executable=runtime,
process_isolation=True,
container_image=defaults.default_container_image,
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In my_project' in stdout


def test_inventory_absolute_path(project_fixtures):
private_data_dir = project_fixtures / 'debug'

Expand Down
4 changes: 3 additions & 1 deletion test/unit/config/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,9 @@ def test_containerization_settings(tmp_path, runtime, mocker):
else:
extra_container_args = [f'--user={os.getuid()}']

expected_command_start = [runtime, 'run', '--rm', '--tty', '--interactive', '--workdir', '/runner/project'] + \
expected_command_start = [runtime, 'run', '--rm', '--tty', '--interactive'] + \
['-v', '{}/project/:/runner/project/'.format(rc.private_data_dir)] + \
['--workdir', '/runner/project'] + \
['-v', '{}/:/runner/:Z'.format(rc.private_data_dir)] + \
['-v', '/host1/:/container1/', '-v', '/host2/:/container2/'] + \
['--env-file', '{}/env.list'.format(rc.artifact_dir)] + \
Expand Down