diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.org> | 2019-08-15 04:05:57 -0400 |
|---|---|---|
| committer | Christopher Lemmer Webber <cwebber@dustycloud.org> | 2019-08-15 07:43:09 -0400 |
| commit | 9c70c460a05b2bc60f3f3602f0a2dba0f79ce86c (patch) | |
| tree | 5b55aca91aba654177e117e61020b68236b8dc58 /gnu/machine | |
| parent | 5ea7537b9a650cfa525401c19879080a9cf42e13 (diff) | |
machine: Implement 'roll-back-machine'.
* gnu/machine.scm (roll-back-machine, &deploy-error, deploy-error?)
(deploy-error-should-roll-back)
(deploy-error-captured-args): New variable.
* gnu/machine/ssh.scm (roll-back-managed-host): New variable.
* guix/scripts/deploy.scm (guix-deploy): Roll-back systems when a
deployment fails.
Diffstat (limited to 'gnu/machine')
| -rw-r--r-- | gnu/machine/ssh.scm | 72 |
1 files changed, 69 insertions, 3 deletions
diff --git a/gnu/machine/ssh.scm b/gnu/machine/ssh.scm index fb15d39e610..4b5d5fe3a2f 100644 --- a/gnu/machine/ssh.scm +++ b/gnu/machine/ssh.scm | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 18 | 18 | ||
| 19 | (define-module (gnu machine ssh) | 19 | (define-module (gnu machine ssh) |
| 20 | #:use-module (gnu bootloader) | ||
| 20 | #:use-module (gnu machine) | 21 | #:use-module (gnu machine) |
| 21 | #:autoload (gnu packages gnupg) (guile-gcrypt) | 22 | #:autoload (gnu packages gnupg) (guile-gcrypt) |
| 22 | #:use-module (gnu system) | 23 | #:use-module (gnu system) |
| @@ -34,6 +35,7 @@ | |||
| 34 | #:use-module (guix store) | 35 | #:use-module (guix store) |
| 35 | #:use-module (guix utils) | 36 | #:use-module (guix utils) |
| 36 | #:use-module (ice-9 match) | 37 | #:use-module (ice-9 match) |
| 38 | #:use-module (srfi srfi-1) | ||
| 37 | #:use-module (srfi srfi-19) | 39 | #:use-module (srfi srfi-19) |
| 38 | #:use-module (srfi srfi-26) | 40 | #:use-module (srfi srfi-26) |
| 39 | #:use-module (srfi srfi-34) | 41 | #:use-module (srfi srfi-34) |
| @@ -341,6 +343,18 @@ of MACHINE's system profile, ordered from most recent to oldest." | |||
| 341 | (boot-parameters-kernel-arguments params)))))))) | 343 | (boot-parameters-kernel-arguments params)))))))) |
| 342 | generations)))) | 344 | generations)))) |
| 343 | 345 | ||
| 346 | (define-syntax-rule (with-roll-back should-roll-back? mbody ...) | ||
| 347 | "Catch exceptions that arise when binding MBODY, a monadic expression in | ||
| 348 | %STORE-MONAD, and collect their arguments in a &deploy-error condition, with | ||
| 349 | the 'should-roll-back' field set to SHOULD-ROLL-BACK?" | ||
| 350 | (catch #t | ||
| 351 | (lambda () | ||
| 352 | mbody ...) | ||
| 353 | (lambda args | ||
| 354 | (raise (condition (&deploy-error | ||
| 355 | (should-roll-back should-roll-back?) | ||
| 356 | (captured-args args))))))) | ||
| 357 | |||
| 344 | (define (deploy-managed-host machine) | 358 | (define (deploy-managed-host machine) |
| 345 | "Internal implementation of 'deploy-machine' for MACHINE instances with an | 359 | "Internal implementation of 'deploy-machine' for MACHINE instances with an |
| 346 | environment type of 'managed-host." | 360 | environment type of 'managed-host." |
| @@ -353,9 +367,60 @@ environment type of 'managed-host." | |||
| 353 | (bootloader-configuration (operating-system-bootloader os)) | 367 | (bootloader-configuration (operating-system-bootloader os)) |
| 354 | (bootcfg (operating-system-bootcfg os menu-entries))) | 368 | (bootcfg (operating-system-bootcfg os menu-entries))) |
| 355 | (mbegin %store-monad | 369 | (mbegin %store-monad |
| 356 | (switch-to-system eval os) | 370 | (with-roll-back #f |
| 357 | (upgrade-shepherd-services eval os) | 371 | (switch-to-system eval os)) |
| 358 | (install-bootloader eval bootloader-configuration bootcfg))))) | 372 | (with-roll-back #t |
| 373 | (mbegin %store-monad | ||
| 374 | (upgrade-shepherd-services eval os) | ||
| 375 | (install-bootloader eval bootloader-configuration bootcfg))))))) | ||
| 376 | |||
| 377 | |||
| 378 | ;;; | ||
| 379 | ;;; Roll-back. | ||
| 380 | ;;; | ||
| 381 | |||
| 382 | (define (roll-back-managed-host machine) | ||
| 383 | "Internal implementation of 'roll-back-machine' for MACHINE instances with | ||
| 384 | an environment type of 'managed-host." | ||
| 385 | (define remote-exp | ||
| 386 | (with-extensions (list guile-gcrypt) | ||
| 387 | (with-imported-modules (source-module-closure '((guix config) | ||
| 388 | (guix profiles))) | ||
| 389 | #~(begin | ||
| 390 | (use-modules (guix config) | ||
| 391 | (guix profiles)) | ||
| 392 | |||
| 393 | (define %system-profile | ||
| 394 | (string-append %state-directory "/profiles/system")) | ||
| 395 | |||
| 396 | (define target-generation | ||
| 397 | (relative-generation %system-profile -1)) | ||
| 398 | |||
| 399 | (if target-generation | ||
| 400 | (switch-to-generation %system-profile target-generation) | ||
| 401 | 'error))))) | ||
| 402 | |||
| 403 | (define roll-back-failure | ||
| 404 | (condition (&message (message (G_ "could not roll-back machine"))))) | ||
| 405 | |||
| 406 | (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine)) | ||
| 407 | (_ -> (if (< (length boot-parameters) 2) | ||
| 408 | (raise roll-back-failure))) | ||
| 409 | (entries -> (map boot-parameters->menu-entry | ||
| 410 | (list (second boot-parameters)))) | ||
| 411 | (old-entries -> (map boot-parameters->menu-entry | ||
| 412 | (drop boot-parameters 2))) | ||
| 413 | (bootloader -> (operating-system-bootloader | ||
| 414 | (machine-operating-system machine))) | ||
| 415 | (bootcfg (lower-object | ||
| 416 | ((bootloader-configuration-file-generator | ||
| 417 | (bootloader-configuration-bootloader | ||
| 418 | bootloader)) | ||
| 419 | bootloader entries | ||
| 420 | #:old-entries old-entries))) | ||
| 421 | (remote-result (machine-remote-eval machine remote-exp))) | ||
| 422 | (when (eqv? 'error remote-result) | ||
| 423 | (raise roll-back-failure)))) | ||
| 359 | 424 | ||
| 360 | 425 | ||
| 361 | ;;; | 426 | ;;; |
| @@ -366,6 +431,7 @@ environment type of 'managed-host." | |||
| 366 | (environment-type | 431 | (environment-type |
| 367 | (machine-remote-eval managed-host-remote-eval) | 432 | (machine-remote-eval managed-host-remote-eval) |
| 368 | (deploy-machine deploy-managed-host) | 433 | (deploy-machine deploy-managed-host) |
| 434 | (roll-back-machine roll-back-managed-host) | ||
| 369 | (name 'managed-host-environment-type) | 435 | (name 'managed-host-environment-type) |
| 370 | (description "Provisioning for machines that are accessible over SSH | 436 | (description "Provisioning for machines that are accessible over SSH |
| 371 | and have a known host-name. This entails little more than maintaining an SSH | 437 | and have a known host-name. This entails little more than maintaining an SSH |
