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