projects/guix·blob wip-postfix · refs

644 lines · 27.2 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2019 Jakob L. Kreuze <[email protected]>
3;;; Copyright © 2020-2023 Ludovic Courtès <[email protected]>
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu machine ssh)
21 #:use-module (gnu bootloader)
22 #:use-module (gnu machine)
23 #:autoload (gnu packages gnupg) (guile-gcrypt)
24 #:use-module (gnu system)
25 #:use-module (gnu system file-systems)
26 #:use-module (gnu system uuid)
27 #:use-module ((gnu services) #:select (sexp->system-provenance))
28 #:use-module (guix diagnostics)
29 #:use-module (guix memoization)
30 #:use-module (guix gexp)
31 #:use-module (guix i18n)
32 #:use-module (guix modules)
33 #:use-module (guix monads)
34 #:use-module (guix pki)
35 #:use-module (guix records)
36 #:use-module (guix remote)
37 #:use-module (guix scripts system reconfigure)
38 #:use-module (guix ssh)
39 #:use-module (guix store)
40 #:use-module (guix utils)
41 #:use-module ((guix self) #:select (make-config.scm))
42 #:use-module ((guix inferior)
43 #:select (inferior-exception?
44 inferior-exception-arguments))
45 #:use-module ((guix platform) #:select (systems))
46 #:use-module (gcrypt pk-crypto)
47 #:use-module (ice-9 format)
48 #:use-module (ice-9 match)
49 #:use-module (ice-9 textual-ports)
50 #:use-module (srfi srfi-1)
51 #:use-module (srfi srfi-9)
52 #:use-module (srfi srfi-19)
53 #:use-module (srfi srfi-26)
54 #:use-module (srfi srfi-34)
55 #:use-module (srfi srfi-35)
56 #:export (managed-host-environment-type
57
58 machine-ssh-configuration
59 machine-ssh-configuration?
60 machine-ssh-configuration
61
62 machine-ssh-configuration-host-name
63 machine-ssh-configuration-build-locally?
64 machine-ssh-configuration-authorize?
65 machine-ssh-configuration-allow-downgrades?
66 machine-ssh-configuration-port
67 machine-ssh-configuration-user
68 machine-ssh-configuration-host-key
69 machine-ssh-configuration-session))
70
71;;; Commentary:
72;;;
73;;; This module implements remote evaluation and system deployment for
74;;; machines that are accessible over SSH and have a known host-name. In the
75;;; sense of the broader "machine" interface, we describe the environment for
76;;; such machines as 'managed-host.
77;;;
78;;; Code:
79
80
81;;;
82;;; Parameters for the SSH client.
83;;;
84
85(define-record-type* <machine-ssh-configuration> machine-ssh-configuration
86 make-machine-ssh-configuration
87 machine-ssh-configuration?
88 this-machine-ssh-configuration
89 (host-name machine-ssh-configuration-host-name) ; string
90 (system machine-ssh-configuration-system ; string
91 (sanitize validate-system-type))
92 (build-locally? machine-ssh-configuration-build-locally? ; boolean
93 (default #t))
94 (authorize? machine-ssh-configuration-authorize? ; boolean
95 (default #t))
96 (allow-downgrades? machine-ssh-configuration-allow-downgrades? ; boolean
97 (default #f))
98 (safety-checks? machine-ssh-configuration-safety-checks? ;boolean
99 (default #t))
100 (port machine-ssh-configuration-port ; integer
101 (default 22))
102 (user machine-ssh-configuration-user ; string
103 (default "root"))
104 (identity machine-ssh-configuration-identity ; path to a private key
105 (default #f))
106 (session machine-ssh-configuration-session ; session
107 (thunked)
108 (default
109 ;; By default, open the session once and cache it.
110 (open-machine-ssh-session* this-machine-ssh-configuration)))
111 (host-key machine-ssh-configuration-host-key ; #f | string
112 (default #f)))
113
114(define-with-syntax-properties (validate-system-type (value properties))
115 ;; Raise an error if VALUE is not a valid system type.
116 (unless (string? value)
117 (raise (make-compound-condition
118 (condition
119 (&error-location
120 (location (source-properties->location properties))))
121 (formatted-message
122 (G_ "~a: invalid system type; must be a string")
123 value))))
124 (unless (member value (systems))
125 (raise (apply make-compound-condition
126 (condition
127 (&error-location
128 (location (source-properties->location properties))))
129 (formatted-message (G_ "~a: unknown system type") value)
130 (let ((closest (string-closest value (systems)
131 #:threshold 5)))
132 (if closest
133 (list (condition
134 (&fix-hint
135 (hint (format #f (G_ "Did you mean @code{~a}?")
136 closest)))))
137 '())))))
138 value)
139
140(define (open-machine-ssh-session config)
141 "Open an SSH session for CONFIG, a <machine-ssh-configuration> record."
142 (let ((host-name (machine-ssh-configuration-host-name config))
143 (user (machine-ssh-configuration-user config))
144 (port (machine-ssh-configuration-port config))
145 (identity (machine-ssh-configuration-identity config))
146 (host-key (machine-ssh-configuration-host-key config)))
147 (unless host-key
148 (warning (G_ "<machine-ssh-configuration> without a 'host-key' \
149is deprecated~%")))
150 (open-ssh-session host-name
151 #:user user
152 #:port port
153 #:identity identity
154 #:host-key host-key)))
155
156(define open-machine-ssh-session*
157 (mlambdaq (config)
158 "Memoizing variant of 'open-machine-ssh-session'."
159 (open-machine-ssh-session config)))
160
161(define (machine-ssh-session machine)
162 "Return the SSH session that was given in MACHINE's configuration, or create
163one from the configuration's parameters if one was not provided."
164 (maybe-raise-unsupported-configuration-error machine)
165 (let ((config (machine-configuration machine)))
166 (or (machine-ssh-configuration-session config)
167 (open-machine-ssh-session config))))
168
169
170;;;
171;;; Remote evaluation.
172;;;
173
174(define (machine-become-command machine)
175 "Return as a list of strings the program and arguments necessary to run a
176shell command with escalated privileges for MACHINE's configuration."
177 (if (string= "root" (machine-ssh-configuration-user
178 (machine-configuration machine)))
179 '()
180 '("/run/setuid-programs/sudo" "-n" "--")))
181
182(define (managed-host-remote-eval machine exp)
183 "Internal implementation of 'machine-remote-eval' for MACHINE instances with
184an environment type of 'managed-host."
185 (maybe-raise-unsupported-configuration-error machine)
186 (let ((config (machine-configuration machine)))
187 (remote-eval exp (machine-ssh-session machine)
188 #:build-locally?
189 (machine-ssh-configuration-build-locally? config)
190 #:system
191 (machine-ssh-configuration-system config)
192 #:become-command
193 (machine-become-command machine))))
194
195
196;;;
197;;; Safety checks.
198;;;
199
200;; Assertion to be executed remotely. This abstraction exists to allow us to
201;; gather a list of expressions to be evaluated and eventually evaluate them
202;; all at once instead of one by one. (This is pretty much a monad.)
203(define-record-type <remote-assertion>
204 (remote-assertion exp proc)
205 remote-assertion?
206 (exp remote-assertion-expression)
207 (proc remote-assertion-procedure))
208
209(define-syntax-rule (remote-let ((var exp)) body ...)
210 "Return a <remote-assertion> that binds VAR to the result of evaluating EXP,
211a gexp, remotely, and evaluate BODY in that context."
212 (remote-assertion exp (lambda (var) body ...)))
213
214(define (machine-check-file-system-availability machine)
215 "Return a list of <remote-assertion> that raise a '&message' error condition
216if any of the file-systems specified in MACHINE's 'system' declaration do not
217exist on the machine."
218 (define file-systems
219 (filter (lambda (fs)
220 (and (file-system-mount? fs)
221 (not (member (file-system-type fs)
222 %pseudo-file-system-types))
223 ;; Don't try to validate network file systems.
224 (not (string-prefix? "nfs" (file-system-type fs)))
225 (not (memq 'bind-mount (file-system-flags fs)))))
226 (operating-system-file-systems (machine-operating-system machine))))
227
228 (define (check-literal-file-system fs)
229 (remote-let ((errno #~(catch 'system-error
230 (lambda ()
231 (stat #$(file-system-device fs))
232 #t)
233 (lambda args
234 (system-error-errno args)))))
235 (when (number? errno)
236 (raise (formatted-message (G_ "device '~a' not found: ~a")
237 (file-system-device fs)
238 (strerror errno))))))
239
240 (define (check-labeled-file-system fs)
241 (define remote-exp
242 (with-imported-modules (source-module-closure
243 '((gnu build file-systems)))
244 #~(begin
245 (use-modules (gnu build file-systems))
246 (find-partition-by-label #$(file-system-label->string
247 (file-system-device fs))))))
248
249 (remote-let ((result remote-exp))
250 (unless result
251 (raise (formatted-message (G_ "no file system with label '~a'")
252 (file-system-label->string
253 (file-system-device fs)))))))
254
255 (define (check-uuid-file-system fs)
256 (define remote-exp
257 (with-imported-modules (source-module-closure
258 '((gnu build file-systems)
259 (gnu system uuid)))
260 #~(begin
261 (use-modules (gnu build file-systems)
262 (gnu system uuid))
263
264 (let ((uuid (uuid #$(uuid->string (file-system-device fs))
265 '#$(uuid-type (file-system-device fs)))))
266 (find-partition-by-uuid uuid)))))
267
268 (remote-let ((result remote-exp))
269 (unless result
270 (raise (formatted-message (G_ "no file system with UUID '~a'")
271 (uuid->string (file-system-device fs)))))))
272
273 (if (machine-ssh-configuration-safety-checks?
274 (machine-configuration machine))
275 (append (map check-literal-file-system
276 (filter (lambda (fs)
277 (string? (file-system-device fs)))
278 file-systems))
279 (map check-labeled-file-system
280 (filter (lambda (fs)
281 (file-system-label? (file-system-device fs)))
282 file-systems))
283 (map check-uuid-file-system
284 (filter (lambda (fs)
285 (uuid? (file-system-device fs)))
286 file-systems)))
287 '()))
288
289(define (machine-check-initrd-modules machine)
290 "Return a list of <remote-assertion> that raise a '&message' error condition
291if any of the modules needed by 'needed-for-boot' file systems in MACHINE are
292not available in the initrd."
293 (define file-systems
294 (filter (lambda (file-system)
295 (and (file-system-needed-for-boot? file-system)
296 (not (member (file-system-type file-system)
297 %pseudo-file-system-types))))
298 (operating-system-file-systems (machine-operating-system machine))))
299
300 (define (missing-modules fs)
301 (define remote-exp
302 (let ((device (file-system-device fs)))
303 (with-imported-modules (source-module-closure
304 '((gnu build file-systems)
305 (gnu build linux-modules)
306 (gnu system uuid)))
307 #~(begin
308 (use-modules (gnu build file-systems)
309 (gnu build linux-modules)
310 (gnu system uuid))
311
312 (define dev
313 #$(cond ((string? device) device)
314 ((uuid? device) #~(find-partition-by-uuid
315 (string->uuid
316 #$(uuid->string device))))
317 ((file-system-label? device)
318 #~(find-partition-by-label
319 #$(file-system-label->string device)))))
320
321 (missing-modules dev '#$(operating-system-initrd-modules
322 (machine-operating-system machine)))))))
323
324 (remote-let ((missing remote-exp))
325 (unless (null? missing)
326 (raise (formatted-message (G_ "missing modules for ~a:~{ ~a~}~%")
327 (file-system-device fs)
328 missing)))))
329
330 (if (machine-ssh-configuration-safety-checks?
331 (machine-configuration machine))
332 (map missing-modules file-systems)
333 '()))
334
335(define* (machine-check-forward-update machine)
336 "Check whether we are making a forward update for MACHINE. Depending on its
337'allow-upgrades?' field, raise an error or display a warning if we are
338potentially downgrading it."
339 (define config
340 (machine-configuration machine))
341
342 (define validate-reconfigure
343 (if (machine-ssh-configuration-allow-downgrades? config)
344 warn-about-backward-reconfigure
345 ensure-forward-reconfigure))
346
347 (remote-let ((provenance #~(call-with-input-file
348 "/run/current-system/provenance"
349 read)))
350 (define channels
351 (sexp->system-provenance provenance))
352
353 (check-forward-update validate-reconfigure
354 #:current-channels channels)))
355
356(define (machine-check-building-for-appropriate-system machine)
357 "Raise a '&message' error condition if MACHINE is configured to be built
358locally and the 'system' field does not match the '%current-system' reported
359by MACHINE."
360 (let ((config (machine-configuration machine))
361 (system (remote-system (machine-ssh-session machine))))
362 (when (and (machine-ssh-configuration-build-locally? config)
363 (not (string= system (machine-ssh-configuration-system config))))
364 (raise (formatted-message (G_ "incorrect target system\
365 ('~a' was given, while the system reports that it is '~a')~%")
366 (machine-ssh-configuration-system config)
367 system)))))
368
369(define (check-deployment-sanity machine)
370 "Raise a '&message' error condition if it is clear that deploying MACHINE's
371'system' declaration would fail."
372 (define assertions
373 (parameterize ((%current-system
374 (machine-ssh-configuration-system
375 (machine-configuration machine)))
376 (%current-target-system #f))
377 (append (machine-check-file-system-availability machine)
378 (machine-check-initrd-modules machine)
379 (list (machine-check-forward-update machine)))))
380
381 (define aggregate-exp
382 ;; Gather all the expressions so that a single round-trip is enough to
383 ;; evaluate all the ASSERTIONS remotely.
384 #~(map (lambda (file)
385 (false-if-exception (primitive-load file)))
386 '#$(map (lambda (assertion)
387 (scheme-file "remote-assertion.scm"
388 (remote-assertion-expression assertion)))
389 assertions)))
390
391 ;; First check MACHINE's system type--an incorrect value for 'system' would
392 ;; cause subsequent invocations of 'remote-eval' to fail.
393 (machine-check-building-for-appropriate-system machine)
394
395 (mlet %store-monad ((values (machine-remote-eval machine aggregate-exp)))
396 (for-each (lambda (proc value)
397 (proc value))
398 (map remote-assertion-procedure assertions)
399 values)
400 (return #t)))
401
402
403;;;
404;;; System deployment.
405;;;
406
407(define not-config?
408 ;; Select (guix …) and (gnu …) modules, except (guix config).
409 (match-lambda
410 (('guix 'config) #f)
411 (('guix _ ...) #t)
412 (('gnu _ ...) #t)
413 (_ #f)))
414
415(define (machine-boot-parameters machine)
416 "Monadic procedure returning a list of 'boot-parameters' for the generations
417of MACHINE's system profile, ordered from most recent to oldest."
418 (define bootable-kernel-arguments
419 (@@ (gnu system) bootable-kernel-arguments))
420
421 (define remote-exp
422 (with-extensions (list guile-gcrypt)
423 (with-imported-modules `(((guix config) => ,(make-config.scm))
424 ,@(source-module-closure
425 '((guix profiles))
426 #:select? not-config?))
427 #~(begin
428 (use-modules (guix config)
429 (guix profiles)
430 (ice-9 textual-ports))
431
432 (define %system-profile
433 (string-append %state-directory "/profiles/system"))
434
435 (define (read-file path)
436 (call-with-input-file path
437 (lambda (port)
438 (get-string-all port))))
439
440 (map (lambda (generation)
441 (let* ((system-path (generation-file-name %system-profile
442 generation))
443 (boot-parameters-path (string-append system-path
444 "/parameters"))
445 (time (stat:mtime (lstat system-path))))
446 (list generation
447 system-path
448 time
449 (read-file boot-parameters-path))))
450 (reverse (generation-numbers %system-profile)))))))
451
452 (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
453 (return
454 (map (lambda (generation)
455 (match generation
456 ((generation system-path time serialized-params)
457 (let* ((params (call-with-input-string serialized-params
458 read-boot-parameters))
459 (root (boot-parameters-root-device params))
460 (label (boot-parameters-label params))
461 (version (boot-parameters-version params)))
462 (boot-parameters
463 (inherit params)
464 (label
465 (string-append label " (#"
466 (number->string generation) ", "
467 (let ((time (make-time time-utc 0 time)))
468 (date->string (time-utc->date time)
469 "~Y-~m-~d ~H:~M"))
470 ")"))
471 (kernel-arguments
472 (append (bootable-kernel-arguments system-path root version)
473 (boot-parameters-kernel-arguments params))))))))
474 generations))))
475
476(define-syntax-rule (with-roll-back should-roll-back? mbody ...)
477 "Catch exceptions that arise when binding MBODY, a monadic expression in
478%STORE-MONAD, and collect their arguments in a &deploy-error condition, with
479the 'should-roll-back' field set to SHOULD-ROLL-BACK?"
480 (catch #t
481 (lambda ()
482 mbody ...)
483 (lambda args
484 (raise (condition (&deploy-error
485 (should-roll-back should-roll-back?)
486 (captured-args args)))))))
487
488(define (deploy-managed-host machine)
489 "Internal implementation of 'deploy-machine' for MACHINE instances with an
490environment type of 'managed-host."
491 (define config (machine-configuration machine))
492 (define host (machine-ssh-configuration-host-name config))
493 (define system (machine-ssh-configuration-system config))
494
495 (maybe-raise-unsupported-configuration-error machine)
496 (when (machine-ssh-configuration-authorize?
497 (machine-configuration machine))
498 (unless (file-exists? %public-key-file)
499 (raise (formatted-message (G_ "no signing key '~a'. \
500Have you run 'guix archive --generate-key'?")
501 %public-key-file)))
502 (remote-authorize-signing-key (call-with-input-file %public-key-file
503 (lambda (port)
504 (string->canonical-sexp
505 (get-string-all port))))
506 (machine-ssh-session machine)
507 (machine-become-command machine)))
508
509 (mlet %store-monad ((_ (check-deployment-sanity machine))
510 (boot-parameters (machine-boot-parameters machine)))
511 ;; Make sure code that check %CURRENT-SYSTEM, such as
512 ;; %BASE-INITRD-MODULES, gets to see the right value.
513 (parameterize ((%current-system system)
514 (%current-target-system #f))
515 (let* ((os (machine-operating-system machine))
516 (eval (cut machine-remote-eval machine <>))
517 (menu-entries (map boot-parameters->menu-entry boot-parameters))
518 (bootloader-configuration (operating-system-bootloader os))
519 (bootcfg (operating-system-bootcfg os menu-entries)))
520 (define-syntax-rule (eval/error-handling condition handler ...)
521 ;; Return a wrapper around EVAL such that HANDLER is evaluated if an
522 ;; exception is raised.
523 (lambda (exp)
524 (lambda (store)
525 (guard (condition ((inferior-exception? condition)
526 (values (begin handler ...) store)))
527 (values (run-with-store store (eval exp)
528 #:system system)
529 store)))))
530
531 (mbegin %store-monad
532 (with-roll-back #f
533 (switch-to-system (eval/error-handling c
534 (raise (formatted-message
535 (G_ "\
536failed to switch systems while deploying '~a':~%~{~s ~}")
537 host
538 (inferior-exception-arguments c))))
539 os))
540 (with-roll-back #t
541 (mbegin %store-monad
542 (upgrade-shepherd-services (eval/error-handling c
543 (warning (G_ "\
544an error occurred while upgrading services on '~a':~%~{~s ~}~%")
545 host
546 (inferior-exception-arguments
547 c)))
548 os)
549 (install-bootloader (eval/error-handling c
550 (raise (formatted-message
551 (G_ "\
552failed to install bootloader on '~a':~%~{~s ~}~%")
553 host
554 (inferior-exception-arguments c))))
555 bootloader-configuration bootcfg))))))))
556
557
558;;;
559;;; Roll-back.
560;;;
561
562(define (roll-back-managed-host machine)
563 "Internal implementation of 'roll-back-machine' for MACHINE instances with
564an environment type of 'managed-host."
565 (define remote-exp
566 (with-extensions (list guile-gcrypt)
567 (with-imported-modules (source-module-closure '((guix config)
568 (guix profiles)))
569 #~(begin
570 (use-modules (guix config)
571 (guix profiles))
572
573 (define %system-profile
574 (string-append %state-directory "/profiles/system"))
575
576 (define target-generation
577 (relative-generation %system-profile -1))
578
579 (if target-generation
580 (switch-to-generation %system-profile target-generation)
581 'error)))))
582
583 (define roll-back-failure
584 (condition (&message (message (G_ "could not roll-back machine")))))
585
586 (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine))
587 (_ -> (if (< (length boot-parameters) 2)
588 (raise roll-back-failure)))
589 (entries -> (map boot-parameters->menu-entry
590 (list (second boot-parameters))))
591 (locale -> (boot-parameters-locale
592 (second boot-parameters)))
593 (crypto-dev -> (boot-parameters-store-crypto-devices
594 (second boot-parameters)))
595 (store-dir -> (boot-parameters-store-directory-prefix
596 (second boot-parameters)))
597 (old-entries -> (map boot-parameters->menu-entry
598 (drop boot-parameters 2)))
599 (bootloader -> (operating-system-bootloader
600 (machine-operating-system machine)))
601 (bootcfg (lower-object
602 ((bootloader-configuration-file-generator
603 (bootloader-configuration-bootloader
604 bootloader))
605 bootloader entries
606 #:locale locale
607 #:store-crypto-devices crypto-dev
608 #:store-directory-prefix store-dir
609 #:old-entries old-entries)))
610 (remote-result (machine-remote-eval machine remote-exp)))
611 (when (eqv? 'error remote-result)
612 (raise roll-back-failure))))
613
614
615;;;
616;;; Environment type.
617;;;
618
619(define managed-host-environment-type
620 (environment-type
621 (machine-remote-eval managed-host-remote-eval)
622 (deploy-machine deploy-managed-host)
623 (roll-back-machine roll-back-managed-host)
624 (name 'managed-host-environment-type)
625 (description "Provisioning for machines that are accessible over SSH
626and have a known host-name. This entails little more than maintaining an SSH
627connection to the host.")))
628
629(define (maybe-raise-unsupported-configuration-error machine)
630 "Raise an error if MACHINE's configuration is not an instance of
631<machine-ssh-configuration>."
632 (let ((config (machine-configuration machine))
633 (environment (environment-type-name (machine-environment machine))))
634 (unless (and config (machine-ssh-configuration? config))
635 (raise (formatted-message (G_ "unsupported machine configuration '~a'
636for environment of type '~a'")
637 config
638 environment)))))
639
640;; Local Variables:
641;; eval: (put 'remote-let 'scheme-indent-function 1)
642;; eval: (put 'with-roll-back 'scheme-indent-function 1)
643;; eval: (put 'eval/error-handling 'scheme-indent-function 1)
644;; End: