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