Commit | Line | Data |
---|---|---|
f11c444d LC |
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 scripts copy) | |
20 | #:use-module (guix ui) | |
21 | #:use-module (guix scripts) | |
22 | #:use-module (guix ssh) | |
23 | #:use-module (guix store) | |
24 | #:use-module (guix utils) | |
25 | #:use-module (guix derivations) | |
26 | #:use-module (guix scripts build) | |
27 | #:use-module ((guix scripts archive) #:select (options->derivations+files)) | |
28 | #:use-module (ssh session) | |
29 | #:use-module (ssh auth) | |
30 | #:use-module (ssh key) | |
31 | #:use-module (srfi srfi-1) | |
32 | #:use-module (srfi srfi-11) | |
33 | #:use-module (srfi srfi-37) | |
34 | #:use-module (ice-9 match) | |
35 | #:use-module (ice-9 format) | |
36 | #:export (guix-copy)) | |
37 | ||
38 | \f | |
39 | ;;; | |
40 | ;;; Exchanging store items over SSH. | |
41 | ;;; | |
42 | ||
43 | (define %compression | |
44 | "zlib@openssh.com,zlib") | |
45 | ||
46 | (define* (open-ssh-session host #:key user port) | |
47 | "Open an SSH session for HOST and return it. When USER and PORT are #f, use | |
48 | default values or whatever '~/.ssh/config' specifies; otherwise use them. | |
49 | Throw an error on failure." | |
50 | (let ((session (make-session #:user user | |
51 | #:host host | |
52 | #:port port | |
53 | #:timeout 10 ;seconds | |
54 | ;; #:log-verbosity 'protocol | |
55 | ||
56 | ;; We need lightweight compression when | |
57 | ;; exchanging full archives. | |
58 | #:compression %compression | |
59 | #:compression-level 3))) | |
60 | ||
61 | ;; Honor ~/.ssh/config. | |
62 | (session-parse-config! session) | |
63 | ||
64 | (match (connect! session) | |
65 | ('ok | |
db6afe38 MO |
66 | ;; Use public key authentication, via the SSH agent if it's available. |
67 | (match (userauth-public-key/auto! session) | |
f11c444d LC |
68 | ('success |
69 | session) | |
70 | (x | |
71 | (disconnect! session) | |
72 | (leave (_ "SSH authentication failed for '~a': ~a~%") | |
73 | host (get-error session))))) | |
74 | (x | |
75 | ;; Connection failed or timeout expired. | |
76 | (leave (_ "SSH connection to '~a' failed: ~a~%") | |
77 | host (get-error session)))))) | |
78 | ||
79 | (define (ssh-spec->user+host+port spec) | |
80 | "Parse SPEC, a string like \"user@host:port\" or just \"host\", and return | |
81 | three values: the user name (or #f), the host name, and the TCP port | |
82 | number (or #f) corresponding to SPEC." | |
83 | (define tokens | |
84 | (char-set #\@ #\:)) | |
85 | ||
86 | (match (string-tokenize spec (char-set-complement tokens)) | |
87 | ((host) | |
88 | (values #f host #f)) | |
89 | ((left right) | |
90 | (if (string-index spec #\@) | |
91 | (values left right #f) | |
92 | (values #f left (string->number right)))) | |
93 | ((user host port) | |
94 | (match (string->number port) | |
95 | ((? integer? port) | |
96 | (values user host port)) | |
97 | (x | |
98 | (leave (_ "~a: invalid TCP port number~%") port)))) | |
99 | (x | |
100 | (leave (_ "~a: invalid SSH specification~%") spec)))) | |
101 | ||
102 | (define (send-to-remote-host target opts) | |
103 | "Send ITEMS to TARGET. ITEMS is a list of store items or package names; for ; | |
104 | package names, build the underlying packages before sending them." | |
105 | (with-store local | |
106 | (set-build-options-from-command-line local opts) | |
107 | (let-values (((user host port) | |
108 | (ssh-spec->user+host+port target)) | |
109 | ((drv items) | |
110 | (options->derivations+files local opts))) | |
111 | (show-what-to-build local drv | |
112 | #:use-substitutes? (assoc-ref opts 'substitutes?) | |
113 | #:dry-run? (assoc-ref opts 'dry-run?)) | |
114 | ||
115 | (and (or (assoc-ref opts 'dry-run?) | |
116 | (build-derivations local drv)) | |
117 | (let* ((session (open-ssh-session host #:user user #:port port)) | |
118 | (sent (send-files local items | |
119 | (connect-to-remote-daemon session) | |
120 | #:recursive? #t))) | |
121 | (format #t "~{~a~%~}" sent) | |
122 | sent))))) | |
123 | ||
124 | (define (retrieve-from-remote-host source opts) | |
125 | "Retrieve ITEMS from SOURCE." | |
126 | (with-store local | |
127 | (let*-values (((user host port) | |
128 | (ssh-spec->user+host+port source)) | |
129 | ((session) | |
130 | (open-ssh-session host #:user user #:port port)) | |
131 | ((remote) | |
132 | (connect-to-remote-daemon session))) | |
133 | (set-build-options-from-command-line local opts) | |
134 | ;; TODO: Here we could to compute and build the derivations on REMOTE | |
135 | ;; rather than on LOCAL (one-off offloading) but that is currently too | |
136 | ;; slow due to the many RPC round trips. So we just assume that REMOTE | |
137 | ;; contains ITEMS. | |
138 | (let*-values (((drv items) | |
139 | (options->derivations+files local opts)) | |
140 | ((retrieved) | |
141 | (retrieve-files local items remote #:recursive? #t))) | |
142 | (format #t "~{~a~%~}" retrieved) | |
143 | retrieved)))) | |
144 | ||
145 | \f | |
146 | ;;; | |
147 | ;;; Options. | |
148 | ;;; | |
149 | ||
150 | (define (show-help) | |
151 | (display (_ "Usage: guix copy [OPTION]... ITEMS... | |
152 | Copy ITEMS to or from the specified host over SSH.\n")) | |
153 | (display (_ " | |
154 | --to=HOST send ITEMS to HOST")) | |
155 | (display (_ " | |
156 | --from=HOST receive ITEMS from HOST")) | |
157 | (newline) | |
158 | (show-build-options-help) | |
159 | (newline) | |
160 | (display (_ " | |
161 | -h, --help display this help and exit")) | |
162 | (display (_ " | |
163 | -V, --version display version information and exit")) | |
164 | (newline) | |
165 | (show-bug-report-information)) | |
166 | ||
167 | (define %options | |
168 | ;; Specifications of the command-line options. | |
169 | (cons* (option '("to") #t #f | |
170 | (lambda (opt name arg result) | |
171 | (alist-cons 'destination arg result))) | |
172 | (option '("from") #t #f | |
173 | (lambda (opt name arg result) | |
174 | (alist-cons 'source arg result))) | |
175 | (option '(#\h "help") #f #f | |
176 | (lambda args | |
177 | (show-help) | |
178 | (exit 0))) | |
179 | (option '(#\V "version") #f #f | |
180 | (lambda args | |
181 | (show-version-and-exit "guix copy"))) | |
182 | (option '(#\s "system") #t #f | |
183 | (lambda (opt name arg result) | |
184 | (alist-cons 'system arg | |
185 | (alist-delete 'system result eq?)))) | |
186 | %standard-build-options)) | |
187 | ||
188 | (define %default-options | |
189 | `((system . ,(%current-system)) | |
190 | (substitutes? . #t) | |
191 | (graft? . #t) | |
192 | (max-silent-time . 3600) | |
193 | (verbosity . 0))) | |
194 | ||
195 | \f | |
196 | ;;; | |
197 | ;;; Entry point. | |
198 | ;;; | |
199 | ||
200 | (define (guix-copy . args) | |
201 | (with-error-handling | |
202 | (let* ((opts (parse-command-line args %options (list %default-options))) | |
203 | (source (assoc-ref opts 'source)) | |
204 | (target (assoc-ref opts 'destination))) | |
205 | (cond (target (send-to-remote-host target opts)) | |
206 | (source (retrieve-from-remote-host source opts)) | |
207 | (else (leave (_ "use '--to' or '--from'~%"))))))) |