WIP:afs-service-commit
[jackhill/guix/guix.git] / gnu / services / databases.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
5 ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
6 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
8 ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
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)
27 #:use-module (gnu services shepherd)
28 #:use-module (gnu system shadow)
29 #:use-module (gnu packages admin)
30 #:use-module (gnu packages databases)
31 #:use-module (guix build-system trivial)
32 #:use-module (guix build union)
33 #:use-module (guix modules)
34 #:use-module (guix packages)
35 #:use-module (guix records)
36 #:use-module (guix gexp)
37 #:use-module (srfi srfi-1)
38 #:use-module (ice-9 match)
39 #:export (<postgresql-config-file>
40 postgresql-config-file
41 postgresql-config-file?
42 postgresql-config-file-log-destination
43 postgresql-config-file-hba-file
44 postgresql-config-file-ident-file
45 postgresql-config-file-extra-config
46
47 <postgresql-configuration>
48 postgresql-configuration
49 postgresql-configuration?
50 postgresql-configuration-postgresql
51 postgresql-configuration-port
52 postgresql-configuration-locale
53 postgresql-configuration-file
54 postgresql-configuration-data-directory
55
56 postgresql-service
57 postgresql-service-type
58
59 memcached-service-type
60 <memcached-configuration>
61 memcached-configuration
62 memcached-configuration?
63 memcached-configuration-memecached
64 memcached-configuration-interfaces
65 memcached-configuration-tcp-port
66 memcached-configuration-udp-port
67 memcached-configuration-additional-options
68
69 <mongodb-configuration>
70 mongodb-configuration
71 mongodb-configuration?
72 mongodb-configuration-mongodb
73 mongodb-configuration-config-file
74 mongodb-configuration-data-directory
75 mongodb-service-type
76
77 mysql-service
78 mysql-service-type
79 mysql-configuration
80 mysql-configuration?
81
82 redis-configuration
83 redis-configuration?
84 redis-service-type))
85
86 ;;; Commentary:
87 ;;;
88 ;;; Database services.
89 ;;;
90 ;;; Code:
91
92 (define %default-postgres-hba
93 (plain-file "pg_hba.conf"
94 "
95 local all all peer
96 host all all 127.0.0.1/32 md5
97 host all all ::1/128 md5"))
98
99 (define %default-postgres-ident
100 (plain-file "pg_ident.conf"
101 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
102
103 (define-record-type* <postgresql-config-file>
104 postgresql-config-file make-postgresql-config-file
105 postgresql-config-file?
106 (log-destination postgresql-config-file-log-destination
107 (default "syslog"))
108 (hba-file postgresql-config-file-hba-file
109 (default %default-postgres-hba))
110 (ident-file postgresql-config-file-ident-file
111 (default %default-postgres-ident))
112 (extra-config postgresql-config-file-extra-config
113 (default '())))
114
115 (define-gexp-compiler (postgresql-config-file-compiler
116 (file <postgresql-config-file>) system target)
117 (match file
118 (($ <postgresql-config-file> log-destination hba-file
119 ident-file extra-config)
120 (define (single-quote string)
121 (if string
122 (list "'" string "'")
123 '()))
124
125 (define contents
126 (append-map
127 (match-lambda
128 ((key) '())
129 ((key . #f) '())
130 ((key values ...) `(,key " = " ,@values "\n")))
131
132 `(("log_destination" ,@(single-quote log-destination))
133 ("hba_file" ,@(single-quote hba-file))
134 ("ident_file" ,@(single-quote ident-file))
135 ,@extra-config)))
136
137 (gexp->derivation
138 "postgresql.conf"
139 #~(call-with-output-file (ungexp output "out")
140 (lambda (port)
141 (display
142 (string-append #$@contents)
143 port)))
144 #:local-build? #t))))
145
146 (define-record-type* <postgresql-configuration>
147 postgresql-configuration make-postgresql-configuration
148 postgresql-configuration?
149 (postgresql postgresql-configuration-postgresql ;<package>
150 (default postgresql))
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 '())))
161
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")
170 (shell (file-append shadow "/sbin/nologin")))))
171
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
191 (define postgresql-activation
192 (match-lambda
193 (($ <postgresql-configuration> postgresql port locale config-file data-directory
194 extension-packages)
195 #~(begin
196 (use-modules (guix build utils)
197 (ice-9 match))
198
199 (let ((user (getpwnam "postgres"))
200 (initdb (string-append #$(final-postgresql postgresql extension-packages)
201 "/bin/initdb"))
202 (initdb-args
203 (append
204 (if #$locale
205 (list (string-append "--locale=" #$locale))
206 '()))))
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))
221 (primitive-exit
222 (apply system*
223 initdb
224 "-D"
225 #$data-directory
226 initdb-args)))
227 (lambda ()
228 (primitive-exit 1))))
229 (pid (waitpid pid))))))))
230
231 (define postgresql-shepherd-service
232 (match-lambda
233 (($ <postgresql-configuration> postgresql port locale config-file data-directory
234 extension-packages)
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"))
246 (pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
247 "/bin/pg_ctl"))
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)))))))
254 (pid-file (in-vicinity data-directory "postmaster.pid"))
255 (action (lambda args
256 #~(lambda _
257 (invoke #$pg_ctl-wrapper #$@args)
258 (match '#$args
259 (("start")
260 (call-with-input-file #$pid-file read))
261 (_ #t))))))
262 (list (shepherd-service
263 (provision '(postgres))
264 (documentation "Run the PostgreSQL daemon.")
265 (requirement '(user-processes loopback syslogd))
266 (modules `((ice-9 match)
267 ,@%default-modules))
268 (start (action "start"))
269 (stop (action "stop"))))))))
270
271 (define postgresql-service-type
272 (service-type (name 'postgresql)
273 (extensions
274 (list (service-extension shepherd-root-service-type
275 postgresql-shepherd-service)
276 (service-extension activation-service-type
277 postgresql-activation)
278 (service-extension account-service-type
279 (const %postgresql-accounts))
280 (service-extension profile-service-type
281 (compose list postgresql-configuration-postgresql))))
282 (default-value (postgresql-configuration))))
283
284 (define* (postgresql-service #:key (postgresql postgresql)
285 (port 5432)
286 (locale "en_US.utf8")
287 (config-file (postgresql-config-file))
288 (data-directory "/var/lib/postgresql/data")
289 (extension-packages '()))
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}."
294 (service postgresql-service-type
295 (postgresql-configuration
296 (postgresql postgresql)
297 (port port)
298 (locale locale)
299 (config-file config-file)
300 (data-directory data-directory)
301 (extension-packages extension-packages))))
302
303 \f
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
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
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"
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"
361 "-u" "memcached"
362 ,#$@additional-options)
363 #:log-file "/var/log/memcached"
364 #:pid-file "/var/run/memcached/pid"))
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)
372 (service-extension activation-service-type
373 (const memcached-activation))
374 (service-extension account-service-type
375 (const %memcached-accounts))))
376 (default-value (memcached-configuration))))
377
378 \f
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
460 ;;;
461 ;;; MySQL.
462 ;;;
463
464 (define-record-type* <mysql-configuration>
465 mysql-configuration make-mysql-configuration
466 mysql-configuration?
467 (mysql mysql-configuration-mysql (default mariadb))
468 (port mysql-configuration-port (default 3306))
469 (extra-content mysql-configuration-extra-content (default "")))
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")
480 (shell (file-append shadow "/sbin/nologin")))))
481
482 (define mysql-configuration-file
483 (match-lambda
484 (($ <mysql-configuration> mysql port extra-content)
485 (mixed-text-file "my.cnf" "[mysqld]
486 datadir=/var/lib/mysql
487 socket=/run/mysqld/mysqld.sock
488 port=" (number->string port) "
489 " extra-content "
490 "))))
491
492 (define (%mysql-activation config)
493 "Return an activation gexp for the MySQL or MariaDB database server."
494 (let ((mysql (mysql-configuration-mysql config))
495 (my.cnf (mysql-configuration-file config)))
496 #~(begin
497 (use-modules (ice-9 popen)
498 (guix build utils))
499 (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
500 (user (getpwnam "mysql"))
501 (uid (passwd:uid user))
502 (gid (passwd:gid user))
503 (datadir "/var/lib/mysql")
504 (rundir "/run/mysqld"))
505 (mkdir-p datadir)
506 (chown datadir uid gid)
507 (mkdir-p rundir)
508 (chown rundir uid gid)
509 ;; Initialize the database when it doesn't exist.
510 (when (not (file-exists? (string-append datadir "/mysql")))
511 (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
512 ;; For MySQL.
513 (system* mysqld
514 (string-append "--defaults-file=" #$my.cnf)
515 "--initialize"
516 "--user=mysql")
517 ;; For MariaDB.
518 ;; XXX: The 'mysql_install_db' script doesn't work directly
519 ;; due to missing 'mkdir' in PATH.
520 (let ((p (open-pipe* OPEN_WRITE mysqld
521 (string-append
522 "--defaults-file=" #$my.cnf)
523 "--bootstrap"
524 "--user=mysql")))
525 ;; Create the system database, as does by 'mysql_install_db'.
526 (display "create database mysql;\n" p)
527 (display "use mysql;\n" p)
528 (for-each
529 (lambda (sql)
530 (call-with-input-file
531 (string-append #$mysql:lib "/share/mysql/" sql)
532 (lambda (in) (dump-port in p))))
533 '("mysql_system_tables.sql"
534 "mysql_performance_tables.sql"
535 "mysql_system_tables_data.sql"
536 "fill_help_tables.sql"))
537 ;; Remove the anonymous user and disable root access from
538 ;; remote machines, as does by 'mysql_secure_installation'.
539 (display "
540 DELETE FROM user WHERE User='';
541 DELETE FROM user WHERE User='root' AND
542 Host NOT IN ('localhost', '127.0.0.1', '::1');
543 FLUSH PRIVILEGES;
544 " p)
545 (close-pipe p))))))))
546
547 (define (mysql-shepherd-service config)
548 (list (shepherd-service
549 (provision '(mysql))
550 (documentation "Run the MySQL server.")
551 (start (let ((mysql (mysql-configuration-mysql config))
552 (my.cnf (mysql-configuration-file config)))
553 #~(make-forkexec-constructor
554 (list (string-append #$mysql "/bin/mysqld")
555 (string-append "--defaults-file=" #$my.cnf))
556 #:user "mysql" #:group "mysql")))
557 (stop #~(make-kill-destructor)))))
558
559 (define mysql-service-type
560 (service-type
561 (name 'mysql)
562 (extensions
563 (list (service-extension account-service-type
564 (const %mysql-accounts))
565 (service-extension activation-service-type
566 %mysql-activation)
567 (service-extension shepherd-root-service-type
568 mysql-shepherd-service)))
569 (default-value (mysql-configuration))))
570
571 (define* (mysql-service #:key (config (mysql-configuration)))
572 "Return a service that runs @command{mysqld}, the MySQL or MariaDB
573 database server.
574
575 The optional @var{config} argument specifies the configuration for
576 @command{mysqld}, which should be a @code{<mysql-configuration>} object."
577 (service mysql-service-type config))
578
579 \f
580 ;;;
581 ;;; Redis
582 ;;;
583
584 (define-record-type* <redis-configuration>
585 redis-configuration make-redis-configuration
586 redis-configuration?
587 (redis redis-configuration-redis ;<package>
588 (default redis))
589 (bind redis-configuration-bind
590 (default "127.0.0.1"))
591 (port redis-configuration-port
592 (default 6379))
593 (working-directory redis-configuration-working-directory
594 (default "/var/lib/redis"))
595 (config-file redis-configuration-config-file
596 (default #f)))
597
598 (define (default-redis.conf bind port working-directory)
599 (mixed-text-file "redis.conf"
600 "bind " bind "\n"
601 "port " (number->string port) "\n"
602 "dir " working-directory "\n"
603 "daemonize no\n"))
604
605 (define %redis-accounts
606 (list (user-group (name "redis") (system? #t))
607 (user-account
608 (name "redis")
609 (group "redis")
610 (system? #t)
611 (comment "Redis server user")
612 (home-directory "/var/empty")
613 (shell (file-append shadow "/sbin/nologin")))))
614
615 (define redis-activation
616 (match-lambda
617 (($ <redis-configuration> redis bind port working-directory config-file)
618 #~(begin
619 (use-modules (guix build utils)
620 (ice-9 match))
621
622 (let ((user (getpwnam "redis")))
623 (mkdir-p #$working-directory)
624 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
625
626 (define redis-shepherd-service
627 (match-lambda
628 (($ <redis-configuration> redis bind port working-directory config-file)
629 (let ((config-file
630 (or config-file
631 (default-redis.conf bind port working-directory))))
632 (list (shepherd-service
633 (provision '(redis))
634 (documentation "Run the Redis daemon.")
635 (requirement '(user-processes syslogd))
636 (start #~(make-forkexec-constructor
637 '(#$(file-append redis "/bin/redis-server")
638 #$config-file)
639 #:user "redis"
640 #:group "redis"))
641 (stop #~(make-kill-destructor))))))))
642
643 (define redis-service-type
644 (service-type (name 'redis)
645 (extensions
646 (list (service-extension shepherd-root-service-type
647 redis-shepherd-service)
648 (service-extension activation-service-type
649 redis-activation)
650 (service-extension account-service-type
651 (const %redis-accounts))))
652 (default-value (redis-configuration))))