src/Pure/General/ssh.scala
author wenzelm
Wed, 14 Sep 2022 14:54:21 +0200
changeset 76147 75f0fc965539
parent 76145 a6bdf4b889ca
child 76148 769ebb139a32
permissions -rw-r--r--
misc tuning and clarification; proper shutdown_hook;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/ssh.scala
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     3
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
     4
SSH client on OpenSSH command-line tools, preferably with connection
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
     5
multiplexing, but this does not work on Windows.
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
*/
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
package isabelle
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    10
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73897
diff changeset
    11
import java.util.{Map => JMap}
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    12
import java.net.ServerSocket
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    13
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
    15
object SSH {
64185
wenzelm
parents: 64166
diff changeset
    16
  /* target machine: user@host syntax */
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
    18
  object Target {
76128
wenzelm
parents: 76125
diff changeset
    19
    def parse(s: String): (String, String) = {
wenzelm
parents: 76125
diff changeset
    20
      val i = s.indexOf('@')
wenzelm
parents: 76125
diff changeset
    21
      if (i <= 0) ("", s)
wenzelm
parents: 76125
diff changeset
    22
      else (s.substring(0, i), s.substring(i + 1))
wenzelm
parents: 76125
diff changeset
    23
    }
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    24
76128
wenzelm
parents: 76125
diff changeset
    25
    def unapplySeq(s: String): Option[List[String]] = {
wenzelm
parents: 76125
diff changeset
    26
      val (user, host) = parse(s)
wenzelm
parents: 76125
diff changeset
    27
      if (host.isEmpty) None else Some(List(user, host))
wenzelm
parents: 76125
diff changeset
    28
    }
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    29
  }
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    30
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    31
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    32
  /* OpenSSH configuration and command-line */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    33
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    34
  // see https://linux.die.net/man/5/ssh_config
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    35
  object Config {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    36
    def entry(x: String, y: String): String = x + "=" + y
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    37
    def entry(x: String, y: Int): String = entry(x, y.toString)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    38
    def entry(x: String, y: Boolean): String = entry(x, if (y) "yes" else "no")
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    39
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    40
    def make(options: Options,
76131
8b695e59db3f clarified default: do not override port from ssh_config, which could be different from 22;
wenzelm
parents: 76130
diff changeset
    41
      port: Int = 0,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    42
      user: String = "",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    43
      control_master: Boolean = false,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    44
      control_path: String = ""
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    45
    ): List[String] = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    46
      List("BatchMode=yes",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    47
        entry("ConnectTimeout", options.seconds("ssh_connect_timeout").ms.toInt),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    48
        entry("ServerAliveInterval", options.seconds("ssh_alive_interval").ms.toInt),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    49
        entry("ServerAliveCountMax", options.int("ssh_alive_count_max")),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    50
        entry("Compression", options.bool("ssh_compression"))) :::
76131
8b695e59db3f clarified default: do not override port from ssh_config, which could be different from 22;
wenzelm
parents: 76130
diff changeset
    51
      (if (port > 0) List(entry("Port", port)) else Nil) :::
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    52
      (if (user.nonEmpty) List(entry("User", user)) else Nil) :::
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    53
      (if (control_master) List("ControlMaster=yes", "ControlPersist=yes") else Nil) :::
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    54
      (if (control_path.nonEmpty) List("ControlPath=" + control_path) else Nil)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    55
    }
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    56
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    57
    def option(entry: String): String = "-o " + Bash.string(entry)
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    58
    def option(x: String, y: String): String = option(entry(x, y))
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    59
    def option(x: String, y: Int): String = option(entry(x, y))
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    60
    def option(x: String, y: Boolean): String = option(entry(x, y))
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    61
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    62
    def command(command: String, config: List[String]): String =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    63
      Bash.string(command) + config.map(entry => " " + option(entry)).mkString
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    64
  }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    65
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    66
  def sftp_string(str: String): String = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    67
    val special = Set(' ', '*', '?', '{', '}')
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    68
    if (str.exists(special)) {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    69
      val res = new StringBuilder
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    70
      for (c <- str) {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    71
        if (special(c)) res += '\\'
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    72
        res += c
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    73
      }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    74
      res.toString()
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    75
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    76
    else str
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    77
  }
67273
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
    78
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    79
76115
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    80
  /* open session */
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    81
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    82
  def open_session(
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    83
    options: Options,
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    84
    host: String,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    85
    port: Int = 0,
76115
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    86
    user: String = "",
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    87
    multiplex: Boolean = !Platform.is_windows
76115
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    88
  ): Session = {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    89
    val (control_master, control_path) =
76144
35a279a2d246 clarified signature;
wenzelm
parents: 76142
diff changeset
    90
      if (multiplex) (true, Isabelle_System.tmp_file("ssh_socket", initialized = false).getPath)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    91
      else (false, "")
76131
8b695e59db3f clarified default: do not override port from ssh_config, which could be different from 22;
wenzelm
parents: 76130
diff changeset
    92
    new Session(options, host, port, user, control_master, control_path)
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    93
  }
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    94
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    95
  class Session private[SSH](
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    96
    val options: Options,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    97
    val host: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    98
    val port: Int,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    99
    val user: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   100
    control_master: Boolean,
76136
1bb677cceea4 let rsync re-use ssh connection via control path;
wenzelm
parents: 76133
diff changeset
   101
    val control_path: String
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   102
  ) extends System {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   103
    ssh =>
64128
wenzelm
parents: 64127
diff changeset
   104
76133
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   105
    def port_suffix: String = if (port > 0) ":" + port else ""
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   106
    def user_prefix: String = if (user.nonEmpty) user + "@" else ""
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   107
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   108
    override def toString: String = user_prefix + host + port_suffix
76132
2bb6eb6df6c2 proper port for Mercurial;
wenzelm
parents: 76131
diff changeset
   109
    override def hg_url: String = "ssh://" + toString + "/"
76133
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   110
    override def rsync_prefix: String = user_prefix + host + ":"
64191
wenzelm
parents: 64190
diff changeset
   111
wenzelm
parents: 64190
diff changeset
   112
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   113
    /* local ssh commands */
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   114
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   115
    def run_command(command: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   116
      master: Boolean = false,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   117
      opts: String = "",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   118
      args: String = "",
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   119
      progress_stdout: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   120
      progress_stderr: String => Unit = (_: String) => (),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   121
      strict: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   122
    ): Process_Result = {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   123
      val config =
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   124
        Config.make(options, port = port, user = user,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   125
          control_master = master, control_path = control_path)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   126
      val cmd =
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   127
        Config.command(command, config) +
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   128
        (if (opts.nonEmpty) " " + opts else "") +
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   129
        (if (args.nonEmpty) " -- " + args else "")
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   130
      Isabelle_System.bash(cmd, progress_stdout = progress_stdout,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   131
        progress_stderr = progress_stderr, strict = strict)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   132
    }
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   133
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   134
    def run_sftp(script: String, opts: String = "", args: String = ""): Process_Result = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   135
      Isabelle_System.with_tmp_file("sftp") { tmp_path =>
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   136
        File.write(tmp_path, script)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   137
        val opts1 = "-b " + File.bash_path(tmp_path) + (if (opts.nonEmpty) " " + opts else "")
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   138
        val args1 = Bash.string(host) + (if (args.nonEmpty) " " + args else "")
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   139
        run_command("sftp", opts = opts1, args = args1)
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   140
      }
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   141
    }
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   142
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   143
    def run_ssh(master: Boolean = false, opts: String = "", args: String = ""): Process_Result = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   144
      val args1 = Bash.string(host) + (if (args.nonEmpty) " " + args else "")
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   145
      run_command("ssh", master = master, opts = opts, args = args1)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   146
    }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   147
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   148
    def run_scp(opts: String = "", args: String = ""): Process_Result = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   149
      val opts1 = "-s -p -q" + (if (opts.nonEmpty) " " + opts else "")
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   150
      run_command("scp", opts = opts1, args = args)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   151
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   152
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   153
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   154
    /* init and exit */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   155
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   156
    val user_home: String = run_ssh(master = control_master, args = "printenv HOME").check.out
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   157
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   158
    val settings: JMap[String, String] = JMap.of("HOME", user_home, "USER_HOME", user_home)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   159
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   160
    override def close(): Unit = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   161
      if (control_path.nonEmpty) run_ssh(opts = "-O exit").check
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   162
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   163
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   164
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   165
    /* remote commands */
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   166
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   167
    override def execute(cmd_line: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   168
      progress_stdout: String => Unit = (_: String) => (),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   169
      progress_stderr: String => Unit = (_: String) => (),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   170
      settings: Boolean = true,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   171
      strict: Boolean = true
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   172
    ): Process_Result = {
76142
e8d4013c49d1 more robust adhoc shell script: work with Isabelle_System.export_isabelle_identifier;
wenzelm
parents: 76136
diff changeset
   173
      val args1 =
e8d4013c49d1 more robust adhoc shell script: work with Isabelle_System.export_isabelle_identifier;
wenzelm
parents: 76136
diff changeset
   174
        Bash.string(host) + " export " + Bash.string("USER_HOME=\"$HOME\"") + "\n" + cmd_line
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   175
      run_command("ssh", args = args1, progress_stdout = progress_stdout,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   176
        progress_stderr = progress_stderr, strict = strict)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   177
    }
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   178
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   179
    override lazy val isabelle_platform: Isabelle_Platform = Isabelle_Platform(ssh = Some(ssh))
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   180
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   181
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   182
    /* remote file-system */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   183
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   184
    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
   185
    def remote_path(path: Path): String = expand_path(path).implode
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   186
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   187
    override def bash_path(path: Path): String = Bash.string(remote_path(path))
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   188
    def sftp_path(path: Path): String = sftp_string(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
   189
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   190
    def rm(path: Path): Unit = run_sftp("rm " + sftp_path(path)).check
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
   191
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   192
    override def is_dir(path: Path): Boolean = run_ssh(args = "test -d " + bash_path(path)).ok
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   193
    override def is_file(path: Path): Boolean = run_ssh(args = "test -f " + bash_path(path)).ok
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
   194
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   195
    override def make_directory(path: Path): Path = {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   196
      if (!execute("mkdir -p " + bash_path(path)).ok) {
76118
e8e3b60d8ecd clarified operation: avoid perl;
wenzelm
parents: 76117
diff changeset
   197
        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
   198
      }
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   199
      path
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   200
    }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   201
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   202
    def read_dir(path: Path): List[String] =
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   203
      run_sftp("ls -1 -a " + sftp_path(path)).check.out_lines.flatMap(s =>
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   204
        if (s == "." || s == ".." || s.endsWith("/.") || s.endsWith("/..")) None
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   205
        else Some(Library.perhaps_unprefix("./", s)))
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   206
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   207
    override def read_file(path: Path, local_path: Path): Unit =
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   208
      run_scp(args = Bash.string(host + ":" + remote_path(path)) + " " +
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   209
        File.bash_platform_path(local_path)).check
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   210
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   211
    override def read_bytes(path: Path): Bytes =
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   212
      Isabelle_System.with_tmp_file("scp") { local_path =>
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   213
        read_file(path, local_path)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   214
        Bytes.read(local_path)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   215
      }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   216
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   217
    override def read(path: Path): String = read_bytes(path).text
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   218
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   219
    override def write_file(path: Path, local_path: Path): Unit =
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   220
      run_scp(args = File.bash_platform_path(local_path) + " " +
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   221
        Bash.string(host + ":" + remote_path(path))).check
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   222
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   223
    def write_bytes(path: Path, bytes: Bytes): Unit =
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   224
      Isabelle_System.with_tmp_file("scp") { local_path =>
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   225
        Bytes.write(local_path, bytes)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   226
        write_file(path, local_path)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   227
      }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   228
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   229
    def write(path: Path, text: String): Unit = write_bytes(path, Bytes(text))
72338
54871a086193 formal platform information, notably for ssh;
wenzelm
parents: 71780
diff changeset
   230
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   231
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   232
    /* tmp dirs */
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   233
64306
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   234
    def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir))
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   235
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   236
    def rm_tree(remote_dir: String): Unit =
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64258
diff changeset
   237
      execute("rm -r -f " + Bash.string(remote_dir)).check
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   238
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   239
    def tmp_dir(): String =
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   240
      execute("mktemp -d -t tmp.XXXXXXXXXX").check.out
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   241
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   242
    override def with_tmp_dir[A](body: Path => A): A = {
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   243
      val remote_dir = tmp_dir()
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   244
      try { body(Path.explode(remote_dir)) }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   245
      finally { rm_tree(remote_dir) }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   246
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   247
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   248
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   249
    /* port forwarding */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   250
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   251
    def port_forwarding(
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   252
      remote_port: Int,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   253
      remote_host: String = "localhost",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   254
      local_port: Int = 0,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   255
      local_host: String = "localhost",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   256
      ssh_close: Boolean = false
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   257
    ): Port_Forwarding = {
76145
a6bdf4b889ca clarified signature;
wenzelm
parents: 76144
diff changeset
   258
      val port = if (local_port > 0) local_port else Isabelle_System.local_port()
a6bdf4b889ca clarified signature;
wenzelm
parents: 76144
diff changeset
   259
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   260
      val forward = List(local_host, port, remote_host, remote_port).mkString(":")
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   261
      val forward_option = "-L " + Bash.string(forward)
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   262
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   263
      val cancel: () => Unit =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   264
        if (control_path.nonEmpty) {
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   265
          run_ssh(opts = forward_option + " -O forward").check
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   266
          () => run_ssh(opts = forward_option + " -O cancel")  // permissive
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   267
        }
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   268
        else error("SSH port forwarding requires multiplexing")
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   269
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   270
      val shutdown_hook =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   271
        Isabelle_System.create_shutdown_hook { cancel() }
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   272
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   273
      new Port_Forwarding(host, port, remote_host, remote_port) {
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   274
        override def toString: String = forward
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   275
        override def close(): Unit = {
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   276
          cancel()
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   277
          Isabelle_System.remove_shutdown_hook(shutdown_hook)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   278
          if (ssh_close) ssh.close()
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   279
        }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   280
      }
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   281
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   282
  }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   283
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   284
  abstract class Port_Forwarding private[SSH](
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   285
    val host: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   286
    val port: Int,
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   287
    val remote_host: String,
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   288
    val remote_port: Int
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   289
  ) extends AutoCloseable
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   290
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   291
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   292
  /* system operations */
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   293
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   294
  trait System extends AutoCloseable {
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   295
    def close(): Unit = ()
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   296
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   297
    def hg_url: String = ""
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   298
75500
57e292106d71 more operations;
wenzelm
parents: 75480
diff changeset
   299
    def rsync_prefix: String = ""
75517
292d7a9dc8a3 proper operation on String, not Path;
wenzelm
parents: 75513
diff changeset
   300
    def rsync_path(path: Path): String = rsync_prefix + expand_path(path).implode
75500
57e292106d71 more operations;
wenzelm
parents: 75480
diff changeset
   301
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   302
    def expand_path(path: Path): Path = path.expand
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   303
    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
   304
    def is_dir(path: Path): Boolean = path.is_dir
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   305
    def is_file(path: Path): Boolean = path.is_file
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   306
    def make_directory(path: Path): Path = Isabelle_System.make_directory(path)
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   307
    def with_tmp_dir[A](body: Path => A): A = Isabelle_System.with_tmp_dir("tmp")(body)
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   308
    def read_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path1, path2)
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   309
    def write_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path2, path1)
75513
36316c6a3fc2 clarified signature: more operations;
wenzelm
parents: 75500
diff changeset
   310
    def read_bytes(path: Path): Bytes = Bytes.read(path)
36316c6a3fc2 clarified signature: more operations;
wenzelm
parents: 75500
diff changeset
   311
    def read(path: Path): String = File.read(path)
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   312
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   313
    def execute(command: String,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   314
        progress_stdout: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   315
        progress_stderr: String => Unit = (_: String) => (),
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   316
        settings: Boolean = true,
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   317
        strict: Boolean = true): Process_Result =
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   318
      Isabelle_System.bash(command,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   319
        progress_stdout = progress_stdout,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   320
        progress_stderr = progress_stderr,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   321
        env = if (settings) Isabelle_System.settings() else null,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   322
        strict = strict)
72338
54871a086193 formal platform information, notably for ssh;
wenzelm
parents: 71780
diff changeset
   323
72340
676066aa4798 clarified signature;
wenzelm
parents: 72338
diff changeset
   324
    def isabelle_platform: Isabelle_Platform = Isabelle_Platform()
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   325
  }
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   326
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   327
  object Local extends System
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   328
}