1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu machine ssh)
20 #:use-module (gnu bootloader)
21 #:use-module (gnu machine)
22 #:autoload (gnu packages gnupg) (guile-gcrypt)
23 #:use-module (gnu system)
24 #:use-module (gnu system file-systems)
25 #:use-module (gnu system uuid)
26 #:use-module (guix diagnostics)
27 #:use-module (guix gexp)
28 #:use-module (guix i18n)
29 #:use-module (guix modules)
30 #:use-module (guix monads)
31 #:use-module (guix pki)
32 #:use-module (guix records)
33 #:use-module (guix remote)
34 #:use-module (guix scripts system reconfigure)
35 #:use-module (guix ssh)
36 #:use-module (guix store)
37 #:use-module (guix utils)
38 #:use-module (gcrypt pk-crypto)
39 #:use-module (ice-9 match)
40 #:use-module (ice-9 textual-ports)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-19)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-35)
46 #:export (managed-host-environment-type
48 machine-ssh-configuration
49 machine-ssh-configuration?
50 machine-ssh-configuration
52 machine-ssh-configuration-host-name
53 machine-ssh-configuration-build-locally?
54 machine-ssh-configuration-authorize?
55 machine-ssh-configuration-port
56 machine-ssh-configuration-user
57 machine-ssh-configuration-session))
61 ;;; This module implements remote evaluation and system deployment for
62 ;;; machines that are accessible over SSH and have a known host-name. In the
63 ;;; sense of the broader "machine" interface, we describe the environment for
64 ;;; such machines as 'managed-host.
70 ;;; Parameters for the SSH client.
73 (define-record-type* <machine-ssh-configuration> machine-ssh-configuration
74 make-machine-ssh-configuration
75 machine-ssh-configuration?
76 this-machine-ssh-configuration
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
81 (authorize? machine-ssh-configuration-authorize? ; boolean
83 (port machine-ssh-configuration-port ; integer
85 (user machine-ssh-configuration-user ; string
87 (identity machine-ssh-configuration-identity ; path to a private key
89 (session machine-ssh-configuration-session ; session
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
105 #:identity identity)))))
109 ;;; Remote evaluation.
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)))
118 '("/run/setuid-programs/sudo" "-n" "--")))
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)
124 (let ((config (machine-configuration machine)))
125 (remote-eval exp (machine-ssh-session machine)
127 (machine-ssh-configuration-build-locally? config)
129 (machine-ssh-configuration-system config)
131 (machine-become-command machine))))
138 (define (machine-check-file-system-availability machine)
139 "Raise a '&message' error condition if any of the file-systems specified in
140 MACHINE's 'system' declaration do not exist on the machine."
143 (and (file-system-mount? fs)
144 (not (member (file-system-type fs)
145 %pseudo-file-system-types))
146 (not (memq 'bind-mount (file-system-flags fs)))))
147 (operating-system-file-systems (machine-operating-system machine))))
149 (define (check-literal-file-system fs)
151 #~(catch 'system-error
153 (stat #$(file-system-device fs))
156 (system-error-errno args))))
158 (mlet %store-monad ((errno (machine-remote-eval machine remote-exp)))
159 (when (number? errno)
162 (message (format #f (G_ "device '~a' not found: ~a")
163 (file-system-device fs)
164 (strerror errno)))))))
167 (define (check-labeled-file-system fs)
169 (with-imported-modules '((gnu build file-systems))
171 (use-modules (gnu build file-systems))
172 (find-partition-by-label #$(file-system-label->string
173 (file-system-device fs))))))
175 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
179 (message (format #f (G_ "no file system with label '~a'")
180 (file-system-label->string
181 (file-system-device fs))))))))
184 (define (check-uuid-file-system fs)
186 (with-imported-modules (source-module-closure
187 '((gnu build file-systems)
190 (use-modules (gnu build file-systems)
194 (string->uuid #$(uuid->string (file-system-device fs))))
196 (find-partition-by-uuid uuid))))
198 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
202 (message (format #f (G_ "no file system with UUID '~a'")
203 (uuid->string (file-system-device fs))))))))
207 (mapm %store-monad check-literal-file-system
209 (string? (file-system-device fs)))
211 (mapm %store-monad check-labeled-file-system
213 (file-system-label? (file-system-device fs)))
215 (mapm %store-monad check-uuid-file-system
217 (uuid? (file-system-device fs)))
220 (define (machine-check-initrd-modules machine)
221 "Raise a '&message' error condition if any of the modules needed by
222 'needed-for-boot' file systems in MACHINE are not available in the initrd."
224 (filter file-system-needed-for-boot?
225 (operating-system-file-systems (machine-operating-system machine))))
227 (define (missing-modules fs)
229 (let ((device (file-system-device fs)))
230 (with-imported-modules (source-module-closure
231 '((gnu build file-systems)
232 (gnu build linux-modules)
235 (use-modules (gnu build file-systems)
236 (gnu build linux-modules)
240 #$(cond ((string? device) device)
241 ((uuid? device) #~(find-partition-by-uuid
243 #$(uuid->string device))))
244 ((file-system-label? device)
245 #~(find-partition-by-label
246 (file-system-label->string #$device)))))
248 (missing-modules dev '#$(operating-system-initrd-modules
249 (machine-operating-system machine)))))))
250 (mlet %store-monad ((missing (machine-remote-eval machine remote-exp)))
251 (return (list fs missing))))
253 (mlet %store-monad ((device (mapm %store-monad missing-modules file-systems)))
254 (for-each (match-lambda
256 (unless (null? missing)
259 (message (format #f (G_ "~a missing modules ~{ ~a~}~%")
260 (file-system-device fs)
265 (define (machine-check-building-for-appropriate-system machine)
266 "Raise a '&message' error condition if MACHINE is configured to be built
267 locally and the 'system' field does not match the '%current-system' reported
269 (let ((config (machine-configuration machine))
270 (system (remote-system (machine-ssh-session machine))))
271 (when (and (machine-ssh-configuration-build-locally? config)
272 (not (string= system (machine-ssh-configuration-system config))))
275 (message (format #f (G_ "incorrect target system \
276 ('~a' was given, while the system reports that it is '~a')~%")
277 (machine-ssh-configuration-system config)
279 (with-monad %store-monad (return #t)))
281 (define (check-deployment-sanity machine)
282 "Raise a '&message' error condition if it is clear that deploying MACHINE's
283 'system' declaration would fail."
284 ;; Order is important here -- an incorrect value for 'system' will cause
285 ;; invocations of 'remote-eval' to fail.
287 (machine-check-building-for-appropriate-system machine)
288 (machine-check-file-system-availability machine)
289 (machine-check-initrd-modules machine)))
293 ;;; System deployment.
296 (define (machine-boot-parameters machine)
297 "Monadic procedure returning a list of 'boot-parameters' for the generations
298 of MACHINE's system profile, ordered from most recent to oldest."
299 (define bootable-kernel-arguments
300 (@@ (gnu system) bootable-kernel-arguments))
303 (with-extensions (list guile-gcrypt)
304 (with-imported-modules (source-module-closure '((guix config)
307 (use-modules (guix config)
309 (ice-9 textual-ports))
311 (define %system-profile
312 (string-append %state-directory "/profiles/system"))
314 (define (read-file path)
315 (call-with-input-file path
317 (get-string-all port))))
319 (map (lambda (generation)
320 (let* ((system-path (generation-file-name %system-profile
322 (boot-parameters-path (string-append system-path
324 (time (stat:mtime (lstat system-path))))
328 (read-file boot-parameters-path))))
329 (reverse (generation-numbers %system-profile)))))))
331 (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
333 (map (lambda (generation)
335 ((generation system-path time serialized-params)
336 (let* ((params (call-with-input-string serialized-params
337 read-boot-parameters))
338 (root (boot-parameters-root-device params))
339 (label (boot-parameters-label params)))
343 (string-append label " (#"
344 (number->string generation) ", "
345 (let ((time (make-time time-utc 0 time)))
346 (date->string (time-utc->date time)
350 (append (bootable-kernel-arguments system-path root)
351 (boot-parameters-kernel-arguments params))))))))
354 (define-syntax-rule (with-roll-back should-roll-back? mbody ...)
355 "Catch exceptions that arise when binding MBODY, a monadic expression in
356 %STORE-MONAD, and collect their arguments in a &deploy-error condition, with
357 the 'should-roll-back' field set to SHOULD-ROLL-BACK?"
362 (raise (condition (&deploy-error
363 (should-roll-back should-roll-back?)
364 (captured-args args)))))))
366 (define (deploy-managed-host machine)
367 "Internal implementation of 'deploy-machine' for MACHINE instances with an
368 environment type of 'managed-host."
369 (maybe-raise-unsupported-configuration-error machine)
370 (when (machine-ssh-configuration-authorize?
371 (machine-configuration machine))
372 (unless (file-exists? %public-key-file)
375 (message (format #f (G_ "no signing key '~a'. \
376 have you run 'guix archive --generate-key?'")
377 %public-key-file))))))
378 (remote-authorize-signing-key (call-with-input-file %public-key-file
380 (string->canonical-sexp
381 (get-string-all port))))
382 (machine-ssh-session machine)
383 (machine-become-command machine)))
384 (mlet %store-monad ((_ (check-deployment-sanity machine))
385 (boot-parameters (machine-boot-parameters machine)))
386 (let* ((os (machine-operating-system machine))
387 (eval (cut machine-remote-eval machine <>))
388 (menu-entries (map boot-parameters->menu-entry boot-parameters))
389 (bootloader-configuration (operating-system-bootloader os))
390 (bootcfg (operating-system-bootcfg os menu-entries)))
393 (switch-to-system eval os))
396 (upgrade-shepherd-services eval os)
397 (install-bootloader eval bootloader-configuration bootcfg)))))))
404 (define (roll-back-managed-host machine)
405 "Internal implementation of 'roll-back-machine' for MACHINE instances with
406 an environment type of 'managed-host."
408 (with-extensions (list guile-gcrypt)
409 (with-imported-modules (source-module-closure '((guix config)
412 (use-modules (guix config)
415 (define %system-profile
416 (string-append %state-directory "/profiles/system"))
418 (define target-generation
419 (relative-generation %system-profile -1))
421 (if target-generation
422 (switch-to-generation %system-profile target-generation)
425 (define roll-back-failure
426 (condition (&message (message (G_ "could not roll-back machine")))))
428 (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine))
429 (_ -> (if (< (length boot-parameters) 2)
430 (raise roll-back-failure)))
431 (entries -> (map boot-parameters->menu-entry
432 (list (second boot-parameters))))
433 (old-entries -> (map boot-parameters->menu-entry
434 (drop boot-parameters 2)))
435 (bootloader -> (operating-system-bootloader
436 (machine-operating-system machine)))
437 (bootcfg (lower-object
438 ((bootloader-configuration-file-generator
439 (bootloader-configuration-bootloader
442 #:old-entries old-entries)))
443 (remote-result (machine-remote-eval machine remote-exp)))
444 (when (eqv? 'error remote-result)
445 (raise roll-back-failure))))
449 ;;; Environment type.
452 (define managed-host-environment-type
454 (machine-remote-eval managed-host-remote-eval)
455 (deploy-machine deploy-managed-host)
456 (roll-back-machine roll-back-managed-host)
457 (name 'managed-host-environment-type)
458 (description "Provisioning for machines that are accessible over SSH
459 and have a known host-name. This entails little more than maintaining an SSH
460 connection to the host.")))
462 (define (maybe-raise-unsupported-configuration-error machine)
463 "Raise an error if MACHINE's configuration is not an instance of
464 <machine-ssh-configuration>."
465 (let ((config (machine-configuration machine))
466 (environment (environment-type-name (machine-environment machine))))
467 (unless (and config (machine-ssh-configuration? config))
470 (message (format #f (G_ "unsupported machine configuration '~a'
471 for environment of type '~a'")