ssh.scm 27 KB

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