Add (guix ssh) module.
[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 (ice-9 match)
29 #:export (connect-to-remote-daemon
30 send-files
31 retrieve-files
32 remote-store-host
33
34 file-retrieval-port))
35
36 ;;; Commentary:
37 ;;;
38 ;;; This module provides tools to support communication with remote stores
39 ;;; over SSH, using Guile-SSH.
40 ;;;
41 ;;; Code:
42
43 (define* (connect-to-remote-daemon session
44 #:optional
45 (socket-name "/var/guix/daemon-socket/socket"))
46 "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
47 an SSH session. Return a <nix-server> object."
48 (define redirect
49 ;; Code run in SESSION to redirect the remote process' stdin/stdout to the
50 ;; daemon's socket, à la socat. The SSH protocol supports forwarding to
51 ;; Unix-domain sockets but libssh doesn't have an API for that, hence this
52 ;; hack.
53 `(begin
54 (use-modules (ice-9 match) (rnrs io ports))
55
56 (let ((sock (socket AF_UNIX SOCK_STREAM 0))
57 (stdin (current-input-port))
58 (stdout (current-output-port)))
59 (setvbuf stdin _IONBF)
60 (setvbuf stdout _IONBF)
61 (connect sock AF_UNIX ,socket-name)
62
63 (let loop ()
64 (match (select (list stdin sock) '() (list stdin stdout sock))
65 ((reads writes ())
66 (when (memq stdin reads)
67 (match (get-bytevector-some stdin)
68 ((? eof-object?)
69 (primitive-exit 0))
70 (bv
71 (put-bytevector sock bv))))
72 (when (memq sock reads)
73 (match (get-bytevector-some sock)
74 ((? eof-object?)
75 (primitive-exit 0))
76 (bv
77 (put-bytevector stdout bv))))
78 (loop))
79 (_
80 (primitive-exit 1)))))))
81
82 (let ((channel
83 (open-remote-pipe* session OPEN_BOTH
84 ;; Sort-of shell-quote REDIRECT.
85 "guile" "-c"
86 (object->string
87 (object->string redirect)))))
88 (open-connection #:port channel)))
89
90 (define (store-import-channel session)
91 "Return an output port to which archives to be exported to SESSION's store
92 can be written."
93 ;; Using the 'import-paths' RPC on a remote store would be slow because it
94 ;; makes a round trip every time 32 KiB have been transferred. This
95 ;; procedure instead opens a separate channel to use the remote
96 ;; 'import-paths' procedure, which consumes all the data in a single round
97 ;; trip.
98 (define import
99 `(begin
100 (use-modules (guix))
101
102 (with-store store
103 (setvbuf (current-input-port) _IONBF)
104
105 ;; FIXME: Exceptions are silently swallowed. We should report them
106 ;; somehow.
107 (import-paths store (current-input-port)))))
108
109 (open-remote-output-pipe session
110 (string-join
111 `("guile" "-c"
112 ,(object->string
113 (object->string import))))))
114
115 (define (store-export-channel session files)
116 "Return an input port from which an export of FILES from SESSION's store can
117 be read."
118 ;; Same as above: this is more efficient than calling 'export-paths' on a
119 ;; remote store.
120 (define export
121 `(begin
122 (use-modules (guix))
123
124 (with-store store
125 (setvbuf (current-output-port) _IONBF)
126
127 ;; FIXME: Exceptions are silently swallowed. We should report them
128 ;; somehow.
129 (export-paths store ',files (current-output-port)))))
130
131 (open-remote-input-pipe session
132 (string-join
133 `("guile" "-c"
134 ,(object->string
135 (object->string export))))))
136
137 (define* (send-files local files remote
138 #:key (log-port (current-error-port)))
139 "Send the subset of FILES from LOCAL (a local store) that's missing to
140 REMOTE, a remote store."
141 ;; Compute the subset of FILES missing on SESSION and send them.
142 (let* ((session (channel-get-session (nix-server-socket remote)))
143 (node (make-node session))
144 (missing (node-eval node
145 `(begin
146 (use-modules (guix)
147 (srfi srfi-1) (srfi srfi-26))
148
149 (with-store store
150 (remove (cut valid-path? store <>)
151 ',files)))))
152 (count (length missing))
153 (port (store-import-channel session)))
154 (format log-port (N_ "sending ~a store item to '~a'...~%"
155 "sending ~a store items to '~a'...~%" count)
156 count (session-get session 'host))
157
158 ;; Send MISSING in topological order.
159 (export-paths local missing port)
160
161 ;; Tell the remote process that we're done. (In theory the end-of-archive
162 ;; mark of 'export-paths' would be enough, but in practice it's not.)
163 (channel-send-eof port)
164
165 ;; Wait for completion of the remote process.
166 (let ((result (zero? (channel-get-exit-status port))))
167 (close-port port)
168 result)))
169
170 (define (remote-store-session remote)
171 "Return the SSH channel beneath REMOTE, a remote store as returned by
172 'connect-to-remote-daemon', or #f."
173 (channel-get-session (nix-server-socket remote)))
174
175 (define (remote-store-host remote)
176 "Return the name of the host REMOTE is connected to, where REMOTE is a
177 remote store as returned by 'connect-to-remote-daemon'."
178 (match (remote-store-session remote)
179 (#f #f)
180 ((? session? session)
181 (session-get session 'host))))
182
183 (define (file-retrieval-port files remote)
184 "Return an input port from which to retrieve FILES (a list of store items)
185 from REMOTE, along with the number of items to retrieve (lower than or equal
186 to the length of FILES.)"
187 (values (store-export-channel (remote-store-session remote) files)
188 (length files)))
189
190 (define* (retrieve-files local files remote
191 #:key (log-port (current-error-port)))
192 "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
193 LOCAL."
194 (let-values (((port count)
195 (file-retrieval-port files remote)))
196 (format #t (N_ "retrieving ~a store item from '~a'...~%"
197 "retrieving ~a store items from '~a'...~%" count)
198 count (remote-store-host remote))
199
200 (let ((result (import-paths local port)))
201 (close-port port)
202 result)))
203
204 ;;; ssh.scm ends here