cuirass.scm 7.9 KB

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