author | wenzelm |
Fri, 02 Mar 2018 18:45:11 +0100 | |
changeset 67745 | d83efbe52438 |
parent 67273 | c573cfb2c407 |
child 67770 | 25f3a278df3d |
permissions | -rw-r--r-- |
64123 | 1 |
/* Title: Pure/General/ssh.scala |
2 |
Author: Makarius |
|
3 |
||
64124 | 4 |
SSH client based on JSch (see also http://www.jcraft.com/jsch/examples). |
64123 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
10 |
import java.io.{InputStream, OutputStream, ByteArrayOutputStream} |
64131 | 11 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
12 |
import scala.collection.{mutable, JavaConversions} |
64131 | 13 |
|
64222 | 14 |
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session, SftpException, |
64132 | 15 |
OpenSSHConfig, UserInfo, Channel => JSch_Channel, ChannelExec, ChannelSftp, SftpATTRS} |
64123 | 16 |
|
17 |
||
18 |
object SSH |
|
19 |
{ |
|
64185 | 20 |
/* target machine: user@host syntax */ |
64141 | 21 |
|
64185 | 22 |
object Target |
64141 | 23 |
{ |
24 |
val Pattern = "^([^@]+)@(.+)$".r |
|
25 |
||
26 |
def parse(s: String): (String, String) = |
|
27 |
s match { |
|
28 |
case Pattern(user, host) => (user, host) |
|
29 |
case _ => ("", s) |
|
30 |
} |
|
31 |
||
32 |
def unapplySeq(s: String): Option[List[String]] = |
|
64185 | 33 |
parse(s) match { |
34 |
case (_, "") => None |
|
35 |
case (user, host) => Some(List(user, host)) |
|
36 |
} |
|
64141 | 37 |
} |
38 |
||
64142 | 39 |
val default_port = 22 |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
40 |
def make_port(port: Int): Int = if (port > 0) port else default_port |
64142 | 41 |
|
64257 | 42 |
def connect_timeout(options: Options): Int = |
43 |
options.seconds("ssh_connect_timeout").ms.toInt |
|
64141 | 44 |
|
64325 | 45 |
def alive_interval(options: Options): Int = |
46 |
options.seconds("ssh_alive_interval").ms.toInt |
|
47 |
||
67273
c573cfb2c407
more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents:
67067
diff
changeset
|
48 |
def alive_count_max(options: Options): Int = |
c573cfb2c407
more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents:
67067
diff
changeset
|
49 |
options.int("ssh_alive_count_max") |
c573cfb2c407
more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents:
67067
diff
changeset
|
50 |
|
64123 | 51 |
|
64257 | 52 |
/* init context */ |
53 |
||
54 |
def init_context(options: Options): Context = |
|
64123 | 55 |
{ |
64130 | 56 |
val config_dir = Path.explode(options.string("ssh_config_dir")) |
64123 | 57 |
if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir) |
58 |
||
59 |
val jsch = new JSch |
|
60 |
||
64130 | 61 |
val config_file = Path.explode(options.string("ssh_config_file")) |
64123 | 62 |
if (config_file.is_file) |
63 |
jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file))) |
|
64 |
||
65 |
val known_hosts = config_dir + Path.explode("known_hosts") |
|
66 |
if (!known_hosts.is_file) known_hosts.file.createNewFile |
|
67 |
jsch.setKnownHosts(File.platform_path(known_hosts)) |
|
68 |
||
64130 | 69 |
val identity_files = |
66923 | 70 |
space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_)) |
64123 | 71 |
for (identity_file <- identity_files if identity_file.is_file) |
72 |
jsch.addIdentity(File.platform_path(identity_file)) |
|
73 |
||
64257 | 74 |
new Context(options, jsch) |
64123 | 75 |
} |
76 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
77 |
def open_session(options: Options, host: String, user: String = "", port: Int = 0, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
78 |
proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0): Session = |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
79 |
init_context(options).open_session(host = host, user = user, port = port, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
80 |
proxy_host = proxy_host, proxy_user = proxy_user, proxy_port = proxy_port) |
67067 | 81 |
|
64257 | 82 |
class Context private[SSH](val options: Options, val jsch: JSch) |
83 |
{ |
|
84 |
def update_options(new_options: Options): Context = new Context(new_options, jsch) |
|
85 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
86 |
def connect_session(host: String, user: String = "", port: Int = 0, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
87 |
host_key_alias: String = "", on_close: () => Unit = () => ()): Session = |
64257 | 88 |
{ |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
89 |
val session = jsch.getSession(proper_string(user) getOrElse null, host, make_port(port)) |
64257 | 90 |
|
91 |
session.setUserInfo(No_User_Info) |
|
64325 | 92 |
session.setServerAliveInterval(alive_interval(options)) |
67273
c573cfb2c407
more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents:
67067
diff
changeset
|
93 |
session.setServerAliveCountMax(alive_count_max(options)) |
64257 | 94 |
session.setConfig("MaxAuthTries", "3") |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
95 |
if (host_key_alias != "") session.setHostKeyAlias(host_key_alias) |
64257 | 96 |
|
97 |
if (options.bool("ssh_compression")) { |
|
98 |
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none") |
|
99 |
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none") |
|
100 |
session.setConfig("compression_level", "9") |
|
101 |
} |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
102 |
session.connect(connect_timeout(options)) |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
103 |
new Session(options, session, on_close) |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
104 |
} |
64257 | 105 |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
106 |
def open_session(host: String, user: String = "", port: Int = 0, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
107 |
proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0): Session = |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
108 |
{ |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
109 |
if (proxy_host == "") connect_session(host = host, user = user, port = port) |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
110 |
else { |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
111 |
val proxy = connect_session(host = proxy_host, port = proxy_port, user = proxy_user) |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
112 |
|
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
113 |
val fw = |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
114 |
try { proxy.port_forwarding(remote_host = host, remote_port = make_port(port)) } |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
115 |
catch { case exn: Throwable => proxy.close; throw exn } |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
116 |
|
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
117 |
try { |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
118 |
connect_session(host = fw.local_host, port = fw.local_port, host_key_alias = host, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
119 |
user = user, on_close = () => { fw.close; proxy.close }) |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
120 |
} |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
121 |
catch { case exn: Throwable => fw.close; proxy.close; throw exn } |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
122 |
} |
64257 | 123 |
} |
124 |
} |
|
64130 | 125 |
|
64123 | 126 |
|
127 |
/* logging */ |
|
128 |
||
129 |
def logging(verbose: Boolean = true, debug: Boolean = false) |
|
130 |
{ |
|
131 |
JSch.setLogger(if (verbose) new Logger(debug) else null) |
|
132 |
} |
|
133 |
||
134 |
private class Logger(debug: Boolean) extends JSch_Logger |
|
135 |
{ |
|
136 |
def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug |
|
137 |
||
138 |
def log(level: Int, msg: String) |
|
139 |
{ |
|
140 |
level match { |
|
141 |
case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg) |
|
142 |
case JSch_Logger.WARN => Output.warning(msg) |
|
143 |
case _ => Output.writeln(msg) |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
||
64128 | 149 |
/* user info */ |
150 |
||
151 |
object No_User_Info extends UserInfo |
|
152 |
{ |
|
153 |
def getPassphrase: String = null |
|
154 |
def getPassword: String = null |
|
155 |
def promptPassword(msg: String): Boolean = false |
|
156 |
def promptPassphrase(msg: String): Boolean = false |
|
157 |
def promptYesNo(msg: String): Boolean = false |
|
158 |
def showMessage(msg: String): Unit = Output.writeln(msg) |
|
159 |
} |
|
160 |
||
161 |
||
65009 | 162 |
/* port forwarding */ |
163 |
||
65010 | 164 |
object Port_Forwarding |
65009 | 165 |
{ |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
166 |
def open(ssh: Session, ssh_close: Boolean, |
65010 | 167 |
local_host: String, local_port: Int, remote_host: String, remote_port: Int): Port_Forwarding = |
168 |
{ |
|
169 |
val port = ssh.session.setPortForwardingL(local_host, local_port, remote_host, remote_port) |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
170 |
new Port_Forwarding(ssh, ssh_close, local_host, port, remote_host, remote_port) |
65010 | 171 |
} |
172 |
} |
|
173 |
||
174 |
class Port_Forwarding private[SSH]( |
|
175 |
ssh: SSH.Session, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
176 |
ssh_close: Boolean, |
65010 | 177 |
val local_host: String, |
178 |
val local_port: Int, |
|
179 |
val remote_host: String, |
|
180 |
val remote_port: Int) |
|
181 |
{ |
|
182 |
override def toString: String = |
|
183 |
local_host + ":" + local_port + ":" + remote_host + ":" + remote_port |
|
184 |
||
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
185 |
def close() |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
186 |
{ |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
187 |
ssh.session.delPortForwardingL(local_host, local_port) |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
188 |
if (ssh_close) ssh.close() |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
189 |
} |
65009 | 190 |
} |
191 |
||
192 |
||
64191 | 193 |
/* Sftp channel */ |
194 |
||
195 |
type Attrs = SftpATTRS |
|
196 |
||
64233 | 197 |
sealed case class Dir_Entry(name: Path, attrs: Attrs) |
64191 | 198 |
{ |
199 |
def is_file: Boolean = attrs.isReg |
|
200 |
def is_dir: Boolean = attrs.isDir |
|
201 |
} |
|
202 |
||
203 |
||
64132 | 204 |
/* exec channel */ |
205 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
206 |
private val exec_wait_delay = Time.seconds(0.3) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
207 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
208 |
class Exec private[SSH](session: Session, channel: ChannelExec) |
64131 | 209 |
{ |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
210 |
override def toString: String = "exec " + session.toString |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
211 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
212 |
def close() { channel.disconnect } |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
213 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
214 |
val exit_status: Future[Int] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
215 |
Future.thread("ssh_wait") { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
216 |
while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
217 |
channel.getExitStatus |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
218 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
219 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
220 |
val stdin: OutputStream = channel.getOutputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
221 |
val stdout: InputStream = channel.getInputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
222 |
val stderr: InputStream = channel.getErrStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
223 |
|
64166 | 224 |
// connect after preparing streams |
225 |
channel.connect(connect_timeout(session.options)) |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
226 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
227 |
def result( |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
228 |
progress_stdout: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
229 |
progress_stderr: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
230 |
strict: Boolean = true): Process_Result = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
231 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
232 |
stdin.close |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
233 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
234 |
def read_lines(stream: InputStream, progress: String => Unit): List[String] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
235 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
236 |
val result = new mutable.ListBuffer[String] |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
237 |
val line_buffer = new ByteArrayOutputStream(100) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
238 |
def line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
239 |
{ |
64326 | 240 |
val line = Library.trim_line(line_buffer.toString(UTF8.charset_name)) |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
241 |
progress(line) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
242 |
result += line |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
243 |
line_buffer.reset |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
244 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
245 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
246 |
var c = 0 |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
247 |
var finished = false |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
248 |
while (!finished) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
249 |
while ({ c = stream.read; c != -1 && c != 10 }) line_buffer.write(c) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
250 |
if (c == 10) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
251 |
else if (channel.isClosed) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
252 |
if (line_buffer.size > 0) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
253 |
finished = true |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
254 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
255 |
else Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
256 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
257 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
258 |
result.toList |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
259 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
260 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
261 |
val out_lines = Future.thread("ssh_stdout") { read_lines(stdout, progress_stdout) } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
262 |
val err_lines = Future.thread("ssh_stderr") { read_lines(stderr, progress_stderr) } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
263 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
264 |
def terminate() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
265 |
{ |
64136 | 266 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
267 |
out_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
268 |
err_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
269 |
exit_status.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
270 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
271 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
272 |
val rc = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
273 |
try { exit_status.join } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
274 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
275 |
|
64136 | 276 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
277 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
278 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
279 |
Process_Result(rc, out_lines.join, err_lines.join) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
280 |
} |
64131 | 281 |
} |
282 |
||
283 |
||
64123 | 284 |
/* session */ |
285 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
286 |
class Session private[SSH]( |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
287 |
val options: Options, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
288 |
val session: JSch_Session, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
289 |
on_close: () => Unit) extends System |
64123 | 290 |
{ |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
291 |
def update_options(new_options: Options): Session = |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
292 |
new Session(new_options, session, on_close) |
64166 | 293 |
|
64347 | 294 |
def user_prefix: String = if (session.getUserName == null) "" else session.getUserName + "@" |
295 |
def host: String = if (session.getHost == null) "" else session.getHost |
|
296 |
def port_suffix: String = if (session.getPort == default_port) "" else ":" + session.getPort |
|
66570 | 297 |
override def hg_url: String = "ssh://" + user_prefix + host + port_suffix + "/" |
298 |
||
299 |
override def prefix: String = |
|
300 |
user_prefix + host + port_suffix + ":" |
|
64347 | 301 |
|
64123 | 302 |
override def toString: String = |
64347 | 303 |
user_prefix + host + port_suffix + (if (session.isConnected) "" else " (disconnected)") |
64123 | 304 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
305 |
|
65009 | 306 |
/* port forwarding */ |
307 |
||
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
308 |
def port_forwarding( |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
309 |
remote_port: Int, remote_host: String = "localhost", |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
310 |
local_port: Int = 0, local_host: String = "localhost", |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
311 |
ssh_close: Boolean = false): Port_Forwarding = |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
312 |
Port_Forwarding.open(this, ssh_close, local_host, local_port, remote_host, remote_port) |
65009 | 313 |
|
314 |
||
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
315 |
/* sftp channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
316 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
317 |
val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp] |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
318 |
sftp.connect(connect_timeout(options)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
319 |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
320 |
def close() { sftp.disconnect; session.disconnect; on_close() } |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
321 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
322 |
val settings: Map[String, String] = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
323 |
{ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
324 |
val home = sftp.getHome |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
325 |
Map("HOME" -> home, "USER_HOME" -> home) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
326 |
} |
66570 | 327 |
override def expand_path(path: Path): Path = path.expand_env(settings) |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
328 |
def remote_path(path: Path): String = expand_path(path).implode |
67066 | 329 |
override def bash_path(path: Path): String = Bash.string(remote_path(path)) |
64123 | 330 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
331 |
def chmod(permissions: Int, path: Path): Unit = sftp.chmod(permissions, remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
332 |
def mv(path1: Path, path2: Path): Unit = sftp.rename(remote_path(path1), remote_path(path2)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
333 |
def rm(path: Path): Unit = sftp.rm(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
334 |
def mkdir(path: Path): Unit = sftp.mkdir(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
335 |
def rmdir(path: Path): Unit = sftp.rmdir(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
336 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
337 |
def stat(path: Path): Option[Dir_Entry] = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
338 |
try { Some(Dir_Entry(expand_path(path), sftp.stat(remote_path(path)))) } |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
339 |
catch { case _: SftpException => None } |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
340 |
|
66570 | 341 |
override def is_file(path: Path): Boolean = stat(path).map(_.is_file) getOrElse false |
342 |
override def is_dir(path: Path): Boolean = stat(path).map(_.is_dir) getOrElse false |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
343 |
|
66570 | 344 |
override def mkdirs(path: Path): Unit = |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
345 |
if (!is_dir(path)) { |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
346 |
execute( |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
347 |
"perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"") |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
348 |
if (!is_dir(path)) error("Failed to create directory: " + quote(remote_path(path))) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
349 |
} |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
350 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
351 |
def read_dir(path: Path): List[Dir_Entry] = |
64191 | 352 |
{ |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
353 |
val dir = sftp.ls(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
354 |
(for { |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
355 |
i <- (0 until dir.size).iterator |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
356 |
a = dir.get(i).asInstanceOf[AnyRef] |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
357 |
name = Untyped.get[String](a, "filename") |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
358 |
attrs = Untyped.get[Attrs](a, "attrs") |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
359 |
if name != "." && name != ".." |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
360 |
} yield Dir_Entry(Path.basic(name), attrs)).toList |
64191 | 361 |
} |
64135
865dda40e1cc
provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents:
64134
diff
changeset
|
362 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
363 |
def find_files(root: Path, pred: Dir_Entry => Boolean = _ => true): List[Dir_Entry] = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
364 |
{ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
365 |
def find(dir: Path): List[Dir_Entry] = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
366 |
read_dir(dir).flatMap(entry => |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
367 |
{ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
368 |
val file = dir + entry.name |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
369 |
if (entry.is_dir) find(file) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
370 |
else if (pred(entry)) List(entry.copy(name = file)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
371 |
else Nil |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
372 |
}) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
373 |
find(root) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
374 |
} |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
375 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
376 |
def open_input(path: Path): InputStream = sftp.get(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
377 |
def open_output(path: Path): OutputStream = sftp.put(remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
378 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
379 |
def read_file(path: Path, local_path: Path): Unit = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
380 |
sftp.get(remote_path(path), File.platform_path(local_path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
381 |
def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
382 |
def read(path: Path): String = using(open_input(path))(File.read_stream(_)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
383 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
384 |
def write_file(path: Path, local_path: Path): Unit = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
385 |
sftp.put(File.platform_path(local_path), remote_path(path)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
386 |
def write_bytes(path: Path, bytes: Bytes): Unit = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
387 |
using(open_output(path))(bytes.write_stream(_)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
388 |
def write(path: Path, text: String): Unit = |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
389 |
using(open_output(path))(stream => Bytes(text).write_stream(stream)) |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
390 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
391 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
392 |
/* exec channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
393 |
|
64166 | 394 |
def exec(command: String): Exec = |
64129 | 395 |
{ |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
396 |
val channel = session.openChannel("exec").asInstanceOf[ChannelExec] |
64190
c62b99e3ec07
provide USER_HOME, such that symbolic Path.explode("~") can be used remotely;
wenzelm
parents:
64185
diff
changeset
|
397 |
channel.setCommand("export USER_HOME=\"$HOME\"\n" + command) |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
398 |
new Exec(this, channel) |
64129 | 399 |
} |
64123 | 400 |
|
66570 | 401 |
override def execute(command: String, |
64191 | 402 |
progress_stdout: String => Unit = (_: String) => (), |
403 |
progress_stderr: String => Unit = (_: String) => (), |
|
404 |
strict: Boolean = true): Process_Result = |
|
405 |
exec(command).result(progress_stdout, progress_stderr, strict) |
|
64137 | 406 |
|
407 |
||
408 |
/* tmp dirs */ |
|
409 |
||
64306
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
410 |
def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir)) |
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
411 |
|
64137 | 412 |
def rm_tree(remote_dir: String): Unit = |
64304 | 413 |
execute("rm -r -f " + Bash.string(remote_dir)).check |
64137 | 414 |
|
415 |
def tmp_dir(): String = |
|
416 |
execute("mktemp -d -t tmp.XXXXXXXXXX").check.out |
|
417 |
||
64233 | 418 |
def with_tmp_dir[A](body: Path => A): A = |
64137 | 419 |
{ |
420 |
val remote_dir = tmp_dir() |
|
64233 | 421 |
try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) } |
64137 | 422 |
} |
64123 | 423 |
} |
66570 | 424 |
|
425 |
||
426 |
||
427 |
/* system operations */ |
|
428 |
||
429 |
trait System |
|
430 |
{ |
|
431 |
def hg_url: String = "" |
|
432 |
def prefix: String = "" |
|
433 |
||
434 |
def expand_path(path: Path): Path = path.expand |
|
67066 | 435 |
def bash_path(path: Path): String = File.bash_path(path) |
66570 | 436 |
def is_file(path: Path): Boolean = path.is_file |
437 |
def is_dir(path: Path): Boolean = path.is_dir |
|
438 |
def mkdirs(path: Path): Unit = Isabelle_System.mkdirs(path) |
|
439 |
||
440 |
def execute(command: String, |
|
441 |
progress_stdout: String => Unit = (_: String) => (), |
|
442 |
progress_stderr: String => Unit = (_: String) => (), |
|
443 |
strict: Boolean = true): Process_Result = |
|
444 |
Isabelle_System.bash(command, progress_stdout = progress_stdout, |
|
445 |
progress_stderr = progress_stderr, strict = strict) |
|
446 |
} |
|
447 |
||
448 |
object Local extends System |
|
64123 | 449 |
} |