steps.scm 10 KB

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