| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
""" |
| 21 |
ClusterShell Ssh/Scp support |
| 22 |
|
| 23 |
This module implements OpenSSH engine client and task's worker. |
| 24 |
""" |
| 25 |
|
| 26 |
import os |
| 27 |
|
| 28 |
|
| 29 |
import shlex |
| 30 |
|
| 31 |
from ClusterShell.Worker.Exec import ExecClient, CopyClient, ExecWorker |
| 32 |
|
| 33 |
|
| 34 |
class SshClient(ExecClient): |
| 35 |
""" |
| 36 |
Ssh EngineClient. |
| 37 |
""" |
| 38 |
|
| 39 |
def _build_cmd(self): |
| 40 |
""" |
| 41 |
Build the shell command line to start the ssh command. |
| 42 |
Return an array of command and arguments. |
| 43 |
""" |
| 44 |
|
| 45 |
task = self.worker.task |
| 46 |
path = task.info("ssh_path") or "ssh" |
| 47 |
user = task.info("ssh_user") |
| 48 |
options = task.info("ssh_options") |
| 49 |
|
| 50 |
|
| 51 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if options: |
| 56 |
|
| 57 |
cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)] |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
cmd_l += ["-oForwardAgent=no", "-oForwardX11=no"] |
| 62 |
|
| 63 |
if user: |
| 64 |
cmd_l.append("-l") |
| 65 |
cmd_l.append(user) |
| 66 |
|
| 67 |
connect_timeout = task.info("connect_timeout", 0) |
| 68 |
if connect_timeout > 0: |
| 69 |
cmd_l.append("-oConnectTimeout=%d" % connect_timeout) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
cmd_l.append("-oBatchMode=yes") |
| 75 |
|
| 76 |
cmd_l.append("%s" % self.key) |
| 77 |
cmd_l.append("%s" % self.command) |
| 78 |
|
| 79 |
return (cmd_l, None) |
| 80 |
|
| 81 |
class ScpClient(CopyClient): |
| 82 |
""" |
| 83 |
Scp EngineClient. |
| 84 |
""" |
| 85 |
|
| 86 |
def _build_cmd(self): |
| 87 |
""" |
| 88 |
Build the shell command line to start the scp command. |
| 89 |
Return an array of command and arguments. |
| 90 |
""" |
| 91 |
|
| 92 |
task = self.worker.task |
| 93 |
path = task.info("scp_path") or "scp" |
| 94 |
user = task.info("scp_user") or task.info("ssh_user") |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
options = task.info("scp_options") or task.info("ssh_options") |
| 99 |
|
| 100 |
|
| 101 |
cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
if options: |
| 106 |
|
| 107 |
cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)] |
| 108 |
|
| 109 |
|
| 110 |
if self.isdir: |
| 111 |
cmd_l.append("-r") |
| 112 |
|
| 113 |
if self.preserve: |
| 114 |
cmd_l.append("-p") |
| 115 |
|
| 116 |
connect_timeout = task.info("connect_timeout", 0) |
| 117 |
if connect_timeout > 0: |
| 118 |
cmd_l.append("-oConnectTimeout=%d" % connect_timeout) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if self.reverse: |
| 127 |
if user: |
| 128 |
cmd_l.append("%s@[%s]:%s" % (user, self.key, self.source)) |
| 129 |
else: |
| 130 |
cmd_l.append("[%s]:%s" % (self.key, self.source)) |
| 131 |
|
| 132 |
cmd_l.append(os.path.join(self.dest, "%s.%s" % \ |
| 133 |
(os.path.basename(self.source), self.key))) |
| 134 |
else: |
| 135 |
cmd_l.append(self.source) |
| 136 |
if user: |
| 137 |
cmd_l.append("%s@[%s]:%s" % (user, self.key, self.dest)) |
| 138 |
else: |
| 139 |
cmd_l.append("[%s]:%s" % (self.key, self.dest)) |
| 140 |
|
| 141 |
return (cmd_l, None) |
| 142 |
|
| 143 |
class WorkerSsh(ExecWorker): |
| 144 |
""" |
| 145 |
ClusterShell ssh-based worker Class. |
| 146 |
|
| 147 |
Remote Shell (ssh) usage example: |
| 148 |
>>> worker = WorkerSsh(nodeset, handler=MyEventHandler(), |
| 149 |
... timeout=30, command="/bin/hostname") |
| 150 |
>>> task.schedule(worker) # schedule worker for execution |
| 151 |
>>> task.resume() # run |
| 152 |
|
| 153 |
Remote Copy (scp) usage example: |
| 154 |
>>> worker = WorkerSsh(nodeset, handler=MyEventHandler(), |
| 155 |
... timeout=30, source="/etc/my.conf", |
| 156 |
... dest="/etc/my.conf") |
| 157 |
>>> task.schedule(worker) # schedule worker for execution |
| 158 |
>>> task.resume() # run |
| 159 |
""" |
| 160 |
|
| 161 |
SHELL_CLASS = SshClient |
| 162 |
COPY_CLASS = ScpClient |
| 163 |
|
| 164 |
WORKER_CLASS=WorkerSsh |