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