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