Commit | Line | Data |
---|---|---|
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 | |
94 | one 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 | |
114 | shell 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 | |
122 | an 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 | |
129 | (machine-ssh-configuration-system config)))) | |
fa9edf09 JK |
130 | |
131 | \f | |
fd3119db JK |
132 | ;;; |
133 | ;;; Safety checks. | |
134 | ;;; | |
135 | ||
136 | (define (machine-check-file-system-availability machine) | |
137 | "Raise a '&message' error condition if any of the file-systems specified in | |
138 | MACHINE's 'system' declaration do not exist on the machine." | |
139 | (define file-systems | |
140 | (filter (lambda (fs) | |
141 | (and (file-system-mount? fs) | |
142 | (not (member (file-system-type fs) | |
143 | %pseudo-file-system-types)) | |
144 | (not (memq 'bind-mount (file-system-flags fs))))) | |
145 | (operating-system-file-systems (machine-operating-system machine)))) | |
146 | ||
147 | (define (check-literal-file-system fs) | |
148 | (define remote-exp | |
149 | #~(catch 'system-error | |
150 | (lambda () | |
151 | (stat #$(file-system-device fs)) | |
152 | #t) | |
153 | (lambda args | |
154 | (system-error-errno args)))) | |
155 | ||
156 | (mlet %store-monad ((errno (machine-remote-eval machine remote-exp))) | |
157 | (when (number? errno) | |
158 | (raise (condition | |
159 | (&message | |
160 | (message (format #f (G_ "device '~a' not found: ~a") | |
161 | (file-system-device fs) | |
162 | (strerror errno))))))) | |
163 | (return #t))) | |
164 | ||
165 | (define (check-labeled-file-system fs) | |
166 | (define remote-exp | |
167 | (with-imported-modules '((gnu build file-systems)) | |
168 | #~(begin | |
169 | (use-modules (gnu build file-systems)) | |
170 | (find-partition-by-label #$(file-system-label->string | |
171 | (file-system-device fs)))))) | |
172 | ||
173 | (mlet %store-monad ((result (machine-remote-eval machine remote-exp))) | |
174 | (unless result | |
175 | (raise (condition | |
176 | (&message | |
177 | (message (format #f (G_ "no file system with label '~a'") | |
178 | (file-system-label->string | |
179 | (file-system-device fs)))))))) | |
180 | (return #t))) | |
181 | ||
182 | (define (check-uuid-file-system fs) | |
183 | (define remote-exp | |
184 | (with-imported-modules (source-module-closure | |
185 | '((gnu build file-systems) | |
186 | (gnu system uuid))) | |
187 | #~(begin | |
188 | (use-modules (gnu build file-systems) | |
189 | (gnu system uuid)) | |
190 | ||
191 | (define uuid | |
192 | (string->uuid #$(uuid->string (file-system-device fs)))) | |
193 | ||
194 | (find-partition-by-uuid uuid)))) | |
195 | ||
196 | (mlet %store-monad ((result (machine-remote-eval machine remote-exp))) | |
197 | (unless result | |
198 | (raise (condition | |
199 | (&message | |
200 | (message (format #f (G_ "no file system with UUID '~a'") | |
201 | (uuid->string (file-system-device fs)))))))) | |
202 | (return #t))) | |
203 | ||
204 | (mbegin %store-monad | |
205 | (mapm %store-monad check-literal-file-system | |
206 | (filter (lambda (fs) | |
207 | (string? (file-system-device fs))) | |
208 | file-systems)) | |
209 | (mapm %store-monad check-labeled-file-system | |
210 | (filter (lambda (fs) | |
211 | (file-system-label? (file-system-device fs))) | |
212 | file-systems)) | |
213 | (mapm %store-monad check-uuid-file-system | |
214 | (filter (lambda (fs) | |
215 | (uuid? (file-system-device fs))) | |
216 | file-systems)))) | |
217 | ||
218 | (define (machine-check-initrd-modules machine) | |
219 | "Raise a '&message' error condition if any of the modules needed by | |
220 | 'needed-for-boot' file systems in MACHINE are not available in the initrd." | |
221 | (define file-systems | |
222 | (filter file-system-needed-for-boot? | |
223 | (operating-system-file-systems (machine-operating-system machine)))) | |
224 | ||
225 | (define (missing-modules fs) | |
226 | (define remote-exp | |
227 | (let ((device (file-system-device fs))) | |
228 | (with-imported-modules (source-module-closure | |
229 | '((gnu build file-systems) | |
230 | (gnu build linux-modules) | |
231 | (gnu system uuid))) | |
232 | #~(begin | |
233 | (use-modules (gnu build file-systems) | |
234 | (gnu build linux-modules) | |
235 | (gnu system uuid)) | |
236 | ||
237 | (define dev | |
238 | #$(cond ((string? device) device) | |
239 | ((uuid? device) #~(find-partition-by-uuid | |
240 | (string->uuid | |
241 | #$(uuid->string device)))) | |
242 | ((file-system-label? device) | |
243 | #~(find-partition-by-label | |
244 | (file-system-label->string #$device))))) | |
245 | ||
246 | (missing-modules dev '#$(operating-system-initrd-modules | |
247 | (machine-operating-system machine))))))) | |
248 | (mlet %store-monad ((missing (machine-remote-eval machine remote-exp))) | |
249 | (return (list fs missing)))) | |
250 | ||
251 | (mlet %store-monad ((device (mapm %store-monad missing-modules file-systems))) | |
252 | (for-each (match-lambda | |
253 | ((fs missing) | |
254 | (unless (null? missing) | |
255 | (raise (condition | |
256 | (&message | |
257 | (message (format #f (G_ "~a missing modules ~{ ~a~}~%") | |
258 | (file-system-device fs) | |
259 | missing)))))))) | |
260 | device) | |
261 | (return #t))) | |
262 | ||
2c8e04f1 JK |
263 | (define (machine-check-building-for-appropriate-system machine) |
264 | "Raise a '&message' error condition if MACHINE is configured to be built | |
265 | locally and the 'system' field does not match the '%current-system' reported | |
266 | by MACHINE." | |
267 | (let ((config (machine-configuration machine)) | |
268 | (system (remote-system (machine-ssh-session machine)))) | |
269 | (when (and (machine-ssh-configuration-build-locally? config) | |
270 | (not (string= system (machine-ssh-configuration-system config)))) | |
271 | (raise (condition | |
272 | (&message | |
273 | (message (format #f (G_ "incorrect target system \ | |
274 | ('~a' was given, while the system reports that it is '~a')~%") | |
275 | (machine-ssh-configuration-system config) | |
276 | system))))))) | |
277 | (with-monad %store-monad (return #t))) | |
278 | ||
fd3119db JK |
279 | (define (check-deployment-sanity machine) |
280 | "Raise a '&message' error condition if it is clear that deploying MACHINE's | |
281 | 'system' declaration would fail." | |
2c8e04f1 JK |
282 | ;; Order is important here -- an incorrect value for 'system' will cause |
283 | ;; invocations of 'remote-eval' to fail. | |
fd3119db | 284 | (mbegin %store-monad |
2c8e04f1 | 285 | (machine-check-building-for-appropriate-system machine) |
fd3119db JK |
286 | (machine-check-file-system-availability machine) |
287 | (machine-check-initrd-modules machine))) | |
288 | ||
289 | \f | |
fa9edf09 JK |
290 | ;;; |
291 | ;;; System deployment. | |
292 | ;;; | |
293 | ||
fa9edf09 JK |
294 | (define (machine-boot-parameters machine) |
295 | "Monadic procedure returning a list of 'boot-parameters' for the generations | |
296 | of MACHINE's system profile, ordered from most recent to oldest." | |
297 | (define bootable-kernel-arguments | |
298 | (@@ (gnu system) bootable-kernel-arguments)) | |
299 | ||
300 | (define remote-exp | |
301 | (with-extensions (list guile-gcrypt) | |
302 | (with-imported-modules (source-module-closure '((guix config) | |
303 | (guix profiles))) | |
304 | #~(begin | |
305 | (use-modules (guix config) | |
306 | (guix profiles) | |
307 | (ice-9 textual-ports)) | |
308 | ||
309 | (define %system-profile | |
310 | (string-append %state-directory "/profiles/system")) | |
311 | ||
312 | (define (read-file path) | |
313 | (call-with-input-file path | |
314 | (lambda (port) | |
315 | (get-string-all port)))) | |
316 | ||
317 | (map (lambda (generation) | |
318 | (let* ((system-path (generation-file-name %system-profile | |
319 | generation)) | |
320 | (boot-parameters-path (string-append system-path | |
321 | "/parameters")) | |
322 | (time (stat:mtime (lstat system-path)))) | |
323 | (list generation | |
324 | system-path | |
325 | time | |
326 | (read-file boot-parameters-path)))) | |
327 | (reverse (generation-numbers %system-profile))))))) | |
328 | ||
329 | (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp))) | |
330 | (return | |
331 | (map (lambda (generation) | |
332 | (match generation | |
333 | ((generation system-path time serialized-params) | |
334 | (let* ((params (call-with-input-string serialized-params | |
335 | read-boot-parameters)) | |
336 | (root (boot-parameters-root-device params)) | |
337 | (label (boot-parameters-label params))) | |
338 | (boot-parameters | |
339 | (inherit params) | |
340 | (label | |
341 | (string-append label " (#" | |
342 | (number->string generation) ", " | |
343 | (let ((time (make-time time-utc 0 time))) | |
344 | (date->string (time-utc->date time) | |
345 | "~Y-~m-~d ~H:~M")) | |
346 | ")")) | |
347 | (kernel-arguments | |
348 | (append (bootable-kernel-arguments system-path root) | |
349 | (boot-parameters-kernel-arguments params)))))))) | |
350 | generations)))) | |
351 | ||
9c70c460 JK |
352 | (define-syntax-rule (with-roll-back should-roll-back? mbody ...) |
353 | "Catch exceptions that arise when binding MBODY, a monadic expression in | |
354 | %STORE-MONAD, and collect their arguments in a &deploy-error condition, with | |
355 | the 'should-roll-back' field set to SHOULD-ROLL-BACK?" | |
356 | (catch #t | |
357 | (lambda () | |
358 | mbody ...) | |
359 | (lambda args | |
360 | (raise (condition (&deploy-error | |
361 | (should-roll-back should-roll-back?) | |
362 | (captured-args args))))))) | |
363 | ||
fa9edf09 JK |
364 | (define (deploy-managed-host machine) |
365 | "Internal implementation of 'deploy-machine' for MACHINE instances with an | |
366 | environment type of 'managed-host." | |
367 | (maybe-raise-unsupported-configuration-error machine) | |
3033d59a JK |
368 | (when (machine-ssh-configuration-authorize? |
369 | (machine-configuration machine)) | |
370 | (unless (file-exists? %public-key-file) | |
371 | (raise (condition | |
372 | (&message | |
373 | (message (format #f (G_ "no signing key '~a'. \ | |
374 | have you run 'guix archive --generate-key?'") | |
375 | %public-key-file)))))) | |
376 | (remote-authorize-signing-key (call-with-input-file %public-key-file | |
377 | (lambda (port) | |
378 | (string->canonical-sexp | |
379 | (get-string-all port)))) | |
380 | (machine-ssh-session machine))) | |
fd3119db JK |
381 | (mlet %store-monad ((_ (check-deployment-sanity machine)) |
382 | (boot-parameters (machine-boot-parameters machine))) | |
d97ce204 | 383 | (let* ((os (machine-operating-system machine)) |
5c793753 JK |
384 | (eval (cut machine-remote-eval machine <>)) |
385 | (menu-entries (map boot-parameters->menu-entry boot-parameters)) | |
386 | (bootloader-configuration (operating-system-bootloader os)) | |
387 | (bootcfg (operating-system-bootcfg os menu-entries))) | |
388 | (mbegin %store-monad | |
9c70c460 JK |
389 | (with-roll-back #f |
390 | (switch-to-system eval os)) | |
391 | (with-roll-back #t | |
392 | (mbegin %store-monad | |
393 | (upgrade-shepherd-services eval os) | |
394 | (install-bootloader eval bootloader-configuration bootcfg))))))) | |
395 | ||
396 | \f | |
397 | ;;; | |
398 | ;;; Roll-back. | |
399 | ;;; | |
400 | ||
401 | (define (roll-back-managed-host machine) | |
402 | "Internal implementation of 'roll-back-machine' for MACHINE instances with | |
403 | an environment type of 'managed-host." | |
404 | (define remote-exp | |
405 | (with-extensions (list guile-gcrypt) | |
406 | (with-imported-modules (source-module-closure '((guix config) | |
407 | (guix profiles))) | |
408 | #~(begin | |
409 | (use-modules (guix config) | |
410 | (guix profiles)) | |
411 | ||
412 | (define %system-profile | |
413 | (string-append %state-directory "/profiles/system")) | |
414 | ||
415 | (define target-generation | |
416 | (relative-generation %system-profile -1)) | |
417 | ||
418 | (if target-generation | |
419 | (switch-to-generation %system-profile target-generation) | |
420 | 'error))))) | |
421 | ||
422 | (define roll-back-failure | |
423 | (condition (&message (message (G_ "could not roll-back machine"))))) | |
424 | ||
425 | (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine)) | |
426 | (_ -> (if (< (length boot-parameters) 2) | |
427 | (raise roll-back-failure))) | |
428 | (entries -> (map boot-parameters->menu-entry | |
429 | (list (second boot-parameters)))) | |
430 | (old-entries -> (map boot-parameters->menu-entry | |
431 | (drop boot-parameters 2))) | |
432 | (bootloader -> (operating-system-bootloader | |
433 | (machine-operating-system machine))) | |
434 | (bootcfg (lower-object | |
435 | ((bootloader-configuration-file-generator | |
436 | (bootloader-configuration-bootloader | |
437 | bootloader)) | |
438 | bootloader entries | |
439 | #:old-entries old-entries))) | |
440 | (remote-result (machine-remote-eval machine remote-exp))) | |
441 | (when (eqv? 'error remote-result) | |
442 | (raise roll-back-failure)))) | |
fa9edf09 JK |
443 | |
444 | \f | |
445 | ;;; | |
446 | ;;; Environment type. | |
447 | ;;; | |
448 | ||
449 | (define managed-host-environment-type | |
450 | (environment-type | |
451 | (machine-remote-eval managed-host-remote-eval) | |
452 | (deploy-machine deploy-managed-host) | |
9c70c460 | 453 | (roll-back-machine roll-back-managed-host) |
fa9edf09 | 454 | (name 'managed-host-environment-type) |
53f21b3f | 455 | (description "Provisioning for machines that are accessible over SSH |
fa9edf09 JK |
456 | and have a known host-name. This entails little more than maintaining an SSH |
457 | connection to the host."))) | |
458 | ||
459 | (define (maybe-raise-unsupported-configuration-error machine) | |
460 | "Raise an error if MACHINE's configuration is not an instance of | |
461 | <machine-ssh-configuration>." | |
462 | (let ((config (machine-configuration machine)) | |
463 | (environment (environment-type-name (machine-environment machine)))) | |
464 | (unless (and config (machine-ssh-configuration? config)) | |
465 | (raise (condition | |
466 | (&message | |
467 | (message (format #f (G_ "unsupported machine configuration '~a' | |
468 | for environment of type '~a'") | |
469 | config | |
470 | environment)))))))) |