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