author | wenzelm |
Sun, 11 Sep 2022 23:50:36 +0200 | |
changeset 76119 | e5cfb05d312e |
parent 76118 | e8e3b60d8ecd |
child 76122 | b8f26c20d3b1 |
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, |
75548 | 17 |
OpenSSHConfig, UserInfo, ChannelExec, ChannelSftp, SftpATTRS, JSchException} |
64123 | 18 |
|
19 |
||
75393 | 20 |
object SSH { |
64185 | 21 |
/* target machine: user@host syntax */ |
64141 | 22 |
|
75393 | 23 |
object Target { |
71601 | 24 |
val User_Host: Regex = "^([^@]+)@(.+)$".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 |
|
75500 | 42 |
def port_suffix(port: Int): String = |
43 |
if (port == default_port) "" else ":" + port |
|
71549 | 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 |
|
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
61 |
/* open session */ |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
62 |
|
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
63 |
private def connect_session( |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
64 |
options: Options, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
65 |
jsch: JSch, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
66 |
host: String, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
67 |
user: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
68 |
port: Int = 0, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
69 |
permissive: Boolean = false, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
70 |
nominal_host: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
71 |
nominal_user: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
72 |
nominal_port: Option[Int] = None, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
73 |
on_close: () => Unit = () => () |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
74 |
): Session = { |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
75 |
val connect_port = make_port(port) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
76 |
val session = jsch.getSession(proper_string(user).orNull, host, connect_port) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
77 |
|
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
78 |
session.setUserInfo(No_User_Info) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
79 |
session.setServerAliveInterval(alive_interval(options)) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
80 |
session.setServerAliveCountMax(alive_count_max(options)) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
81 |
session.setConfig("MaxAuthTries", "3") |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
82 |
if (permissive) session.setConfig("StrictHostKeyChecking", "no") |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
83 |
if (nominal_host != "") session.setHostKeyAlias(nominal_host) |
64257 | 84 |
|
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
85 |
if (options.bool("ssh_compression")) { |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
86 |
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none") |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
87 |
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none") |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
88 |
session.setConfig("compression_level", "9") |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
89 |
} |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
90 |
session.connect(connect_timeout(options)) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
91 |
new Session(options, session, on_close, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
92 |
proper_string(nominal_host) getOrElse host, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
93 |
proper_string(nominal_user) getOrElse user, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
94 |
nominal_port getOrElse connect_port) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
95 |
} |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
96 |
|
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
97 |
def open_session( |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
98 |
options: Options, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
99 |
host: String, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
100 |
user: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
101 |
port: Int = 0, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
102 |
actual_host: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
103 |
proxy_host: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
104 |
proxy_user: String = "", |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
105 |
proxy_port: Int = 0, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
106 |
permissive: Boolean = false |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
107 |
): Session = { |
64130 | 108 |
val config_dir = Path.explode(options.string("ssh_config_dir")) |
64123 | 109 |
if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir) |
110 |
||
111 |
val jsch = new JSch |
|
112 |
||
64130 | 113 |
val config_file = Path.explode(options.string("ssh_config_file")) |
64123 | 114 |
if (config_file.is_file) |
115 |
jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file))) |
|
116 |
||
117 |
val known_hosts = config_dir + Path.explode("known_hosts") |
|
118 |
if (!known_hosts.is_file) known_hosts.file.createNewFile |
|
119 |
jsch.setKnownHosts(File.platform_path(known_hosts)) |
|
120 |
||
64130 | 121 |
val identity_files = |
71601 | 122 |
space_explode(':', options.string("ssh_identity_files")).map(Path.explode) |
71780 | 123 |
for (identity_file <- identity_files if identity_file.is_file) { |
124 |
try { jsch.addIdentity(File.platform_path(identity_file)) } |
|
125 |
catch { |
|
126 |
case exn: JSchException => |
|
127 |
error("Error in ssh identity file " + identity_file + ": " + exn.getMessage) |
|
128 |
} |
|
129 |
} |
|
64123 | 130 |
|
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
131 |
val connect_host = proper_string(actual_host) getOrElse host |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
132 |
val connect_port = make_port(port) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
133 |
if (proxy_host == "") { |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
134 |
connect_session(options, jsch, host = connect_host, user = user, port = connect_port) |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
135 |
} |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
136 |
else { |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
137 |
val proxy = |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
138 |
connect_session(options, jsch, host = proxy_host, port = proxy_port, user = proxy_user) |
67067 | 139 |
|
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
140 |
val fw = |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
141 |
try { proxy.port_forwarding(remote_host = connect_host, remote_port = connect_port) } |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
142 |
catch { case exn: Throwable => proxy.close(); throw exn } |
64257 | 143 |
|
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
144 |
try { |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
145 |
connect_session(options, jsch, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
146 |
host = fw.local_host, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
147 |
port = fw.local_port, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
148 |
permissive = permissive, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
149 |
nominal_host = host, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
150 |
nominal_port = Some(connect_port), |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
151 |
nominal_user = user, user = user, |
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
152 |
on_close = { () => fw.close(); proxy.close() }) |
64257 | 153 |
} |
76115
f17393e21388
clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents:
76100
diff
changeset
|
154 |
catch { case exn: Throwable => fw.close(); proxy.close(); throw exn } |
64257 | 155 |
} |
156 |
} |
|
64130 | 157 |
|
64123 | 158 |
|
64128 | 159 |
/* user info */ |
160 |
||
75393 | 161 |
object No_User_Info extends UserInfo { |
64128 | 162 |
def getPassphrase: String = null |
163 |
def getPassword: String = null |
|
164 |
def promptPassword(msg: String): Boolean = false |
|
165 |
def promptPassphrase(msg: String): Boolean = false |
|
166 |
def promptYesNo(msg: String): Boolean = false |
|
167 |
def showMessage(msg: String): Unit = Output.writeln(msg) |
|
168 |
} |
|
169 |
||
170 |
||
64191 | 171 |
/* Sftp channel */ |
172 |
||
173 |
type Attrs = SftpATTRS |
|
174 |
||
75393 | 175 |
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
|
176 |
def is_file: Boolean = !is_dir |
64191 | 177 |
} |
178 |
||
179 |
||
64132 | 180 |
/* exec channel */ |
181 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
182 |
private val exec_wait_delay = Time.seconds(0.3) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
183 |
|
75393 | 184 |
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
|
185 |
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
|
186 |
|
75548 | 187 |
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
|
188 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
189 |
val exit_status: Future[Int] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
190 |
Future.thread("ssh_wait") { |
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73634
diff
changeset
|
191 |
while (!channel.isClosed) exec_wait_delay.sleep() |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
192 |
channel.getExitStatus |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
193 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
194 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
195 |
val stdin: OutputStream = channel.getOutputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
196 |
val stdout: InputStream = channel.getInputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
197 |
val stderr: InputStream = channel.getErrStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
198 |
|
64166 | 199 |
// connect after preparing streams |
200 |
channel.connect(connect_timeout(session.options)) |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
201 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
202 |
def result( |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
203 |
progress_stdout: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
204 |
progress_stderr: String => Unit = (_: String) => (), |
75393 | 205 |
strict: Boolean = true |
206 |
): Process_Result = { |
|
73367 | 207 |
stdin.close() |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
208 |
|
75393 | 209 |
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
|
210 |
val result = new mutable.ListBuffer[String] |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
211 |
val line_buffer = new ByteArrayOutputStream(100) |
75393 | 212 |
def line_flush(): Unit = { |
64326 | 213 |
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
|
214 |
progress(line) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
215 |
result += line |
75548 | 216 |
line_buffer.reset() |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
217 |
} |
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 |
var c = 0 |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
220 |
var finished = false |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
221 |
while (!finished) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
222 |
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
|
223 |
if (c == 10) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
224 |
else if (channel.isClosed) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
225 |
if (line_buffer.size > 0) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
226 |
finished = true |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
227 |
} |
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73634
diff
changeset
|
228 |
else exec_wait_delay.sleep() |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
229 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
230 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
231 |
result.toList |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
232 |
} |
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 |
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
|
235 |
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
|
236 |
|
75393 | 237 |
def terminate(): Unit = { |
73367 | 238 |
close() |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
239 |
out_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
240 |
err_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
241 |
exit_status.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
242 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
243 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
244 |
val rc = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
245 |
try { exit_status.join } |
74306 | 246 |
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
|
247 |
|
73367 | 248 |
close() |
74306 | 249 |
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
|
250 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
251 |
Process_Result(rc, out_lines.join, err_lines.join) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
252 |
} |
64131 | 253 |
} |
254 |
||
255 |
||
64123 | 256 |
/* session */ |
257 |
||
67745
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
258 |
class Session private[SSH]( |
d83efbe52438
support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents:
67273
diff
changeset
|
259 |
val options: Options, |
76116
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
260 |
session: JSch_Session, |
71549 | 261 |
on_close: () => Unit, |
262 |
val nominal_host: String, |
|
75544
179a3b028b0a
proper nominal_port, notably for port forwarding;
wenzelm
parents:
75517
diff
changeset
|
263 |
val nominal_user: String, |
179a3b028b0a
proper nominal_port, notably for port forwarding;
wenzelm
parents:
75517
diff
changeset
|
264 |
val nominal_port: Int |
75393 | 265 |
) extends System { |
76116
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
266 |
ssh => |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
267 |
|
64347 | 268 |
def host: String = if (session.getHost == null) "" else session.getHost |
71549 | 269 |
|
270 |
override def hg_url: String = |
|
271 |
"ssh://" + user_prefix(nominal_user) + nominal_host + "/" |
|
66570 | 272 |
|
75500 | 273 |
override def rsync_prefix: String = |
274 |
user_prefix(nominal_user) + nominal_host + ":" |
|
275 |
||
64123 | 276 |
override def toString: String = |
75544
179a3b028b0a
proper nominal_port, notably for port forwarding;
wenzelm
parents:
75517
diff
changeset
|
277 |
user_prefix(session.getUserName) + host + port_suffix(session.getPort) + |
71549 | 278 |
(if (session.isConnected) "" else " (disconnected)") |
64123 | 279 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
280 |
|
65009 | 281 |
/* port forwarding */ |
282 |
||
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65594
diff
changeset
|
283 |
def port_forwarding( |
76116
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
284 |
remote_port: Int, |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
285 |
remote_host: String = "localhost", |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
286 |
local_port: Int = 0, |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
287 |
local_host: String = "localhost", |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
288 |
ssh_close: Boolean = false |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
289 |
): Port_Forwarding = { |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
290 |
val local_port1 = session.setPortForwardingL(local_host, local_port, remote_host, remote_port) |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
291 |
new Port_Forwarding(local_host, local_port1, remote_host, remote_port) { |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
292 |
override def close(): Unit = { |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
293 |
session.delPortForwardingL(this.local_host, this.local_port) |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
294 |
if (ssh_close) ssh.close() |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
295 |
} |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
296 |
} |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
297 |
} |
65009 | 298 |
|
299 |
||
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
300 |
/* sftp channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
301 |
|
76116
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
302 |
private val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp] |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
303 |
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
|
304 |
|
75548 | 305 |
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
|
306 |
|
75393 | 307 |
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
|
308 |
val home = sftp.getHome |
73897 | 309 |
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
|
310 |
} |
66570 | 311 |
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
|
312 |
def remote_path(path: Path): String = expand_path(path).implode |
67066 | 313 |
override def bash_path(path: Path): String = Bash.string(remote_path(path)) |
64123 | 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 |
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
|
316 |
|
69302 | 317 |
private def test_entry(path: Path, as_dir: Boolean): Boolean = |
318 |
try { |
|
319 |
val is_dir = sftp.stat(remote_path(path)).isDir |
|
320 |
if (as_dir) is_dir else !is_dir |
|
321 |
} |
|
322 |
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
|
323 |
|
69302 | 324 |
override def is_dir(path: Path): Boolean = test_entry(path, true) |
325 |
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
|
326 |
|
75393 | 327 |
override def make_directory(path: Path): Path = { |
76118 | 328 |
if (!execute("mkdir -p " + remote_path(path)).ok) { |
329 |
error("Failed to create directory: " + quote(remote_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
|
330 |
} |
72376 | 331 |
path |
332 |
} |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
333 |
|
75393 | 334 |
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
|
335 |
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
|
336 |
|
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
|
337 |
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
|
338 |
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
|
339 |
(for { |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
340 |
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
|
341 |
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
|
342 |
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
|
343 |
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
|
344 |
if name != "." && name != ".." |
69302 | 345 |
} |
346 |
yield { |
|
347 |
Dir_Entry(name, |
|
348 |
if (attrs.isLink) { |
|
349 |
try { sftp.stat(dir_name + "/" + name).isDir } |
|
350 |
catch { case _: SftpException => false } |
|
351 |
} |
|
352 |
else attrs.isDir) |
|
69427
ff2f39a221d4
clarified operations: uniform sorting of results;
wenzelm
parents:
69393
diff
changeset
|
353 |
}).toList.sortBy(_.name) |
64191 | 354 |
} |
64135
865dda40e1cc
provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents:
64134
diff
changeset
|
355 |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
356 |
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
|
357 |
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
|
358 |
|
73634 | 359 |
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
|
360 |
sftp.get(remote_path(path), File.platform_path(local_path)) |
75513 | 361 |
override def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_)) |
362 |
override 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
|
363 |
|
73634 | 364 |
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
|
365 |
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
|
366 |
def write_bytes(path: Path, bytes: Bytes): Unit = |
71601 | 367 |
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
|
368 |
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
|
369 |
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
|
370 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
371 |
|
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
372 |
/* exec channel */ |
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64254
diff
changeset
|
373 |
|
75393 | 374 |
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
|
375 |
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
|
376 |
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
|
377 |
new Exec(this, channel) |
64129 | 378 |
} |
64123 | 379 |
|
66570 | 380 |
override def execute(command: String, |
64191 | 381 |
progress_stdout: String => Unit = (_: String) => (), |
382 |
progress_stderr: String => Unit = (_: String) => (), |
|
73634 | 383 |
settings: Boolean = true, |
64191 | 384 |
strict: Boolean = true): Process_Result = |
385 |
exec(command).result(progress_stdout, progress_stderr, strict) |
|
64137 | 386 |
|
72340 | 387 |
override def isabelle_platform: Isabelle_Platform = Isabelle_Platform(ssh = Some(this)) |
72338 | 388 |
|
64137 | 389 |
|
390 |
/* tmp dirs */ |
|
391 |
||
64306
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
392 |
def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir)) |
7b6dc1b36f20
tuned signature, in accordance to Isabelle_System;
wenzelm
parents:
64304
diff
changeset
|
393 |
|
64137 | 394 |
def rm_tree(remote_dir: String): Unit = |
64304 | 395 |
execute("rm -r -f " + Bash.string(remote_dir)).check |
64137 | 396 |
|
397 |
def tmp_dir(): String = |
|
398 |
execute("mktemp -d -t tmp.XXXXXXXXXX").check.out |
|
399 |
||
75393 | 400 |
override def with_tmp_dir[A](body: Path => A): A = { |
64137 | 401 |
val remote_dir = tmp_dir() |
64233 | 402 |
try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) } |
64137 | 403 |
} |
64123 | 404 |
} |
66570 | 405 |
|
76116
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
406 |
abstract class Port_Forwarding private[SSH]( |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
407 |
val local_host: String, |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
408 |
val local_port: Int, |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
409 |
val remote_host: String, |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
410 |
val remote_port: Int |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
411 |
) extends AutoCloseable { |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
412 |
override def toString: String = |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
413 |
local_host + ":" + local_port + ":" + remote_host + ":" + remote_port |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
414 |
} |
c4dc343fdbcb
clarified signature: avoid exposure of JSch types;
wenzelm
parents:
76115
diff
changeset
|
415 |
|
66570 | 416 |
|
417 |
/* system operations */ |
|
418 |
||
75393 | 419 |
trait System extends AutoCloseable { |
73634 | 420 |
def close(): Unit = () |
421 |
||
66570 | 422 |
def hg_url: String = "" |
423 |
||
75500 | 424 |
def rsync_prefix: String = "" |
75517 | 425 |
def rsync_path(path: Path): String = rsync_prefix + expand_path(path).implode |
75500 | 426 |
|
66570 | 427 |
def expand_path(path: Path): Path = path.expand |
67066 | 428 |
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
|
429 |
def is_dir(path: Path): Boolean = path.is_dir |
66570 | 430 |
def is_file(path: Path): Boolean = path.is_file |
72376 | 431 |
def make_directory(path: Path): Path = Isabelle_System.make_directory(path) |
73634 | 432 |
def with_tmp_dir[A](body: Path => A): A = Isabelle_System.with_tmp_dir("tmp")(body) |
433 |
def read_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path1, path2) |
|
434 |
def write_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path2, path1) |
|
75513 | 435 |
def read_bytes(path: Path): Bytes = Bytes.read(path) |
436 |
def read(path: Path): String = File.read(path) |
|
66570 | 437 |
|
438 |
def execute(command: String, |
|
439 |
progress_stdout: String => Unit = (_: String) => (), |
|
440 |
progress_stderr: String => Unit = (_: String) => (), |
|
73634 | 441 |
settings: Boolean = true, |
66570 | 442 |
strict: Boolean = true): Process_Result = |
73634 | 443 |
Isabelle_System.bash(command, |
444 |
progress_stdout = progress_stdout, |
|
445 |
progress_stderr = progress_stderr, |
|
446 |
env = if (settings) Isabelle_System.settings() else null, |
|
447 |
strict = strict) |
|
72338 | 448 |
|
72340 | 449 |
def isabelle_platform: Isabelle_Platform = Isabelle_Platform() |
66570 | 450 |
} |
451 |
||
452 |
object Local extends System |
|
64123 | 453 |
} |