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 machine)
21 #:autoload (gnu packages gnupg) (guile-gcrypt)
22 #:use-module (gnu system)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system uuid)
25 #:use-module (guix diagnostics)
26 #:use-module (guix gexp)
27 #:use-module (guix i18n)
28 #:use-module (guix modules)
29 #:use-module (guix monads)
30 #:use-module (guix records)
31 #:use-module (guix remote)
32 #:use-module (guix scripts system reconfigure)
33 #:use-module (guix ssh)
34 #:use-module (guix store)
35 #:use-module (guix utils)
36 #:use-module (ice-9 match)
37 #:use-module (srfi srfi-19)
38 #:use-module (srfi srfi-26)
39 #:use-module (srfi srfi-35)
40 #:export (managed-host-environment-type
42 machine-ssh-configuration
43 machine-ssh-configuration?
44 machine-ssh-configuration
46 machine-ssh-configuration-host-name
47 machine-ssh-configuration-port
48 machine-ssh-configuration-user
49 machine-ssh-configuration-session))
53 ;;; This module implements remote evaluation and system deployment for
54 ;;; machines that are accessible over SSH and have a known host-name. In the
55 ;;; sense of the broader "machine" interface, we describe the environment for
56 ;;; such machines as 'managed-host.
62 ;;; Parameters for the SSH client.
65 (define-record-type* <machine-ssh-configuration> machine-ssh-configuration
66 make-machine-ssh-configuration
67 machine-ssh-configuration?
68 this-machine-ssh-configuration
69 (host-name machine-ssh-configuration-host-name) ; string
70 (port machine-ssh-configuration-port ; integer
72 (user machine-ssh-configuration-user ; string
74 (identity machine-ssh-configuration-identity ; path to a private key
76 (session machine-ssh-configuration-session ; session
79 (define (machine-ssh-session machine)
80 "Return the SSH session that was given in MACHINE's configuration, or create
81 one from the configuration's parameters if one was not provided."
82 (maybe-raise-unsupported-configuration-error machine)
83 (let ((config (machine-configuration machine)))
84 (or (machine-ssh-configuration-session config)
85 (let ((host-name (machine-ssh-configuration-host-name config))
86 (user (machine-ssh-configuration-user config))
87 (port (machine-ssh-configuration-port config))
88 (identity (machine-ssh-configuration-identity config)))
89 (open-ssh-session host-name
92 #:identity identity)))))
96 ;;; Remote evaluation.
99 (define (managed-host-remote-eval machine exp)
100 "Internal implementation of 'machine-remote-eval' for MACHINE instances with
101 an environment type of 'managed-host."
102 (maybe-raise-unsupported-configuration-error machine)
103 (remote-eval exp (machine-ssh-session machine)))
110 (define (machine-check-file-system-availability machine)
111 "Raise a '&message' error condition if any of the file-systems specified in
112 MACHINE's 'system' declaration do not exist on the machine."
115 (and (file-system-mount? fs)
116 (not (member (file-system-type fs)
117 %pseudo-file-system-types))
118 (not (memq 'bind-mount (file-system-flags fs)))))
119 (operating-system-file-systems (machine-operating-system machine))))
121 (define (check-literal-file-system fs)
123 #~(catch 'system-error
125 (stat #$(file-system-device fs))
128 (system-error-errno args))))
130 (mlet %store-monad ((errno (machine-remote-eval machine remote-exp)))
131 (when (number? errno)
134 (message (format #f (G_ "device '~a' not found: ~a")
135 (file-system-device fs)
136 (strerror errno)))))))
139 (define (check-labeled-file-system fs)
141 (with-imported-modules '((gnu build file-systems))
143 (use-modules (gnu build file-systems))
144 (find-partition-by-label #$(file-system-label->string
145 (file-system-device fs))))))
147 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
151 (message (format #f (G_ "no file system with label '~a'")
152 (file-system-label->string
153 (file-system-device fs))))))))
156 (define (check-uuid-file-system fs)
158 (with-imported-modules (source-module-closure
159 '((gnu build file-systems)
162 (use-modules (gnu build file-systems)
166 (string->uuid #$(uuid->string (file-system-device fs))))
168 (find-partition-by-uuid uuid))))
170 (mlet %store-monad ((result (machine-remote-eval machine remote-exp)))
174 (message (format #f (G_ "no file system with UUID '~a'")
175 (uuid->string (file-system-device fs))))))))
179 (mapm %store-monad check-literal-file-system
181 (string? (file-system-device fs)))
183 (mapm %store-monad check-labeled-file-system
185 (file-system-label? (file-system-device fs)))
187 (mapm %store-monad check-uuid-file-system
189 (uuid? (file-system-device fs)))
192 (define (machine-check-initrd-modules machine)
193 "Raise a '&message' error condition if any of the modules needed by
194 'needed-for-boot' file systems in MACHINE are not available in the initrd."
196 (filter file-system-needed-for-boot?
197 (operating-system-file-systems (machine-operating-system machine))))
199 (define (missing-modules fs)
201 (let ((device (file-system-device fs)))
202 (with-imported-modules (source-module-closure
203 '((gnu build file-systems)
204 (gnu build linux-modules)
207 (use-modules (gnu build file-systems)
208 (gnu build linux-modules)
212 #$(cond ((string? device) device)
213 ((uuid? device) #~(find-partition-by-uuid
215 #$(uuid->string device))))
216 ((file-system-label? device)
217 #~(find-partition-by-label
218 (file-system-label->string #$device)))))
220 (missing-modules dev '#$(operating-system-initrd-modules
221 (machine-operating-system machine)))))))
222 (mlet %store-monad ((missing (machine-remote-eval machine remote-exp)))
223 (return (list fs missing))))
225 (mlet %store-monad ((device (mapm %store-monad missing-modules file-systems)))
226 (for-each (match-lambda
228 (unless (null? missing)
231 (message (format #f (G_ "~a missing modules ~{ ~a~}~%")
232 (file-system-device fs)
237 (define (check-deployment-sanity machine)
238 "Raise a '&message' error condition if it is clear that deploying MACHINE's
239 'system' declaration would fail."
241 (machine-check-file-system-availability machine)
242 (machine-check-initrd-modules machine)))
246 ;;; System deployment.
249 (define (machine-boot-parameters machine)
250 "Monadic procedure returning a list of 'boot-parameters' for the generations
251 of MACHINE's system profile, ordered from most recent to oldest."
252 (define bootable-kernel-arguments
253 (@@ (gnu system) bootable-kernel-arguments))
256 (with-extensions (list guile-gcrypt)
257 (with-imported-modules (source-module-closure '((guix config)
260 (use-modules (guix config)
262 (ice-9 textual-ports))
264 (define %system-profile
265 (string-append %state-directory "/profiles/system"))
267 (define (read-file path)
268 (call-with-input-file path
270 (get-string-all port))))
272 (map (lambda (generation)
273 (let* ((system-path (generation-file-name %system-profile
275 (boot-parameters-path (string-append system-path
277 (time (stat:mtime (lstat system-path))))
281 (read-file boot-parameters-path))))
282 (reverse (generation-numbers %system-profile)))))))
284 (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
286 (map (lambda (generation)
288 ((generation system-path time serialized-params)
289 (let* ((params (call-with-input-string serialized-params
290 read-boot-parameters))
291 (root (boot-parameters-root-device params))
292 (label (boot-parameters-label params)))
296 (string-append label " (#"
297 (number->string generation) ", "
298 (let ((time (make-time time-utc 0 time)))
299 (date->string (time-utc->date time)
303 (append (bootable-kernel-arguments system-path root)
304 (boot-parameters-kernel-arguments params))))))))
307 (define (deploy-managed-host machine)
308 "Internal implementation of 'deploy-machine' for MACHINE instances with an
309 environment type of 'managed-host."
310 (maybe-raise-unsupported-configuration-error machine)
311 (mlet %store-monad ((_ (check-deployment-sanity machine))
312 (boot-parameters (machine-boot-parameters machine)))
313 (let* ((os (machine-operating-system machine))
314 (eval (cut machine-remote-eval machine <>))
315 (menu-entries (map boot-parameters->menu-entry boot-parameters))
316 (bootloader-configuration (operating-system-bootloader os))
317 (bootcfg (operating-system-bootcfg os menu-entries)))
319 (switch-to-system eval os)
320 (upgrade-shepherd-services eval os)
321 (install-bootloader eval bootloader-configuration bootcfg)))))
325 ;;; Environment type.
328 (define managed-host-environment-type
330 (machine-remote-eval managed-host-remote-eval)
331 (deploy-machine deploy-managed-host)
332 (name 'managed-host-environment-type)
333 (description "Provisioning for machines that are accessible over SSH
334 and have a known host-name. This entails little more than maintaining an SSH
335 connection to the host.")))
337 (define (maybe-raise-unsupported-configuration-error machine)
338 "Raise an error if MACHINE's configuration is not an instance of
339 <machine-ssh-configuration>."
340 (let ((config (machine-configuration machine))
341 (environment (environment-type-name (machine-environment machine))))
342 (unless (and config (machine-ssh-configuration? config))
345 (message (format #f (G_ "unsupported machine configuration '~a'
346 for environment of type '~a'")