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

fix: ensure inventory files are copied #683

Closed
Closed
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
10 changes: 6 additions & 4 deletions ansible_rulebook/action/run_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ async def _pre_process(self) -> None:
os.mkdir(inventory_dir)

if self.helper.control.inventory:
create_inventory(inventory_dir, self.helper.control.inventory)
self.inventory = os.path.join(
inventory_dir, os.path.basename(self.helper.control.inventory)
self.inventory = create_inventory(
inventory_dir,
self.helper.control.inventory,
copy_files=self.action_args.get(
"copy_files", self.default_copy_files
),
)
os.mkdir(project_dir)

Expand Down Expand Up @@ -270,7 +273,6 @@ def _get_latest_artifact(self, component: str, content: bool = True):
return files[0]

async def _untar_project(self, output_dir, project_data_file):

cmd = [tar, "zxvf", project_data_file]
proc = await asyncio.create_subprocess_exec(
*cmd,
Expand Down
30 changes: 23 additions & 7 deletions ansible_rulebook/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,33 @@ async def send_session_stats(event_log: asyncio.Queue, stats: Dict):
)


def create_inventory(runner_inventory_dir: str, inventory: str) -> None:
if os.path.isfile(inventory):
shutil.copy(os.path.abspath(inventory), runner_inventory_dir)
elif os.path.exists(inventory):
def create_inventory(
runner_inventory_dir: str,
inventory: str,
copy_files: bool = False,
) -> str:
inventory_src_basedir = inventory
inventory_dest_path = ""
absolute_inventory_path = os.path.abspath(inventory)

if not os.path.exists(absolute_inventory_path):
raise InventoryNotFound(f"Inventory {inventory} not found")

if os.path.isfile(absolute_inventory_path):
inventory_src_basedir = os.path.dirname(absolute_inventory_path)
inventory_dest_path = os.path.join(
runner_inventory_dir, os.path.basename(inventory)
)
if not copy_files:
shutil.copy(absolute_inventory_path, runner_inventory_dir)

if copy_files or not os.path.isfile(absolute_inventory_path):
shutil.copytree(
os.path.abspath(inventory),
inventory_src_basedir,
runner_inventory_dir,
dirs_exist_ok=True,
)
else:
raise InventoryNotFound(f"Inventory {inventory} not found")
return os.path.join(runner_inventory_dir, inventory_dest_path)


def _builtin_filter_path(name: str) -> Tuple[bool, str]:
Expand Down
56 changes: 29 additions & 27 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ async def test_start_source_with_args(

@pytest.mark.asyncio
async def test_run_rulesets():

ruleset_queues, event_log = load_rulebook("rules/test_rules.yml")

queue = ruleset_queues[0][1]
Expand Down Expand Up @@ -495,33 +494,36 @@ async def test_run_assert_facts():
inventory = dict(
all=dict(hosts=dict(localhost=dict(ansible_connection="local")))
)
with tempfile.NamedTemporaryFile(mode="w+") as temp:
temp.write(yaml.dump(inventory))
temp.flush()
queue = ruleset_queues[0][1]
queue.put_nowait(dict())
queue.put_nowait(dict(i=1, meta=dict(hosts="localhost")))
queue.put_nowait(Shutdown())
await run_rulesets(
event_log,
ruleset_queues,
dict(Naboo="naboo"),
temp.name,
)
with tempfile.TemporaryDirectory() as tempdir:
with tempfile.NamedTemporaryFile(mode="w+", dir=tempdir) as temp:
temp.write(yaml.dump(inventory))
temp.flush()
queue = ruleset_queues[0][1]
queue.put_nowait(dict())
queue.put_nowait(dict(i=1, meta=dict(hosts="localhost")))
queue.put_nowait(Shutdown())
await run_rulesets(
event_log,
ruleset_queues,
dict(Naboo="naboo"),
temp.name,
)

assert event_log.get_nowait()["type"] == "EmptyEvent", "0"
assert event_log.get_nowait()["type"] == "Action", "0.1"
assert event_log.get_nowait()["type"] == "Job", "1.0"
for i in range(47):
assert event_log.get_nowait()["type"] == "AnsibleEvent", f"1.{i}"

event = event_log.get_nowait()
assert event["type"] == "Action", "2.1"
assert event["action"] == "run_playbook", "2.2"
assert event["rc"] == 0, "2.3"
assert event["status"] == "successful", "2.4"
assert event_log.get_nowait()["type"] == "Shutdown", "4"
assert event_log.empty()
assert event_log.get_nowait()["type"] == "EmptyEvent", "0"
assert event_log.get_nowait()["type"] == "Action", "0.1"
assert event_log.get_nowait()["type"] == "Job", "1.0"
for i in range(47):
assert (
event_log.get_nowait()["type"] == "AnsibleEvent"
), f"1.{i}"

event = event_log.get_nowait()
assert event["type"] == "Action", "2.1"
assert event["action"] == "run_playbook", "2.2"
assert event["rc"] == 0, "2.3"
assert event["status"] == "successful", "2.4"
assert event_log.get_nowait()["type"] == "Shutdown", "4"
assert event_log.empty()


@pytest.mark.asyncio
Expand Down
Loading