deploy.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. ;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix scripts deploy)
  21. #:use-module (gnu machine)
  22. #:use-module (guix discovery)
  23. #:use-module (guix scripts)
  24. #:use-module (guix scripts build)
  25. #:use-module (guix store)
  26. #:use-module (guix ui)
  27. #:use-module (guix utils)
  28. #:use-module (guix grafts)
  29. #:use-module (guix status)
  30. #:use-module (guix diagnostics)
  31. #:use-module (guix i18n)
  32. #:use-module (ice-9 format)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:use-module (srfi srfi-37)
  38. #:export (guix-deploy))
  39. ;;; Commentary:
  40. ;;;
  41. ;;; This program provides a command-line interface to (gnu machine), allowing
  42. ;;; users to perform remote deployments through specification files.
  43. ;;;
  44. ;;; Code:
  45. (define (show-help)
  46. (display (G_ "Usage: guix deploy [OPTION] FILE...
  47. Perform the deployment specified by FILE.\n"))
  48. (show-build-options-help)
  49. (newline)
  50. (display (G_ "
  51. -h, --help display this help and exit"))
  52. (display (G_ "
  53. -V, --version display version information and exit"))
  54. (newline)
  55. (display (G_ "
  56. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  57. (show-bug-report-information))
  58. (define %options
  59. (cons* (option '(#\h "help") #f #f
  60. (lambda args
  61. (show-help)
  62. (exit 0)))
  63. (option '(#\V "version") #f #f
  64. (lambda args
  65. (show-version-and-exit "guix deploy")))
  66. (option '(#\s "system") #t #f
  67. (lambda (opt name arg result)
  68. (alist-cons 'system arg
  69. (alist-delete 'system result eq?))))
  70. (option '(#\v "verbosity") #t #f
  71. (lambda (opt name arg result)
  72. (let ((level (string->number* arg)))
  73. (alist-cons 'verbosity level
  74. (alist-delete 'verbosity result)))))
  75. %standard-build-options))
  76. (define %default-options
  77. ;; Alist of default option values.
  78. `((verbosity . 1)
  79. (debug . 0)
  80. (graft? . #t)
  81. (substitutes? . #t)
  82. (offload? . #t)
  83. (print-build-trace? . #t)
  84. (print-extended-build-trace? . #t)
  85. (multiplexed-build-output? . #t)))
  86. (define (load-source-file file)
  87. "Load FILE as a user module."
  88. (let* ((guix-path (dirname (search-path %load-path "guix.scm")))
  89. (environment-modules (scheme-modules* guix-path "gnu/machine"))
  90. (module (make-user-module (append '((gnu) (gnu machine))
  91. environment-modules))))
  92. (load* file module)))
  93. (define (show-what-to-deploy machines)
  94. "Show the list of machines to deploy, MACHINES."
  95. (let ((count (length machines)))
  96. (format (current-error-port)
  97. (N_ "The following ~d machine will be deployed:~%"
  98. "The following ~d machines will be deployed:~%"
  99. count)
  100. count)
  101. (display (indented-string
  102. (fill-paragraph (string-join (map machine-display-name machines)
  103. ", ")
  104. (- (%text-width) 2) 2)
  105. 2)
  106. (current-error-port))
  107. (display "\n\n" (current-error-port))))
  108. (define (deploy-machine* store machine)
  109. "Deploy MACHINE, taking care of error handling."
  110. (info (G_ "deploying to ~a...~%")
  111. (machine-display-name machine))
  112. (guard* (c
  113. ;; On Guile 3.0, exceptions such as 'unbound-variable' are compound
  114. ;; and include a '&message'. However, that message only contains
  115. ;; the format string. Thus, special-case it here to avoid
  116. ;; displaying a bare format string.
  117. (((exception-predicate &exception-with-kind-and-args) c)
  118. (raise c))
  119. ((message-condition? c)
  120. (leave (G_ "failed to deploy ~a: ~a~%")
  121. (machine-display-name machine)
  122. (condition-message c)))
  123. ((formatted-message? c)
  124. (leave (G_ "failed to deploy ~a: ~a~%")
  125. (machine-display-name machine)
  126. (apply format #f
  127. (gettext (formatted-message-string c)
  128. %gettext-domain)
  129. (formatted-message-arguments c))))
  130. ((deploy-error? c)
  131. (when (deploy-error-should-roll-back c)
  132. (info (G_ "rolling back ~a...~%")
  133. (machine-display-name machine))
  134. (run-with-store store (roll-back-machine machine)))
  135. (apply throw (deploy-error-captured-args c))))
  136. (run-with-store store (deploy-machine machine))
  137. (info (G_ "successfully deployed ~a~%")
  138. (machine-display-name machine))))
  139. (define-command (guix-deploy . args)
  140. (synopsis "deploy operating systems on a set of machines")
  141. (define (handle-argument arg result)
  142. (alist-cons 'file arg result))
  143. (with-error-handling
  144. (let* ((opts (parse-command-line args %options (list %default-options)
  145. #:argument-handler handle-argument))
  146. (file (assq-ref opts 'file))
  147. (machines (and file (load-source-file file))))
  148. (unless file
  149. (leave (G_ "missing deployment file argument~%")))
  150. (show-what-to-deploy machines)
  151. (with-status-verbosity (assoc-ref opts 'verbosity)
  152. (with-store store
  153. (set-build-options-from-command-line store opts)
  154. (with-build-handler (build-notifier #:use-substitutes?
  155. (assoc-ref opts 'substitutes?)
  156. #:verbosity
  157. (assoc-ref opts 'verbosity))
  158. (parameterize ((%graft? (assq-ref opts 'graft?)))
  159. (map/accumulate-builds store
  160. (cut deploy-machine* store <>)
  161. machines))))))))