Skip to content

Commit

Permalink
Merge pull request #4 from ispras/ansible_binary
Browse files Browse the repository at this point in the history
add ability to specify path to ansible-playbook binary file in runner
  • Loading branch information
davidBMSTU authored Mar 14, 2024
2 parents bb44893 + 1aa4f24 commit 42f784a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/cotea/runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import threading

import cotea.utils as cotea_utils
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.isfile(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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = ""

Expand Down
9 changes: 7 additions & 2 deletions src/cotea_ansible_error_case.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import unittest

from cotea.runner import runner
Expand All @@ -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():
Expand All @@ -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():
Expand Down
27 changes: 25 additions & 2 deletions src/cotea_internal_error_case.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import unittest


Expand All @@ -10,16 +11,37 @@ 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

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():
Expand All @@ -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():
Expand Down
5 changes: 4 additions & 1 deletion src/cotea_ok_case.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import unittest

from cotea.runner import runner
Expand All @@ -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

Expand Down

0 comments on commit 42f784a

Please sign in to comment.