From 2640ff9da9c8f12eb94226002f48801a1f91e38c Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Tue, 16 Apr 2024 17:05:35 +0200 Subject: [PATCH] Pass metadata, module_executors and executor_opts to ssh --- salt/client/ssh/__init__.py | 18 ++++++++++++++++++ salt/client/ssh/client.py | 5 ++++- salt/client/ssh/ssh_py_shim.py | 8 +++++++- salt/utils/parsers.py | 7 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py index 1d8426b7c2..6223d00f98 100644 --- a/salt/client/ssh/__init__.py +++ b/salt/client/ssh/__init__.py @@ -1084,6 +1084,7 @@ def __init__( self.argv = argv self.fun, self.args, self.kwargs = self.__arg_comps() + self.call_args = self.__get_call_args() self.id = id_ self.set_path = kwargs.get("set_path", "") @@ -1148,6 +1149,21 @@ def __arg_comps(self): kws = parsed[1] return fun, args, kws + def __get_call_args(self): + """ + Put extra arguments to the salt-call for the remote client. + """ + call_args = [] + args_list = ( + ("module_executors", "--module-executors"), + ("executor_opts", "--executor-opts"), + ("metadata", "--set-metadata"), + ) + for src, dst in args_list: + if src in self.opts: + call_args.append(f"{dst}={self.opts[src]}") + return call_args if call_args else None + def _escape_arg(self, arg): """ Properly escape argument to protect special characters from shell @@ -1509,6 +1525,7 @@ def _cmd_str(self): OPTIONS.tty = {tty} OPTIONS.cmd_umask = {cmd_umask} OPTIONS.code_checksum = {code_checksum} +CALL_ARGS = {call_args} ARGS = {arguments}\n'''.format( config=self.minion_config, delimeter=RSTR, @@ -1521,6 +1538,7 @@ def _cmd_str(self): tty=self.tty, cmd_umask=self.cmd_umask, code_checksum=thin_code_digest, + call_args=self.call_args, arguments=self.argv, ) py_code = SSH_PY_SHIM.replace("#%%OPTS", arg_str) diff --git a/salt/client/ssh/client.py b/salt/client/ssh/client.py index a00f5de423..3177cf312a 100644 --- a/salt/client/ssh/client.py +++ b/salt/client/ssh/client.py @@ -82,6 +82,9 @@ def sanitize_kwargs(self, kwargs): ("ssh_run_pre_flight", bool), ("no_host_keys", bool), ("saltfile", str), + ("module_executors", list), + ("executor_opts", dict), + ("metadata", dict), ] sane_kwargs = {} for name, kind in roster_vals: @@ -92,7 +95,7 @@ def sanitize_kwargs(self, kwargs): except ValueError: log.warning("Unable to cast kwarg %s", name) continue - if kind is bool or kind is int: + if kind in (bool, int, dict): sane_kwargs[name] = val elif kind is str: if val.find("ProxyCommand") != -1: diff --git a/salt/client/ssh/ssh_py_shim.py b/salt/client/ssh/ssh_py_shim.py index 95171f7aea..3b6c904bd4 100644 --- a/salt/client/ssh/ssh_py_shim.py +++ b/salt/client/ssh/ssh_py_shim.py @@ -37,6 +37,7 @@ class OptionsContainer: OPTIONS = OptionsContainer() +CALL_ARGS = None ARGS = None # The below line is where OPTIONS can be redefined with internal options # (rather than cli arguments) when the shim is bundled by @@ -284,13 +285,13 @@ def main(argv): # pylint: disable=W0613 # VIRTUAL_ENV environment variable is defined by venv-salt-minion wrapper # it's used to check if the shim is running under this wrapper venv_salt_call = None + cache_dir = os.path.join(OPTIONS.saltdir, "running_data", "var", "cache") if virt_env and "venv-salt-minion" in virt_env: venv_salt_call = os.path.join(virt_env, "bin", "salt-call") if not os.path.exists(venv_salt_call): venv_salt_call = None elif not os.path.exists(OPTIONS.saltdir): os.makedirs(OPTIONS.saltdir) - cache_dir = os.path.join(OPTIONS.saltdir, "running_data", "var", "cache") os.makedirs(os.path.join(cache_dir, "salt")) os.symlink( "salt", os.path.relpath(os.path.join(cache_dir, "venv-salt-minion")) @@ -401,6 +402,8 @@ def main(argv): # pylint: disable=W0613 "quiet", "-c", OPTIONS.saltdir, + "--cachedir", + cache_dir, ] try: @@ -409,6 +412,9 @@ def main(argv): # pylint: disable=W0613 except (IndexError, TypeError): pass + if CALL_ARGS and isinstance(CALL_ARGS, list): + salt_argv.extend(CALL_ARGS) + salt_argv.append("--") salt_argv.extend(argv_prepared) diff --git a/salt/utils/parsers.py b/salt/utils/parsers.py index 6c7f9f2f66..d7408446b9 100644 --- a/salt/utils/parsers.py +++ b/salt/utils/parsers.py @@ -3201,6 +3201,7 @@ def setup_config(self): class SaltSSHOptionParser( OptionParser, ConfigDirMixIn, + ExecutorsMixIn, MergeConfigMixIn, LogLevelMixIn, TargetOptionsMixIn, @@ -3369,6 +3370,12 @@ def _mixin_setup(self): dest="ssh_run_pre_flight", help="Run the defined ssh_pre_flight script in the roster", ) + self.add_option( + "--metadata", + default="", + metavar="METADATA", + help="Pass metadata into Salt, used to search jobs.", + ) ssh_group = optparse.OptionGroup( self, "SSH Options", "Parameters for the SSH client."