From af0363864716208fbb136ecdcee7dd12679f1ef6 Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Fri, 15 Sep 2023 12:23:57 +0200 Subject: [PATCH 1/3] Use salt-call from the bundle with transactional_update --- salt/modules/transactional_update.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/salt/modules/transactional_update.py b/salt/modules/transactional_update.py index 6fff73e4e61d..bc377130bd9a 100644 --- a/salt/modules/transactional_update.py +++ b/salt/modules/transactional_update.py @@ -276,6 +276,9 @@ """ import logging +import os.path +import pathlib +import sys import salt.client.ssh.state import salt.client.ssh.wrapper.state @@ -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") + safe_kwargs = salt.utils.args.clean_kwargs(**kwargs) salt_argv = ( [ - "salt-call", + salt_call_cmd, "--out", "json", "-l", From 9983a1356950db47157a7619be86d7e84a13bad1 Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Fri, 15 Sep 2023 13:12:25 +0200 Subject: [PATCH 2/3] Add test checking which salt-call is selected by executable --- .../unit/modules/test_transactional_update.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/pytests/unit/modules/test_transactional_update.py b/tests/pytests/unit/modules/test_transactional_update.py index 5d9294c49bd8..dbd72fd74bf7 100644 --- a/tests/pytests/unit/modules/test_transactional_update.py +++ b/tests/pytests/unit/modules/test_transactional_update.py @@ -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", + ] + ) From f6e54bc3ec21545163aaeabdaf40e9ac11e55ea6 Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Mon, 18 Sep 2023 09:15:00 +0200 Subject: [PATCH 3/3] Add changelog entry --- changelog/65204.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/65204.fixed.md diff --git a/changelog/65204.fixed.md b/changelog/65204.fixed.md new file mode 100644 index 000000000000..b1540ac1d2e3 --- /dev/null +++ b/changelog/65204.fixed.md @@ -0,0 +1 @@ +Use salt-call from the salt bundle with transactional_update module on executing actions inside transactions.