ssh: 'retrieve-files' detects remote export failures.
[jackhill/guix/guix.git] / guix / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 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
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
33 send-files
34 retrieve-files
35 remote-store-host
36
37 file-retrieval-port))
38
39 ;;; Commentary:
40 ;;;
41 ;;; This module provides tools to support communication with remote stores
42 ;;; over SSH, using Guile-SSH.
43 ;;;
44 ;;; Code:
45
46 (define* (connect-to-remote-daemon session
47 #:optional
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."
51 (define redirect
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
55 ;; hack.
56 `(begin
57 (use-modules (ice-9 match) (rnrs io ports))
58
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)
65
66 (let loop ()
67 (match (select (list stdin sock) '() (list stdin stdout sock))
68 ((reads writes ())
69 (when (memq stdin reads)
70 (match (get-bytevector-some stdin)
71 ((? eof-object?)
72 (primitive-exit 0))
73 (bv
74 (put-bytevector sock bv))))
75 (when (memq sock reads)
76 (match (get-bytevector-some sock)
77 ((? eof-object?)
78 (primitive-exit 0))
79 (bv
80 (put-bytevector stdout bv))))
81 (loop))
82 (_
83 (primitive-exit 1)))))))
84
85 (let ((channel
86 (open-remote-pipe* session OPEN_BOTH
87 ;; Sort-of shell-quote REDIRECT.
88 "guile" "-c"
89 (object->string
90 (object->string redirect)))))
91 (open-connection #:port channel)))
92
93 (define (store-import-channel session)
94 "Return an output port to which archives to be exported to SESSION's store
95 can be written."
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
100 ;; trip.
101 (define import
102 `(begin
103 (use-modules (guix))
104
105 (with-store store
106 (setvbuf (current-input-port) _IONBF)
107
108 ;; FIXME: Exceptions are silently swallowed. We should report them
109 ;; somehow.
110 (import-paths store (current-input-port)))))
111
112 (open-remote-output-pipe session
113 (string-join
114 `("guile" "-c"
115 ,(object->string
116 (object->string import))))))
117
118 (define* (store-export-channel session files
119 #:key recursive?)
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
123 ;; remote store.
124 (define export
125 `(begin
126 (use-modules (guix))
127
128 (with-store store
129 (setvbuf (current-output-port) _IONBF)
130
131 ;; FIXME: Exceptions are silently swallowed. We should report them
132 ;; somehow.
133 (export-paths store ',files (current-output-port)
134 #:recursive? ,recursive?))))
135
136 (open-remote-input-pipe session
137 (string-join
138 `("guile" "-c"
139 ,(object->string
140 (object->string export))))))
141
142 (define* (send-files local files remote
143 #:key
144 recursive?
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
154 `(begin
155 (use-modules (guix)
156 (srfi srfi-1) (srfi srfi-26))
157
158 (with-store store
159 (remove (cut valid-path? store <>)
160 ',files)))))
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))
166
167 ;; Send MISSING in topological order.
168 (export-paths local missing port)
169
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)
173
174 ;; Wait for completion of the remote process.
175 (let ((result (zero? (channel-get-exit-status port))))
176 (close-port port)
177 missing)))
178
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)))
183
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)
188 (#f #f)
189 ((? session? session)
190 (session-get session 'host))))
191
192 (define* (file-retrieval-port files remote
193 #:key recursive?)
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
200
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.
215 (raise (condition
216 (&message
217 (message
218 (format #f
219 (_ "failed to retrieve store items from '~a'")
220 (remote-store-host remote)))))))
221
222 (let ((result (import-paths local port)))
223 (close-port port)
224 result)))
225
226 ;;; ssh.scm ends here