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

[master] Use salt-call from salt bundle with transactional_update #65204

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog/65204.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use salt-call from the salt bundle with transactional_update module on executing actions inside transactions.
13 changes: 12 additions & 1 deletion salt/modules/transactional_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@
"""

import logging
import os.path
import pathlib
import sys

import salt.client.ssh.state
import salt.client.ssh.wrapper.state
Expand Down Expand Up @@ -941,10 +944,18 @@ def call(function, *args, **kwargs):
activate_transaction = kwargs.pop("activate_transaction", False)

try:
# Set default salt-call command
salt_call_cmd = "salt-call"
python_exec_dir = os.path.dirname(sys.executable)
if "venv-salt-minion" in pathlib.Path(python_exec_dir).parts:
# If the module is executed with the Salt Bundle,
# use salt-call from the Salt Bundle
salt_call_cmd = os.path.join(python_exec_dir, "salt-call")
Comment on lines +947 to +953
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it needed to do the path juggling inside Python code, can't we prepend the "Salt Bundle" bin/ directory to $PATH?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can do it with PATH outside, but actually I think to do it this way is more raliable as it's more strict and in case of handling transactional_update with salt-ssh it will push us to make the external solution more conditional


safe_kwargs = salt.utils.args.clean_kwargs(**kwargs)
salt_argv = (
[
"salt-call",
salt_call_cmd,
"--out",
"json",
"-l",
Expand Down
44 changes: 44 additions & 0 deletions tests/pytests/unit/modules/test_transactional_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,47 @@ def test_single_queue_true():
"salt.modules.transactional_update.call", MagicMock(return_value="result")
):
assert tu.single("pkg.installed", name="emacs", queue=True) == "result"


@pytest.mark.parametrize(
"executable,salt_call_cmd",
[
("/usr/bin/python3", "salt-call"),
(
"/usr/lib/venv-salt-minion/bin/python",
"/usr/lib/venv-salt-minion/bin/salt-call",
),
],
)
def test_call_which_salt_call_selected_with_executable(executable, salt_call_cmd):
"""Test transactional_update.chroot which salt-call used"""
utils_mock = {
"json.find_json": MagicMock(return_value={"return": "result"}),
}
salt_mock = {
"cmd.run_all": MagicMock(return_value={"retcode": 0, "stdout": ""}),
}
with patch("sys.executable", executable), patch.dict(
tu.__utils__, utils_mock
), patch.dict(tu.__salt__, salt_mock):
assert tu.call("test.ping") == "result"

salt_mock["cmd.run_all"].assert_called_with(
[
"transactional-update",
"--non-interactive",
"--drop-if-no-change",
"--no-selfupdate",
"--continue",
"--quiet",
"run",
salt_call_cmd,
"--out",
"json",
"-l",
"quiet",
"--no-return-event",
"--",
"test.ping",
]
)
Loading