ssh.scm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021, 2021 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. (define connect-to-daemon
  227. ;; XXX: 'connect-to-daemon' used to be private and before that it
  228. ;; didn't even exist, hence these shenanigans.
  229. (let ((connect-to-daemon
  230. (false-if-exception (module-ref (resolve-module '(guix store))
  231. 'connect-to-daemon))))
  232. (lambda (uri)
  233. (if connect-to-daemon
  234. (connect-to-daemon uri)
  235. (let ((sock (socket AF_UNIX SOCK_STREAM 0)))
  236. (connect sock AF_UNIX ,socket-name)
  237. sock)))))
  238. ;; Use 'connect-to-daemon' to honor GUIX_DAEMON_SOCKET.
  239. (let ((sock (connect-to-daemon (or (getenv "GUIX_DAEMON_SOCKET")
  240. ,socket-name)))
  241. (stdin (current-input-port))
  242. (stdout (current-output-port))
  243. (select* (lambda (read write except)
  244. ;; This is a workaround for
  245. ;; <https://bugs.gnu.org/30365> in Guile < 2.2.4:
  246. ;; since 'select' sometimes returns non-empty sets for
  247. ;; no good reason, call 'select' a second time with a
  248. ;; zero timeout to filter out incorrect replies.
  249. (match (select read write except)
  250. ((read write except)
  251. (select read write except 0))))))
  252. (setvbuf stdout 'none)
  253. ;; Use buffered ports so that 'get-bytevector-some' returns up to the
  254. ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
  255. (setvbuf stdin 'block 65536)
  256. (setvbuf sock 'block 65536)
  257. (let loop ()
  258. (match (select* (list stdin sock) '() '())
  259. ((reads () ())
  260. (when (memq stdin reads)
  261. (match (get-bytevector-some stdin)
  262. ((? eof-object?)
  263. (primitive-exit 0))
  264. (bv
  265. (put-bytevector sock bv)
  266. (force-output sock))))
  267. (when (memq sock reads)
  268. (match (get-bytevector-some sock)
  269. ((? eof-object?)
  270. (primitive-exit 0))
  271. (bv
  272. (put-bytevector stdout bv))))
  273. (loop))
  274. (_
  275. (primitive-exit 1)))))))
  276. (remote-run redirect session))
  277. (define* (connect-to-remote-daemon session
  278. #:optional
  279. (socket-name
  280. "/var/guix/daemon-socket/socket"))
  281. "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
  282. an SSH session. Return a <store-connection> object."
  283. (guard (c ((store-connection-error? c)
  284. ;; Raise a more focused error condition.
  285. (raise (formatted-message
  286. (G_ "failed to connect over SSH to daemon at '~a', socket ~a")
  287. (session-get session 'host)
  288. socket-name))))
  289. (open-connection #:port (remote-daemon-channel session socket-name))))
  290. (define (store-import-channel session)
  291. "Return an output port to which archives to be exported to SESSION's store
  292. can be written."
  293. ;; Using the 'import-paths' RPC on a remote store would be slow because it
  294. ;; makes a round trip every time 32 KiB have been transferred. This
  295. ;; procedure instead opens a separate channel to use the remote
  296. ;; 'import-paths' procedure, which consumes all the data in a single round
  297. ;; trip. This optimizes the successful case at the expense of error
  298. ;; conditions: errors can only be reported once all the input has been
  299. ;; consumed.
  300. (define import
  301. `(begin
  302. (use-modules (guix) (srfi srfi-34)
  303. (rnrs io ports) (rnrs bytevectors))
  304. (define (consume-input port)
  305. (let ((bv (make-bytevector 32768)))
  306. (let loop ()
  307. (let ((n (get-bytevector-n! port bv 0
  308. (bytevector-length bv))))
  309. (unless (eof-object? n)
  310. (loop))))))
  311. ;; Upon completion, write an sexp that denotes the status.
  312. (write
  313. (catch #t
  314. (lambda ()
  315. (guard (c ((nix-protocol-error? c)
  316. ;; Consume all the input since the only time we can
  317. ;; report the error is after everything has been
  318. ;; consumed.
  319. (consume-input (current-input-port))
  320. (list 'protocol-error (nix-protocol-error-message c))))
  321. (with-store store
  322. (write '(importing)) ;we're ready
  323. (force-output)
  324. (setvbuf (current-input-port) 'none)
  325. (import-paths store (current-input-port))
  326. '(success))))
  327. (lambda args
  328. (cons 'error args))))
  329. (primitive-exit 0)))
  330. (remote-run import session))
  331. (define* (store-export-channel session files
  332. #:key recursive?)
  333. "Return an input port from which an export of FILES from SESSION's store can
  334. be read. When RECURSIVE? is true, the closure of FILES is exported."
  335. ;; Same as above: this is more efficient than calling 'export-paths' on a
  336. ;; remote store.
  337. (define export
  338. `(begin
  339. (use-modules (guix) (srfi srfi-1)
  340. (srfi srfi-26) (srfi srfi-34))
  341. (guard (c ((nix-connection-error? c)
  342. (write `(connection-error ,(nix-connection-error-file c)
  343. ,(nix-connection-error-code c)))
  344. (primitive-exit 1))
  345. ((nix-protocol-error? c)
  346. (write `(protocol-error ,(nix-protocol-error-status c)
  347. ,(nix-protocol-error-message c)))
  348. (primitive-exit 2))
  349. (else
  350. (write `(exception))
  351. (primitive-exit 3)))
  352. (with-store store
  353. (let* ((files ',files)
  354. (invalid (remove (cut valid-path? store <>)
  355. files)))
  356. (unless (null? invalid)
  357. (write `(invalid-items ,invalid))
  358. (exit 1))
  359. ;; TODO: When RECURSIVE? is true, we could send the list of store
  360. ;; items in the closure so that the other end can filter out
  361. ;; those it already has.
  362. (write '(exporting)) ;we're ready
  363. (force-output)
  364. (setvbuf (current-output-port) 'none)
  365. (export-paths store files (current-output-port)
  366. #:recursive? ,recursive?)
  367. (primitive-exit 0))))))
  368. (remote-run export session))
  369. (define (remote-system session)
  370. "Return the system type as expected by Nix, usually ARCHITECTURE-KERNEL, of
  371. the machine on the other end of SESSION."
  372. (inferior-remote-eval '(begin (use-modules (guix utils)) (%current-system))
  373. session))
  374. (define* (remote-authorize-signing-key key session #:optional become-command)
  375. "Send KEY, a canonical sexp containing a public key, over SESSION and add it
  376. to the system ACL file if it has not yet been authorized."
  377. (inferior-remote-eval
  378. `(begin
  379. (use-modules (guix build utils)
  380. (guix pki)
  381. (guix utils)
  382. (gcrypt pk-crypto)
  383. (srfi srfi-26))
  384. (define acl (current-acl))
  385. (define key (string->canonical-sexp ,(canonical-sexp->string key)))
  386. (unless (authorized-key? key)
  387. (let ((acl (public-keys->acl (cons key (acl->public-keys acl)))))
  388. (mkdir-p (dirname %acl-file))
  389. (with-atomic-file-output %acl-file
  390. (cut write-acl acl <>)))))
  391. session
  392. become-command))
  393. (define (prepare-to-send store host log-port items)
  394. "Notify the user that we're about to send ITEMS to HOST. Return three
  395. values allowing 'notify-send-progress' to track the state of this transfer."
  396. (let* ((count (length items))
  397. (sizes (fold (lambda (item result)
  398. (vhash-cons item
  399. (path-info-nar-size
  400. (query-path-info store item))
  401. result))
  402. vlist-null
  403. items))
  404. (total (vlist-fold (lambda (pair result)
  405. (match pair
  406. ((_ . size) (+ size result))))
  407. 0
  408. sizes)))
  409. (info (N_ "sending ~a store item (~h MiB) to '~a'...~%"
  410. "sending ~a store items (~h MiB) to '~a'...~%" count)
  411. count
  412. (inexact->exact (round (/ total (expt 2. 20))))
  413. host)
  414. (values log-port sizes total 0)))
  415. (define (notify-transfer-progress item port sizes total sent)
  416. "Notify the user that we've already transferred SENT bytes out of TOTAL.
  417. Use SIZES to determine the size of ITEM, which is about to be sent."
  418. (define (display-bar %)
  419. (erase-current-line port)
  420. (format port "~3@a% ~a"
  421. (inexact->exact (round (* 100. (/ sent total))))
  422. (progress-bar % (- (max (current-terminal-columns) 5) 5)))
  423. (force-output port))
  424. (unless (zero? total)
  425. (let ((% (* 100. (/ sent total))))
  426. (match (vhash-assoc item sizes)
  427. (#f
  428. (display-bar %)
  429. (values port sizes total sent))
  430. ((_ . size)
  431. (display-bar %)
  432. (values port sizes total (+ sent size)))))))
  433. (define (notify-transfer-completion port . args)
  434. "Notify the user that the transfer has completed."
  435. (apply notify-transfer-progress "" port args) ;display the 100% progress bar
  436. (erase-current-line port)
  437. (force-output port))
  438. (define* (send-files local files remote
  439. #:key
  440. recursive?
  441. (log-port (current-error-port)))
  442. "Send the subset of FILES from LOCAL (a local store) that's missing to
  443. REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES.
  444. Return the list of store items actually sent."
  445. ;; Compute the subset of FILES missing on SESSION and send them.
  446. (let* ((files (if recursive? (requisites local files) files))
  447. (session (channel-get-session (store-connection-socket remote)))
  448. (missing (inferior-remote-eval
  449. `(begin
  450. (use-modules (guix)
  451. (srfi srfi-1) (srfi srfi-26))
  452. (with-store store
  453. (remove (cut valid-path? store <>)
  454. ',files)))
  455. session))
  456. (port (store-import-channel session))
  457. (host (session-get session 'host)))
  458. ;; Make sure everything alright on the remote side.
  459. (match (read port)
  460. (('importing)
  461. #t)
  462. (sexp
  463. (handle-import/export-channel-error sexp remote)))
  464. ;; Send MISSING in topological order.
  465. (let ((tty? (isatty? log-port)))
  466. (export-paths local missing port
  467. #:start (cut prepare-to-send local host log-port <>)
  468. #:progress (if tty? notify-transfer-progress (const #f))
  469. #:finish (if tty? notify-transfer-completion (const #f))))
  470. ;; Tell the remote process that we're done. (In theory the end-of-archive
  471. ;; mark of 'export-paths' would be enough, but in practice it's not.)
  472. (channel-send-eof port)
  473. ;; Wait for completion of the remote process and read the status sexp from
  474. ;; PORT. Wait for the exit status only when 'read' completed; otherwise,
  475. ;; we might wait forever if the other end is stuck.
  476. (let* ((result (false-if-exception (read port)))
  477. (status (and result
  478. (zero? (channel-get-exit-status port)))))
  479. (close-port port)
  480. (match result
  481. (('success . _)
  482. missing)
  483. (('protocol-error message)
  484. (raise (condition
  485. (&store-protocol-error (message message) (status 42)))))
  486. (('error key args ...)
  487. (raise (condition
  488. (&store-protocol-error
  489. (message (call-with-output-string
  490. (lambda (port)
  491. (print-exception port #f key args))))
  492. (status 43)))))
  493. (_
  494. (raise (condition
  495. (&store-protocol-error
  496. (message "unknown error while sending files over SSH")
  497. (status 44)))))))))
  498. (define (remote-store-session remote)
  499. "Return the SSH channel beneath REMOTE, a remote store as returned by
  500. 'connect-to-remote-daemon', or #f."
  501. (channel-get-session (store-connection-socket remote)))
  502. (define (remote-store-host remote)
  503. "Return the name of the host REMOTE is connected to, where REMOTE is a
  504. remote store as returned by 'connect-to-remote-daemon'."
  505. (match (remote-store-session remote)
  506. (#f #f)
  507. ((? session? session)
  508. (session-get session 'host))))
  509. (define* (file-retrieval-port files remote
  510. #:key recursive?)
  511. "Return an input port from which to retrieve FILES (a list of store items)
  512. from REMOTE, along with the number of items to retrieve (lower than or equal
  513. to the length of FILES.)"
  514. (values (store-export-channel (remote-store-session remote) files
  515. #:recursive? recursive?)
  516. (length files))) ;XXX: inaccurate when RECURSIVE? is true
  517. (define-syntax raise-error
  518. (syntax-rules (=>)
  519. ((_ fmt args ... (=> hint-fmt hint-args ...))
  520. (raise (condition
  521. (&message
  522. (message (format #f fmt args ...)))
  523. (&fix-hint
  524. (hint (format #f hint-fmt hint-args ...))))))
  525. ((_ fmt args ...)
  526. (raise (condition
  527. (&message
  528. (message (format #f fmt args ...))))))))
  529. (define (handle-import/export-channel-error sexp remote)
  530. "Report an error corresponding to SEXP, the EOF object or an sexp read from
  531. REMOTE."
  532. (match sexp
  533. ((? eof-object?)
  534. (report-guile-error (remote-store-host remote)))
  535. (('connection-error file code . _)
  536. (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
  537. file (remote-store-host remote) (strerror code)))
  538. (('invalid-items items . _)
  539. (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
  540. "no such items on remote host '~A':~{ ~a~}"
  541. (length items))
  542. (remote-store-host remote) items))
  543. (('protocol-error status message . _)
  544. (raise-error (G_ "protocol error on remote host '~A': ~a")
  545. (remote-store-host remote) message))
  546. (_
  547. (raise-error (G_ "failed to retrieve store items from '~a'")
  548. (remote-store-host remote)))))
  549. (define* (retrieve-files* files remote
  550. #:key recursive? (log-port (current-error-port))
  551. (import (const #f)))
  552. "Pass IMPORT an input port from which to read the sequence of FILES coming
  553. from REMOTE. When RECURSIVE? is true, retrieve the closure of FILES."
  554. (let-values (((port count)
  555. (file-retrieval-port files remote
  556. #:recursive? recursive?)))
  557. (match (read port) ;read the initial status
  558. (('exporting)
  559. (format #t (N_ "retrieving ~a store item from '~a'...~%"
  560. "retrieving ~a store items from '~a'...~%" count)
  561. count (remote-store-host remote))
  562. (dynamic-wind
  563. (const #t)
  564. (lambda ()
  565. (import port))
  566. (lambda ()
  567. (close-port port))))
  568. (sexp
  569. (handle-import/export-channel-error sexp remote)))))
  570. (define* (retrieve-files local files remote
  571. #:key recursive? (log-port (current-error-port)))
  572. "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
  573. LOCAL. When RECURSIVE? is true, retrieve the closure of FILES."
  574. (retrieve-files* (remove (cut valid-path? local <>) files)
  575. remote
  576. #:recursive? recursive?
  577. #:log-port log-port
  578. #:import (lambda (port)
  579. (import-paths local port))))
  580. ;;;
  581. ;;; Error reporting.
  582. ;;;
  583. (define (report-guile-error host)
  584. (raise-error (G_ "failed to start Guile on remote host '~A'") host
  585. (=> (G_ "Make sure @command{guile} can be found in
  586. @code{$PATH} on the remote host. Run @command{ssh ~A guile --version} to
  587. check.")
  588. host)))
  589. (define (report-inferior-exception exception host)
  590. "Report EXCEPTION, an &inferior-exception that occurred on HOST."
  591. (raise-error (G_ "exception occurred on remote host '~A': ~s")
  592. host (inferior-exception-arguments exception)))
  593. ;;; ssh.scm ends here