deploy.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 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 (ice-9 format)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (srfi srfi-34)
  34. #:use-module (srfi srfi-35)
  35. #:use-module (srfi srfi-37)
  36. #:export (guix-deploy))
  37. ;;; Commentary:
  38. ;;;
  39. ;;; This program provides a command-line interface to (gnu machine), allowing
  40. ;;; users to perform remote deployments through specification files.
  41. ;;;
  42. ;;; Code:
  43. (define (show-help)
  44. (display (G_ "Usage: guix deploy [OPTION] FILE...
  45. Perform the deployment specified by FILE.\n"))
  46. (show-build-options-help)
  47. (newline)
  48. (display (G_ "
  49. -h, --help display this help and exit"))
  50. (display (G_ "
  51. -V, --version display version information and exit"))
  52. (newline)
  53. (display (G_ "
  54. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  55. (show-bug-report-information))
  56. (define %options
  57. (cons* (option '(#\h "help") #f #f
  58. (lambda args
  59. (show-help)
  60. (exit 0)))
  61. (option '(#\V "version") #f #f
  62. (lambda args
  63. (show-version-and-exit "guix deploy")))
  64. (option '(#\s "system") #t #f
  65. (lambda (opt name arg result)
  66. (alist-cons 'system arg
  67. (alist-delete 'system result eq?))))
  68. (option '(#\v "verbosity") #t #f
  69. (lambda (opt name arg result)
  70. (let ((level (string->number* arg)))
  71. (alist-cons 'verbosity level
  72. (alist-delete 'verbosity result)))))
  73. %standard-build-options))
  74. (define %default-options
  75. ;; Alist of default option values.
  76. `((verbosity . 1)
  77. (debug . 0)
  78. (graft? . #t)
  79. (substitutes? . #t)
  80. (offload? . #t)
  81. (print-build-trace? . #t)
  82. (print-extended-build-trace? . #t)
  83. (multiplexed-build-output? . #t)))
  84. (define (load-source-file file)
  85. "Load FILE as a user module."
  86. (let* ((guix-path (dirname (search-path %load-path "guix.scm")))
  87. (environment-modules (scheme-modules* guix-path "gnu/machine"))
  88. (module (make-user-module (append '((gnu) (gnu machine))
  89. environment-modules))))
  90. (load* file module)))
  91. (define (show-what-to-deploy machines)
  92. "Show the list of machines to deploy, MACHINES."
  93. (let ((count (length machines)))
  94. (format (current-error-port)
  95. (N_ "The following ~d machine will be deployed:~%"
  96. "The following ~d machines will be deployed:~%"
  97. count)
  98. count)
  99. (display (indented-string
  100. (fill-paragraph (string-join (map machine-display-name machines)
  101. ", ")
  102. (- (%text-width) 2) 2)
  103. 2)
  104. (current-error-port))
  105. (display "\n\n" (current-error-port))))
  106. (define (deploy-machine* store machine)
  107. "Deploy MACHINE, taking care of error handling."
  108. (info (G_ "deploying to ~a...~%")
  109. (machine-display-name machine))
  110. (guard* (c
  111. ;; On Guile 3.0, exceptions such as 'unbound-variable' are compound
  112. ;; and include a '&message'. However, that message only contains
  113. ;; the format string. Thus, special-case it here to avoid
  114. ;; displaying a bare format string.
  115. ((cond-expand
  116. (guile-3
  117. ((exception-predicate &exception-with-kind-and-args) c))
  118. (else #f))
  119. (raise c))
  120. ((message-condition? c)
  121. (report-error (G_ "failed to deploy ~a: ~a~%")
  122. (machine-display-name machine)
  123. (condition-message c)))
  124. ((deploy-error? c)
  125. (when (deploy-error-should-roll-back c)
  126. (info (G_ "rolling back ~a...~%")
  127. (machine-display-name machine))
  128. (run-with-store store (roll-back-machine machine)))
  129. (apply throw (deploy-error-captured-args c))))
  130. (run-with-store store (deploy-machine machine))
  131. (info (G_ "successfully deployed ~a~%")
  132. (machine-display-name machine))))
  133. (define-command (guix-deploy . args)
  134. (synopsis "deploy operating systems on a set of machines")
  135. (define (handle-argument arg result)
  136. (alist-cons 'file arg result))
  137. (with-error-handling
  138. (let* ((opts (parse-command-line args %options (list %default-options)
  139. #:argument-handler handle-argument))
  140. (file (assq-ref opts 'file))
  141. (machines (or (and file (load-source-file file)) '())))
  142. (show-what-to-deploy machines)
  143. (with-status-verbosity (assoc-ref opts 'verbosity)
  144. (with-store store
  145. (set-build-options-from-command-line store opts)
  146. (with-build-handler (build-notifier #:use-substitutes?
  147. (assoc-ref opts 'substitutes?)
  148. #:verbosity
  149. (assoc-ref opts 'verbosity))
  150. (parameterize ((%graft? (assq-ref opts 'graft?)))
  151. (map/accumulate-builds store
  152. (cut deploy-machine* store <>)
  153. machines))))))))