machine.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu machine)
  20. #:use-module (gnu system)
  21. #:use-module (guix derivations)
  22. #:use-module (guix monads)
  23. #:use-module (guix records)
  24. #:use-module (guix store)
  25. #:use-module ((guix diagnostics) #:select (source-properties->location))
  26. #:use-module (srfi srfi-35)
  27. #:export (environment-type
  28. environment-type?
  29. environment-type-name
  30. environment-type-description
  31. environment-type-location
  32. machine
  33. machine?
  34. machine-operating-system
  35. machine-environment
  36. machine-configuration
  37. machine-display-name
  38. deploy-machine
  39. roll-back-machine
  40. machine-remote-eval
  41. &deploy-error
  42. deploy-error?
  43. deploy-error-should-roll-back
  44. deploy-error-captured-args))
  45. ;;; Commentary:
  46. ;;;
  47. ;;; This module provides the types used to declare individual machines in a
  48. ;;; heterogeneous Guix deployment. The interface allows users of specify system
  49. ;;; configurations and the means by which resources should be provisioned on a
  50. ;;; per-host basis.
  51. ;;;
  52. ;;; Code:
  53. ;;;
  54. ;;; Declarations for resources that can be provisioned.
  55. ;;;
  56. (define-record-type* <environment-type> environment-type
  57. make-environment-type
  58. environment-type?
  59. ;; Interface to the environment type's deployment code. Each procedure
  60. ;; should take the same arguments as the top-level procedure of this file
  61. ;; that shares the same name. For example, 'machine-remote-eval' should be
  62. ;; of the form '(machine-remote-eval machine exp)'.
  63. (machine-remote-eval environment-type-machine-remote-eval) ; procedure
  64. (deploy-machine environment-type-deploy-machine) ; procedure
  65. (roll-back-machine environment-type-roll-back-machine) ; procedure
  66. ;; Metadata.
  67. (name environment-type-name) ; symbol
  68. (description environment-type-description ; string
  69. (default #f))
  70. (location environment-type-location ; <location>
  71. (default (and=> (current-source-location)
  72. source-properties->location))
  73. (innate)))
  74. ;;;
  75. ;;; Declarations for machines in a deployment.
  76. ;;;
  77. (define-record-type* <machine> machine make-machine
  78. machine?
  79. (operating-system %machine-operating-system); <operating-system>
  80. (environment machine-environment) ; symbol
  81. (configuration machine-configuration ; configuration object
  82. (default #f))) ; specific to environment
  83. (define (machine-operating-system machine)
  84. "Return the operating system of MACHINE."
  85. (operating-system-with-provenance
  86. (%machine-operating-system machine)))
  87. (define (machine-display-name machine)
  88. "Return the host-name identifying MACHINE."
  89. (operating-system-host-name (machine-operating-system machine)))
  90. (define (machine-remote-eval machine exp)
  91. "Evaluate EXP, a gexp, on MACHINE. Ensure that all the elements EXP refers to
  92. are built and deployed to MACHINE beforehand."
  93. (let ((environment (machine-environment machine)))
  94. ((environment-type-machine-remote-eval environment) machine exp)))
  95. (define (deploy-machine machine)
  96. "Monadic procedure transferring the new system's OS closure to the remote
  97. MACHINE, activating it on MACHINE and switching MACHINE to the new generation."
  98. (let ((environment (machine-environment machine)))
  99. ((environment-type-deploy-machine environment) machine)))
  100. (define (roll-back-machine machine)
  101. "Monadic procedure rolling back to the previous system generation on
  102. MACHINE. Return the number of the generation that was current before switching
  103. and the new generation number."
  104. (let ((environment (machine-environment machine)))
  105. ((environment-type-roll-back-machine environment) machine)))
  106. ;;;
  107. ;;; Error types.
  108. ;;;
  109. (define-condition-type &deploy-error &error
  110. deploy-error?
  111. (should-roll-back deploy-error-should-roll-back)
  112. (captured-args deploy-error-captured-args))