cuirass.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  6. ;;; Copyright © 2018, 2019 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 (guix utils)
  27. #:use-module (gnu packages admin)
  28. #:use-module (gnu packages ci)
  29. #:use-module (gnu packages version-control)
  30. #:use-module (gnu services)
  31. #:use-module (gnu services base)
  32. #:use-module (gnu services databases)
  33. #:use-module (gnu services shepherd)
  34. #:use-module (gnu services admin)
  35. #:use-module (gnu system shadow)
  36. #:export (<cuirass-remote-server-configuration>
  37. cuirass-remote-server-configuration
  38. cuirass-remote-server-configuration?
  39. <cuirass-configuration>
  40. cuirass-configuration
  41. cuirass-configuration?
  42. cuirass-service-type
  43. <cuirass-remote-worker-configuration>
  44. cuirass-remote-worker-configuration
  45. cuirass-remote-worker-configuration?
  46. cuirass-remote-worker-service-type))
  47. ;;;; Commentary:
  48. ;;;
  49. ;;; This module implements a service that to run instances of Cuirass, a
  50. ;;; continuous integration tool.
  51. ;;;
  52. ;;;; Code:
  53. (define %cuirass-default-database
  54. "dbname=cuirass host=/var/run/postgresql")
  55. (define-record-type* <cuirass-remote-server-configuration>
  56. cuirass-remote-server-configuration make-cuirass-remote-server-configuration
  57. cuirass-remote-server-configuration?
  58. (backend-port cuirass-remote-server-configuration-backend-port ;int
  59. (default #f))
  60. (publish-port cuirass-remote-server-configuration-publish-port ;int
  61. (default #f))
  62. (log-file cuirass-remote-server-log-file ;string
  63. (default "/var/log/cuirass-remote-server.log"))
  64. (cache cuirass-remote-server-configuration-cache ;string
  65. (default "/var/cache/cuirass/remote/"))
  66. (trigger-url cuirass-remote-server-trigger-url ;string
  67. (default #f))
  68. (public-key cuirass-remote-server-configuration-public-key ;string
  69. (default #f))
  70. (private-key cuirass-remote-server-configuration-private-key ;string
  71. (default #f)))
  72. (define-record-type* <cuirass-configuration>
  73. cuirass-configuration make-cuirass-configuration
  74. cuirass-configuration?
  75. (cuirass cuirass-configuration-cuirass ;package
  76. (default cuirass))
  77. (log-file cuirass-configuration-log-file ;string
  78. (default "/var/log/cuirass.log"))
  79. (web-log-file cuirass-configuration-web-log-file ;string
  80. (default "/var/log/cuirass-web.log"))
  81. (cache-directory cuirass-configuration-cache-directory ;string (dir-name)
  82. (default "/var/cache/cuirass"))
  83. (user cuirass-configuration-user ;string
  84. (default "cuirass"))
  85. (group cuirass-configuration-group ;string
  86. (default "cuirass"))
  87. (interval cuirass-configuration-interval ;integer (seconds)
  88. (default 60))
  89. (remote-server cuirass-configuration-remote-server
  90. (default #f))
  91. (database cuirass-configuration-database ;string
  92. (default %cuirass-default-database))
  93. (port cuirass-configuration-port ;integer (port)
  94. (default 8081))
  95. (host cuirass-configuration-host ;string
  96. (default "localhost"))
  97. (specifications cuirass-configuration-specifications)
  98. ;gexp that evaluates to specification-alist
  99. (use-substitutes? cuirass-configuration-use-substitutes? ;boolean
  100. (default #f))
  101. (one-shot? cuirass-configuration-one-shot? ;boolean
  102. (default #f))
  103. (fallback? cuirass-configuration-fallback? ;boolean
  104. (default #f))
  105. (extra-options cuirass-configuration-extra-options
  106. (default '())))
  107. (define (cuirass-shepherd-service config)
  108. "Return a <shepherd-service> for the Cuirass service with CONFIG."
  109. (let ((cuirass (cuirass-configuration-cuirass config))
  110. (cache-directory (cuirass-configuration-cache-directory config))
  111. (web-log-file (cuirass-configuration-web-log-file config))
  112. (log-file (cuirass-configuration-log-file config))
  113. (user (cuirass-configuration-user config))
  114. (group (cuirass-configuration-group config))
  115. (interval (cuirass-configuration-interval config))
  116. (remote-server (cuirass-configuration-remote-server config))
  117. (database (cuirass-configuration-database config))
  118. (port (cuirass-configuration-port config))
  119. (host (cuirass-configuration-host config))
  120. (specs (cuirass-configuration-specifications config))
  121. (use-substitutes? (cuirass-configuration-use-substitutes? config))
  122. (one-shot? (cuirass-configuration-one-shot? config))
  123. (fallback? (cuirass-configuration-fallback? config))
  124. (extra-options (cuirass-configuration-extra-options config)))
  125. `(,(shepherd-service
  126. (documentation "Run Cuirass.")
  127. (provision '(cuirass))
  128. (requirement '(guix-daemon postgres networking))
  129. (start #~(make-forkexec-constructor
  130. (list (string-append #$cuirass "/bin/cuirass")
  131. "--cache-directory" #$cache-directory
  132. "--specifications"
  133. #$(scheme-file "cuirass-specs.scm" specs)
  134. "--database" #$database
  135. "--interval" #$(number->string interval)
  136. #$@(if remote-server '("--build-remote") '())
  137. #$@(if use-substitutes? '("--use-substitutes") '())
  138. #$@(if one-shot? '("--one-shot") '())
  139. #$@(if fallback? '("--fallback") '())
  140. #$@extra-options)
  141. #:environment-variables
  142. (list "GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt"
  143. (string-append "GIT_EXEC_PATH=" #$git
  144. "/libexec/git-core"))
  145. #:user #$user
  146. #:group #$group
  147. #:log-file #$log-file))
  148. (stop #~(make-kill-destructor)))
  149. ,(shepherd-service
  150. (documentation "Run Cuirass web interface.")
  151. (provision '(cuirass-web))
  152. (requirement '(guix-daemon postgres networking))
  153. (start #~(make-forkexec-constructor
  154. (list (string-append #$cuirass "/bin/cuirass")
  155. "--cache-directory" #$cache-directory
  156. "--database" #$database
  157. "--web"
  158. "--port" #$(number->string port)
  159. "--listen" #$host
  160. "--interval" #$(number->string interval)
  161. #$@(if use-substitutes? '("--use-substitutes") '())
  162. #$@(if fallback? '("--fallback") '())
  163. #$@extra-options)
  164. #:user #$user
  165. #:group #$group
  166. #:log-file #$web-log-file))
  167. (stop #~(make-kill-destructor)))
  168. ,@(if remote-server
  169. (match-record remote-server <cuirass-remote-server-configuration>
  170. (backend-port publish-port log-file cache trigger-url
  171. public-key private-key)
  172. (list
  173. (shepherd-service
  174. (documentation "Run Cuirass remote build server.")
  175. (provision '(cuirass-remote-server))
  176. (requirement '(avahi-daemon cuirass guix-daemon networking))
  177. (start #~(make-forkexec-constructor
  178. (list (string-append #$cuirass "/bin/remote-server")
  179. (string-append "--database=" #$database)
  180. (string-append "--cache=" #$cache)
  181. (string-append "--user=" #$user)
  182. #$@(if backend-port
  183. (list (string-append
  184. "--backend-port="
  185. (number->string backend-port)))
  186. '())
  187. #$@(if publish-port
  188. (list (string-append
  189. "--publish-port="
  190. (number->string publish-port)))
  191. '())
  192. #$@(if trigger-url
  193. (list
  194. (string-append
  195. "--trigger-substitute-url="
  196. trigger-url))
  197. '())
  198. #$@(if public-key
  199. (list
  200. (string-append "--public-key="
  201. public-key))
  202. '())
  203. #$@(if private-key
  204. (list
  205. (string-append "--private-key="
  206. private-key))
  207. '()))
  208. #:log-file #$log-file))
  209. (stop #~(make-kill-destructor)))))
  210. '()))))
  211. (define (cuirass-account config)
  212. "Return the user accounts and user groups for CONFIG."
  213. (let ((cuirass-user (cuirass-configuration-user config))
  214. (cuirass-group (cuirass-configuration-group config)))
  215. (list (user-group
  216. (name cuirass-group)
  217. (system? #t))
  218. (user-account
  219. (name cuirass-user)
  220. (group cuirass-group)
  221. (system? #t)
  222. (comment "Cuirass privilege separation user")
  223. (home-directory (string-append "/var/lib/" cuirass-user))
  224. (shell (file-append shadow "/sbin/nologin"))))))
  225. (define (cuirass-postgresql-role config)
  226. (let ((user (cuirass-configuration-user config)))
  227. (list (postgresql-role
  228. (name user)
  229. (create-database? #t)))))
  230. (define (cuirass-activation config)
  231. "Return the activation code for CONFIG."
  232. (let* ((cache (cuirass-configuration-cache-directory config))
  233. (remote-server (cuirass-configuration-remote-server config))
  234. (remote-cache (and remote-server
  235. (cuirass-remote-server-configuration-cache
  236. remote-server)))
  237. (db (dirname
  238. (cuirass-configuration-database config)))
  239. (user (cuirass-configuration-user config))
  240. (log "/var/log/cuirass")
  241. (group (cuirass-configuration-group config)))
  242. (with-imported-modules '((guix build utils))
  243. #~(begin
  244. (use-modules (guix build utils))
  245. (mkdir-p #$cache)
  246. (mkdir-p #$db)
  247. (mkdir-p #$log)
  248. (when #$remote-cache
  249. (mkdir-p #$remote-cache))
  250. (let ((uid (passwd:uid (getpw #$user)))
  251. (gid (group:gid (getgr #$group))))
  252. (chown #$cache uid gid)
  253. (chown #$db uid gid)
  254. (chown #$log uid gid)
  255. (when #$remote-cache
  256. (chown #$remote-cache uid gid)))))))
  257. (define (cuirass-log-rotations config)
  258. "Return the list of log rotations that corresponds to CONFIG."
  259. (list (log-rotation
  260. (files (list (cuirass-configuration-log-file config)))
  261. (frequency 'weekly)
  262. (options '("rotate 40"))))) ;worth keeping
  263. (define cuirass-service-type
  264. (service-type
  265. (name 'cuirass)
  266. (extensions
  267. (list
  268. (service-extension profile-service-type ;for 'info cuirass'
  269. (compose list cuirass-configuration-cuirass))
  270. (service-extension rottlog-service-type cuirass-log-rotations)
  271. (service-extension activation-service-type cuirass-activation)
  272. (service-extension shepherd-root-service-type cuirass-shepherd-service)
  273. (service-extension account-service-type cuirass-account)
  274. (service-extension postgresql-role-service-type
  275. cuirass-postgresql-role)))
  276. (description
  277. "Run the Cuirass continuous integration service.")))
  278. (define-record-type* <cuirass-remote-worker-configuration>
  279. cuirass-remote-worker-configuration make-cuirass-remote-worker-configuration
  280. cuirass-remote-worker-configuration?
  281. (cuirass cuirass-remote-worker-configuration-cuirass ;package
  282. (default cuirass))
  283. (workers cuirass-remote-worker-workers ;int
  284. (default 1))
  285. (systems cuirass-remote-worker-systems ;list
  286. (default (list (%current-system))))
  287. (log-file cuirass-remote-worker-log-file ;string
  288. (default "/var/log/cuirass-remote-worker.log"))
  289. (publish-port cuirass-remote-worker-configuration-publish-port ;int
  290. (default #f))
  291. (public-key cuirass-remote-worker-configuration-public-key ;string
  292. (default #f))
  293. (private-key cuirass-remote-worker-configuration-private-key ;string
  294. (default #f)))
  295. (define (cuirass-remote-worker-shepherd-service config)
  296. "Return a <shepherd-service> for the Cuirass remote worker service with
  297. CONFIG."
  298. (match-record config <cuirass-remote-worker-configuration>
  299. (cuirass workers systems log-file publish-port public-key private-key)
  300. (list (shepherd-service
  301. (documentation "Run Cuirass remote build worker.")
  302. (provision '(cuirass-remote-worker))
  303. (requirement '(avahi-daemon guix-daemon networking))
  304. (start #~(make-forkexec-constructor
  305. (list (string-append #$cuirass "/bin/remote-worker")
  306. (string-append "--workers="
  307. #$(number->string workers))
  308. #$@(if systems
  309. (list (string-append
  310. "--systems="
  311. (string-join systems ",")))
  312. '())
  313. #$@(if publish-port
  314. (list (string-append
  315. "--publish-port="
  316. (number->string publish-port)))
  317. '())
  318. #$@(if public-key
  319. (list
  320. (string-append "--public-key="
  321. public-key))
  322. '())
  323. #$@(if private-key
  324. (list
  325. (string-append "--private-key="
  326. private-key))
  327. '()))
  328. #:log-file #$log-file))
  329. (stop #~(make-kill-destructor))))))
  330. (define cuirass-remote-worker-service-type
  331. (service-type
  332. (name 'cuirass-remote-worker)
  333. (extensions
  334. (list
  335. (service-extension shepherd-root-service-type
  336. cuirass-remote-worker-shepherd-service)))
  337. (description
  338. "Run the Cuirass remote build worker service.")))