ssh.scm 25 KB

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