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
35 """
36 Ssh EngineClient.
37 """
38
40 """
41 Build the shell command line to start the ssh commmand.
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 cmd_l += [ "-a", "-x" ]
61
62 if user:
63 cmd_l.append("-l")
64 cmd_l.append(user)
65
66 connect_timeout = task.info("connect_timeout", 0)
67 if connect_timeout > 0:
68 cmd_l.append("-oConnectTimeout=%d" % connect_timeout)
69
70
71
72
73 cmd_l.append("-oBatchMode=yes")
74
75 cmd_l.append("%s" % self.key)
76 cmd_l.append("%s" % self.command)
77
78 return (cmd_l, None)
79
81 """
82 Scp EngineClient.
83 """
84
86 """
87 Build the shell command line to start the scp commmand.
88 Return an array of command and arguments.
89 """
90
91 task = self.worker.task
92 path = task.info("scp_path") or "scp"
93 user = task.info("scp_user") or task.info("ssh_user")
94
95
96
97 options = task.info("scp_options") or task.info("ssh_options")
98
99
100 cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)]
101
102
103
104 if options:
105
106 cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)]
107
108
109 if self.isdir:
110 cmd_l.append("-r")
111
112 if self.preserve:
113 cmd_l.append("-p")
114
115 connect_timeout = task.info("connect_timeout", 0)
116 if connect_timeout > 0:
117 cmd_l.append("-oConnectTimeout=%d" % connect_timeout)
118
119
120
121
122 cmd_l.append("-oBatchMode=yes")
123
124
125 if self.reverse:
126 if user:
127 cmd_l.append("%s@%s:%s" % (user, self.key, self.source))
128 else:
129 cmd_l.append("%s:%s" % (self.key, self.source))
130
131 cmd_l.append(os.path.join(self.dest, "%s.%s" % \
132 (os.path.basename(self.source), self.key)))
133 else:
134 cmd_l.append(self.source)
135 if user:
136 cmd_l.append("%s@%s:%s" % (user, self.key, self.dest))
137 else:
138 cmd_l.append("%s:%s" % (self.key, self.dest))
139
140 return (cmd_l, None)
141
143 """
144 ClusterShell ssh-based worker Class.
145
146 Remote Shell (ssh) usage example:
147 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(),
148 ... timeout=30, command="/bin/hostname")
149 >>> task.schedule(worker) # schedule worker for execution
150 >>> task.resume() # run
151
152 Remote Copy (scp) usage example:
153 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(),
154 ... timeout=30, source="/etc/my.conf",
155 ... dest="/etc/my.conf")
156 >>> task.schedule(worker) # schedule worker for execution
157 >>> task.resume() # run
158 """
159
160 SHELL_CLASS = SshClient
161 COPY_CLASS = ScpClient
162
163 WORKER_CLASS=WorkerSsh
164