author | wenzelm |
Sat, 14 Mar 2020 13:44:52 +0100 | |
changeset 71549 | 319599ba28cf |
parent 71383 | 8313dca6dee9 |
child 71562 | 794c8b0ad8f1 |
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 |
|
71358 | 12 |
import scala.collection.mutable |
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 |
{ |
71307 | 24 |
val User_Host = "^([^@]+)@(.+)$".r |
64141 | 25 |
|
26 |
def parse(s: String): (String, String) = |
|
27 |
s match { |
|
71307 | 28 |
case User_Host(user, host) => (user, host) |
64141 | 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 |
|
71549 | 42 |
def port_suffix(port: Int): String = |
43 |
if (port == default_port) "" else ":" + port |
|
44 |
||
45 |
def user_prefix(user: String): String = |
|
46 |
proper_string(user) match { |
|
47 |
case None => "" |
|
48 |
case Some(name) => name + "@" |
|
49 |
} |
|
50 |
||
64257 | 51 |
def connect_timeout(options: Options): Int = |
52 |
options.seconds("ssh_connect_timeout").ms.toInt |
|
64141 | 53 |
|
64325 | 54 |
def alive_interval(options: Options): Int = |
55 |
options.seconds("ssh_alive_interval").ms.toInt |
|
56 |
||
67273
c573cfb2c407
more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents:
67067
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
|
64123 | 60 |
|
64257 | 61 |
/* init context */ |
62 |
||
63 |
def init_context(options: Options): Context = |
|
64123 | 64 |
{ |
64130 | 65 |
val config_dir = Path.explode(options.string("ssh_config_dir")) |
64123 | 66 |
if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir) |
67 |
||
68 |
val jsch = new JSch |
|
69 |
||
64130 | 70 |
val config_file = Path.explode(options.string("ssh_config_file")) |
64123 | 71 |
if (config_file.is_file) |
72 |
jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file))) |
|
73 |
||
74 |
val known_hosts = config_dir + Path.explode("known_hosts") |
|
75 |
if (!known_hosts.is_file) known_hosts.file.createNewFile |
|
76 |
jsch.setKnownHosts(File.platform_path(known_hosts)) |
|
77 |
||
64130 | 78 |
val identity_files = |
66923 | 79 |
space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_)) |
64123 | 80 |
for (identity_file <- identity_files if identity_file.is_file) |
81 |
jsch.addIdentity(File.platform_path(identity_file)) |
|
82 |
||
64257 | 83 |
new Context(options, jsch) |
64123 | 84 |
} |
85 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
86 |
def open_session(options: Options, host: String, user: String = "", port: Int = 0, |
67771 | 87 |
proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0, |
88 |
permissive: Boolean = false): Session = |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
89 |
init_context(options).open_session(host = host, user = user, port = port, |
67771 | 90 |
proxy_host = proxy_host, proxy_user = proxy_user, proxy_port = proxy_port, |
91 |
permissive = permissive) |
|
67067 | 92 |
|
64257 | 93 |
class Context private[SSH](val options: Options, val jsch: JSch) |
94 |
{ |
|
95 |
def update_options(new_options: Options): Context = new Context(new_options, jsch) |
|
96 |
||
71549 | 97 |
private def connect_session(host: String, user: String = "", port: Int = 0, |
67770
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
98 |
host_key_permissive: Boolean = false, |
71549 | 99 |
nominal_host: String = "", |
100 |
nominal_user: String = "", |
|
67770
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
101 |
on_close: () => Unit = () => ()): Session = |
64257 | 102 |
{ |
71383 | 103 |
val session = jsch.getSession(proper_string(user).orNull, host, make_port(port)) |
64257 | 104 |
|
105 |
session.setUserInfo(No_User_Info) |
|
64325 | 106 |
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
|
107 |
session.setServerAliveCountMax(alive_count_max(options)) |
64257 | 108 |
session.setConfig("MaxAuthTries", "3") |
67770
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
109 |
if (host_key_permissive) session.setConfig("StrictHostKeyChecking", "no") |
71549 | 110 |
if (nominal_host != "") session.setHostKeyAlias(nominal_host) |
64257 | 111 |
|
112 |
if (options.bool("ssh_compression")) { |
|
113 |
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none") |
|
114 |
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none") |
|
115 |
session.setConfig("compression_level", "9") |
|
116 |
} |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
117 |
session.connect(connect_timeout(options)) |
71549 | 118 |
new Session(options, session, on_close, |
119 |
proper_string(nominal_host) getOrElse host, |
|
120 |
proper_string(nominal_user) getOrElse user) |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
121 |
} |
64257 | 122 |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
123 |
def open_session(host: String, user: String = "", port: Int = 0, |
67770
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
124 |
proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0, |
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
125 |
permissive: Boolean = false): Session = |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
126 |
{ |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
127 |
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
|
128 |
else { |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
129 |
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
|
130 |
|
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
131 |
val fw = |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
132 |
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
|
133 |
catch { case exn: Throwable => proxy.close; throw exn } |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
134 |
|
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
135 |
try { |
67770
25f3a278df3d
support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents:
67745
diff
changeset
|
136 |
connect_session(host = fw.local_host, port = fw.local_port, |
71549 | 137 |
host_key_permissive = permissive, |
138 |
nominal_host = host, nominal_user = user, user = user, |
|
139 |
on_close = () => { fw.close; proxy.close }) |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
140 |
} |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
141 |
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
|
142 |
} |
64257 | 143 |
} |
144 |
} |
|
64130 | 145 |
|
64123 | 146 |
|
147 |
/* logging */ |
|
148 |
||
149 |
def logging(verbose: Boolean = true, debug: Boolean = false) |
|
150 |
{ |
|
151 |
JSch.setLogger(if (verbose) new Logger(debug) else null) |
|
152 |
} |
|
153 |
||
154 |
private class Logger(debug: Boolean) extends JSch_Logger |
|
155 |
{ |
|
156 |
def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug |
|
157 |
||
158 |
def log(level: Int, msg: String) |
|
159 |
{ |
|
160 |
level match { |
|
161 |
case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg) |
|
162 |
case JSch_Logger.WARN => Output.warning(msg) |
|
163 |
case _ => Output.writeln(msg) |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
||
64128 | 169 |
/* user info */ |
170 |
||
171 |
object No_User_Info extends UserInfo |
|
172 |
{ |
|
173 |
def getPassphrase: String = null |
|
174 |
def getPassword: String = null |
|
175 |
def promptPassword(msg: String): Boolean = false |
|
176 |
def promptPassphrase(msg: String): Boolean = false |
|
177 |
def promptYesNo(msg: String): Boolean = false |
|
178 |
def showMessage(msg: String): Unit = Output.writeln(msg) |
|
179 |
} |
|
180 |
||
181 |
||
65009 | 182 |
/* port forwarding */ |
183 |
||
65010 | 184 |
object Port_Forwarding |
65009 | 185 |
{ |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
186 |
def open(ssh: Session, ssh_close: Boolean, |
65010 | 187 |
local_host: String, local_port: Int, remote_host: String, remote_port: Int): Port_Forwarding = |
188 |
{ |
|
189 |
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
|
190 |
new Port_Forwarding(ssh, ssh_close, local_host, port, remote_host, remote_port) |
65010 | 191 |
} |
192 |
} |
|
193 |
||
194 |
class Port_Forwarding private[SSH]( |
|
195 |
ssh: SSH.Session, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
196 |
ssh_close: Boolean, |
65010 | 197 |
val local_host: String, |
198 |
val local_port: Int, |
|
199 |
val remote_host: String, |
|
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69303
diff
changeset
|
200 |
val remote_port: Int) extends AutoCloseable |
65010 | 201 |
{ |
202 |
override def toString: String = |
|
203 |
local_host + ":" + local_port + ":" + remote_host + ":" + remote_port |
|
204 |
||
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
205 |
def close() |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
206 |
{ |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
207 |
ssh.session.delPortForwardingL(local_host, local_port) |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
208 |
if (ssh_close) ssh.close() |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
209 |
} |
65009 | 210 |
} |
211 |
||
212 |
||
64191 | 213 |
/* Sftp channel */ |
214 |
||
215 |
type Attrs = SftpATTRS |
|
216 |
||
69302 | 217 |
sealed case class Dir_Entry(name: String, is_dir: Boolean) |
64191 | 218 |
{ |
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
219 |
def is_file: Boolean = !is_dir |
64191 | 220 |
} |
221 |
||
222 |
||
64132 | 223 |
/* exec channel */ |
224 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
225 |
private val exec_wait_delay = Time.seconds(0.3) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
226 |
|
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69303
diff
changeset
|
227 |
class Exec private[SSH](session: Session, channel: ChannelExec) extends AutoCloseable |
64131 | 228 |
{ |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
229 |
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
|
230 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
231 |
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
|
232 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
233 |
val exit_status: Future[Int] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
234 |
Future.thread("ssh_wait") { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
235 |
while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
236 |
channel.getExitStatus |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
237 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
238 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
239 |
val stdin: OutputStream = channel.getOutputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
240 |
val stdout: InputStream = channel.getInputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
241 |
val stderr: InputStream = channel.getErrStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
242 |
|
64166 | 243 |
// connect after preparing streams |
244 |
channel.connect(connect_timeout(session.options)) |
|
64134
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 |
def result( |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
247 |
progress_stdout: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
248 |
progress_stderr: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
249 |
strict: Boolean = true): Process_Result = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
250 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
251 |
stdin.close |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
252 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
253 |
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
|
254 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
255 |
val result = new mutable.ListBuffer[String] |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
256 |
val line_buffer = new ByteArrayOutputStream(100) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
257 |
def line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
258 |
{ |
64326 | 259 |
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
|
260 |
progress(line) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
261 |
result += line |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
262 |
line_buffer.reset |
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 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
265 |
var c = 0 |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
266 |
var finished = false |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
267 |
while (!finished) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
268 |
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
|
269 |
if (c == 10) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
270 |
else if (channel.isClosed) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
271 |
if (line_buffer.size > 0) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
272 |
finished = true |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
273 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
274 |
else Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
275 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
276 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
277 |
result.toList |
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 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
280 |
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
|
281 |
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
|
282 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
283 |
def terminate() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
284 |
{ |
64136 | 285 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
286 |
out_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
287 |
err_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
288 |
exit_status.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
289 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
290 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
291 |
val rc = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
292 |
try { exit_status.join } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
293 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
294 |
|
64136 | 295 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
296 |
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
|
297 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
298 |
Process_Result(rc, out_lines.join, err_lines.join) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
299 |
} |
64131 | 300 |
} |
301 |
||
302 |
||
64123 | 303 |
/* session */ |
304 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
305 |
class Session private[SSH]( |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
306 |
val options: Options, |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
307 |
val session: JSch_Session, |
71549 | 308 |
on_close: () => Unit, |
309 |
val nominal_host: String, |
|
310 |
val nominal_user: String) extends System with AutoCloseable |
|
64123 | 311 |
{ |
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
312 |
def update_options(new_options: Options): Session = |
71549 | 313 |
new Session(new_options, session, on_close, nominal_host, nominal_user) |
64166 | 314 |
|
64347 | 315 |
def host: String = if (session.getHost == null) "" else session.getHost |
71549 | 316 |
|
317 |
override def hg_url: String = |
|
318 |
"ssh://" + user_prefix(nominal_user) + nominal_host + "/" |
|
66570 | 319 |
|
320 |
override def prefix: String = |
|
71549 | 321 |
user_prefix(session.getUserName) + host + port_suffix(session.getPort) + ":" |
64347 | 322 |
|
64123 | 323 |
override def toString: String = |
71549 | 324 |
user_prefix(session.getUserName) + host + port_suffix(session.getPort) + |
325 |
(if (session.isConnected) "" else " (disconnected)") |
|
64123 | 326 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
327 |
|
65009 | 328 |
/* port forwarding */ |
329 |
||
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
330 |
def port_forwarding( |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
331 |
remote_port: Int, remote_host: String = "localhost", |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
332 |
local_port: Int = 0, local_host: String = "localhost", |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
333 |
ssh_close: Boolean = false): Port_Forwarding = |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
334 |
Port_Forwarding.open(this, ssh_close, local_host, local_port, remote_host, remote_port) |
65009 | 335 |
|
336 |
||
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
337 |
/* sftp channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
338 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
339 |
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
|
340 |
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
|
341 |
|
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
342 |
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
|
343 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
344 |
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
|
345 |
{ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
346 |
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
|
347 |
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
|
348 |
} |
66570 | 349 |
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
|
350 |
def remote_path(path: Path): String = expand_path(path).implode |
67066 | 351 |
override def bash_path(path: Path): String = Bash.string(remote_path(path)) |
64123 | 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 |
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
|
354 |
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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
|
69302 | 359 |
private def test_entry(path: Path, as_dir: Boolean): Boolean = |
360 |
try { |
|
361 |
val is_dir = sftp.stat(remote_path(path)).isDir |
|
362 |
if (as_dir) is_dir else !is_dir |
|
363 |
} |
|
364 |
catch { case _: SftpException => false } |
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
365 |
|
69302 | 366 |
override def is_dir(path: Path): Boolean = test_entry(path, true) |
367 |
override def is_file(path: Path): Boolean = test_entry(path, false) |
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
368 |
|
69301 | 369 |
def is_link(path: Path): Boolean = |
370 |
try { sftp.lstat(remote_path(path)).isLink } |
|
371 |
catch { case _: SftpException => false } |
|
372 |
||
66570 | 373 |
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
|
374 |
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
|
375 |
execute( |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
376 |
"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
|
377 |
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
|
378 |
} |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
379 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
380 |
def read_dir(path: Path): List[Dir_Entry] = |
64191 | 381 |
{ |
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
382 |
if (!is_dir(path)) error("No such directory: " + path.toString) |
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
383 |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
384 |
val dir_name = remote_path(path) |
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
385 |
val dir = sftp.ls(dir_name) |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
386 |
(for { |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
387 |
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
|
388 |
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
|
389 |
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
|
390 |
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
|
391 |
if name != "." && name != ".." |
69302 | 392 |
} |
393 |
yield { |
|
394 |
Dir_Entry(name, |
|
395 |
if (attrs.isLink) { |
|
396 |
try { sftp.stat(dir_name + "/" + name).isDir } |
|
397 |
catch { case _: SftpException => false } |
|
398 |
} |
|
399 |
else attrs.isDir) |
|
69427
ff2f39a221d4
clarified operations: uniform sorting of results;
wenzelm
parents:
69393
diff
changeset
|
400 |
}).toList.sortBy(_.name) |
64191 | 401 |
} |
64135
865dda40e1cc
provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents:
64134
diff
changeset
|
402 |
|
69301 | 403 |
def find_files( |
404 |
start: Path, |
|
405 |
pred: Path => Boolean = _ => true, |
|
406 |
include_dirs: Boolean = false, |
|
407 |
follow_links: Boolean = false): List[Path] = |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
408 |
{ |
69301 | 409 |
val result = new mutable.ListBuffer[Path] |
410 |
def check(path: Path) { if (pred(path)) result += path } |
|
411 |
||
412 |
def find(dir: Path) |
|
413 |
{ |
|
69303 | 414 |
if (include_dirs) check(dir) |
69301 | 415 |
if (follow_links || !is_link(dir)) { |
416 |
for (entry <- read_dir(dir)) { |
|
69302 | 417 |
val path = dir + Path.basic(entry.name) |
418 |
if (entry.is_file) check(path) else find(path) |
|
69301 | 419 |
} |
420 |
} |
|
421 |
} |
|
422 |
if (is_file(start)) check(start) else find(start) |
|
423 |
||
424 |
result.toList |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
425 |
} |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
426 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
427 |
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
|
428 |
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
|
429 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
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
|
434 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
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
|
439 |
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
|
440 |
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
|
441 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
442 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
443 |
/* exec channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
444 |
|
64166 | 445 |
def exec(command: String): Exec = |
64129 | 446 |
{ |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
447 |
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
|
448 |
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
|
449 |
new Exec(this, channel) |
64129 | 450 |
} |
64123 | 451 |
|
66570 | 452 |
override def execute(command: String, |
64191 | 453 |
progress_stdout: String => Unit = (_: String) => (), |
454 |
progress_stderr: String => Unit = (_: String) => (), |
|
455 |
strict: Boolean = true): Process_Result = |
|
456 |
exec(command).result(progress_stdout, progress_stderr, strict) |
|
64137 | 457 |
|
458 |
||
459 |
/* tmp dirs */ |
|
460 |
||
64306
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
461 |
def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir)) |
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
462 |
|
64137 | 463 |
def rm_tree(remote_dir: String): Unit = |
64304 | 464 |
execute("rm -r -f " + Bash.string(remote_dir)).check |
64137 | 465 |
|
466 |
def tmp_dir(): String = |
|
467 |
execute("mktemp -d -t tmp.XXXXXXXXXX").check.out |
|
468 |
||
64233 | 469 |
def with_tmp_dir[A](body: Path => A): A = |
64137 | 470 |
{ |
471 |
val remote_dir = tmp_dir() |
|
64233 | 472 |
try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) } |
64137 | 473 |
} |
64123 | 474 |
} |
66570 | 475 |
|
476 |
||
477 |
||
478 |
/* system operations */ |
|
479 |
||
480 |
trait System |
|
481 |
{ |
|
482 |
def hg_url: String = "" |
|
483 |
def prefix: String = "" |
|
484 |
||
485 |
def expand_path(path: Path): Path = path.expand |
|
67066 | 486 |
def bash_path(path: Path): String = File.bash_path(path) |
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
67771
diff
changeset
|
487 |
def is_dir(path: Path): Boolean = path.is_dir |
66570 | 488 |
def is_file(path: Path): Boolean = path.is_file |
489 |
def mkdirs(path: Path): Unit = Isabelle_System.mkdirs(path) |
|
490 |
||
491 |
def execute(command: String, |
|
492 |
progress_stdout: String => Unit = (_: String) => (), |
|
493 |
progress_stderr: String => Unit = (_: String) => (), |
|
494 |
strict: Boolean = true): Process_Result = |
|
495 |
Isabelle_System.bash(command, progress_stdout = progress_stdout, |
|
496 |
progress_stderr = progress_stderr, strict = strict) |
|
497 |
} |
|
498 |
||
499 |
object Local extends System |
|
64123 | 500 |
} |