Commit | Line | Data |
---|---|---|
105369a4 DT |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | |
9b1cee97 | 3 | ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
8823ed4e | 4 | ;;; Copyright © 2016 Leo Famulari <leo@famulari.name> |
67cadaca | 5 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> |
5ee4cd69 | 6 | ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org> |
0d57a50a | 7 | ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu> |
334a2f4d | 8 | ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net> |
e20388ad | 9 | ;;; Copyright © 2020 Marius Bakke <marius@gnu.org> |
105369a4 DT |
10 | ;;; |
11 | ;;; This file is part of GNU Guix. | |
12 | ;;; | |
13 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
14 | ;;; under the terms of the GNU General Public License as published by | |
15 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
16 | ;;; your option) any later version. | |
17 | ;;; | |
18 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
19 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;;; GNU General Public License for more details. | |
22 | ;;; | |
23 | ;;; You should have received a copy of the GNU General Public License | |
24 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
25 | ||
26 | (define-module (gnu services databases) | |
27 | #:use-module (gnu services) | |
0190c1c0 | 28 | #:use-module (gnu services shepherd) |
105369a4 DT |
29 | #:use-module (gnu system shadow) |
30 | #:use-module (gnu packages admin) | |
31 | #:use-module (gnu packages databases) | |
0d57a50a JL |
32 | #:use-module (guix build-system trivial) |
33 | #:use-module (guix build union) | |
a698df72 | 34 | #:use-module (guix deprecation) |
119fdd0d | 35 | #:use-module (guix modules) |
0d57a50a | 36 | #:use-module (guix packages) |
105369a4 | 37 | #:use-module (guix records) |
105369a4 | 38 | #:use-module (guix gexp) |
936e7a52 | 39 | #:use-module (srfi srfi-1) |
0adfe95a | 40 | #:use-module (ice-9 match) |
383c51ca | 41 | #:export (postgresql-config-file |
936e7a52 CB |
42 | postgresql-config-file? |
43 | postgresql-config-file-log-destination | |
44 | postgresql-config-file-hba-file | |
45 | postgresql-config-file-ident-file | |
46 | postgresql-config-file-extra-config | |
47 | ||
488ea71e | 48 | postgresql-configuration |
24e96431 | 49 | postgresql-configuration? |
488ea71e CB |
50 | postgresql-configuration-postgresql |
51 | postgresql-configuration-port | |
52 | postgresql-configuration-locale | |
53 | postgresql-configuration-file | |
54 | postgresql-configuration-data-directory | |
55 | ||
24e96431 TČ |
56 | postgresql-service |
57 | postgresql-service-type | |
58 | ||
119fdd0d | 59 | memcached-service-type |
119fdd0d CB |
60 | memcached-configuration |
61 | memcached-configuration? | |
62 | memcached-configuration-memecached | |
63 | memcached-configuration-interfaces | |
64 | memcached-configuration-tcp-port | |
65 | memcached-configuration-udp-port | |
66 | memcached-configuration-additional-options | |
67 | ||
5266ff71 CB |
68 | mongodb-configuration |
69 | mongodb-configuration? | |
70 | mongodb-configuration-mongodb | |
71 | mongodb-configuration-config-file | |
72 | mongodb-configuration-data-directory | |
73 | mongodb-service-type | |
74 | ||
6575183b | 75 | mysql-service |
24e96431 TČ |
76 | mysql-service-type |
77 | mysql-configuration | |
67cadaca CB |
78 | mysql-configuration? |
79 | ||
80 | redis-configuration | |
81 | redis-configuration? | |
82 | redis-service-type)) | |
105369a4 DT |
83 | |
84 | ;;; Commentary: | |
85 | ;;; | |
86 | ;;; Database services. | |
87 | ;;; | |
88 | ;;; Code: | |
89 | ||
936e7a52 CB |
90 | (define %default-postgres-hba |
91 | (plain-file "pg_hba.conf" | |
92 | " | |
334a2f4d RV |
93 | local all all peer |
94 | host all all 127.0.0.1/32 md5 | |
95 | host all all ::1/128 md5")) | |
936e7a52 CB |
96 | |
97 | (define %default-postgres-ident | |
98 | (plain-file "pg_ident.conf" | |
99 | "# MAPNAME SYSTEM-USERNAME PG-USERNAME")) | |
100 | ||
101 | (define-record-type* <postgresql-config-file> | |
102 | postgresql-config-file make-postgresql-config-file | |
103 | postgresql-config-file? | |
104 | (log-destination postgresql-config-file-log-destination | |
105 | (default "syslog")) | |
106 | (hba-file postgresql-config-file-hba-file | |
107 | (default %default-postgres-hba)) | |
108 | (ident-file postgresql-config-file-ident-file | |
109 | (default %default-postgres-ident)) | |
110 | (extra-config postgresql-config-file-extra-config | |
111 | (default '()))) | |
112 | ||
113 | (define-gexp-compiler (postgresql-config-file-compiler | |
114 | (file <postgresql-config-file>) system target) | |
115 | (match file | |
116 | (($ <postgresql-config-file> log-destination hba-file | |
117 | ident-file extra-config) | |
53380838 | 118 | (define (single-quote string) |
936e7a52 CB |
119 | (if string |
120 | (list "'" string "'") | |
121 | '())) | |
122 | ||
123 | (define contents | |
124 | (append-map | |
125 | (match-lambda | |
126 | ((key) '()) | |
127 | ((key . #f) '()) | |
128 | ((key values ...) `(,key " = " ,@values "\n"))) | |
129 | ||
53380838 CB |
130 | `(("log_destination" ,@(single-quote log-destination)) |
131 | ("hba_file" ,@(single-quote hba-file)) | |
132 | ("ident_file" ,@(single-quote ident-file)) | |
936e7a52 CB |
133 | ,@extra-config))) |
134 | ||
135 | (gexp->derivation | |
136 | "postgresql.conf" | |
137 | #~(call-with-output-file (ungexp output "out") | |
138 | (lambda (port) | |
139 | (display | |
140 | (string-append #$@contents) | |
141 | port))) | |
142 | #:local-build? #t)))) | |
143 | ||
0adfe95a LC |
144 | (define-record-type* <postgresql-configuration> |
145 | postgresql-configuration make-postgresql-configuration | |
146 | postgresql-configuration? | |
bdcf4d88 | 147 | (postgresql postgresql-configuration-postgresql) ;<package> |
0d57a50a JL |
148 | (port postgresql-configuration-port |
149 | (default 5432)) | |
150 | (locale postgresql-configuration-locale | |
151 | (default "en_US.utf8")) | |
152 | (config-file postgresql-configuration-file | |
153 | (default (postgresql-config-file))) | |
154 | (data-directory postgresql-configuration-data-directory | |
155 | (default "/var/lib/postgresql/data")) | |
156 | (extension-packages postgresql-configuration-extension-packages | |
157 | (default '()))) | |
0adfe95a | 158 | |
0adfe95a LC |
159 | (define %postgresql-accounts |
160 | (list (user-group (name "postgres") (system? #t)) | |
161 | (user-account | |
162 | (name "postgres") | |
163 | (group "postgres") | |
164 | (system? #t) | |
165 | (comment "PostgreSQL server user") | |
166 | (home-directory "/var/empty") | |
9e41130b | 167 | (shell (file-append shadow "/sbin/nologin"))))) |
0adfe95a | 168 | |
0d57a50a JL |
169 | (define (final-postgresql postgresql extension-packages) |
170 | (if (null? extension-packages) | |
171 | postgresql | |
172 | (package | |
173 | (inherit postgresql) | |
174 | (source #f) | |
175 | (build-system trivial-build-system) | |
176 | (arguments | |
177 | `(#:modules ((guix build utils) (guix build union)) | |
178 | #:builder | |
179 | (begin | |
180 | (use-modules (guix build utils) (guix build union) (srfi srfi-26)) | |
181 | (union-build (assoc-ref %outputs "out") (map (lambda (input) (cdr input)) %build-inputs)) | |
182 | #t))) | |
183 | (inputs | |
184 | `(("postgresql" ,postgresql) | |
185 | ,@(map (lambda (extension) (list "extension" extension)) | |
186 | extension-packages)))))) | |
187 | ||
0adfe95a LC |
188 | (define postgresql-activation |
189 | (match-lambda | |
0d57a50a JL |
190 | (($ <postgresql-configuration> postgresql port locale config-file data-directory |
191 | extension-packages) | |
0adfe95a LC |
192 | #~(begin |
193 | (use-modules (guix build utils) | |
194 | (ice-9 match)) | |
195 | ||
196 | (let ((user (getpwnam "postgres")) | |
0d57a50a JL |
197 | (initdb (string-append #$(final-postgresql postgresql extension-packages) |
198 | "/bin/initdb")) | |
e05b780a CB |
199 | (initdb-args |
200 | (append | |
201 | (if #$locale | |
202 | (list (string-append "--locale=" #$locale)) | |
203 | '())))) | |
0adfe95a LC |
204 | ;; Create db state directory. |
205 | (mkdir-p #$data-directory) | |
206 | (chown #$data-directory (passwd:uid user) (passwd:gid user)) | |
207 | ||
208 | ;; Drop privileges and init state directory in a new | |
209 | ;; process. Wait for it to finish before proceeding. | |
210 | (match (primitive-fork) | |
211 | (0 | |
212 | ;; Exit with a non-zero status code if an exception is thrown. | |
213 | (dynamic-wind | |
214 | (const #t) | |
215 | (lambda () | |
216 | (setgid (passwd:gid user)) | |
217 | (setuid (passwd:uid user)) | |
e05b780a CB |
218 | (primitive-exit |
219 | (apply system* | |
220 | initdb | |
221 | "-D" | |
222 | #$data-directory | |
223 | initdb-args))) | |
0adfe95a LC |
224 | (lambda () |
225 | (primitive-exit 1)))) | |
226 | (pid (waitpid pid)))))))) | |
227 | ||
d4053c71 | 228 | (define postgresql-shepherd-service |
0adfe95a | 229 | (match-lambda |
0d57a50a JL |
230 | (($ <postgresql-configuration> postgresql port locale config-file data-directory |
231 | extension-packages) | |
5ee4cd69 CL |
232 | (let* ((pg_ctl-wrapper |
233 | ;; Wrapper script that switches to the 'postgres' user before | |
234 | ;; launching daemon. | |
235 | (program-file | |
236 | "pg_ctl-wrapper" | |
237 | #~(begin | |
238 | (use-modules (ice-9 match) | |
239 | (ice-9 format)) | |
240 | (match (command-line) | |
241 | ((_ mode) | |
242 | (let ((user (getpwnam "postgres")) | |
0d57a50a JL |
243 | (pg_ctl #$(file-append (final-postgresql postgresql extension-packages) |
244 | "/bin/pg_ctl")) | |
5ee4cd69 CL |
245 | (options (format #f "--config-file=~a -p ~d" |
246 | #$config-file #$port))) | |
247 | (setgid (passwd:gid user)) | |
248 | (setuid (passwd:uid user)) | |
249 | (execl pg_ctl pg_ctl "-D" #$data-directory "-o" options | |
250 | mode))))))) | |
ef2dda8e | 251 | (pid-file (in-vicinity data-directory "postmaster.pid")) |
5ee4cd69 CL |
252 | (action (lambda args |
253 | #~(lambda _ | |
ef2dda8e CL |
254 | (invoke #$pg_ctl-wrapper #$@args) |
255 | (match '#$args | |
256 | (("start") | |
257 | (call-with-input-file #$pid-file read)) | |
258 | (_ #t)))))) | |
d4053c71 | 259 | (list (shepherd-service |
0adfe95a LC |
260 | (provision '(postgres)) |
261 | (documentation "Run the PostgreSQL daemon.") | |
9b1cee97 | 262 | (requirement '(user-processes loopback syslogd)) |
ef2dda8e CL |
263 | (modules `((ice-9 match) |
264 | ,@%default-modules)) | |
5ee4cd69 CL |
265 | (start (action "start")) |
266 | (stop (action "stop")))))))) | |
0adfe95a LC |
267 | |
268 | (define postgresql-service-type | |
269 | (service-type (name 'postgresql) | |
270 | (extensions | |
d4053c71 AK |
271 | (list (service-extension shepherd-root-service-type |
272 | postgresql-shepherd-service) | |
0adfe95a LC |
273 | (service-extension activation-service-type |
274 | postgresql-activation) | |
275 | (service-extension account-service-type | |
3bf4761e PN |
276 | (const %postgresql-accounts)) |
277 | (service-extension profile-service-type | |
bdcf4d88 | 278 | (compose list postgresql-configuration-postgresql)))))) |
0adfe95a | 279 | |
a698df72 CB |
280 | (define-deprecated (postgresql-service #:key (postgresql postgresql) |
281 | (port 5432) | |
282 | (locale "en_US.utf8") | |
283 | (config-file (postgresql-config-file)) | |
284 | (data-directory "/var/lib/postgresql/data") | |
285 | (extension-packages '())) | |
286 | postgresql-service-type | |
105369a4 DT |
287 | "Return a service that runs @var{postgresql}, the PostgreSQL database server. |
288 | ||
289 | The PostgreSQL daemon loads its runtime configuration from @var{config-file} | |
290 | and stores the database cluster in @var{data-directory}." | |
0adfe95a LC |
291 | (service postgresql-service-type |
292 | (postgresql-configuration | |
293 | (postgresql postgresql) | |
2d3d5cc5 | 294 | (port port) |
e05b780a | 295 | (locale locale) |
0adfe95a | 296 | (config-file config-file) |
0d57a50a JL |
297 | (data-directory data-directory) |
298 | (extension-packages extension-packages)))) | |
6575183b SB |
299 | |
300 | \f | |
119fdd0d CB |
301 | ;;; |
302 | ;;; Memcached | |
303 | ;;; | |
304 | ||
305 | (define-record-type* <memcached-configuration> | |
306 | memcached-configuration make-memcached-configuration | |
307 | memcached-configuration? | |
308 | (memcached memcached-configuration-memcached ;<package> | |
309 | (default memcached)) | |
310 | (interfaces memcached-configuration-interfaces | |
311 | (default '("0.0.0.0"))) | |
312 | (tcp-port memcached-configuration-tcp-port | |
313 | (default 11211)) | |
314 | (udp-port memcached-configuration-udp-port | |
315 | (default 11211)) | |
316 | (additional-options memcached-configuration-additional-options | |
317 | (default '()))) | |
318 | ||
319 | (define %memcached-accounts | |
320 | (list (user-group (name "memcached") (system? #t)) | |
321 | (user-account | |
322 | (name "memcached") | |
323 | (group "memcached") | |
324 | (system? #t) | |
325 | (comment "Memcached server user") | |
326 | (home-directory "/var/empty") | |
327 | (shell (file-append shadow "/sbin/nologin"))))) | |
328 | ||
6230e155 CB |
329 | (define memcached-activation |
330 | #~(begin | |
331 | (use-modules (guix build utils)) | |
332 | (let ((user (getpwnam "memcached"))) | |
333 | (mkdir-p "/var/run/memcached") | |
334 | (chown "/var/run/memcached" | |
335 | (passwd:uid user) (passwd:gid user))))) | |
336 | ||
119fdd0d CB |
337 | (define memcached-shepherd-service |
338 | (match-lambda | |
339 | (($ <memcached-configuration> memcached interfaces tcp-port udp-port | |
340 | additional-options) | |
341 | (with-imported-modules (source-module-closure | |
342 | '((gnu build shepherd))) | |
343 | (list (shepherd-service | |
344 | (provision '(memcached)) | |
345 | (documentation "Run the Memcached daemon.") | |
346 | (requirement '(user-processes loopback)) | |
347 | (modules '((gnu build shepherd))) | |
348 | (start #~(make-forkexec-constructor | |
349 | `(#$(file-append memcached "/bin/memcached") | |
350 | "-l" #$(string-join interfaces ",") | |
351 | "-p" #$(number->string tcp-port) | |
352 | "-U" #$(number->string udp-port) | |
353 | "--daemon" | |
6230e155 CB |
354 | ;; Memcached changes to the memcached user prior to |
355 | ;; writing the pid file, so write it to a directory | |
356 | ;; that memcached owns. | |
357 | "-P" "/var/run/memcached/pid" | |
119fdd0d CB |
358 | "-u" "memcached" |
359 | ,#$@additional-options) | |
360 | #:log-file "/var/log/memcached" | |
6230e155 | 361 | #:pid-file "/var/run/memcached/pid")) |
119fdd0d CB |
362 | (stop #~(make-kill-destructor)))))))) |
363 | ||
364 | (define memcached-service-type | |
365 | (service-type (name 'memcached) | |
366 | (extensions | |
367 | (list (service-extension shepherd-root-service-type | |
368 | memcached-shepherd-service) | |
6230e155 CB |
369 | (service-extension activation-service-type |
370 | (const memcached-activation)) | |
119fdd0d CB |
371 | (service-extension account-service-type |
372 | (const %memcached-accounts)))) | |
373 | (default-value (memcached-configuration)))) | |
374 | ||
375 | \f | |
5266ff71 CB |
376 | ;;; |
377 | ;;; MongoDB | |
378 | ;;; | |
379 | ||
380 | (define %default-mongodb-configuration-file | |
381 | (plain-file | |
382 | "mongodb.yaml" | |
383 | "# GNU Guix: MongoDB default configuration file | |
384 | processManagement: | |
385 | pidFilePath: /var/run/mongodb/pid | |
386 | storage: | |
387 | dbPath: /var/lib/mongodb | |
388 | ")) | |
389 | ||
390 | ||
391 | (define-record-type* <mongodb-configuration> | |
392 | mongodb-configuration make-mongodb-configuration | |
393 | mongodb-configuration? | |
394 | (mongodb mongodb-configuration-mongodb | |
395 | (default mongodb)) | |
396 | (config-file mongodb-configuration-config-file | |
397 | (default %default-mongodb-configuration-file)) | |
398 | (data-directory mongodb-configuration-data-directory | |
399 | (default "/var/lib/mongodb"))) | |
400 | ||
401 | (define %mongodb-accounts | |
402 | (list (user-group (name "mongodb") (system? #t)) | |
403 | (user-account | |
404 | (name "mongodb") | |
405 | (group "mongodb") | |
406 | (system? #t) | |
407 | (comment "Mongodb server user") | |
408 | (home-directory "/var/lib/mongodb") | |
409 | (shell (file-append shadow "/sbin/nologin"))))) | |
410 | ||
411 | (define mongodb-activation | |
412 | (match-lambda | |
413 | (($ <mongodb-configuration> mongodb config-file data-directory) | |
414 | #~(begin | |
415 | (use-modules (guix build utils)) | |
416 | (let ((user (getpwnam "mongodb"))) | |
417 | (for-each | |
418 | (lambda (directory) | |
419 | (mkdir-p directory) | |
420 | (chown directory | |
421 | (passwd:uid user) (passwd:gid user))) | |
422 | '("/var/run/mongodb" #$data-directory))))))) | |
423 | ||
424 | (define mongodb-shepherd-service | |
425 | (match-lambda | |
426 | (($ <mongodb-configuration> mongodb config-file data-directory) | |
427 | (shepherd-service | |
428 | (provision '(mongodb)) | |
429 | (documentation "Run the Mongodb daemon.") | |
430 | (requirement '(user-processes loopback)) | |
431 | (start #~(make-forkexec-constructor | |
432 | `(,(string-append #$mongodb "/bin/mongod") | |
433 | "--config" | |
434 | ,#$config-file) | |
435 | #:user "mongodb" | |
436 | #:group "mongodb" | |
437 | #:pid-file "/var/run/mongodb/pid" | |
438 | #:log-file "/var/log/mongodb.log")) | |
439 | (stop #~(make-kill-destructor)))))) | |
440 | ||
441 | (define mongodb-service-type | |
442 | (service-type | |
443 | (name 'mongodb) | |
444 | (description "Run the MongoDB document database server.") | |
445 | (extensions | |
446 | (list (service-extension shepherd-root-service-type | |
447 | (compose list | |
448 | mongodb-shepherd-service)) | |
449 | (service-extension activation-service-type | |
450 | mongodb-activation) | |
451 | (service-extension account-service-type | |
452 | (const %mongodb-accounts)))) | |
453 | (default-value | |
454 | (mongodb-configuration)))) | |
455 | ||
456 | \f | |
6575183b SB |
457 | ;;; |
458 | ;;; MySQL. | |
459 | ;;; | |
460 | ||
461 | (define-record-type* <mysql-configuration> | |
462 | mysql-configuration make-mysql-configuration | |
463 | mysql-configuration? | |
4b41febf | 464 | (mysql mysql-configuration-mysql (default mariadb)) |
27d7cdbf | 465 | (bind-address mysql-configuration-bind-address (default "127.0.0.1")) |
33ed8296 | 466 | (port mysql-configuration-port (default 3306)) |
927bf98e | 467 | (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock")) |
e20388ad MB |
468 | (extra-content mysql-configuration-extra-content (default "")) |
469 | (auto-upgrade? mysql-configuration-auto-upgrade? (default #t))) | |
6575183b SB |
470 | |
471 | (define %mysql-accounts | |
472 | (list (user-group | |
473 | (name "mysql") | |
474 | (system? #t)) | |
475 | (user-account | |
476 | (name "mysql") | |
477 | (group "mysql") | |
478 | (system? #t) | |
479 | (home-directory "/var/empty") | |
9e41130b | 480 | (shell (file-append shadow "/sbin/nologin"))))) |
6575183b SB |
481 | |
482 | (define mysql-configuration-file | |
483 | (match-lambda | |
927bf98e | 484 | (($ <mysql-configuration> mysql bind-address port socket extra-content) |
4b41febf | 485 | (mixed-text-file "my.cnf" "[mysqld] |
6575183b | 486 | datadir=/var/lib/mysql |
927bf98e | 487 | socket=" socket " |
27d7cdbf | 488 | bind-address=" bind-address " |
4b41febf | 489 | port=" (number->string port) " |
33ed8296 | 490 | " extra-content " |
6575183b SB |
491 | ")))) |
492 | ||
493 | (define (%mysql-activation config) | |
494 | "Return an activation gexp for the MySQL or MariaDB database server." | |
495 | (let ((mysql (mysql-configuration-mysql config)) | |
496 | (my.cnf (mysql-configuration-file config))) | |
497 | #~(begin | |
498 | (use-modules (ice-9 popen) | |
499 | (guix build utils)) | |
500 | (let* ((mysqld (string-append #$mysql "/bin/mysqld")) | |
501 | (user (getpwnam "mysql")) | |
502 | (uid (passwd:uid user)) | |
503 | (gid (passwd:gid user)) | |
504 | (datadir "/var/lib/mysql") | |
505 | (rundir "/run/mysqld")) | |
506 | (mkdir-p datadir) | |
507 | (chown datadir uid gid) | |
508 | (mkdir-p rundir) | |
509 | (chown rundir uid gid) | |
510 | ;; Initialize the database when it doesn't exist. | |
511 | (when (not (file-exists? (string-append datadir "/mysql"))) | |
512 | (if (string-prefix? "mysql-" (strip-store-file-name #$mysql)) | |
513 | ;; For MySQL. | |
514 | (system* mysqld | |
515 | (string-append "--defaults-file=" #$my.cnf) | |
516 | "--initialize" | |
517 | "--user=mysql") | |
518 | ;; For MariaDB. | |
519 | ;; XXX: The 'mysql_install_db' script doesn't work directly | |
520 | ;; due to missing 'mkdir' in PATH. | |
521 | (let ((p (open-pipe* OPEN_WRITE mysqld | |
522 | (string-append | |
523 | "--defaults-file=" #$my.cnf) | |
524 | "--bootstrap" | |
525 | "--user=mysql"))) | |
526 | ;; Create the system database, as does by 'mysql_install_db'. | |
527 | (display "create database mysql;\n" p) | |
528 | (display "use mysql;\n" p) | |
529 | (for-each | |
530 | (lambda (sql) | |
531 | (call-with-input-file | |
5c3d77c3 | 532 | (string-append #$mysql:lib "/share/mysql/" sql) |
6575183b SB |
533 | (lambda (in) (dump-port in p)))) |
534 | '("mysql_system_tables.sql" | |
535 | "mysql_performance_tables.sql" | |
536 | "mysql_system_tables_data.sql" | |
537 | "fill_help_tables.sql")) | |
538 | ;; Remove the anonymous user and disable root access from | |
539 | ;; remote machines, as does by 'mysql_secure_installation'. | |
540 | (display " | |
541 | DELETE FROM user WHERE User=''; | |
542 | DELETE FROM user WHERE User='root' AND | |
543 | Host NOT IN ('localhost', '127.0.0.1', '::1'); | |
544 | FLUSH PRIVILEGES; | |
545 | " p) | |
546 | (close-pipe p)))))))) | |
547 | ||
548 | (define (mysql-shepherd-service config) | |
549 | (list (shepherd-service | |
550 | (provision '(mysql)) | |
551 | (documentation "Run the MySQL server.") | |
552 | (start (let ((mysql (mysql-configuration-mysql config)) | |
553 | (my.cnf (mysql-configuration-file config))) | |
554 | #~(make-forkexec-constructor | |
555 | (list (string-append #$mysql "/bin/mysqld") | |
556 | (string-append "--defaults-file=" #$my.cnf)) | |
557 | #:user "mysql" #:group "mysql"))) | |
558 | (stop #~(make-kill-destructor))))) | |
559 | ||
e20388ad MB |
560 | (define (mysql-upgrade-wrapper mysql socket-file) |
561 | ;; The MySQL socket and PID file may appear before the server is ready to | |
562 | ;; accept connections. Ensure the socket is responsive before attempting | |
563 | ;; to run the upgrade script. | |
564 | (program-file | |
565 | "mysql-upgrade-wrapper" | |
566 | #~(begin | |
567 | (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade")) | |
568 | (timeout 10)) | |
569 | (begin | |
570 | (let loop ((i 0)) | |
571 | (catch 'system-error | |
572 | (lambda () | |
573 | (let ((sock (socket PF_UNIX SOCK_STREAM 0))) | |
574 | (connect sock AF_UNIX #$socket-file) | |
575 | (close-port sock) | |
576 | ;; The socket is ready! | |
577 | (execl mysql-upgrade mysql-upgrade | |
578 | (string-append "--socket=" #$socket-file)))) | |
3cf19b83 MB |
579 | (lambda args |
580 | (if (< i timeout) | |
581 | (begin | |
582 | (sleep 1) | |
583 | (loop (+ 1 i))) | |
584 | ;; No luck, give up. | |
585 | (throw 'timeout-error | |
586 | "MySQL server did not appear in time!")))))))))) | |
e20388ad MB |
587 | |
588 | (define (mysql-upgrade-shepherd-service config) | |
589 | (list (shepherd-service | |
590 | (provision '(mysql-upgrade)) | |
591 | (requirement '(mysql)) | |
592 | (one-shot? #t) | |
593 | (documentation "Upgrade MySQL database schemas.") | |
594 | (start (let ((mysql (mysql-configuration-mysql config)) | |
595 | (socket (mysql-configuration-socket config))) | |
596 | #~(make-forkexec-constructor | |
597 | (list #$(mysql-upgrade-wrapper mysql socket)) | |
598 | #:user "mysql" #:group "mysql")))))) | |
599 | ||
600 | (define (mysql-shepherd-services config) | |
601 | (if (mysql-configuration-auto-upgrade? config) | |
602 | (append (mysql-shepherd-service config) | |
603 | (mysql-upgrade-shepherd-service config)) | |
604 | (mysql-shepherd-service config))) | |
605 | ||
6575183b SB |
606 | (define mysql-service-type |
607 | (service-type | |
608 | (name 'mysql) | |
609 | (extensions | |
610 | (list (service-extension account-service-type | |
611 | (const %mysql-accounts)) | |
612 | (service-extension activation-service-type | |
613 | %mysql-activation) | |
614 | (service-extension shepherd-root-service-type | |
e20388ad | 615 | mysql-shepherd-services))) |
e903738f | 616 | (default-value (mysql-configuration)))) |
6575183b | 617 | |
89b704a4 MB |
618 | (define-deprecated (mysql-service #:key (config (mysql-configuration))) |
619 | mysql-service-type | |
6575183b | 620 | (service mysql-service-type config)) |
67cadaca CB |
621 | |
622 | \f | |
623 | ;;; | |
624 | ;;; Redis | |
625 | ;;; | |
626 | ||
627 | (define-record-type* <redis-configuration> | |
628 | redis-configuration make-redis-configuration | |
629 | redis-configuration? | |
630 | (redis redis-configuration-redis ;<package> | |
631 | (default redis)) | |
632 | (bind redis-configuration-bind | |
633 | (default "127.0.0.1")) | |
634 | (port redis-configuration-port | |
635 | (default 6379)) | |
636 | (working-directory redis-configuration-working-directory | |
637 | (default "/var/lib/redis")) | |
638 | (config-file redis-configuration-config-file | |
639 | (default #f))) | |
640 | ||
641 | (define (default-redis.conf bind port working-directory) | |
642 | (mixed-text-file "redis.conf" | |
643 | "bind " bind "\n" | |
644 | "port " (number->string port) "\n" | |
645 | "dir " working-directory "\n" | |
646 | "daemonize no\n")) | |
647 | ||
648 | (define %redis-accounts | |
649 | (list (user-group (name "redis") (system? #t)) | |
650 | (user-account | |
651 | (name "redis") | |
652 | (group "redis") | |
653 | (system? #t) | |
654 | (comment "Redis server user") | |
655 | (home-directory "/var/empty") | |
656 | (shell (file-append shadow "/sbin/nologin"))))) | |
657 | ||
658 | (define redis-activation | |
659 | (match-lambda | |
660 | (($ <redis-configuration> redis bind port working-directory config-file) | |
661 | #~(begin | |
662 | (use-modules (guix build utils) | |
663 | (ice-9 match)) | |
664 | ||
665 | (let ((user (getpwnam "redis"))) | |
666 | (mkdir-p #$working-directory) | |
667 | (chown #$working-directory (passwd:uid user) (passwd:gid user))))))) | |
668 | ||
669 | (define redis-shepherd-service | |
670 | (match-lambda | |
671 | (($ <redis-configuration> redis bind port working-directory config-file) | |
672 | (let ((config-file | |
673 | (or config-file | |
674 | (default-redis.conf bind port working-directory)))) | |
675 | (list (shepherd-service | |
676 | (provision '(redis)) | |
677 | (documentation "Run the Redis daemon.") | |
678 | (requirement '(user-processes syslogd)) | |
679 | (start #~(make-forkexec-constructor | |
680 | '(#$(file-append redis "/bin/redis-server") | |
681 | #$config-file) | |
682 | #:user "redis" | |
683 | #:group "redis")) | |
684 | (stop #~(make-kill-destructor)))))))) | |
685 | ||
686 | (define redis-service-type | |
687 | (service-type (name 'redis) | |
688 | (extensions | |
689 | (list (service-extension shepherd-root-service-type | |
690 | redis-shepherd-service) | |
691 | (service-extension activation-service-type | |
692 | redis-activation) | |
693 | (service-extension account-service-type | |
bc037c1b CB |
694 | (const %redis-accounts)))) |
695 | (default-value (redis-configuration)))) |