From b1dae5c00af85d3d234b91a31807a90668c0d0d3 Mon Sep 17 00:00:00 2001 From: Andrew Biryukov Date: Wed, 13 Mar 2024 19:38:33 +0300 Subject: [PATCH 1/2] add ability to specify path to ansible-playbook binary file in runner --- src/cotea/runner.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cotea/runner.py b/src/cotea/runner.py index 12dc95f..3a543f0 100644 --- a/src/cotea/runner.py +++ b/src/cotea/runner.py @@ -1,4 +1,5 @@ import json +import os import threading import cotea.utils as cotea_utils @@ -35,7 +36,8 @@ class runner: - def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False): + def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False, + ansible_pb_bin="/usr/local/bin/ansible-playbook"): logging_lvl = logging.INFO if debug_mod: logging_lvl= logging.DEBUG @@ -64,6 +66,11 @@ def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False): self.progress_bar = ansible_progress_bar() self.execution_tree = AnsibleExecTree() + if os.path.exists(ansible_pb_bin): + self.ansible_pb_bin = ansible_pb_bin + else: + raise Exception(f"Ansible playbook bin {ansible_pb_bin} not found") + self._set_wrappers() start_ok = self._start_ansible() self.logger.debug("Ansible start ok: %s", start_ok) @@ -133,7 +140,7 @@ def _except_hook(self, args, /): def _start_ansible(self): args = self.arg_maker.args - args.insert(0, "/usr/local/bin/ansible-playbook") + args.insert(0, self.ansible_pb_bin) args.insert(1, self.pb_path) self.pbCLI = PlaybookCLI(args) @@ -363,7 +370,7 @@ def get_all_error_msgs(self): return self.update_conn_wrapper.error_msgs - # returns last error msg that wasn't ignored + # returns last error msg that wasn't ignored def get_error_msg(self): res = "" From 1aa4f2402321e6b78a2f68fe3120313bb88b099b Mon Sep 17 00:00:00 2001 From: Andrew Biryukov Date: Thu, 14 Mar 2024 16:52:19 +0300 Subject: [PATCH 2/2] add test for ansible playbook bin path --- src/cotea/runner.py | 2 +- src/cotea_ansible_error_case.py | 9 +++++++-- src/cotea_internal_error_case.py | 27 +++++++++++++++++++++++++-- src/cotea_ok_case.py | 5 ++++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/cotea/runner.py b/src/cotea/runner.py index 3a543f0..72c0410 100644 --- a/src/cotea/runner.py +++ b/src/cotea/runner.py @@ -66,7 +66,7 @@ def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False, self.progress_bar = ansible_progress_bar() self.execution_tree = AnsibleExecTree() - if os.path.exists(ansible_pb_bin): + if os.path.isfile(ansible_pb_bin): self.ansible_pb_bin = ansible_pb_bin else: raise Exception(f"Ansible playbook bin {ansible_pb_bin} not found") diff --git a/src/cotea_ansible_error_case.py b/src/cotea_ansible_error_case.py index 83a8d1d..164eda8 100644 --- a/src/cotea_ansible_error_case.py +++ b/src/cotea_ansible_error_case.py @@ -1,3 +1,4 @@ +import shutil import unittest from cotea.runner import runner @@ -10,7 +11,9 @@ def run_ansible_error_case(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) while r.has_next_play(): while r.has_next_task(): @@ -30,7 +33,9 @@ def run_ansible_error_case_with_ignore(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) while r.has_next_play(): while r.has_next_task(): diff --git a/src/cotea_internal_error_case.py b/src/cotea_internal_error_case.py index e495dc4..121d7f1 100644 --- a/src/cotea_internal_error_case.py +++ b/src/cotea_internal_error_case.py @@ -1,3 +1,4 @@ +import shutil import unittest @@ -10,6 +11,25 @@ def tearDown(self) -> None: # to clear previous execution context remove_modules_from_imported(module_name_like="cotea") + def test_incorrect_playbook_bin_path(self): + from cotea.runner import runner + from cotea.arguments_maker import argument_maker + + pb_path = "cotea_run_files/ok.yaml" + inv_path = "cotea_run_files/inv" + ansible_pb_bin = "/path/to/.venv/bin/ansible-playbook" + + arg_maker = argument_maker() + arg_maker.add_arg("-i", inv_path) + + try: + runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=ansible_pb_bin) + except Exception as e: + self.assertTrue(str(e) == f"Ansible playbook bin {ansible_pb_bin} not found", + msg="Unexpected exception message") + else: + self.assertFalse(True, msg="Ansible is supposed to fail due to incorrect ansible playbook bin path") + def test_incorrect_playbook_path_case(self): from cotea.runner import runner from cotea.arguments_maker import argument_maker @@ -17,9 +37,11 @@ def test_incorrect_playbook_path_case(self): pb_path = "cotea_run_files/#%|&" inv_path = "cotea_run_files/inv" + bin_path = shutil.which('ansible-playbook') + arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) try: while r.has_next_play(): @@ -41,10 +63,11 @@ def test_incorrect_syntax_case(self): pb_path = "cotea_run_files/incorrect.yaml" inv_path = "cotea_run_files/inv" + bin_path = shutil.which('ansible-playbook') arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) try: while r.has_next_play(): diff --git a/src/cotea_ok_case.py b/src/cotea_ok_case.py index 5f3fa6a..80d8df8 100644 --- a/src/cotea_ok_case.py +++ b/src/cotea_ok_case.py @@ -1,3 +1,4 @@ +import shutil import unittest from cotea.runner import runner @@ -24,7 +25,9 @@ def run_cotea_ok_case(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) plays_ind = 0 tasks_ind = 0