Skip to content

Commit 9b6cce0

Browse files
Wait for sudo before ansible
1 parent 22512e7 commit 9b6cce0

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

scripts/qesap/lib/cmds.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ def ansible_command_sequence(
372372
ssh_share += ' --ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
373373
ansible_cmd_seq.append({"cmd": ssh_share})
374374

375+
sudo_wait = (
376+
f'{ansible_bin_paths["ansible"]} {ansible_common} all '
377+
'-m shell '
378+
"-a 'i=0; while [ $i -lt 35 ]; do sudo -n true && exit 0; sleep 5; i=$((i+1)); done; exit 1' "
379+
'--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
380+
)
381+
ansible_cmd_seq.append({"cmd": sudo_wait})
382+
375383
selected_list_of_playbooks = []
376384
if apiver < 4:
377385
selected_list_of_playbooks = configure_data_ansible[sequence]

scripts/qesap/test/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ def _callback(inventory):
215215
return _callback
216216

217217

218+
@pytest.fixture()
219+
def ansible_sudo_wait_call():
220+
def _callback(inventory):
221+
return (
222+
f"{ANSIBLE_EXE} -vv -i {inventory} all "
223+
"-m shell "
224+
"-a 'i=0; while [ $i -lt 35 ]; do sudo -n true && exit 0; sleep 5; i=$((i+1)); done; exit 1' "
225+
'--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
226+
)
227+
return _callback
228+
229+
218230
@pytest.fixture
219231
def mock_call_ansibleplaybook():
220232
"""

scripts/qesap/test/unit/test_qesap_ansible.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_ansible_no_bin(
281281

282282

283283
@mock.patch("shutil.which", side_effect=lambda x: fake_ansible_path(x))
284-
@mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (1, [])])
284+
@mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (0, []), (1, [])])
285285
def test_ansible_stop(
286286
run,
287287
_,
@@ -538,6 +538,7 @@ def test_ansible_ssh(
538538
ansible_config,
539539
mock_call_ansibleplaybook,
540540
ansible_exe_call,
541+
ansible_sudo_wait_call,
541542
):
542543
"""
543544
This first Ansible command has to be called before all the others
@@ -578,6 +579,7 @@ def test_ansible_ssh(
578579
calls = []
579580

580581
calls.append(mock.call(cmd=ansible_exe_call(inventory)))
582+
calls.append(mock.call(cmd=ansible_sudo_wait_call(inventory)))
581583
for playbook in playbook_list:
582584
calls.append(mock_call_ansibleplaybook(inventory, playbook))
583585

scripts/qesap/test/unit/test_qesap_lib_cmd_ansible.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_cmd_ansible(
4545
create_playbooks,
4646
create_inventory,
4747
ansible_exe_call,
48+
ansible_sudo_wait_call,
4849
mock_call_ansibleplaybook,
4950
):
5051
"""
@@ -82,6 +83,7 @@ def test_cmd_ansible(
8283
)
8384

8485
calls = [mock.call(cmd=ansible_exe_call(inventory))]
86+
calls.append(mock.call(cmd=ansible_sudo_wait_call(inventory)))
8587
calls.append(mock_call_ansibleplaybook(inventory, playbook[0]))
8688

8789
ret = cmd_ansible(data, tmpdir, False, False)

0 commit comments

Comments
 (0)