From 10efac43d1583aa237ec8999fddb4b666e62323f Mon Sep 17 00:00:00 2001 From: Barney Sowood Date: Fri, 18 Aug 2023 13:39:44 +0100 Subject: [PATCH 1/3] Add support for show_jid to salt-run Adds support for show_jid to the salt-run cli command to match the behaviour of the salt cli command. --- salt/cli/run.py | 3 +- salt/runner.py | 5 ++- .../functional/cli/test_salt_run_show_jid.py | 42 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/pytests/functional/cli/test_salt_run_show_jid.py diff --git a/salt/cli/run.py b/salt/cli/run.py index fba70fffbecc..eac0cd54031a 100644 --- a/salt/cli/run.py +++ b/salt/cli/run.py @@ -30,8 +30,9 @@ def run(self): try: if check_user(self.config["user"]): pr = salt.utils.profile.activate_profile(profiling_enabled) + show_jid = self.config.get("show_jid", False) try: - ret = runner.run(full_return=True) + ret = runner.run(full_return=True, show_jid=show_jid) # In older versions ret['data']['retcode'] was used # for signaling the return code. This has been # changed for the orchestrate runner, but external diff --git a/salt/runner.py b/salt/runner.py index d3501b8f9190..2ab0c075fd95 100644 --- a/salt/runner.py +++ b/salt/runner.py @@ -207,7 +207,7 @@ def print_docs(self): print(docs[fun]) # TODO: move to mixin whenever we want a salt-wheel cli - def run(self, full_return=False): + def run(self, full_return=False, show_jid=False): """ Execute the runner sequence """ @@ -288,6 +288,9 @@ def run(self, full_return=False): return async_pub["jid"] # return the jid # otherwise run it in the main process + if show_jid: + print(f"jid: {self.jid}") + if self.opts.get("eauth"): ret = self.cmd_sync(low) if isinstance(ret, dict) and set(ret) == {"data", "outputter"}: diff --git a/tests/pytests/functional/cli/test_salt_run_show_jid.py b/tests/pytests/functional/cli/test_salt_run_show_jid.py new file mode 100644 index 000000000000..3fdfe9fb91ae --- /dev/null +++ b/tests/pytests/functional/cli/test_salt_run_show_jid.py @@ -0,0 +1,42 @@ +""" +Tests for salt-run with show_jid +""" + +import logging +import re + +import pytest + +log = logging.getLogger(__name__) + + +@pytest.fixture(scope="module") +def salt_master(salt_factories): + """ + Salt master with `show_jid: True` + """ + config_defaults = { + "show_jid": True, + } + salt_master = salt_factories.salt_master_daemon( + "salt-run-show-jid-master", defaults=config_defaults + ) + with salt_master.started(): + yield salt_master + + +@pytest.fixture(scope="module") +def salt_run_cli(salt_master): + """ + The ``salt-run`` CLI as a fixture against the running master + """ + assert salt_master.is_running() + return salt_master.salt_run_cli(timeout=30) + + +def test_salt_run_show_jid(salt_run_cli): + """ + Test that jid is output + """ + ret = salt_run_cli.run("test.stdout_print") + assert re.match(r"jid: \d+", ret.stdout) From 04670b5870b15226fbb8a0040fa9f6329a1f5566 Mon Sep 17 00:00:00 2001 From: Barney Sowood Date: Fri, 18 Aug 2023 13:53:00 +0100 Subject: [PATCH 2/3] Add changelog --- changelog/65008.added.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/65008.added.md diff --git a/changelog/65008.added.md b/changelog/65008.added.md new file mode 100644 index 000000000000..2e8b5adec5e1 --- /dev/null +++ b/changelog/65008.added.md @@ -0,0 +1,3 @@ +Add support for show_jid to salt-run + +Adds support for show_jid master config option to salt-run, so its behaviour matches the salt cli command. From 7dd8961633796d4be6405705f81f448046579f4b Mon Sep 17 00:00:00 2001 From: Barney Sowood Date: Sun, 8 Oct 2023 14:06:05 +0100 Subject: [PATCH 3/3] Simplify to just use opt in runner Simplify to just use opts in runner to get show_jid. Had thought I'd need to do that so not to print when called via python api or netapi, but doesn't seem to be an issue. --- salt/cli/run.py | 3 +-- salt/runner.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/salt/cli/run.py b/salt/cli/run.py index eac0cd54031a..fba70fffbecc 100644 --- a/salt/cli/run.py +++ b/salt/cli/run.py @@ -30,9 +30,8 @@ def run(self): try: if check_user(self.config["user"]): pr = salt.utils.profile.activate_profile(profiling_enabled) - show_jid = self.config.get("show_jid", False) try: - ret = runner.run(full_return=True, show_jid=show_jid) + ret = runner.run(full_return=True) # In older versions ret['data']['retcode'] was used # for signaling the return code. This has been # changed for the orchestrate runner, but external diff --git a/salt/runner.py b/salt/runner.py index 2ab0c075fd95..d606cb485104 100644 --- a/salt/runner.py +++ b/salt/runner.py @@ -207,7 +207,7 @@ def print_docs(self): print(docs[fun]) # TODO: move to mixin whenever we want a salt-wheel cli - def run(self, full_return=False, show_jid=False): + def run(self, full_return=False): """ Execute the runner sequence """ @@ -288,7 +288,7 @@ def run(self, full_return=False, show_jid=False): return async_pub["jid"] # return the jid # otherwise run it in the main process - if show_jid: + if self.opts.get("show_jid"): print(f"jid: {self.jid}") if self.opts.get("eauth"):