Skip to content
Merged
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: 10 additions & 0 deletions scripts/qesap/lib/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ def ansible_command_sequence(
ssh_share += ' --ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
ansible_cmd_seq.append({"cmd": ssh_share})

# This command is used to wait until user and sudo permissions are ready before running the playbooks
# this is needed due to GCP guest agent taking some time to set these up after VM creation
sudo_wait = (
f'{ansible_bin_paths["ansible"]} {ansible_common} all '
'-m shell '
"-a 'i=0; while [ $i -lt 35 ]; do sudo -n true && exit 0; sleep 5; i=$((i+1)); done; exit 1' "
'--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this line is needed. This one is to let keys/host to be accepted. It is only needed for first command. Not needed in this second one.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really like to avoid hardcoding the username (cloudadmin) here ....

)
ansible_cmd_seq.append({"cmd": sudo_wait})

selected_list_of_playbooks = []
if apiver < 4:
selected_list_of_playbooks = configure_data_ansible[sequence]
Expand Down
12 changes: 12 additions & 0 deletions scripts/qesap/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ def _callback(inventory):
return _callback


@pytest.fixture()
def ansible_sudo_wait_call():
def _callback(inventory):
return (
f"{ANSIBLE_EXE} -vv -i {inventory} all "
"-m shell "
"-a 'i=0; while [ $i -lt 35 ]; do sudo -n true && exit 0; sleep 5; i=$((i+1)); done; exit 1' "
'--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
)
return _callback


@pytest.fixture
def mock_call_ansibleplaybook():
"""
Expand Down
2 changes: 1 addition & 1 deletion scripts/qesap/test/e2e/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ count=$(grep -cE "terraform " "${THIS_LOG}")
[[ $count -eq 3 ]] || test_die "${THIS_LOG} there is not exactly 3 terraform lines but $count."

count=$(grep -cE "ansible " "${THIS_LOG}")
[[ $count -eq 1 ]] || test_die "${THIS_LOG} there is not exactly 1 ansible line but $count."
[[ $count -eq 2 ]] || test_die "${THIS_LOG} there are not exactly 2 ansible lines but $count."

count=$(grep -cE "ansible-playbook " "${THIS_LOG}")
[[ $count -eq 1 ]] || test_die "${THIS_LOG} there is not exactly 1 ansible-playbook line but $count."
Expand Down
4 changes: 3 additions & 1 deletion scripts/qesap/test/unit/test_qesap_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test_ansible_no_bin(


@mock.patch("shutil.which", side_effect=lambda x: fake_ansible_path(x))
@mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (1, [])])
@mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (0, []), (1, [])])
def test_ansible_stop(
run,
_,
Expand Down Expand Up @@ -538,6 +538,7 @@ def test_ansible_ssh(
ansible_config,
mock_call_ansibleplaybook,
ansible_exe_call,
ansible_sudo_wait_call,
):
"""
This first Ansible command has to be called before all the others
Expand Down Expand Up @@ -578,6 +579,7 @@ def test_ansible_ssh(
calls = []

calls.append(mock.call(cmd=ansible_exe_call(inventory)))
calls.append(mock.call(cmd=ansible_sudo_wait_call(inventory)))
for playbook in playbook_list:
calls.append(mock_call_ansibleplaybook(inventory, playbook))

Expand Down
2 changes: 2 additions & 0 deletions scripts/qesap/test/unit/test_qesap_lib_cmd_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_cmd_ansible(
create_playbooks,
create_inventory,
ansible_exe_call,
ansible_sudo_wait_call,
mock_call_ansibleplaybook,
):
"""
Expand Down Expand Up @@ -82,6 +83,7 @@ def test_cmd_ansible(
)

calls = [mock.call(cmd=ansible_exe_call(inventory))]
calls.append(mock.call(cmd=ansible_sudo_wait_call(inventory)))
calls.append(mock_call_ansibleplaybook(inventory, playbook[0]))

ret = cmd_ansible(data, tmpdir, False, False)
Expand Down