ssh.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016-2021, 2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix ssh)
  19. #:use-module (guix store)
  20. #:use-module (guix inferior)
  21. #:use-module (guix i18n)
  22. #:use-module ((guix diagnostics)
  23. #:select (info &fix-hint formatted-message))
  24. #:use-module ((guix progress)
  25. #:select (progress-bar
  26. erase-current-line current-terminal-columns))
  27. #:use-module (gcrypt pk-crypto)
  28. #:use-module (ssh session)
  29. #:use-module (ssh auth)
  30. #:use-module (ssh key)
  31. #:use-module (ssh channel)
  32. #:use-module (ssh popen)
  33. #:use-module (ssh session)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-11)
  36. #:use-module (srfi srfi-26)
  37. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-35)
  39. #:use-module (ice-9 match)
  40. #:use-module (ice-9 format)
  41. #:use-module (ice-9 binary-ports)
  42. #:use-module (ice-9 vlist)
  43. #:export (open-ssh-session
  44. authenticate-server*
  45. remote-inferior
  46. remote-daemon-channel
  47. connect-to-remote-daemon
  48. remote-system
  49. remote-authorize-signing-key
  50. send-files
  51. retrieve-files
  52. retrieve-files*
  53. remote-store-host
  54. report-guile-error))
  55. ;;; Commentary:
  56. ;;;
  57. ;;; This module provides tools to support communication with remote stores
  58. ;;; over SSH, using Guile-SSH.
  59. ;;;
  60. ;;; Code:
  61. (define %compression
  62. "zlib@openssh.com,zlib")
  63. (define (host-key->type+key host-key)
  64. "Destructure HOST-KEY, an OpenSSH host key string, and return two values:
  65. its key type as a symbol, and the actual base64-encoded string."
  66. (define (type->symbol type)
  67. (and (string-prefix? "ssh-" type)
  68. (string->symbol (string-drop type 4))))
  69. (match (string-tokenize host-key)
  70. ((type key x)
  71. (values (type->symbol type) key))
  72. ((type key)
  73. (values (type->symbol type) key))))
  74. (define (authenticate-server* session key)
  75. "Make sure the server for SESSION has the given KEY, where KEY is a string
  76. such as \"ssh-ed25519 AAAAC3Nz… root@example.org\". Raise an exception if the
  77. actual key does not match."
  78. (let-values (((server) (get-server-public-key session))
  79. ((type key) (host-key->type+key key)))
  80. (unless (and (or (not (get-key-type server))
  81. (eq? (get-key-type server) type))
  82. (string=? (public-key->string server) key))
  83. ;; Key mismatch: something's wrong. XXX: It could be that the server
  84. ;; provided its Ed25519 key when we where expecting its RSA key. XXX:
  85. ;; Guile-SSH 0.10.1 doesn't know about ed25519 keys and 'get-key-type'
  86. ;; returns #f in that case.
  87. (raise (formatted-message (G_ "server at '~a' returned host key \
  88. '~a' of type '~a' instead of '~a' of type '~a'~%")
  89. (session-get session 'host)
  90. (public-key->string server)
  91. (get-key-type server)
  92. key type)))))
  93. (define* (open-ssh-session host #:key user port identity
  94. host-key
  95. (compression %compression)
  96. (timeout 3600)
  97. (connection-timeout 10))
  98. "Open an SSH session for HOST and return it. IDENTITY specifies the file
  99. name of a private key to use for authenticating with the host. When USER,
  100. PORT, or IDENTITY are #f, use default values or whatever '~/.ssh/config'
  101. specifies; otherwise use them.
  102. When HOST-KEY is true, it must be a string like \"ssh-ed25519 AAAAC3Nz…
  103. root@example.org\"; the server is authenticated and an error is raised if its
  104. host key is different from HOST-KEY.
  105. Error out if connection establishment takes more than CONNECTION-TIMEOUT
  106. seconds. Install TIMEOUT as the maximum time in seconds after which a read or
  107. write operation on a channel of the returned session is considered as failing.
  108. Throw an error on failure."
  109. (let ((session (make-session #:user user
  110. #:identity identity
  111. #:host host
  112. #:port port
  113. #:timeout connection-timeout
  114. ;; #:log-verbosity 'protocol
  115. ;; Prevent libssh from reading
  116. ;; ~/.ssh/known_hosts when the caller provides
  117. ;; a HOST-KEY to match against.
  118. #:knownhosts (and host-key "/dev/null")
  119. ;; We need lightweight compression when
  120. ;; exchanging full archives.
  121. #:compression compression
  122. #:compression-level 3
  123. ;; Speed up RPCs by creating sockets with
  124. ;; TCP_NODELAY.
  125. #:nodelay #t)))
  126. ;; Honor ~/.ssh/config.
  127. (session-parse-config! session)
  128. (match (connect! session)
  129. ('ok
  130. (if host-key
  131. ;; Make sure the server's key is what we expect.
  132. (authenticate-server* session host-key)
  133. ;; Authenticate against ~/.ssh/known_hosts.
  134. (match (authenticate-server session)
  135. ('ok #f)
  136. (reason
  137. (raise (formatted-message (G_ "failed to authenticate \
  138. server at '~a': ~a")
  139. (session-get session 'host)
  140. reason)))))
  141. ;; Use public key authentication, via the SSH agent if it's available.
  142. (match (userauth-public-key/auto! session)
  143. ('success
  144. (session-set! session 'timeout timeout)
  145. session)
  146. ('again
  147. (raise (formatted-message (G_ "timeout while connecting \
  148. to SSH server at '~a'")
  149. (session-get session 'host))))
  150. (x
  151. (match (userauth-gssapi! session)
  152. ('success
  153. (session-set! session 'timeout timeout)
  154. session)
  155. (x
  156. (disconnect! session)
  157. (raise (condition
  158. (&message
  159. (message (format #f (G_ "SSH authentication failed for '~a@~a': ~a~%")
  160. (session-get session 'user) host
  161. (get-error session)))))))))))
  162. (x
  163. ;; Connection failed or timeout expired.
  164. (raise (formatted-message (G_ "SSH connection to '~a' failed: ~a~%")
  165. host (get-error session)))))))
  166. (define* (remote-inferior session #:optional become-command)
  167. "Return a remote inferior for the given SESSION. If BECOME-COMMAND is
  168. given, use that to invoke the remote Guile REPL."
  169. (let* ((repl-command (append (or become-command '())
  170. '("guix" "repl" "-t" "machine")))
  171. (pipe (apply open-remote-pipe* session OPEN_BOTH repl-command)))
  172. (when (eof-object? (peek-char pipe))
  173. (let ((status (channel-get-exit-status pipe)))
  174. (close-port pipe)
  175. (raise (formatted-message (G_ "remote command '~{~a~^ ~}' failed \
  176. with status ~a")
  177. repl-command status))))
  178. (port->inferior pipe)))
  179. (define* (inferior-remote-eval exp session #:optional become-command)
  180. "Evaluate EXP in a new inferior running in SESSION, and close the inferior
  181. right away. If BECOME-COMMAND is given, use that to invoke the remote Guile
  182. REPL."
  183. (let ((inferior (remote-inferior session become-command)))
  184. (dynamic-wind
  185. (const #t)
  186. (lambda ()
  187. (inferior-eval exp inferior))
  188. (lambda ()
  189. ;; Close INFERIOR right away to prevent finalization from happening in
  190. ;; another thread at the wrong time (see
  191. ;; <https://bugs.gnu.org/26976>.)
  192. (close-inferior inferior)))))
  193. (define (remote-run exp session)
  194. "Run EXP in a new process in SESSION and return a remote pipe.
  195. Unlike 'inferior-remote-eval', this is used for side effects and may
  196. communicate over stdout/stdin as it sees fit. EXP is typically a loop that
  197. processes data from stdin and/or sends data to stdout. The assumption is that
  198. EXP never returns or calls 'primitive-exit' when it's done."
  199. (define pipe
  200. (open-remote-pipe* session OPEN_BOTH
  201. "guix" "repl" "-t" "machine"))
  202. (match (read pipe)
  203. (('repl-version _ ...)
  204. #t)
  205. ((? eof-object?)
  206. (close-port pipe)
  207. (raise (formatted-message
  208. (G_ "failed to start 'guix repl' on '~a'")
  209. (session-get session 'host)))))
  210. ;; Disable buffering so 'guix repl' does not read more than what's really
  211. ;; sent to itself.
  212. (write '(setvbuf (current-input-port) 'none) pipe)
  213. (force-output pipe)
  214. ;; Read the reply and subsequent newline.
  215. (read pipe) (get-u8 pipe)
  216. (write exp pipe)
  217. (force-output pipe)
  218. ;; From now on, we stop following the inferior protocol.
  219. pipe)
  220. (define* (remote-daemon-channel session
  221. #:optional
  222. (socket-name
  223. "/var/guix/daemon-socket/socket"))
  224. "Return an input/output port (an SSH channel) to the daemon at SESSION."
  225. (define redirect
  226. ;; Code run in SESSION to redirect the remote process' stdin/stdout to the
  227. ;; daemon's socket, à la socat. The SSH protocol supports forwarding to
  228. ;; Unix-domain sockets but libssh doesn't have an API for that, hence this
  229. ;; hack.
  230. `(begin
  231. (use-modules (ice-9 match) (rnrs io ports)
  232. (rnrs bytevectors))
  233. (define connect-to-daemon
  234. ;; XXX: 'connect-to-daemon' used to be private and before that it
  235. ;; didn't even exist, hence these shenanigans.
  236. (let ((connect-to-daemon
  237. (false-if-exception (module-ref (resolve-module '(guix store))
  238. 'connect-to-daemon))))
  239. (lambda (uri)
  240. (if connect-to-daemon
  241. (connect-to-daemon uri)
  242. (let ((sock (socket AF_UNIX SOCK_STREAM 0)))
  243. (connect sock AF_UNIX ,socket-name)
  244. sock)))))
  245. ;; Use 'connect-to-daemon' to honor GUIX_DAEMON_SOCKET.
  246. (let ((sock (connect-to-daemon (or (getenv "GUIX_DAEMON_SOCKET")
  247. ,socket-name)))
  248. (stdin (current-input-port))
  249. (stdout (current-output-port))
  250. (select* (lambda (read write except)
  251. ;; This is a workaround for
  252. ;; <https://bugs.gnu.org/30365> in Guile < 2.2.4:
  253. ;; since 'select' sometimes returns non-empty sets for
  254. ;; no good reason, call 'select' a second time with a
  255. ;; zero timeout to filter out incorrect replies.
  256. (match (select read write except)
  257. ((read write except)
  258. (select read write except 0))))))
  259. (setvbuf stdout 'none)
  260. ;; Use buffered ports so that 'get-bytevector-some' returns up to the
  261. ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
  262. (setvbuf stdin 'block 65536)
  263. (setvbuf sock 'block 65536)
  264. (let loop ()
  265. (match (select* (list stdin sock) '() '())
  266. ((reads () ())
  267. (when (memq stdin reads)
  268. (match (get-bytevector-some stdin)
  269. ((? eof-object?)
  270. (primitive-exit 0))
  271. (bv
  272. (put-bytevector sock bv)
  273. (force-output sock))))
  274. (when (memq sock reads)
  275. (match (get-bytevector-some sock)
  276. ((? eof-object?)
  277. (primitive-exit 0))
  278. (bv
  279. (put-bytevector stdout bv))))
  280. (loop))
  281. (_
  282. (primitive-exit 1)))))))
  283. (remote-run redirect session))
  284. (define* (connect-to-remote-daemon session
  285. #:optional
  286. (socket-name
  287. "/var/guix/daemon-socket/socket"))
  288. "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
  289. an SSH session. Return a <store-connection> object."
  290. (guard (c ((store-connection-error? c)
  291. ;; Raise a more focused error condition.
  292. (raise (formatted-message
  293. (G_ "failed to connect over SSH to daemon at '~a', socket ~a")
  294. (session-get session 'host)
  295. socket-name))))
  296. (open-connection #:port (remote-daemon-channel session socket-name))))
  297. (define (store-import-channel session)
  298. "Return an output port to which archives to be exported to SESSION's store
  299. can be written."
  300. ;; Using the 'import-paths' RPC on a remote store would be slow because it
  301. ;; makes a round trip every time 32 KiB have been transferred. This
  302. ;; procedure instead opens a separate channel to use the remote
  303. ;; 'import-paths' procedure, which consumes all the data in a single round
  304. ;; trip. This optimizes the successful case at the expense of error
  305. ;; conditions: errors can only be reported once all the input has been
  306. ;; consumed.
  307. (define import
  308. `(begin
  309. (use-modules (guix) (srfi srfi-34)
  310. (rnrs io ports) (rnrs bytevectors))
  311. (define (consume-input port)
  312. (let ((bv (make-bytevector 32768)))
  313. (let loop ()
  314. (let ((n (get-bytevector-n! port bv 0
  315. (bytevector-length bv))))
  316. (unless (eof-object? n)
  317. (loop))))))
  318. ;; Upon completion, write an sexp that denotes the status.
  319. (write
  320. (catch #t
  321. (lambda ()
  322. (guard (c ((nix-protocol-error? c)
  323. ;; Consume all the input since the only time we can
  324. ;; report the error is after everything has been
  325. ;; consumed.
  326. (consume-input (current-input-port))
  327. (list 'protocol-error (nix-protocol-error-message c))))
  328. (with-store store
  329. (write '(importing)) ;we're ready
  330. (force-output)
  331. (setvbuf (current-input-port) 'none)
  332. ;; If 'guix-daemon' is running with '--debug', a lot of
  333. ;; debugging info goes to 'current-build-output-port' (stderr
  334. ;; by default). However, since nobody's reading it, this
  335. ;; could lead to a deadlock. Thus, disable debugging output.
  336. (set-build-options store #:verbosity 0)
  337. (import-paths store (current-input-port))
  338. '(success))))
  339. (lambda args
  340. (cons 'error args))))
  341. (primitive-exit 0)))
  342. (remote-run import session))
  343. (define* (store-export-channel session files
  344. #:key recursive?)
  345. "Return an input port from which an export of FILES from SESSION's store can
  346. be read. When RECURSIVE? is true, the closure of FILES is exported."
  347. ;; Same as above: this is more efficient than calling 'export-paths' on a
  348. ;; remote store.
  349. (define export
  350. `(begin
  351. (use-modules (guix) (srfi srfi-1)
  352. (srfi srfi-26) (srfi srfi-34))
  353. (guard (c ((nix-connection-error? c)
  354. (write `(connection-error ,(nix-connection-error-file c)
  355. ,(nix-connection-error-code c)))
  356. (primitive-exit 1))
  357. ((nix-protocol-error? c)
  358. (write `(protocol-error ,(nix-protocol-error-status c)
  359. ,(nix-protocol-error-message c)))
  360. (primitive-exit 2))
  361. (else
  362. (write `(exception))
  363. (primitive-exit 3)))
  364. (with-store store
  365. (let* ((files ',files)
  366. (invalid (remove (cut valid-path? store <>)
  367. files)))
  368. (unless (null? invalid)
  369. (write `(invalid-items ,invalid))
  370. (exit 1))
  371. ;; TODO: When RECURSIVE? is true, we could send the list of store
  372. ;; items in the closure so that the other end can filter out
  373. ;; those it already has.
  374. (write '(exporting)) ;we're ready
  375. (force-output)
  376. (setvbuf (current-output-port) 'none)
  377. (export-paths store files (current-output-port)
  378. #:recursive? ,recursive?)
  379. (primitive-exit 0))))))
  380. (remote-run export session))
  381. (define (remote-system session)
  382. "Return the system type as expected by Nix, usually ARCHITECTURE-KERNEL, of
  383. the machine on the other end of SESSION."
  384. (inferior-remote-eval '(begin (use-modules (guix utils)) (%current-system))
  385. session))
  386. (define* (remote-authorize-signing-key key session #:optional become-command)
  387. "Send KEY, a canonical sexp containing a public key, over SESSION and add it
  388. to the system ACL file if it has not yet been authorized."
  389. (inferior-remote-eval
  390. `(begin
  391. (use-modules (guix build utils)
  392. (guix pki)
  393. (guix utils)
  394. (gcrypt pk-crypto)
  395. (srfi srfi-26))
  396. (define acl (current-acl))
  397. (define key (string->canonical-sexp ,(canonical-sexp->string key)))
  398. (unless (authorized-key? key)
  399. (let ((acl (public-keys->acl (cons key (acl->public-keys acl)))))
  400. (mkdir-p (dirname %acl-file))
  401. (with-atomic-file-output %acl-file
  402. (cut write-acl acl <>)))))
  403. session
  404. become-command))
  405. (define (prepare-to-send store host log-port items)
  406. "Notify the user that we're about to send ITEMS to HOST. Return three
  407. values allowing 'notify-send-progress' to track the state of this transfer."
  408. (let* ((count (length items))
  409. (sizes (fold (lambda (item result)
  410. (vhash-cons item
  411. (path-info-nar-size
  412. (query-path-info store item))
  413. result))
  414. vlist-null
  415. items))
  416. (total (vlist-fold (lambda (pair result)
  417. (match pair
  418. ((_ . size) (+ size result))))
  419. 0
  420. sizes)))
  421. (info (N_ "sending ~a store item (~h MiB) to '~a'...~%"
  422. "sending ~a store items (~h MiB) to '~a'...~%" count)
  423. count
  424. (inexact->exact (round (/ total (expt 2. 20))))
  425. host)
  426. (values log-port sizes total 0)))
  427. (define (notify-transfer-progress item port sizes total sent)
  428. "Notify the user that we've already transferred SENT bytes out of TOTAL.
  429. Use SIZES to determine the size of ITEM, which is about to be sent."
  430. (define (display-bar %)
  431. (erase-current-line port)
  432. (format port "~3@a% ~a"
  433. (inexact->exact (round %))
  434. (progress-bar % (- (max (current-terminal-columns) 5) 5)))
  435. (force-output port))
  436. (unless (zero? total)
  437. (let ((% (* 100. (/ sent total))))
  438. (match (vhash-assoc item sizes)
  439. (#f
  440. (display-bar %)
  441. (values port sizes total sent))
  442. ((_ . size)
  443. (display-bar %)
  444. (values port sizes total (+ sent size)))))))
  445. (define (notify-transfer-completion port . args)
  446. "Notify the user that the transfer has completed."
  447. (apply notify-transfer-progress "" port args) ;display the 100% progress bar
  448. (erase-current-line port)
  449. (force-output port))
  450. (define* (send-files local files remote
  451. #:key
  452. recursive?
  453. (log-port (current-error-port)))
  454. "Send the subset of FILES from LOCAL (a local store) that's missing to
  455. REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES.
  456. Return the list of store items actually sent."
  457. ;; Compute the subset of FILES missing on SESSION and send them.
  458. (let* ((files (if recursive? (requisites local files) files))
  459. (session (channel-get-session (store-connection-socket remote)))
  460. (missing (inferior-remote-eval
  461. `(begin
  462. (use-modules (guix)
  463. (srfi srfi-1) (srfi srfi-26))
  464. (with-store store
  465. (remove (cut valid-path? store <>)
  466. ',files)))
  467. session))
  468. (port (store-import-channel session))
  469. (host (session-get session 'host)))
  470. ;; Make sure everything alright on the remote side.
  471. (match (read port)
  472. (('importing)
  473. #t)
  474. (sexp
  475. (handle-import/export-channel-error sexp remote)))
  476. ;; Send MISSING in topological order.
  477. (let ((tty? (isatty? log-port)))
  478. (export-paths local missing port
  479. #:start (cut prepare-to-send local host log-port <>)
  480. #:progress (if tty? notify-transfer-progress (const #f))
  481. #:finish (if tty? notify-transfer-completion (const #f))))
  482. ;; Tell the remote process that we're done. (In theory the end-of-archive
  483. ;; mark of 'export-paths' would be enough, but in practice it's not.)
  484. (channel-send-eof port)
  485. ;; Wait for completion of the remote process and read the status sexp from
  486. ;; PORT. Wait for the exit status only when 'read' completed; otherwise,
  487. ;; we might wait forever if the other end is stuck.
  488. (let* ((result (false-if-exception (read port)))
  489. (status (and result
  490. (zero? (channel-get-exit-status port)))))
  491. (close-port port)
  492. (match result
  493. (('success . _)
  494. missing)
  495. (('protocol-error message)
  496. (raise (condition
  497. (&store-protocol-error (message message) (status 42)))))
  498. (('error key args ...)
  499. (raise (condition
  500. (&store-protocol-error
  501. (message (call-with-output-string
  502. (lambda (port)
  503. (print-exception port #f key args))))
  504. (status 43)))))
  505. (_
  506. (raise (condition
  507. (&store-protocol-error
  508. (message "unknown error while sending files over SSH")
  509. (status 44)))))))))
  510. (define (remote-store-session remote)
  511. "Return the SSH channel beneath REMOTE, a remote store as returned by
  512. 'connect-to-remote-daemon', or #f."
  513. (channel-get-session (store-connection-socket remote)))
  514. (define (remote-store-host remote)
  515. "Return the name of the host REMOTE is connected to, where REMOTE is a
  516. remote store as returned by 'connect-to-remote-daemon'."
  517. (match (remote-store-session remote)
  518. (#f #f)
  519. ((? session? session)
  520. (session-get session 'host))))
  521. (define* (file-retrieval-port files remote
  522. #:key recursive?)
  523. "Return an input port from which to retrieve FILES (a list of store items)
  524. from REMOTE, along with the number of items to retrieve (lower than or equal
  525. to the length of FILES.)"
  526. (values (store-export-channel (remote-store-session remote) files
  527. #:recursive? recursive?)
  528. (length files))) ;XXX: inaccurate when RECURSIVE? is true
  529. (define-syntax raise-error
  530. (syntax-rules (=>)
  531. ((_ fmt args ... (=> hint-fmt hint-args ...))
  532. (raise (condition
  533. (&message
  534. (message (format #f fmt args ...)))
  535. (&fix-hint
  536. (hint (format #f hint-fmt hint-args ...))))))
  537. ((_ fmt args ...)
  538. (raise (condition
  539. (&message
  540. (message (format #f fmt args ...))))))))
  541. (define (handle-import/export-channel-error sexp remote)
  542. "Report an error corresponding to SEXP, the EOF object or an sexp read from
  543. REMOTE."
  544. (match sexp
  545. ((? eof-object?)
  546. (report-guile-error (remote-store-host remote)))
  547. (('connection-error file code . _)
  548. (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
  549. file (remote-store-host remote) (strerror code)))
  550. (('invalid-items items . _)
  551. (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
  552. "no such items on remote host '~A':~{ ~a~}"
  553. (length items))
  554. (remote-store-host remote) items))
  555. (('protocol-error status message . _)
  556. (raise-error (G_ "protocol error on remote host '~A': ~a")
  557. (remote-store-host remote) message))
  558. (_
  559. (raise-error (G_ "failed to retrieve store items from '~a'")
  560. (remote-store-host remote)))))
  561. (define* (retrieve-files* files remote
  562. #:key recursive? (log-port (current-error-port))
  563. (import (const #f)))
  564. "Pass IMPORT an input port from which to read the sequence of FILES coming
  565. from REMOTE. When RECURSIVE? is true, retrieve the closure of FILES."
  566. (let-values (((port count)
  567. (file-retrieval-port files remote
  568. #:recursive? recursive?)))
  569. (match (read port) ;read the initial status
  570. (('exporting)
  571. (format #t (N_ "retrieving ~a store item from '~a'...~%"
  572. "retrieving ~a store items from '~a'...~%" count)
  573. count (remote-store-host remote))
  574. (dynamic-wind
  575. (const #t)
  576. (lambda ()
  577. (import port))
  578. (lambda ()
  579. (close-port port))))
  580. (sexp
  581. (handle-import/export-channel-error sexp remote)))))
  582. (define* (retrieve-files local files remote
  583. #:key recursive? (log-port (current-error-port)))
  584. "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
  585. LOCAL. When RECURSIVE? is true, retrieve the closure of FILES."
  586. (retrieve-files* (remove (cut valid-path? local <>) files)
  587. remote
  588. #:recursive? recursive?
  589. #:log-port log-port
  590. #:import (lambda (port)
  591. (import-paths local port))))
  592. ;;;
  593. ;;; Error reporting.
  594. ;;;
  595. (define (report-guile-error host)
  596. (raise-error (G_ "failed to start Guile on remote host '~A'") host
  597. (=> (G_ "Make sure @command{guile} can be found in
  598. @code{$PATH} on the remote host. Run @command{ssh ~A guile --version} to
  599. check.")
  600. host)))
  601. (define (report-inferior-exception exception host)
  602. "Report EXCEPTION, an &inferior-exception that occurred on HOST."
  603. (raise-error (G_ "exception occurred on remote host '~A': ~s")
  604. host (inferior-exception-arguments exception)))
  605. ;;; ssh.scm ends here