From 7f3725ca8f5bb0efcc4d50df327646d757cd6465 Mon Sep 17 00:00:00 2001 From: valuefish Date: Wed, 18 Aug 2021 19:51:54 +0800 Subject: [PATCH] Worker/Ssh: support %h %n variables --- lib/ClusterShell/Worker/Exec.py | 50 ++++++++++++++++----------------- lib/ClusterShell/Worker/Ssh.py | 1 + 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/ClusterShell/Worker/Exec.py b/lib/ClusterShell/Worker/Exec.py index e1ac7d35..ca104a1d 100644 --- a/lib/ClusterShell/Worker/Exec.py +++ b/lib/ClusterShell/Worker/Exec.py @@ -38,30 +38,6 @@ from ClusterShell.Worker.Worker import _eh_sigspec_invoke_compat -def _replace_cmd(pattern, node, rank): - """ - Replace keywords in `pattern' with value from `node' and `rank'. - - %h, %host map `node' - %n, %rank map `rank' - """ - variables = { - 'h': node, - 'host': node, - 'hosts': node, - 'n': rank or 0, - 'rank': rank or 0, - # 'u': None, - } - class Replacer(Template): - delimiter = '%' - try: - cmd = Replacer(pattern).substitute(variables) - except (KeyError, ValueError) as error: - msg = "%s is not a valid pattern, use '%%%%' to escape '%%'" % error - raise WorkerError(msg) - return cmd - class ExecClient(EngineClient): """ Run a simple local command. @@ -83,6 +59,30 @@ def __init__(self, node, command, worker, stderr, timeout, autoclose=False, # Declare writer stream to allow early buffering self.streams.set_writer(worker.SNAME_STDIN, None, retain=True) + def _replace_cmd(self): + """ + Replace keywords in `pattern' with value from `node' and `rank'. + + %h, %host map `node' + %n, %rank map `rank' + """ + variables = { + 'h': self.key, + 'host': self.key, + 'hosts': self.key, + 'n': self.rank or 0, + 'rank': self.rank or 0, + # 'u': None, + } + class Replacer(Template): + delimiter = '%' + try: + cmd = Replacer(self.command).substitute(variables) + except (KeyError, ValueError) as error: + msg = "%s is not a valid pattern, use '%%%%' to escape '%%'" % error + raise WorkerError(msg) + return cmd + def _build_cmd(self): """ Build the shell command line to start the commmand. @@ -91,7 +91,7 @@ def _build_cmd(self): of string, and a dict of additional environment variables. None could be returned if no environment change is required. """ - return (_replace_cmd(self.command, self.key, self.rank), None) + return (self._replace_cmd(), None) def _start(self): """Prepare command and start client.""" diff --git a/lib/ClusterShell/Worker/Ssh.py b/lib/ClusterShell/Worker/Ssh.py index c7a6d505..c991bdfd 100644 --- a/lib/ClusterShell/Worker/Ssh.py +++ b/lib/ClusterShell/Worker/Ssh.py @@ -42,6 +42,7 @@ def _build_cmd(self): Return an array of command and arguments. """ + self.command = self._replace_cmd() task = self.worker.task path = task.info("ssh_path") or "ssh" user = task.info("ssh_user")