| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
""" |
| 21 |
ClusterShell RSH support |
| 22 |
|
| 23 |
It could also handles rsh forks, like krsh or mrsh. |
| 24 |
This is also the base class for rsh evolutions, like Ssh worker. |
| 25 |
""" |
| 26 |
|
| 27 |
import os |
| 28 |
import shlex |
| 29 |
import re |
| 30 |
|
| 31 |
from ClusterShell.Worker.Exec import ExecClient, CopyClient, ExecWorker |
| 32 |
|
| 33 |
|
| 34 |
class RshClient(ExecClient): |
| 35 |
""" |
| 36 |
Rsh EngineClient. |
| 37 |
""" |
| 38 |
|
| 39 |
def __init__(self, node, command, worker, stderr, timeout, autoclose=False, |
| 40 |
rank=None): |
| 41 |
ExecClient.__init__(self, node, command, worker, stderr, timeout, |
| 42 |
autoclose, rank) |
| 43 |
self.rsh_rc = None |
| 44 |
|
| 45 |
def _build_cmd(self): |
| 46 |
""" |
| 47 |
Build the shell command line to start the rsh command. |
| 48 |
Return an array of command and arguments. |
| 49 |
""" |
| 50 |
|
| 51 |
task = self.worker.task |
| 52 |
path = task.info("rsh_path") or "rsh" |
| 53 |
user = task.info("rsh_user") |
| 54 |
options = task.info("rsh_options") |
| 55 |
|
| 56 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 57 |
|
| 58 |
if user: |
| 59 |
cmd_l.append("-l") |
| 60 |
cmd_l.append(user) |
| 61 |
|
| 62 |
|
| 63 |
if options: |
| 64 |
cmd_l += shlex.split(options) |
| 65 |
|
| 66 |
cmd_l.append("%s" % self.key) |
| 67 |
cmd_l.append("%s" % self.command) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
cmd_l.append("; echo XXRETCODE: $?") |
| 72 |
|
| 73 |
return (cmd_l, None) |
| 74 |
|
| 75 |
def _on_nodeset_msgline(self, nodes, msg, sname): |
| 76 |
"""Override _on_nodeset_msgline to parse magic return code""" |
| 77 |
match = re.search(r"^XXRETCODE: (\d+)$", msg.decode()) |
| 78 |
if match: |
| 79 |
self.rsh_rc = int(match.group(1)) |
| 80 |
else: |
| 81 |
ExecClient._on_nodeset_msgline(self, nodes, msg, sname) |
| 82 |
|
| 83 |
def _on_nodeset_close(self, nodes, rc): |
| 84 |
"""Override _on_nodeset_close to return rsh_rc""" |
| 85 |
if (rc == 0 or rc == 1) and self.rsh_rc is not None: |
| 86 |
rc = self.rsh_rc |
| 87 |
ExecClient._on_nodeset_close(self, nodes, rc) |
| 88 |
|
| 89 |
|
| 90 |
class RcpClient(CopyClient): |
| 91 |
""" |
| 92 |
Rcp EngineClient. |
| 93 |
""" |
| 94 |
|
| 95 |
def _build_cmd(self): |
| 96 |
""" |
| 97 |
Build the shell command line to start the rcp command. |
| 98 |
Return an array of command and arguments. |
| 99 |
""" |
| 100 |
|
| 101 |
|
| 102 |
task = self.worker.task |
| 103 |
path = task.info("rcp_path") or "rcp" |
| 104 |
user = task.info("rsh_user") |
| 105 |
options = task.info("rcp_options") or task.info("rsh_options") |
| 106 |
|
| 107 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 108 |
|
| 109 |
if self.isdir: |
| 110 |
cmd_l.append("-r") |
| 111 |
|
| 112 |
if self.preserve: |
| 113 |
cmd_l.append("-p") |
| 114 |
|
| 115 |
|
| 116 |
if options: |
| 117 |
cmd_l += shlex.split(options) |
| 118 |
|
| 119 |
if self.reverse: |
| 120 |
if user: |
| 121 |
cmd_l.append("%s@%s:%s" % (user, self.key, self.source)) |
| 122 |
else: |
| 123 |
cmd_l.append("%s:%s" % (self.key, self.source)) |
| 124 |
|
| 125 |
cmd_l.append(os.path.join(self.dest, "%s.%s" % \ |
| 126 |
(os.path.basename(self.source), self.key))) |
| 127 |
else: |
| 128 |
cmd_l.append(self.source) |
| 129 |
if user: |
| 130 |
cmd_l.append("%s@%s:%s" % (user, self.key, self.dest)) |
| 131 |
else: |
| 132 |
cmd_l.append("%s:%s" % (self.key, self.dest)) |
| 133 |
|
| 134 |
return (cmd_l, None) |
| 135 |
|
| 136 |
|
| 137 |
class WorkerRsh(ExecWorker): |
| 138 |
""" |
| 139 |
ClusterShell rsh-based worker Class. |
| 140 |
|
| 141 |
Remote Shell (rsh) usage example: |
| 142 |
>>> worker = WorkerRsh(nodeset, handler=MyEventHandler(), |
| 143 |
... timeout=30, command="/bin/hostname") |
| 144 |
>>> task.schedule(worker) # schedule worker for execution |
| 145 |
>>> task.resume() # run |
| 146 |
|
| 147 |
Remote Copy (rcp) usage example: |
| 148 |
>>> worker = WorkerRsh(nodeset, handler=MyEventHandler(), |
| 149 |
... source="/etc/my.conf", |
| 150 |
... dest="/etc/my.conf") |
| 151 |
>>> task.schedule(worker) # schedule worker for execution |
| 152 |
>>> task.resume() # run |
| 153 |
|
| 154 |
connect_timeout option is ignored by this worker. |
| 155 |
""" |
| 156 |
|
| 157 |
SHELL_CLASS = RshClient |
| 158 |
COPY_CLASS = RcpClient |
| 159 |
|
| 160 |
WORKER_CLASS=WorkerRsh |