steps.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.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 installer steps)
  20. #:use-module (guix records)
  21. #:use-module (guix build utils)
  22. #:use-module (guix i18n)
  23. #:use-module (gnu installer utils)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 pretty-print)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-35)
  29. #:use-module (rnrs io ports)
  30. #:export (<installer-step>
  31. installer-step
  32. make-installer-step
  33. installer-step?
  34. installer-step-id
  35. installer-step-description
  36. installer-step-compute
  37. installer-step-configuration-formatter
  38. run-installer-steps
  39. find-step-by-id
  40. result->step-ids
  41. result-step
  42. result-step-done?
  43. %installer-configuration-file
  44. %installer-target-dir
  45. format-configuration
  46. configuration->file
  47. %current-result))
  48. ;; Hash table storing the step results. Use it only for logging and debug
  49. ;; purposes.
  50. (define %current-result (make-hash-table))
  51. ;; An installer-step record is basically an id associated to a compute
  52. ;; procedure. The COMPUTE procedure takes exactly one argument, an association
  53. ;; list containing the results of previously executed installer-steps (see
  54. ;; RUN-INSTALLER-STEPS description). The value returned by the COMPUTE
  55. ;; procedure will be stored in the results list passed to the next
  56. ;; installer-step and so on.
  57. (define-record-type* <installer-step>
  58. installer-step make-installer-step
  59. installer-step?
  60. (id installer-step-id) ;symbol
  61. (description installer-step-description ;string
  62. (default #f)
  63. ;; Make it thunked so that 'G_' is called at the
  64. ;; right time, as opposed to being called once
  65. ;; when the installer starts.
  66. (thunked))
  67. (compute installer-step-compute) ;procedure
  68. (configuration-formatter installer-step-configuration-formatter ;procedure
  69. (default #f)))
  70. (define* (run-installer-steps #:key
  71. steps
  72. (rewind-strategy 'previous)
  73. (menu-proc (const #f)))
  74. "Run the COMPUTE procedure of all <installer-step> records in STEPS
  75. sequentially, inside a the 'installer-step prompt. When aborted to with a
  76. parameter of 'abort, fallback to a previous install-step, accordingly to the
  77. specified REWIND-STRATEGY. When aborted to with a parameter of 'break, stop
  78. the computation and return the accumalated result so far.
  79. REWIND-STRATEGY possible values are 'previous, 'menu and 'start. If 'previous
  80. is selected, the execution will resume at the previous installer-step. If
  81. 'menu is selected, the MENU-PROC procedure will be called. Its return value
  82. has to be an installer-step ID to jump to. The ID has to be the one of a
  83. previously executed step. It is impossible to jump forward. Finally if 'start
  84. is selected, the execution will resume at the first installer-step.
  85. The result of every COMPUTE procedures is stored in an association list, under
  86. the form:
  87. '((STEP-ID . COMPUTE-RESULT) ...)
  88. where STEP-ID is the ID field of the installer-step and COMPUTE-RESULT the
  89. result of the associated COMPUTE procedure. This result association list is
  90. passed as argument of every COMPUTE procedure. It is finally returned when the
  91. computation is over."
  92. (define (pop-result list)
  93. (cdr list))
  94. (define (first-step? steps step)
  95. (match steps
  96. ((first-step . rest-steps)
  97. (equal? first-step step))))
  98. (define* (skip-to-step step result
  99. #:key todo-steps done-steps)
  100. (match todo-steps
  101. ((todo . rest-todo)
  102. (let ((found? (eq? (installer-step-id todo)
  103. (installer-step-id step))))
  104. (cond
  105. (found?
  106. (run result
  107. #:todo-steps todo-steps
  108. #:done-steps done-steps))
  109. ((and (not found?)
  110. (null? done-steps))
  111. (error (format #f "Step ~a not found" (installer-step-id step))))
  112. (else
  113. (match done-steps
  114. ((prev-done ... last-done)
  115. (skip-to-step step (pop-result result)
  116. #:todo-steps (cons last-done todo-steps)
  117. #:done-steps prev-done)))))))))
  118. (define* (run result #:key todo-steps done-steps)
  119. (match todo-steps
  120. (() (reverse result))
  121. ((step . rest-steps)
  122. (call-with-prompt 'installer-step
  123. (lambda ()
  124. (installer-log-line "running step '~a'" (installer-step-id step))
  125. (let* ((id (installer-step-id step))
  126. (compute (installer-step-compute step))
  127. (res (compute result done-steps)))
  128. (hash-set! %current-result id res)
  129. (run (alist-cons id res result)
  130. #:todo-steps rest-steps
  131. #:done-steps (append done-steps (list step)))))
  132. (lambda (k action)
  133. (match action
  134. ('abort
  135. (case rewind-strategy
  136. ((previous)
  137. (match done-steps
  138. (()
  139. ;; We cannot go previous the first step. Abort again to
  140. ;; 'installer-step prompt. It might be useful in the case
  141. ;; of nested run-installer-steps.
  142. (abort-to-prompt 'installer-step action))
  143. ((prev-done ... last-done)
  144. (run (pop-result result)
  145. #:todo-steps (cons last-done todo-steps)
  146. #:done-steps prev-done))))
  147. ((menu)
  148. (let ((goto-step (menu-proc
  149. (append done-steps (list step)))))
  150. (if (eq? goto-step step)
  151. (run result
  152. #:todo-steps todo-steps
  153. #:done-steps done-steps)
  154. (skip-to-step goto-step result
  155. #:todo-steps todo-steps
  156. #:done-steps done-steps))))
  157. ((start)
  158. (if (null? done-steps)
  159. ;; Same as above, it makes no sense to jump to start
  160. ;; when we are at the first installer-step. Abort to
  161. ;; 'installer-step prompt again.
  162. (abort-to-prompt 'installer-step action)
  163. (run '()
  164. #:todo-steps steps
  165. #:done-steps '())))))
  166. ('break
  167. (reverse result))))))))
  168. ;; Ignore SIGPIPE so that we don't die if a client closes the connection
  169. ;; prematurely.
  170. (sigaction SIGPIPE SIG_IGN)
  171. (with-server-socket
  172. (run '()
  173. #:todo-steps steps
  174. #:done-steps '())))
  175. (define (find-step-by-id steps id)
  176. "Find and return the step in STEPS whose id is equal to ID."
  177. (find (lambda (step)
  178. (eq? (installer-step-id step) id))
  179. steps))
  180. (define (result-step results step-id)
  181. "Return the result of the installer-step specified by STEP-ID in
  182. RESULTS."
  183. (assoc-ref results step-id))
  184. (define (result-step-done? results step-id)
  185. "Return #t if the installer-step specified by STEP-ID has a COMPUTE value
  186. stored in RESULTS. Return #f otherwise."
  187. (and (assoc step-id results) #t))
  188. (define %installer-configuration-file (make-parameter "/mnt/etc/config.scm"))
  189. (define %installer-target-dir (make-parameter "/mnt"))
  190. (define (format-configuration steps results)
  191. "Return the list resulting from the application of the procedure defined in
  192. CONFIGURATION-FORMATTER field of <installer-step> on the associated result
  193. found in RESULTS."
  194. (let ((configuration
  195. (append-map
  196. (lambda (step)
  197. (let* ((step-id (installer-step-id step))
  198. (conf-formatter
  199. (installer-step-configuration-formatter step))
  200. (result-step (result-step results step-id)))
  201. (if (and result-step conf-formatter)
  202. (conf-formatter result-step)
  203. '())))
  204. steps))
  205. (modules '((use-modules (gnu))
  206. (use-service-modules cups desktop networking ssh xorg))))
  207. `(,@modules
  208. ()
  209. (operating-system ,@configuration))))
  210. (define* (configuration->file configuration
  211. #:key (filename (%installer-configuration-file)))
  212. "Write the given CONFIGURATION to FILENAME."
  213. (mkdir-p (dirname filename))
  214. (call-with-output-file filename
  215. (lambda (port)
  216. ;; TRANSLATORS: This is a comment within a Scheme file. Each line must
  217. ;; start with ";; " (two semicolons and a space). Please keep line
  218. ;; length below 60 characters.
  219. (display (G_ "\
  220. ;; This is an operating system configuration generated
  221. ;; by the graphical installer.\n")
  222. port)
  223. (newline port)
  224. (for-each (lambda (part)
  225. (if (null? part)
  226. (newline port)
  227. (pretty-print part port)))
  228. configuration)
  229. (flush-output-port port))))
  230. ;;; Local Variables:
  231. ;;; eval: (put 'with-server-socket 'scheme-indent-function 0)
  232. ;;; End: