Skip to content

Worker/Ssh: support %h %n variables #462

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

Open
wants to merge 5 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
50 changes: 25 additions & 25 deletions lib/ClusterShell/Worker/Exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 command.
Expand All @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions lib/ClusterShell/Worker/Ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down