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