1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix ssh)
20 #:use-module (guix store)
21 #:use-module (guix inferior)
22 #:use-module (guix i18n)
23 #:use-module ((guix utils) #:select (&fix-hint))
24 #:use-module (ssh session)
25 #:use-module (ssh auth)
26 #:use-module (ssh key)
27 #:use-module (ssh channel)
28 #:use-module (ssh popen)
29 #:use-module (ssh session)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-11)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
36 #:use-module (ice-9 format)
37 #:use-module (ice-9 binary-ports)
38 #:export (open-ssh-session
41 connect-to-remote-daemon
52 ;;; This module provides tools to support communication with remote stores
53 ;;; over SSH, using Guile-SSH.
58 "zlib@openssh.com,zlib")
60 (define* (open-ssh-session host #:key user port
61 (compression %compression))
62 "Open an SSH session for HOST and return it. When USER and PORT are #f, use
63 default values or whatever '~/.ssh/config' specifies; otherwise use them.
64 Throw an error on failure."
65 (let ((session (make-session #:user user
69 ;; #:log-verbosity 'protocol
71 ;; We need lightweight compression when
72 ;; exchanging full archives.
73 #:compression compression
74 #:compression-level 3)))
76 ;; Honor ~/.ssh/config.
77 (session-parse-config! session)
79 (match (connect! session)
81 ;; Use public key authentication, via the SSH agent if it's available.
82 (match (userauth-public-key/auto! session)
89 (message (format #f (G_ "SSH authentication failed for '~a': ~a~%")
90 host (get-error session)))))))))
92 ;; Connection failed or timeout expired.
95 (message (format #f (G_ "SSH connection to '~a' failed: ~a~%")
96 host (get-error session))))))))))
98 (define (remote-inferior session)
99 "Return a remote inferior for the given SESSION."
100 (let ((pipe (open-remote-pipe* session OPEN_BOTH
101 "guix" "repl" "-t" "machine")))
102 (port->inferior pipe)))
104 (define (inferior-remote-eval exp session)
105 "Evaluate EXP in a new inferior running in SESSION, and close the inferior
107 (let ((inferior (remote-inferior session)))
111 (inferior-eval exp inferior))
113 ;; Close INFERIOR right away to prevent finalization from happening in
114 ;; another thread at the wrong time (see
115 ;; <https://bugs.gnu.org/26976>.)
116 (close-inferior inferior)))))
118 (define* (remote-daemon-channel session
121 "/var/guix/daemon-socket/socket"))
122 "Return an input/output port (an SSH channel) to the daemon at SESSION."
124 ;; Code run in SESSION to redirect the remote process' stdin/stdout to the
125 ;; daemon's socket, à la socat. The SSH protocol supports forwarding to
126 ;; Unix-domain sockets but libssh doesn't have an API for that, hence this
129 (use-modules (ice-9 match) (rnrs io ports)
132 (let ((sock (socket AF_UNIX SOCK_STREAM 0))
133 (stdin (current-input-port))
134 (stdout (current-output-port))
135 (select* (lambda (read write except)
136 ;; This is a workaround for
137 ;; <https://bugs.gnu.org/30365> in Guile < 2.2.4:
138 ;; since 'select' sometimes returns non-empty sets for
139 ;; no good reason, call 'select' a second time with a
140 ;; zero timeout to filter out incorrect replies.
141 (match (select read write except)
143 (select read write except 0))))))
144 (setvbuf stdout 'none)
146 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
147 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
148 (setvbuf stdin 'block 65536)
149 (setvbuf sock 'block 65536)
151 (connect sock AF_UNIX ,socket-name)
154 (match (select* (list stdin sock) '() '())
156 (when (memq stdin reads)
157 (match (get-bytevector-some stdin)
161 (put-bytevector sock bv)
162 (force-output sock))))
163 (when (memq sock reads)
164 (match (get-bytevector-some sock)
168 (put-bytevector stdout bv))))
171 (primitive-exit 1)))))))
173 (open-remote-pipe* session OPEN_BOTH
174 ;; Sort-of shell-quote REDIRECT.
177 (object->string redirect))))
179 (define* (connect-to-remote-daemon session
182 "/var/guix/daemon-socket/socket"))
183 "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
184 an SSH session. Return a <store-connection> object."
185 (open-connection #:port (remote-daemon-channel session socket-name)))
188 (define (store-import-channel session)
189 "Return an output port to which archives to be exported to SESSION's store
191 ;; Using the 'import-paths' RPC on a remote store would be slow because it
192 ;; makes a round trip every time 32 KiB have been transferred. This
193 ;; procedure instead opens a separate channel to use the remote
194 ;; 'import-paths' procedure, which consumes all the data in a single round
195 ;; trip. This optimizes the successful case at the expense of error
196 ;; conditions: errors can only be reported once all the input has been
200 (use-modules (guix) (srfi srfi-34)
201 (rnrs io ports) (rnrs bytevectors))
203 (define (consume-input port)
204 (let ((bv (make-bytevector 32768)))
206 (let ((n (get-bytevector-n! port bv 0
207 (bytevector-length bv))))
208 (unless (eof-object? n)
211 ;; Upon completion, write an sexp that denotes the status.
215 (guard (c ((nix-protocol-error? c)
216 ;; Consume all the input since the only time we can
217 ;; report the error is after everything has been
219 (consume-input (current-input-port))
220 (list 'protocol-error (nix-protocol-error-message c))))
222 (setvbuf (current-input-port) 'none)
223 (import-paths store (current-input-port))
226 (cons 'error args))))))
228 (open-remote-pipe session
231 ,(object->string (object->string import))))
234 (define* (store-export-channel session files
236 "Return an input port from which an export of FILES from SESSION's store can
237 be read. When RECURSIVE? is true, the closure of FILES is exported."
238 ;; Same as above: this is more efficient than calling 'export-paths' on a
242 (eval-when (load expand eval)
243 (unless (resolve-module '(guix) #:ensure #f)
244 (write `(module-error))
247 (use-modules (guix) (srfi srfi-1)
248 (srfi srfi-26) (srfi srfi-34))
250 (guard (c ((nix-connection-error? c)
251 (write `(connection-error ,(nix-connection-error-file c)
252 ,(nix-connection-error-code c))))
253 ((nix-protocol-error? c)
254 (write `(protocol-error ,(nix-protocol-error-status c)
255 ,(nix-protocol-error-message c))))
257 (write `(exception))))
259 (let* ((files ',files)
260 (invalid (remove (cut valid-path? store <>)
262 (unless (null? invalid)
263 (write `(invalid-items ,invalid))
266 ;; TODO: When RECURSIVE? is true, we could send the list of store
267 ;; items in the closure so that the other end can filter out
268 ;; those it already has.
270 (write '(exporting)) ;we're ready
273 (setvbuf (current-output-port) 'none)
274 (export-paths store files (current-output-port)
275 #:recursive? ,recursive?))))))
277 (open-remote-input-pipe session
281 (object->string export))))))
283 (define* (send-files local files remote
286 (log-port (current-error-port)))
287 "Send the subset of FILES from LOCAL (a local store) that's missing to
288 REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES.
289 Return the list of store items actually sent."
290 ;; Compute the subset of FILES missing on SESSION and send them.
291 (let* ((files (if recursive? (requisites local files) files))
292 (session (channel-get-session (store-connection-socket remote)))
293 (missing (inferior-remote-eval
296 (srfi srfi-1) (srfi srfi-26))
299 (remove (cut valid-path? store <>)
302 (count (length missing))
303 (sizes (map (lambda (item)
304 (path-info-nar-size (query-path-info local item)))
306 (port (store-import-channel session)))
307 (format log-port (N_ "sending ~a store item (~h MiB) to '~a'...~%"
308 "sending ~a store items (~h MiB) to '~a'...~%" count)
310 (inexact->exact (round (/ (reduce + 0 sizes) (expt 2. 20))))
311 (session-get session 'host))
313 ;; Send MISSING in topological order.
314 (export-paths local missing port)
316 ;; Tell the remote process that we're done. (In theory the end-of-archive
317 ;; mark of 'export-paths' would be enough, but in practice it's not.)
318 (channel-send-eof port)
320 ;; Wait for completion of the remote process and read the status sexp from
321 ;; PORT. Wait for the exit status only when 'read' completed; otherwise,
322 ;; we might wait forever if the other end is stuck.
323 (let* ((result (false-if-exception (read port)))
325 (zero? (channel-get-exit-status port)))))
330 (('protocol-error message)
332 (&store-protocol-error (message message) (status 42)))))
333 (('error key args ...)
335 (&store-protocol-error
336 (message (call-with-output-string
338 (print-exception port #f key args))))
342 (&store-protocol-error
343 (message "unknown error while sending files over SSH")
346 (define (remote-store-session remote)
347 "Return the SSH channel beneath REMOTE, a remote store as returned by
348 'connect-to-remote-daemon', or #f."
349 (channel-get-session (store-connection-socket remote)))
351 (define (remote-store-host remote)
352 "Return the name of the host REMOTE is connected to, where REMOTE is a
353 remote store as returned by 'connect-to-remote-daemon'."
354 (match (remote-store-session remote)
356 ((? session? session)
357 (session-get session 'host))))
359 (define* (file-retrieval-port files remote
361 "Return an input port from which to retrieve FILES (a list of store items)
362 from REMOTE, along with the number of items to retrieve (lower than or equal
363 to the length of FILES.)"
364 (values (store-export-channel (remote-store-session remote) files
365 #:recursive? recursive?)
366 (length files))) ;XXX: inaccurate when RECURSIVE? is true
368 (define-syntax raise-error
370 ((_ fmt args ... (=> hint-fmt hint-args ...))
373 (message (format #f fmt args ...)))
375 (hint (format #f hint-fmt hint-args ...))))))
379 (message (format #f fmt args ...))))))))
381 (define* (retrieve-files* files remote
382 #:key recursive? (log-port (current-error-port))
384 "Pass IMPORT an input port from which to read the sequence of FILES coming
385 from REMOTE. When RECURSIVE? is true, retrieve the closure of FILES."
386 (let-values (((port count)
387 (file-retrieval-port files remote
388 #:recursive? recursive?)))
389 (match (read port) ;read the initial status
391 (format #t (N_ "retrieving ~a store item from '~a'...~%"
392 "retrieving ~a store items from '~a'...~%" count)
393 count (remote-store-host remote))
402 (report-guile-error (remote-store-host remote)))
404 (report-module-error (remote-store-host remote)))
405 (('connection-error file code . _)
406 (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
407 file (remote-store-host remote) (strerror code)))
408 (('invalid-items items . _)
409 (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
410 "no such items on remote host '~A':~{ ~a~}"
412 (remote-store-host remote) items))
413 (('protocol-error status message . _)
414 (raise-error (G_ "protocol error on remote host '~A': ~a")
415 (remote-store-host remote) message))
417 (raise-error (G_ "failed to retrieve store items from '~a'")
418 (remote-store-host remote))))))
420 (define* (retrieve-files local files remote
421 #:key recursive? (log-port (current-error-port)))
422 "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
423 LOCAL. When RECURSIVE? is true, retrieve the closure of FILES."
424 (retrieve-files* (remove (cut valid-path? local <>) files)
426 #:recursive? recursive?
428 #:import (lambda (port)
429 (import-paths local port))))
436 (define (report-guile-error host)
437 (raise-error (G_ "failed to start Guile on remote host '~A'") host
438 (=> (G_ "Make sure @command{guile} can be found in
439 @code{$PATH} on the remote host. Run @command{ssh ~A guile --version} to
443 (define (report-module-error host)
444 "Report an error about missing Guix modules on HOST."
445 ;; TRANSLATORS: Leave "Guile" untranslated.
446 (raise-error (G_ "Guile modules not found on remote host '~A'") host
447 (=> (G_ "Make sure @code{GUILE_LOAD_PATH} includes Guix'
448 own module directory. Run @command{ssh ~A env | grep GUILE_LOAD_PATH} to
452 ;;; ssh.scm ends here