gnu: wine-staging: Update to 4.14.
[jackhill/guix/guix.git] / gnu / machine / ssh.scm
CommitLineData
fa9edf09
JK
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.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 (gnu machine ssh)
9c70c460 20 #:use-module (gnu bootloader)
fa9edf09
JK
21 #:use-module (gnu machine)
22 #:autoload (gnu packages gnupg) (guile-gcrypt)
fa9edf09 23 #:use-module (gnu system)
fd3119db
JK
24 #:use-module (gnu system file-systems)
25 #:use-module (gnu system uuid)
26 #:use-module (guix diagnostics)
fa9edf09
JK
27 #:use-module (guix gexp)
28 #:use-module (guix i18n)
29 #:use-module (guix modules)
30 #:use-module (guix monads)
3033d59a 31 #:use-module (guix pki)
fa9edf09
JK
32 #:use-module (guix records)
33 #:use-module (guix remote)
5c793753 34 #:use-module (guix scripts system reconfigure)
fa9edf09
JK
35 #:use-module (guix ssh)
36 #:use-module (guix store)
fd3119db 37 #:use-module (guix utils)
3033d59a 38 #:use-module (gcrypt pk-crypto)
fa9edf09 39 #:use-module (ice-9 match)
3033d59a 40 #:use-module (ice-9 textual-ports)
9c70c460 41 #:use-module (srfi srfi-1)
fa9edf09 42 #:use-module (srfi srfi-19)
5c793753 43 #:use-module (srfi srfi-26)
2c8e04f1 44 #:use-module (srfi srfi-34)
fa9edf09
JK
45 #:use-module (srfi srfi-35)
46 #:export (managed-host-environment-type
47
48 machine-ssh-configuration
49 machine-ssh-configuration?
50 machine-ssh-configuration
51
52 machine-ssh-configuration-host-name
d84e9b75 53 machine-ssh-configuration-build-locally?
3033d59a 54 machine-ssh-configuration-authorize?
fa9edf09
JK
55 machine-ssh-configuration-port
56 machine-ssh-configuration-user
57 machine-ssh-configuration-session))
58
59;;; Commentary:
60;;;
61;;; This module implements remote evaluation and system deployment for
53f21b3f 62;;; machines that are accessible over SSH and have a known host-name. In the
fa9edf09
JK
63;;; sense of the broader "machine" interface, we describe the environment for
64;;; such machines as 'managed-host.
65;;;
66;;; Code:
67
68\f
69;;;
70;;; Parameters for the SSH client.
71;;;
72
73(define-record-type* <machine-ssh-configuration> machine-ssh-configuration
74 make-machine-ssh-configuration
75 machine-ssh-configuration?
76 this-machine-ssh-configuration
3033d59a
JK
77 (host-name machine-ssh-configuration-host-name) ; string
78 (system machine-ssh-configuration-system) ; string
79 (build-locally? machine-ssh-configuration-build-locally? ; boolean
d84e9b75 80 (default #t))
3033d59a
JK
81 (authorize? machine-ssh-configuration-authorize? ; boolean
82 (default #t))
83 (port machine-ssh-configuration-port ; integer
d84e9b75 84 (default 22))
3033d59a 85 (user machine-ssh-configuration-user ; string
d84e9b75 86 (default "root"))
3033d59a 87 (identity machine-ssh-configuration-identity ; path to a private key
d84e9b75 88 (default #f))
3033d59a 89 (session machine-ssh-configuration-session ; session
d84e9b75 90 (default #f)))
fa9edf09
JK
91
92(define (machine-ssh-session machine)
93 "Return the SSH session that was given in MACHINE's configuration, or create
94one from the configuration's parameters if one was not provided."
95 (maybe-raise-unsupported-configuration-error machine)
96 (let ((config (machine-configuration machine)))
97 (or (machine-ssh-configuration-session config)
98 (let ((host-name (machine-ssh-configuration-host-name config))
99 (user (machine-ssh-configuration-user config))
100 (port (machine-ssh-configuration-port config))
101 (identity (machine-ssh-configuration-identity config)))
102 (open-ssh-session host-name
103 #:user user
104 #:port port
105 #:identity identity)))))
106
107\f
108;;;
109;;; Remote evaluation.
110;;;
111
5ea7537b
JK
112(define (machine-become-command machine)
113 "Return as a list of strings the program and arguments necessary to run a
114shell command with escalated privileges for MACHINE's configuration."
115 (if (string= "root" (machine-ssh-configuration-user
116 (machine-configuration machine)))
117 '()
118 '("/run/setuid-programs/sudo" "-n" "--")))
119
fa9edf09
JK
120(define (managed-host-remote-eval machine exp)
121 "Internal implementation of 'machine-remote-eval' for MACHINE instances with
122an environment type of 'managed-host."
123 (maybe-raise-unsupported-configuration-error machine)
2c8e04f1
JK
124 (let ((config (machine-configuration machine)))
125 (remote-eval exp (machine-ssh-session machine)
126 #:build-locally?
127 (machine-ssh-configuration-build-locally? config)
128 #:system
4cc5e520
JK
129 (machine-ssh-configuration-system config)
130 #:become-command
131 (machine-become-command machine))))
fa9edf09
JK
132
133\f
fd3119db
JK
134;;;
135;;; Safety checks.
136;;;
137
138(define (machine-check-file-system-availability machine)
139 "Raise a '&message' error condition if any of the file-systems specified in
140MACHINE's 'system' declaration do not exist on the machine."
141 (define file-systems
142 (filter (lambda (fs)
143 (and (file-system-mount? fs)
144 (not (member (file-system-type fs)
145 %pseudo-file-system-types))
146 (not (memq 'bind-mount (file-system-flags fs)))))
147 (operating-system-file-systems (machine-operating-system machine))))
148
149 (define (check-literal-file-system fs)
150 (define remote-exp
151 #~(catch 'system-error
152 (lambda ()
153 (stat #$(file-system-device fs))
154 #t)
155 (lambda args
156 (system-error-errno args))))
157
158 (mlet %store-monad ((errno (machine-remote-eval machine remote-exp)))
159 (when (number? errno)
160 (raise (condition
161 (&message
162 (message (format #f (G_ "device '~a' not found: ~a")
163 (file-system-device fs)
164 (strerror errno)))))))
165 (return #t)))
166
167 (define (check-labeled-file-system fs)
168 (define remote-exp
169 (with-imported-modules '((gnu build file-systems))
170 #~(begin
171 (use-modules (gnu build file-systems))
172 (find-partition-by-label #$(file-system-label->string
173 (file-system-device fs))))))
174
175 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
176 (unless result
177 (raise (condition
178 (&message
179 (message (format #f (G_ "no file system with label '~a'")
180 (file-system-label->string
181 (file-system-device fs))))))))
182 (return #t)))
183
184 (define (check-uuid-file-system fs)
185 (define remote-exp
186 (with-imported-modules (source-module-closure
187 '((gnu build file-systems)
188 (gnu system uuid)))
189 #~(begin
190 (use-modules (gnu build file-systems)
191 (gnu system uuid))
192
193 (define uuid
194 (string->uuid #$(uuid->string (file-system-device fs))))
195
196 (find-partition-by-uuid uuid))))
197
198 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
199 (unless result
200 (raise (condition
201 (&message
202 (message (format #f (G_ "no file system with UUID '~a'")
203 (uuid->string (file-system-device fs))))))))
204 (return #t)))
205
206 (mbegin %store-monad
207 (mapm %store-monad check-literal-file-system
208 (filter (lambda (fs)
209 (string? (file-system-device fs)))
210 file-systems))
211 (mapm %store-monad check-labeled-file-system
212 (filter (lambda (fs)
213 (file-system-label? (file-system-device fs)))
214 file-systems))
215 (mapm %store-monad check-uuid-file-system
216 (filter (lambda (fs)
217 (uuid? (file-system-device fs)))
218 file-systems))))
219
220(define (machine-check-initrd-modules machine)
221 "Raise a '&message' error condition if any of the modules needed by
222'needed-for-boot' file systems in MACHINE are not available in the initrd."
223 (define file-systems
224 (filter file-system-needed-for-boot?
225 (operating-system-file-systems (machine-operating-system machine))))
226
227 (define (missing-modules fs)
228 (define remote-exp
229 (let ((device (file-system-device fs)))
230 (with-imported-modules (source-module-closure
231 '((gnu build file-systems)
232 (gnu build linux-modules)
233 (gnu system uuid)))
234 #~(begin
235 (use-modules (gnu build file-systems)
236 (gnu build linux-modules)
237 (gnu system uuid))
238
239 (define dev
240 #$(cond ((string? device) device)
241 ((uuid? device) #~(find-partition-by-uuid
242 (string->uuid
243 #$(uuid->string device))))
244 ((file-system-label? device)
245 #~(find-partition-by-label
246 (file-system-label->string #$device)))))
247
248 (missing-modules dev '#$(operating-system-initrd-modules
249 (machine-operating-system machine)))))))
250 (mlet %store-monad ((missing (machine-remote-eval machine remote-exp)))
251 (return (list fs missing))))
252
253 (mlet %store-monad ((device (mapm %store-monad missing-modules file-systems)))
254 (for-each (match-lambda
255 ((fs missing)
256 (unless (null? missing)
257 (raise (condition
258 (&message
259 (message (format #f (G_ "~a missing modules ~{ ~a~}~%")
260 (file-system-device fs)
261 missing))))))))
262 device)
263 (return #t)))
264
2c8e04f1
JK
265(define (machine-check-building-for-appropriate-system machine)
266 "Raise a '&message' error condition if MACHINE is configured to be built
267locally and the 'system' field does not match the '%current-system' reported
268by MACHINE."
269 (let ((config (machine-configuration machine))
270 (system (remote-system (machine-ssh-session machine))))
271 (when (and (machine-ssh-configuration-build-locally? config)
272 (not (string= system (machine-ssh-configuration-system config))))
273 (raise (condition
274 (&message
275 (message (format #f (G_ "incorrect target system \
276('~a' was given, while the system reports that it is '~a')~%")
277 (machine-ssh-configuration-system config)
278 system)))))))
279 (with-monad %store-monad (return #t)))
280
fd3119db
JK
281(define (check-deployment-sanity machine)
282 "Raise a '&message' error condition if it is clear that deploying MACHINE's
283'system' declaration would fail."
2c8e04f1
JK
284 ;; Order is important here -- an incorrect value for 'system' will cause
285 ;; invocations of 'remote-eval' to fail.
fd3119db 286 (mbegin %store-monad
2c8e04f1 287 (machine-check-building-for-appropriate-system machine)
fd3119db
JK
288 (machine-check-file-system-availability machine)
289 (machine-check-initrd-modules machine)))
290
291\f
fa9edf09
JK
292;;;
293;;; System deployment.
294;;;
295
fa9edf09
JK
296(define (machine-boot-parameters machine)
297 "Monadic procedure returning a list of 'boot-parameters' for the generations
298of MACHINE's system profile, ordered from most recent to oldest."
299 (define bootable-kernel-arguments
300 (@@ (gnu system) bootable-kernel-arguments))
301
302 (define remote-exp
303 (with-extensions (list guile-gcrypt)
304 (with-imported-modules (source-module-closure '((guix config)
305 (guix profiles)))
306 #~(begin
307 (use-modules (guix config)
308 (guix profiles)
309 (ice-9 textual-ports))
310
311 (define %system-profile
312 (string-append %state-directory "/profiles/system"))
313
314 (define (read-file path)
315 (call-with-input-file path
316 (lambda (port)
317 (get-string-all port))))
318
319 (map (lambda (generation)
320 (let* ((system-path (generation-file-name %system-profile
321 generation))
322 (boot-parameters-path (string-append system-path
323 "/parameters"))
324 (time (stat:mtime (lstat system-path))))
325 (list generation
326 system-path
327 time
328 (read-file boot-parameters-path))))
329 (reverse (generation-numbers %system-profile)))))))
330
331 (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
332 (return
333 (map (lambda (generation)
334 (match generation
335 ((generation system-path time serialized-params)
336 (let* ((params (call-with-input-string serialized-params
337 read-boot-parameters))
338 (root (boot-parameters-root-device params))
339 (label (boot-parameters-label params)))
340 (boot-parameters
341 (inherit params)
342 (label
343 (string-append label " (#"
344 (number->string generation) ", "
345 (let ((time (make-time time-utc 0 time)))
346 (date->string (time-utc->date time)
347 "~Y-~m-~d ~H:~M"))
348 ")"))
349 (kernel-arguments
350 (append (bootable-kernel-arguments system-path root)
351 (boot-parameters-kernel-arguments params))))))))
352 generations))))
353
9c70c460
JK
354(define-syntax-rule (with-roll-back should-roll-back? mbody ...)
355 "Catch exceptions that arise when binding MBODY, a monadic expression in
356%STORE-MONAD, and collect their arguments in a &deploy-error condition, with
357the 'should-roll-back' field set to SHOULD-ROLL-BACK?"
358 (catch #t
359 (lambda ()
360 mbody ...)
361 (lambda args
362 (raise (condition (&deploy-error
363 (should-roll-back should-roll-back?)
364 (captured-args args)))))))
365
fa9edf09
JK
366(define (deploy-managed-host machine)
367 "Internal implementation of 'deploy-machine' for MACHINE instances with an
368environment type of 'managed-host."
369 (maybe-raise-unsupported-configuration-error machine)
3033d59a
JK
370 (when (machine-ssh-configuration-authorize?
371 (machine-configuration machine))
372 (unless (file-exists? %public-key-file)
373 (raise (condition
374 (&message
375 (message (format #f (G_ "no signing key '~a'. \
376have you run 'guix archive --generate-key?'")
377 %public-key-file))))))
378 (remote-authorize-signing-key (call-with-input-file %public-key-file
379 (lambda (port)
380 (string->canonical-sexp
381 (get-string-all port))))
4cc5e520
JK
382 (machine-ssh-session machine)
383 (machine-become-command machine)))
fd3119db
JK
384 (mlet %store-monad ((_ (check-deployment-sanity machine))
385 (boot-parameters (machine-boot-parameters machine)))
d97ce204 386 (let* ((os (machine-operating-system machine))
5c793753
JK
387 (eval (cut machine-remote-eval machine <>))
388 (menu-entries (map boot-parameters->menu-entry boot-parameters))
389 (bootloader-configuration (operating-system-bootloader os))
390 (bootcfg (operating-system-bootcfg os menu-entries)))
391 (mbegin %store-monad
9c70c460
JK
392 (with-roll-back #f
393 (switch-to-system eval os))
394 (with-roll-back #t
395 (mbegin %store-monad
396 (upgrade-shepherd-services eval os)
397 (install-bootloader eval bootloader-configuration bootcfg)))))))
398
399\f
400;;;
401;;; Roll-back.
402;;;
403
404(define (roll-back-managed-host machine)
405 "Internal implementation of 'roll-back-machine' for MACHINE instances with
406an environment type of 'managed-host."
407 (define remote-exp
408 (with-extensions (list guile-gcrypt)
409 (with-imported-modules (source-module-closure '((guix config)
410 (guix profiles)))
411 #~(begin
412 (use-modules (guix config)
413 (guix profiles))
414
415 (define %system-profile
416 (string-append %state-directory "/profiles/system"))
417
418 (define target-generation
419 (relative-generation %system-profile -1))
420
421 (if target-generation
422 (switch-to-generation %system-profile target-generation)
423 'error)))))
424
425 (define roll-back-failure
426 (condition (&message (message (G_ "could not roll-back machine")))))
427
428 (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine))
429 (_ -> (if (< (length boot-parameters) 2)
430 (raise roll-back-failure)))
431 (entries -> (map boot-parameters->menu-entry
432 (list (second boot-parameters))))
433 (old-entries -> (map boot-parameters->menu-entry
434 (drop boot-parameters 2)))
435 (bootloader -> (operating-system-bootloader
436 (machine-operating-system machine)))
437 (bootcfg (lower-object
438 ((bootloader-configuration-file-generator
439 (bootloader-configuration-bootloader
440 bootloader))
441 bootloader entries
442 #:old-entries old-entries)))
443 (remote-result (machine-remote-eval machine remote-exp)))
444 (when (eqv? 'error remote-result)
445 (raise roll-back-failure))))
fa9edf09
JK
446
447\f
448;;;
449;;; Environment type.
450;;;
451
452(define managed-host-environment-type
453 (environment-type
454 (machine-remote-eval managed-host-remote-eval)
455 (deploy-machine deploy-managed-host)
9c70c460 456 (roll-back-machine roll-back-managed-host)
fa9edf09 457 (name 'managed-host-environment-type)
53f21b3f 458 (description "Provisioning for machines that are accessible over SSH
fa9edf09
JK
459and have a known host-name. This entails little more than maintaining an SSH
460connection to the host.")))
461
462(define (maybe-raise-unsupported-configuration-error machine)
463 "Raise an error if MACHINE's configuration is not an instance of
464<machine-ssh-configuration>."
465 (let ((config (machine-configuration machine))
466 (environment (environment-type-name (machine-environment machine))))
467 (unless (and config (machine-ssh-configuration? config))
468 (raise (condition
469 (&message
470 (message (format #f (G_ "unsupported machine configuration '~a'
471for environment of type '~a'")
472 config
473 environment))))))))