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