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