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