cuirass.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  3. ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software: you can redistribute it and/or modify
  10. ;;; it under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation, either version 3 of the License, or
  12. ;;; (at your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful,
  15. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu services cuirass)
  22. #:use-module (guix gexp)
  23. #:use-module (guix records)
  24. #:use-module (gnu packages admin)
  25. #:autoload (gnu packages ci) (cuirass)
  26. #:autoload (gnu packages version-control) (git)
  27. #:use-module (gnu services)
  28. #:use-module (gnu services base)
  29. #:use-module (gnu services shepherd)
  30. #:use-module (gnu services admin)
  31. #:use-module (gnu system shadow)
  32. #:export (<cuirass-configuration>
  33. cuirass-configuration
  34. cuirass-configuration?
  35. cuirass-service-type))
  36. ;;;; Commentary:
  37. ;;;
  38. ;;; This module implements a service that to run instances of Cuirass, a
  39. ;;; continuous integration tool.
  40. ;;;
  41. ;;;; Code:
  42. (define-record-type* <cuirass-configuration>
  43. cuirass-configuration make-cuirass-configuration
  44. cuirass-configuration?
  45. (cuirass cuirass-configuration-cuirass ;package
  46. (default cuirass))
  47. (log-file cuirass-configuration-log-file ;string
  48. (default "/var/log/cuirass.log"))
  49. (cache-directory cuirass-configuration-cache-directory ;string (dir-name)
  50. (default "/var/cache/cuirass"))
  51. (user cuirass-configuration-user ;string
  52. (default "cuirass"))
  53. (group cuirass-configuration-group ;string
  54. (default "cuirass"))
  55. (interval cuirass-configuration-interval ;integer (seconds)
  56. (default 60))
  57. (database cuirass-configuration-database ;string (file-name)
  58. (default "/var/run/cuirass/cuirass.db"))
  59. (port cuirass-configuration-port ;integer (port)
  60. (default 8081))
  61. (host cuirass-configuration-host ;string
  62. (default "localhost"))
  63. (specifications cuirass-configuration-specifications)
  64. ;gexp that evaluates to specification-alist
  65. (use-substitutes? cuirass-configuration-use-substitutes? ;boolean
  66. (default #f))
  67. (one-shot? cuirass-configuration-one-shot? ;boolean
  68. (default #f))
  69. (fallback? cuirass-configuration-fallback? ;boolean
  70. (default #f))
  71. (load-path cuirass-configuration-load-path
  72. (default '())))
  73. (define (cuirass-shepherd-service config)
  74. "Return a <shepherd-service> for the Cuirass service with CONFIG."
  75. (and
  76. (cuirass-configuration? config)
  77. (let ((cuirass (cuirass-configuration-cuirass config))
  78. (cache-directory (cuirass-configuration-cache-directory config))
  79. (log-file (cuirass-configuration-log-file config))
  80. (user (cuirass-configuration-user config))
  81. (group (cuirass-configuration-group config))
  82. (interval (cuirass-configuration-interval config))
  83. (database (cuirass-configuration-database config))
  84. (port (cuirass-configuration-port config))
  85. (host (cuirass-configuration-host config))
  86. (specs (cuirass-configuration-specifications config))
  87. (use-substitutes? (cuirass-configuration-use-substitutes? config))
  88. (one-shot? (cuirass-configuration-one-shot? config))
  89. (fallback? (cuirass-configuration-fallback? config))
  90. (load-path (cuirass-configuration-load-path config)))
  91. (list (shepherd-service
  92. (documentation "Run Cuirass.")
  93. (provision '(cuirass))
  94. (requirement '(guix-daemon networking))
  95. (start #~(make-forkexec-constructor
  96. (list (string-append #$cuirass "/bin/cuirass")
  97. "--cache-directory" #$cache-directory
  98. "--specifications"
  99. #$(scheme-file "cuirass-specs.scm" specs)
  100. "--database" #$database
  101. "--port" #$(number->string port)
  102. "--listen" #$host
  103. "--interval" #$(number->string interval)
  104. #$@(if use-substitutes? '("--use-substitutes") '())
  105. #$@(if one-shot? '("--one-shot") '())
  106. #$@(if fallback? '("--fallback") '())
  107. #$@(if (null? load-path) '()
  108. `("--load-path" ,(string-join load-path ":"))))
  109. #:environment-variables
  110. (list "GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt"
  111. (string-append "GIT_EXEC_PATH=" #$git
  112. "/libexec/git-core"))
  113. #:user #$user
  114. #:group #$group
  115. #:log-file #$log-file))
  116. (stop #~(make-kill-destructor)))))))
  117. (define (cuirass-account config)
  118. "Return the user accounts and user groups for CONFIG."
  119. (let ((cuirass-user (cuirass-configuration-user config))
  120. (cuirass-group (cuirass-configuration-group config)))
  121. (list (user-group
  122. (name cuirass-group)
  123. (system? #t))
  124. (user-account
  125. (name cuirass-user)
  126. (group cuirass-group)
  127. (system? #t)
  128. (comment "Cuirass privilege separation user")
  129. (home-directory (string-append "/var/run/" cuirass-user))
  130. (shell #~(string-append #$shadow "/sbin/nologin"))))))
  131. (define (cuirass-activation config)
  132. "Return the activation code for CONFIG."
  133. (let ((cache (cuirass-configuration-cache-directory config))
  134. (db (dirname (cuirass-configuration-database config)))
  135. (user (cuirass-configuration-user config))
  136. (group (cuirass-configuration-group config)))
  137. (with-imported-modules '((guix build utils))
  138. #~(begin
  139. (use-modules (guix build utils))
  140. (mkdir-p #$cache)
  141. (mkdir-p #$db)
  142. (let ((uid (passwd:uid (getpw #$user)))
  143. (gid (group:gid (getgr #$group))))
  144. (chown #$cache uid gid)
  145. (chown #$db uid gid))))))
  146. (define (cuirass-log-rotations config)
  147. "Return the list of log rotations that corresponds to CONFIG."
  148. (list (log-rotation
  149. (files (list (cuirass-configuration-log-file config)))
  150. (frequency 'weekly)
  151. (options '("rotate 40"))))) ;worth keeping
  152. (define cuirass-service-type
  153. (service-type
  154. (name 'cuirass)
  155. (extensions
  156. (list
  157. (service-extension profile-service-type ;for 'info cuirass'
  158. (compose list cuirass-configuration-cuirass))
  159. (service-extension rottlog-service-type cuirass-log-rotations)
  160. (service-extension activation-service-type cuirass-activation)
  161. (service-extension shepherd-root-service-type cuirass-shepherd-service)
  162. (service-extension account-service-type cuirass-account)))))