1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 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 #:autoload (guix ui) (N_)
22 #:use-module (ssh channel)
23 #:use-module (ssh popen)
24 #:use-module (ssh session)
25 #:use-module (ssh dist)
26 #:use-module (ssh dist node)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-35)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 binary-ports)
32 #:export (connect-to-remote-daemon
41 ;;; This module provides tools to support communication with remote stores
42 ;;; over SSH, using Guile-SSH.
46 (define* (connect-to-remote-daemon session
48 (socket-name "/var/guix/daemon-socket/socket"))
49 "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
50 an SSH session. Return a <nix-server> object."
52 ;; Code run in SESSION to redirect the remote process' stdin/stdout to the
53 ;; daemon's socket, à la socat. The SSH protocol supports forwarding to
54 ;; Unix-domain sockets but libssh doesn't have an API for that, hence this
57 (use-modules (ice-9 match) (rnrs io ports))
59 (let ((sock (socket AF_UNIX SOCK_STREAM 0))
60 (stdin (current-input-port))
61 (stdout (current-output-port)))
62 (setvbuf stdin _IONBF)
63 (setvbuf stdout _IONBF)
64 (connect sock AF_UNIX ,socket-name)
67 (match (select (list stdin sock) '() (list stdin stdout sock))
69 (when (memq stdin reads)
70 (match (get-bytevector-some stdin)
74 (put-bytevector sock bv))))
75 (when (memq sock reads)
76 (match (get-bytevector-some sock)
80 (put-bytevector stdout bv))))
83 (primitive-exit 1)))))))
86 (open-remote-pipe* session OPEN_BOTH
87 ;; Sort-of shell-quote REDIRECT.
90 (object->string redirect)))))
91 (open-connection #:port channel)))
93 (define (store-import-channel session)
94 "Return an output port to which archives to be exported to SESSION's store
96 ;; Using the 'import-paths' RPC on a remote store would be slow because it
97 ;; makes a round trip every time 32 KiB have been transferred. This
98 ;; procedure instead opens a separate channel to use the remote
99 ;; 'import-paths' procedure, which consumes all the data in a single round
106 (setvbuf (current-input-port) _IONBF)
108 ;; FIXME: Exceptions are silently swallowed. We should report them
110 (import-paths store (current-input-port)))))
112 (open-remote-output-pipe session
116 (object->string import))))))
118 (define* (store-export-channel session files
120 "Return an input port from which an export of FILES from SESSION's store can
121 be read. When RECURSIVE? is true, the closure of FILES is exported."
122 ;; Same as above: this is more efficient than calling 'export-paths' on a
129 (setvbuf (current-output-port) _IONBF)
131 ;; FIXME: Exceptions are silently swallowed. We should report them
133 (export-paths store ',files (current-output-port)
134 #:recursive? ,recursive?))))
136 (open-remote-input-pipe session
140 (object->string export))))))
142 (define* (send-files local files remote
145 (log-port (current-error-port)))
146 "Send the subset of FILES from LOCAL (a local store) that's missing to
147 REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES.
148 Return the list of store items actually sent."
149 ;; Compute the subset of FILES missing on SESSION and send them.
150 (let* ((files (if recursive? (requisites local files) files))
151 (session (channel-get-session (nix-server-socket remote)))
152 (node (make-node session))
153 (missing (node-eval node
156 (srfi srfi-1) (srfi srfi-26))
159 (remove (cut valid-path? store <>)
161 (count (length missing))
162 (port (store-import-channel session)))
163 (format log-port (N_ "sending ~a store item to '~a'...~%"
164 "sending ~a store items to '~a'...~%" count)
165 count (session-get session 'host))
167 ;; Send MISSING in topological order.
168 (export-paths local missing port)
170 ;; Tell the remote process that we're done. (In theory the end-of-archive
171 ;; mark of 'export-paths' would be enough, but in practice it's not.)
172 (channel-send-eof port)
174 ;; Wait for completion of the remote process.
175 (let ((result (zero? (channel-get-exit-status port))))
179 (define (remote-store-session remote)
180 "Return the SSH channel beneath REMOTE, a remote store as returned by
181 'connect-to-remote-daemon', or #f."
182 (channel-get-session (nix-server-socket remote)))
184 (define (remote-store-host remote)
185 "Return the name of the host REMOTE is connected to, where REMOTE is a
186 remote store as returned by 'connect-to-remote-daemon'."
187 (match (remote-store-session remote)
189 ((? session? session)
190 (session-get session 'host))))
192 (define* (file-retrieval-port files remote
194 "Return an input port from which to retrieve FILES (a list of store items)
195 from REMOTE, along with the number of items to retrieve (lower than or equal
196 to the length of FILES.)"
197 (values (store-export-channel (remote-store-session remote) files
198 #:recursive? recursive?)
199 (length files))) ;XXX: inaccurate when RECURSIVE? is true
201 (define* (retrieve-files local files remote
202 #:key recursive? (log-port (current-error-port)))
203 "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
204 LOCAL. When RECURSIVE? is true, retrieve the closure of FILES."
205 (let-values (((port count)
206 (file-retrieval-port files remote
207 #:recursive? recursive?)))
208 (format #t (N_ "retrieving ~a store item from '~a'...~%"
209 "retrieving ~a store items from '~a'...~%" count)
210 count (remote-store-host remote))
211 (when (eof-object? (lookahead-u8 port))
212 ;; The failure could be because one of the requested store items is not
213 ;; valid on REMOTE, or because Guile or Guix is improperly installed.
214 ;; TODO: Improve error reporting.
219 (_ "failed to retrieve store items from '~a'")
220 (remote-store-host remote)))))))
222 (let ((result (import-paths local port)))
226 ;;; ssh.scm ends here