Commit | Line | Data |
---|---|---|
987a29ba | 1 | ;;; GNU Guix --- Functional package management for GNU |
8f53d734 | 2 | ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
987a29ba LC |
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 | ||
19 | (define-module (guix ssh) | |
20 | #:use-module (guix store) | |
af15fe13 | 21 | #:use-module (guix inferior) |
52d174d6 | 22 | #:use-module (guix i18n) |
896fec47 | 23 | #:use-module ((guix utils) #:select (&fix-hint)) |
3033d59a | 24 | #:use-module (gcrypt pk-crypto) |
615c5298 LC |
25 | #:use-module (ssh session) |
26 | #:use-module (ssh auth) | |
27 | #:use-module (ssh key) | |
987a29ba LC |
28 | #:use-module (ssh channel) |
29 | #:use-module (ssh popen) | |
30 | #:use-module (ssh session) | |
0e3c8528 | 31 | #:use-module (srfi srfi-1) |
987a29ba | 32 | #:use-module (srfi srfi-11) |
d06d54e3 | 33 | #:use-module (srfi srfi-26) |
13164a21 LC |
34 | #:use-module (srfi srfi-34) |
35 | #:use-module (srfi srfi-35) | |
987a29ba | 36 | #:use-module (ice-9 match) |
416a7c69 | 37 | #:use-module (ice-9 format) |
13164a21 | 38 | #:use-module (ice-9 binary-ports) |
615c5298 | 39 | #:export (open-ssh-session |
114dcb42 LC |
40 | authenticate-server* |
41 | ||
af15fe13 | 42 | remote-inferior |
e5378337 | 43 | remote-daemon-channel |
615c5298 | 44 | connect-to-remote-daemon |
2c8e04f1 | 45 | remote-system |
3033d59a | 46 | remote-authorize-signing-key |
987a29ba LC |
47 | send-files |
48 | retrieve-files | |
d06d54e3 | 49 | retrieve-files* |
4eb0f9ae LC |
50 | remote-store-host |
51 | ||
52 | report-guile-error | |
53 | report-module-error)) | |
987a29ba LC |
54 | |
55 | ;;; Commentary: | |
56 | ;;; | |
57 | ;;; This module provides tools to support communication with remote stores | |
58 | ;;; over SSH, using Guile-SSH. | |
59 | ;;; | |
60 | ;;; Code: | |
61 | ||
615c5298 LC |
62 | (define %compression |
63 | "zlib@openssh.com,zlib") | |
64 | ||
114dcb42 LC |
65 | (define (host-key->type+key host-key) |
66 | "Destructure HOST-KEY, an OpenSSH host key string, and return two values: | |
67 | its key type as a symbol, and the actual base64-encoded string." | |
68 | (define (type->symbol type) | |
69 | (and (string-prefix? "ssh-" type) | |
70 | (string->symbol (string-drop type 4)))) | |
71 | ||
72 | (match (string-tokenize host-key) | |
73 | ((type key x) | |
74 | (values (type->symbol type) key)) | |
75 | ((type key) | |
76 | (values (type->symbol type) key)))) | |
77 | ||
78 | (define (authenticate-server* session key) | |
79 | "Make sure the server for SESSION has the given KEY, where KEY is a string | |
80 | such as \"ssh-ed25519 AAAAC3Nz… root@example.org\". Raise an exception if the | |
81 | actual key does not match." | |
82 | (let-values (((server) (get-server-public-key session)) | |
83 | ((type key) (host-key->type+key key))) | |
84 | (unless (and (or (not (get-key-type server)) | |
85 | (eq? (get-key-type server) type)) | |
86 | (string=? (public-key->string server) key)) | |
87 | ;; Key mismatch: something's wrong. XXX: It could be that the server | |
88 | ;; provided its Ed25519 key when we where expecting its RSA key. XXX: | |
89 | ;; Guile-SSH 0.10.1 doesn't know about ed25519 keys and 'get-key-type' | |
90 | ;; returns #f in that case. | |
91 | (raise (condition | |
92 | (&message | |
93 | (message (format #f (G_ "server at '~a' returned host key \ | |
94 | '~a' of type '~a' instead of '~a' of type '~a'~%") | |
95 | (session-get session 'host) | |
96 | (public-key->string server) | |
97 | (get-key-type server) | |
98 | key type)))))))) | |
99 | ||
a9b09ed7 | 100 | (define* (open-ssh-session host #:key user port identity |
2b868284 | 101 | host-key |
81c5873a LC |
102 | (compression %compression) |
103 | (timeout 3600)) | |
a9b09ed7 JK |
104 | "Open an SSH session for HOST and return it. IDENTITY specifies the file |
105 | name of a private key to use for authenticating with the host. When USER, | |
106 | PORT, or IDENTITY are #f, use default values or whatever '~/.ssh/config' | |
2b868284 LC |
107 | specifies; otherwise use them. |
108 | ||
109 | When HOST-KEY is true, it must be a string like \"ssh-ed25519 AAAAC3Nz… | |
110 | root@example.org\"; the server is authenticated and an error is raised if its | |
111 | host key is different from HOST-KEY. | |
112 | ||
113 | Install TIMEOUT as the maximum time in seconds after which a read or write | |
114 | operation on a channel of the returned session is considered as failing. | |
81c5873a LC |
115 | |
116 | Throw an error on failure." | |
615c5298 | 117 | (let ((session (make-session #:user user |
a9b09ed7 | 118 | #:identity identity |
615c5298 LC |
119 | #:host host |
120 | #:port port | |
121 | #:timeout 10 ;seconds | |
122 | ;; #:log-verbosity 'protocol | |
123 | ||
2b868284 LC |
124 | ;; Prevent libssh from reading |
125 | ;; ~/.ssh/known_hosts when the caller provides | |
126 | ;; a HOST-KEY to match against. | |
127 | #:knownhosts (and host-key "/dev/null") | |
128 | ||
615c5298 LC |
129 | ;; We need lightweight compression when |
130 | ;; exchanging full archives. | |
131 | #:compression compression | |
e8088f0b LDB |
132 | #:compression-level 3 |
133 | ||
134 | ;; Speed up RPCs by creating sockets with | |
135 | ;; TCP_NODELAY. | |
136 | #:nodelay #t))) | |
615c5298 LC |
137 | |
138 | ;; Honor ~/.ssh/config. | |
139 | (session-parse-config! session) | |
140 | ||
141 | (match (connect! session) | |
142 | ('ok | |
2b868284 LC |
143 | (if host-key |
144 | ;; Make sure the server's key is what we expect. | |
145 | (authenticate-server* session host-key) | |
146 | ||
147 | ;; Authenticate against ~/.ssh/known_hosts. | |
148 | (match (authenticate-server session) | |
149 | ('ok #f) | |
150 | (reason | |
151 | (raise (condition | |
152 | (&message | |
153 | (message (format #f (G_ "failed to authenticate \ | |
f5c18018 | 154 | server at '~a': ~a") |
2b868284 LC |
155 | (session-get session 'host) |
156 | reason)))))))) | |
f5c18018 | 157 | |
615c5298 LC |
158 | ;; Use public key authentication, via the SSH agent if it's available. |
159 | (match (userauth-public-key/auto! session) | |
160 | ('success | |
81c5873a | 161 | (session-set! session 'timeout timeout) |
615c5298 LC |
162 | session) |
163 | (x | |
35f35111 LDB |
164 | (match (userauth-gssapi! session) |
165 | ('success | |
166 | (session-set! session 'timeout timeout) | |
167 | session) | |
168 | (x | |
169 | (disconnect! session) | |
170 | (raise (condition | |
171 | (&message | |
172 | (message (format #f (G_ "SSH authentication failed for '~a': ~a~%") | |
173 | host (get-error session))))))))))) | |
615c5298 LC |
174 | (x |
175 | ;; Connection failed or timeout expired. | |
176 | (raise (condition | |
177 | (&message | |
69daee23 | 178 | (message (format #f (G_ "SSH connection to '~a' failed: ~a~%") |
615c5298 LC |
179 | host (get-error session)))))))))) |
180 | ||
5ea7537b JK |
181 | (define* (remote-inferior session #:optional become-command) |
182 | "Return a remote inferior for the given SESSION. If BECOME-COMMAND is | |
183 | given, use that to invoke the remote Guile REPL." | |
184 | (let* ((repl-command (append (or become-command '()) | |
185 | '("guix" "repl" "-t" "machine"))) | |
186 | (pipe (apply open-remote-pipe* session OPEN_BOTH repl-command))) | |
5ea7537b | 187 | (when (eof-object? (peek-char pipe)) |
e09c7f4a LC |
188 | (let ((status (channel-get-exit-status pipe))) |
189 | (close-port pipe) | |
190 | (raise (condition | |
191 | (&message | |
192 | (message (format #f (G_ "remote command '~{~a~^ ~}' failed \ | |
193 | with status ~a") | |
194 | repl-command status))))))) | |
af15fe13 LC |
195 | (port->inferior pipe))) |
196 | ||
5ea7537b | 197 | (define* (inferior-remote-eval exp session #:optional become-command) |
ed7b4437 | 198 | "Evaluate EXP in a new inferior running in SESSION, and close the inferior |
5ea7537b JK |
199 | right away. If BECOME-COMMAND is given, use that to invoke the remote Guile |
200 | REPL." | |
201 | (let ((inferior (remote-inferior session become-command))) | |
ed7b4437 LC |
202 | (dynamic-wind |
203 | (const #t) | |
204 | (lambda () | |
205 | (inferior-eval exp inferior)) | |
206 | (lambda () | |
207 | ;; Close INFERIOR right away to prevent finalization from happening in | |
208 | ;; another thread at the wrong time (see | |
209 | ;; <https://bugs.gnu.org/26976>.) | |
210 | (close-inferior inferior))))) | |
211 | ||
e5378337 LC |
212 | (define* (remote-daemon-channel session |
213 | #:optional | |
214 | (socket-name | |
215 | "/var/guix/daemon-socket/socket")) | |
216 | "Return an input/output port (an SSH channel) to the daemon at SESSION." | |
987a29ba LC |
217 | (define redirect |
218 | ;; Code run in SESSION to redirect the remote process' stdin/stdout to the | |
219 | ;; daemon's socket, à la socat. The SSH protocol supports forwarding to | |
220 | ;; Unix-domain sockets but libssh doesn't have an API for that, hence this | |
221 | ;; hack. | |
222 | `(begin | |
17af5d51 | 223 | (use-modules (ice-9 match) (rnrs io ports) |
0dcf675c | 224 | (rnrs bytevectors)) |
987a29ba | 225 | |
8446dc5a LC |
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)))))) | |
76832d34 | 238 | (setvbuf stdout 'none) |
0dcf675c LC |
239 | |
240 | ;; Use buffered ports so that 'get-bytevector-some' returns up to the | |
241 | ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>. | |
76832d34 LC |
242 | (setvbuf stdin 'block 65536) |
243 | (setvbuf sock 'block 65536) | |
0dcf675c | 244 | |
987a29ba LC |
245 | (connect sock AF_UNIX ,socket-name) |
246 | ||
247 | (let loop () | |
8446dc5a | 248 | (match (select* (list stdin sock) '() '()) |
55f40fdb | 249 | ((reads () ()) |
987a29ba | 250 | (when (memq stdin reads) |
0dcf675c LC |
251 | (match (get-bytevector-some stdin) |
252 | ((? eof-object?) | |
987a29ba | 253 | (primitive-exit 0)) |
0dcf675c LC |
254 | (bv |
255 | (put-bytevector sock bv) | |
256 | (force-output sock)))) | |
987a29ba | 257 | (when (memq sock reads) |
0dcf675c LC |
258 | (match (get-bytevector-some sock) |
259 | ((? eof-object?) | |
987a29ba | 260 | (primitive-exit 0)) |
0dcf675c LC |
261 | (bv |
262 | (put-bytevector stdout bv)))) | |
987a29ba LC |
263 | (loop)) |
264 | (_ | |
265 | (primitive-exit 1))))))) | |
266 | ||
e5378337 LC |
267 | (open-remote-pipe* session OPEN_BOTH |
268 | ;; Sort-of shell-quote REDIRECT. | |
269 | "guile" "-c" | |
270 | (object->string | |
271 | (object->string redirect)))) | |
272 | ||
273 | (define* (connect-to-remote-daemon session | |
274 | #:optional | |
275 | (socket-name | |
276 | "/var/guix/daemon-socket/socket")) | |
277 | "Connect to the remote build daemon listening on SOCKET-NAME over SESSION, | |
de9fbe9c | 278 | an SSH session. Return a <store-connection> object." |
2e4d8339 | 279 | (open-connection #:port (remote-daemon-channel session socket-name))) |
e5378337 | 280 | |
987a29ba LC |
281 | |
282 | (define (store-import-channel session) | |
283 | "Return an output port to which archives to be exported to SESSION's store | |
284 | can be written." | |
285 | ;; Using the 'import-paths' RPC on a remote store would be slow because it | |
286 | ;; makes a round trip every time 32 KiB have been transferred. This | |
287 | ;; procedure instead opens a separate channel to use the remote | |
288 | ;; 'import-paths' procedure, which consumes all the data in a single round | |
de9d8f0e LC |
289 | ;; trip. This optimizes the successful case at the expense of error |
290 | ;; conditions: errors can only be reported once all the input has been | |
291 | ;; consumed. | |
987a29ba LC |
292 | (define import |
293 | `(begin | |
de9d8f0e LC |
294 | (use-modules (guix) (srfi srfi-34) |
295 | (rnrs io ports) (rnrs bytevectors)) | |
987a29ba | 296 | |
de9d8f0e LC |
297 | (define (consume-input port) |
298 | (let ((bv (make-bytevector 32768))) | |
299 | (let loop () | |
300 | (let ((n (get-bytevector-n! port bv 0 | |
301 | (bytevector-length bv)))) | |
302 | (unless (eof-object? n) | |
303 | (loop)))))) | |
987a29ba | 304 | |
de9d8f0e LC |
305 | ;; Upon completion, write an sexp that denotes the status. |
306 | (write | |
307 | (catch #t | |
308 | (lambda () | |
309 | (guard (c ((nix-protocol-error? c) | |
310 | ;; Consume all the input since the only time we can | |
311 | ;; report the error is after everything has been | |
312 | ;; consumed. | |
313 | (consume-input (current-input-port)) | |
314 | (list 'protocol-error (nix-protocol-error-message c)))) | |
315 | (with-store store | |
76832d34 | 316 | (setvbuf (current-input-port) 'none) |
de9d8f0e LC |
317 | (import-paths store (current-input-port)) |
318 | '(success)))) | |
319 | (lambda args | |
320 | (cons 'error args)))))) | |
987a29ba | 321 | |
de9d8f0e LC |
322 | (open-remote-pipe session |
323 | (string-join | |
324 | `("guile" "-c" | |
325 | ,(object->string (object->string import)))) | |
326 | OPEN_BOTH)) | |
987a29ba | 327 | |
e9629e82 LC |
328 | (define* (store-export-channel session files |
329 | #:key recursive?) | |
987a29ba | 330 | "Return an input port from which an export of FILES from SESSION's store can |
e9629e82 | 331 | be read. When RECURSIVE? is true, the closure of FILES is exported." |
987a29ba LC |
332 | ;; Same as above: this is more efficient than calling 'export-paths' on a |
333 | ;; remote store. | |
334 | (define export | |
335 | `(begin | |
896fec47 LC |
336 | (eval-when (load expand eval) |
337 | (unless (resolve-module '(guix) #:ensure #f) | |
338 | (write `(module-error)) | |
339 | (exit 7))) | |
340 | ||
341 | (use-modules (guix) (srfi srfi-1) | |
342 | (srfi srfi-26) (srfi srfi-34)) | |
343 | ||
344 | (guard (c ((nix-connection-error? c) | |
345 | (write `(connection-error ,(nix-connection-error-file c) | |
346 | ,(nix-connection-error-code c)))) | |
347 | ((nix-protocol-error? c) | |
348 | (write `(protocol-error ,(nix-protocol-error-status c) | |
349 | ,(nix-protocol-error-message c)))) | |
350 | (else | |
351 | (write `(exception)))) | |
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 | ||
0e3c8528 LC |
360 | ;; TODO: When RECURSIVE? is true, we could send the list of store |
361 | ;; items in the closure so that the other end can filter out | |
362 | ;; those it already has. | |
363 | ||
896fec47 LC |
364 | (write '(exporting)) ;we're ready |
365 | (force-output) | |
366 | ||
76832d34 | 367 | (setvbuf (current-output-port) 'none) |
896fec47 LC |
368 | (export-paths store files (current-output-port) |
369 | #:recursive? ,recursive?)))))) | |
987a29ba LC |
370 | |
371 | (open-remote-input-pipe session | |
372 | (string-join | |
373 | `("guile" "-c" | |
374 | ,(object->string | |
375 | (object->string export)))))) | |
376 | ||
2c8e04f1 JK |
377 | (define (remote-system session) |
378 | "Return the system type as expected by Nix, usually ARCHITECTURE-KERNEL, of | |
379 | the machine on the other end of SESSION." | |
380 | (inferior-remote-eval '(begin (use-modules (guix utils)) (%current-system)) | |
381 | session)) | |
3033d59a | 382 | |
4cc5e520 | 383 | (define* (remote-authorize-signing-key key session #:optional become-command) |
3033d59a JK |
384 | "Send KEY, a canonical sexp containing a public key, over SESSION and add it |
385 | to the system ACL file if it has not yet been authorized." | |
386 | (inferior-remote-eval | |
387 | `(begin | |
388 | (use-modules (guix build utils) | |
389 | (guix pki) | |
390 | (guix utils) | |
391 | (gcrypt pk-crypto) | |
392 | (srfi srfi-26)) | |
393 | ||
394 | (define acl (current-acl)) | |
395 | (define key (string->canonical-sexp ,(canonical-sexp->string key))) | |
396 | ||
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 <>))))) | |
4cc5e520 JK |
402 | session |
403 | become-command)) | |
2c8e04f1 | 404 | |
987a29ba | 405 | (define* (send-files local files remote |
e9629e82 LC |
406 | #:key |
407 | recursive? | |
408 | (log-port (current-error-port))) | |
987a29ba | 409 | "Send the subset of FILES from LOCAL (a local store) that's missing to |
23973e4f LC |
410 | REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES. |
411 | Return the list of store items actually sent." | |
8f53d734 LC |
412 | (define (inferior-remote-eval* exp session) |
413 | (guard (c ((inferior-exception? c) | |
414 | (match (inferior-exception-arguments c) | |
415 | (('quit 7) | |
416 | (report-module-error (remote-store-host remote))) | |
417 | (_ | |
418 | (report-inferior-exception c (remote-store-host remote)))))) | |
419 | (inferior-remote-eval exp session))) | |
420 | ||
987a29ba | 421 | ;; Compute the subset of FILES missing on SESSION and send them. |
e9629e82 | 422 | (let* ((files (if recursive? (requisites local files) files)) |
de9fbe9c | 423 | (session (channel-get-session (store-connection-socket remote))) |
8f53d734 | 424 | (missing (inferior-remote-eval* |
ed7b4437 | 425 | `(begin |
8f53d734 LC |
426 | (eval-when (load expand eval) |
427 | (unless (resolve-module '(guix) #:ensure #f) | |
428 | (exit 7))) | |
429 | ||
ed7b4437 LC |
430 | (use-modules (guix) |
431 | (srfi srfi-1) (srfi srfi-26)) | |
987a29ba | 432 | |
ed7b4437 LC |
433 | (with-store store |
434 | (remove (cut valid-path? store <>) | |
435 | ',files))) | |
436 | session)) | |
987a29ba | 437 | (count (length missing)) |
b90d97ec LC |
438 | (sizes (map (lambda (item) |
439 | (path-info-nar-size (query-path-info local item))) | |
440 | missing)) | |
987a29ba | 441 | (port (store-import-channel session))) |
b90d97ec LC |
442 | (format log-port (N_ "sending ~a store item (~h MiB) to '~a'...~%" |
443 | "sending ~a store items (~h MiB) to '~a'...~%" count) | |
444 | count | |
445 | (inexact->exact (round (/ (reduce + 0 sizes) (expt 2. 20)))) | |
446 | (session-get session 'host)) | |
987a29ba LC |
447 | |
448 | ;; Send MISSING in topological order. | |
449 | (export-paths local missing port) | |
450 | ||
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 | ||
de9d8f0e | 455 | ;; Wait for completion of the remote process and read the status sexp from |
63fd9f08 LC |
456 | ;; PORT. Wait for the exit status only when 'read' completed; otherwise, |
457 | ;; we might wait forever if the other end is stuck. | |
de9d8f0e | 458 | (let* ((result (false-if-exception (read port))) |
63fd9f08 LC |
459 | (status (and result |
460 | (zero? (channel-get-exit-status port))))) | |
987a29ba | 461 | (close-port port) |
de9d8f0e LC |
462 | (match result |
463 | (('success . _) | |
464 | missing) | |
465 | (('protocol-error message) | |
466 | (raise (condition | |
f9e8a123 | 467 | (&store-protocol-error (message message) (status 42))))) |
de9d8f0e LC |
468 | (('error key args ...) |
469 | (raise (condition | |
f9e8a123 | 470 | (&store-protocol-error |
de9d8f0e LC |
471 | (message (call-with-output-string |
472 | (lambda (port) | |
473 | (print-exception port #f key args)))) | |
474 | (status 43))))) | |
475 | (_ | |
476 | (raise (condition | |
f9e8a123 | 477 | (&store-protocol-error |
de9d8f0e LC |
478 | (message "unknown error while sending files over SSH") |
479 | (status 44))))))))) | |
987a29ba LC |
480 | |
481 | (define (remote-store-session remote) | |
482 | "Return the SSH channel beneath REMOTE, a remote store as returned by | |
483 | 'connect-to-remote-daemon', or #f." | |
de9fbe9c | 484 | (channel-get-session (store-connection-socket remote))) |
987a29ba LC |
485 | |
486 | (define (remote-store-host remote) | |
487 | "Return the name of the host REMOTE is connected to, where REMOTE is a | |
488 | remote store as returned by 'connect-to-remote-daemon'." | |
489 | (match (remote-store-session remote) | |
490 | (#f #f) | |
491 | ((? session? session) | |
492 | (session-get session 'host)))) | |
493 | ||
e9629e82 LC |
494 | (define* (file-retrieval-port files remote |
495 | #:key recursive?) | |
987a29ba LC |
496 | "Return an input port from which to retrieve FILES (a list of store items) |
497 | from REMOTE, along with the number of items to retrieve (lower than or equal | |
498 | to the length of FILES.)" | |
e9629e82 LC |
499 | (values (store-export-channel (remote-store-session remote) files |
500 | #:recursive? recursive?) | |
501 | (length files))) ;XXX: inaccurate when RECURSIVE? is true | |
987a29ba | 502 | |
896fec47 LC |
503 | (define-syntax raise-error |
504 | (syntax-rules (=>) | |
505 | ((_ fmt args ... (=> hint-fmt hint-args ...)) | |
506 | (raise (condition | |
507 | (&message | |
508 | (message (format #f fmt args ...))) | |
509 | (&fix-hint | |
510 | (hint (format #f hint-fmt hint-args ...)))))) | |
511 | ((_ fmt args ...) | |
512 | (raise (condition | |
513 | (&message | |
514 | (message (format #f fmt args ...)))))))) | |
515 | ||
d06d54e3 LC |
516 | (define* (retrieve-files* files remote |
517 | #:key recursive? (log-port (current-error-port)) | |
518 | (import (const #f))) | |
519 | "Pass IMPORT an input port from which to read the sequence of FILES coming | |
520 | from REMOTE. When RECURSIVE? is true, retrieve the closure of FILES." | |
987a29ba | 521 | (let-values (((port count) |
e9629e82 LC |
522 | (file-retrieval-port files remote |
523 | #:recursive? recursive?))) | |
896fec47 LC |
524 | (match (read port) ;read the initial status |
525 | (('exporting) | |
526 | (format #t (N_ "retrieving ~a store item from '~a'...~%" | |
527 | "retrieving ~a store items from '~a'...~%" count) | |
528 | count (remote-store-host remote)) | |
529 | ||
d06d54e3 LC |
530 | (dynamic-wind |
531 | (const #t) | |
532 | (lambda () | |
533 | (import port)) | |
534 | (lambda () | |
535 | (close-port port)))) | |
896fec47 | 536 | ((? eof-object?) |
4eb0f9ae | 537 | (report-guile-error (remote-store-host remote))) |
896fec47 | 538 | (('module-error . _) |
4eb0f9ae | 539 | (report-module-error (remote-store-host remote))) |
896fec47 LC |
540 | (('connection-error file code . _) |
541 | (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a") | |
542 | file (remote-store-host remote) (strerror code))) | |
543 | (('invalid-items items . _) | |
544 | (raise-error (N_ "no such item on remote host '~A':~{ ~a~}" | |
545 | "no such items on remote host '~A':~{ ~a~}" | |
546 | (length items)) | |
547 | (remote-store-host remote) items)) | |
548 | (('protocol-error status message . _) | |
549 | (raise-error (G_ "protocol error on remote host '~A': ~a") | |
550 | (remote-store-host remote) message)) | |
551 | (_ | |
552 | (raise-error (G_ "failed to retrieve store items from '~a'") | |
553 | (remote-store-host remote)))))) | |
987a29ba | 554 | |
d06d54e3 LC |
555 | (define* (retrieve-files local files remote |
556 | #:key recursive? (log-port (current-error-port))) | |
557 | "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on | |
558 | LOCAL. When RECURSIVE? is true, retrieve the closure of FILES." | |
0e3c8528 LC |
559 | (retrieve-files* (remove (cut valid-path? local <>) files) |
560 | remote | |
d06d54e3 LC |
561 | #:recursive? recursive? |
562 | #:log-port log-port | |
563 | #:import (lambda (port) | |
564 | (import-paths local port)))) | |
565 | ||
4eb0f9ae LC |
566 | \f |
567 | ;;; | |
568 | ;;; Error reporting. | |
569 | ;;; | |
570 | ||
571 | (define (report-guile-error host) | |
572 | (raise-error (G_ "failed to start Guile on remote host '~A'") host | |
573 | (=> (G_ "Make sure @command{guile} can be found in | |
574 | @code{$PATH} on the remote host. Run @command{ssh ~A guile --version} to | |
575 | check.") | |
576 | host))) | |
577 | ||
578 | (define (report-module-error host) | |
579 | "Report an error about missing Guix modules on HOST." | |
580 | ;; TRANSLATORS: Leave "Guile" untranslated. | |
581 | (raise-error (G_ "Guile modules not found on remote host '~A'") host | |
582 | (=> (G_ "Make sure @code{GUILE_LOAD_PATH} includes Guix' | |
583 | own module directory. Run @command{ssh ~A env | grep GUILE_LOAD_PATH} to | |
584 | check.") | |
585 | host))) | |
586 | ||
8f53d734 LC |
587 | (define (report-inferior-exception exception host) |
588 | "Report EXCEPTION, an &inferior-exception that occurred on HOST." | |
589 | (raise-error (G_ "exception occurred on remote host '~A': ~s") | |
590 | host (inferior-exception-arguments exception))) | |
591 | ||
987a29ba | 592 | ;;; ssh.scm ends here |