cuirass.scm 19 KB

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