services: postgresql-roles: Fix race condition.
[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>
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
6c067921 46 postgresql-config-file-socket-directory
936e7a52
CB
47 postgresql-config-file-extra-config
48
488ea71e 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
fe4b8823 55 postgresql-configuration-log-directory
488ea71e
CB
56 postgresql-configuration-data-directory
57
24e96431
58 postgresql-service
59 postgresql-service-type
60
ec145a2f
MO
61 postgresql-role
62 postgresql-role?
63 postgresql-role-name
64 postgresql-role-permissions
65 postgresql-role-create-database?
66 postgresql-role-configuration
67 postgresql-role-configuration?
68 postgresql-role-configuration-host
69 postgresql-role-configuration-roles
70
71 postgresql-role-service-type
72
119fdd0d 73 memcached-service-type
119fdd0d
CB
74 memcached-configuration
75 memcached-configuration?
76 memcached-configuration-memecached
77 memcached-configuration-interfaces
78 memcached-configuration-tcp-port
79 memcached-configuration-udp-port
80 memcached-configuration-additional-options
81
5266ff71
CB
82 mongodb-configuration
83 mongodb-configuration?
84 mongodb-configuration-mongodb
85 mongodb-configuration-config-file
86 mongodb-configuration-data-directory
87 mongodb-service-type
88
6575183b 89 mysql-service
24e96431
90 mysql-service-type
91 mysql-configuration
67cadaca
CB
92 mysql-configuration?
93
94 redis-configuration
95 redis-configuration?
96 redis-service-type))
105369a4
DT
97
98;;; Commentary:
99;;;
100;;; Database services.
101;;;
102;;; Code:
103
936e7a52
CB
104(define %default-postgres-hba
105 (plain-file "pg_hba.conf"
106 "
334a2f4d
RV
107local all all peer
108host all all 127.0.0.1/32 md5
109host all all ::1/128 md5"))
936e7a52
CB
110
111(define %default-postgres-ident
112 (plain-file "pg_ident.conf"
113 "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
114
115(define-record-type* <postgresql-config-file>
116 postgresql-config-file make-postgresql-config-file
117 postgresql-config-file?
6c067921
MO
118 (log-destination postgresql-config-file-log-destination
119 (default "syslog"))
120 (hba-file postgresql-config-file-hba-file
121 (default %default-postgres-hba))
122 (ident-file postgresql-config-file-ident-file
123 (default %default-postgres-ident))
124 (socket-directory postgresql-config-file-socket-directory
125 (default "/var/run/postgresql"))
126 (extra-config postgresql-config-file-extra-config
127 (default '())))
936e7a52
CB
128
129(define-gexp-compiler (postgresql-config-file-compiler
130 (file <postgresql-config-file>) system target)
131 (match file
132 (($ <postgresql-config-file> log-destination hba-file
6c067921
MO
133 ident-file socket-directory
134 extra-config)
a38d0b01
MO
135 ;; See: https://www.postgresql.org/docs/current/config-setting.html.
136 (define (format-value value)
137 (cond
138 ((boolean? value)
139 (list (if value "on" "off")))
140 ((number? value)
141 (list (number->string value)))
142 (else
143 (list "'" value "'"))))
144
145 (define contents
146 (append-map
147 (match-lambda
148 ((key) '())
149 ((key . #f) '())
150 ((key values ...)
151 `(,key " = " ,@(append-map format-value values) "\n")))
152
153 `(("log_destination" ,log-destination)
154 ("hba_file" ,hba-file)
155 ("ident_file" ,ident-file)
6c067921
MO
156 ,@(if socket-directory
157 `(("unix_socket_directories" ,socket-directory))
158 '())
a38d0b01 159 ,@extra-config)))
936e7a52
CB
160
161 (gexp->derivation
162 "postgresql.conf"
163 #~(call-with-output-file (ungexp output "out")
164 (lambda (port)
165 (display
166 (string-append #$@contents)
167 port)))
168 #:local-build? #t))))
169
0adfe95a
LC
170(define-record-type* <postgresql-configuration>
171 postgresql-configuration make-postgresql-configuration
172 postgresql-configuration?
bdcf4d88 173 (postgresql postgresql-configuration-postgresql) ;<package>
0d57a50a
JL
174 (port postgresql-configuration-port
175 (default 5432))
176 (locale postgresql-configuration-locale
177 (default "en_US.utf8"))
178 (config-file postgresql-configuration-file
179 (default (postgresql-config-file)))
fe4b8823
MO
180 (log-directory postgresql-configuration-log-directory
181 (default "/var/log/postgresql"))
0d57a50a
JL
182 (data-directory postgresql-configuration-data-directory
183 (default "/var/lib/postgresql/data"))
184 (extension-packages postgresql-configuration-extension-packages
185 (default '())))
0adfe95a 186
0adfe95a
LC
187(define %postgresql-accounts
188 (list (user-group (name "postgres") (system? #t))
189 (user-account
190 (name "postgres")
191 (group "postgres")
192 (system? #t)
193 (comment "PostgreSQL server user")
194 (home-directory "/var/empty")
9e41130b 195 (shell (file-append shadow "/sbin/nologin")))))
0adfe95a 196
0d57a50a
JL
197(define (final-postgresql postgresql extension-packages)
198 (if (null? extension-packages)
199 postgresql
200 (package
201 (inherit postgresql)
202 (source #f)
203 (build-system trivial-build-system)
204 (arguments
205 `(#:modules ((guix build utils) (guix build union))
206 #:builder
207 (begin
208 (use-modules (guix build utils) (guix build union) (srfi srfi-26))
33687aa3
MO
209 (union-build (assoc-ref %outputs "out")
210 (map (lambda (input) (cdr input))
211 %build-inputs))
0d57a50a
JL
212 #t)))
213 (inputs
214 `(("postgresql" ,postgresql)
215 ,@(map (lambda (extension) (list "extension" extension))
216 extension-packages))))))
217
0adfe95a
LC
218(define postgresql-activation
219 (match-lambda
fe4b8823
MO
220 (($ <postgresql-configuration> postgresql port locale config-file
221 log-directory data-directory
222 extension-packages)
0adfe95a
LC
223 #~(begin
224 (use-modules (guix build utils)
225 (ice-9 match))
226
227 (let ((user (getpwnam "postgres"))
fe4b8823
MO
228 (initdb (string-append
229 #$(final-postgresql postgresql
230 extension-packages)
231 "/bin/initdb"))
e05b780a
CB
232 (initdb-args
233 (append
234 (if #$locale
235 (list (string-append "--locale=" #$locale))
236 '()))))
0adfe95a
LC
237 ;; Create db state directory.
238 (mkdir-p #$data-directory)
239 (chown #$data-directory (passwd:uid user) (passwd:gid user))
240
6c067921
MO
241 ;; Create the socket directory.
242 (let ((socket-directory
243 #$(postgresql-config-file-socket-directory config-file)))
244 (when (string? socket-directory)
245 (mkdir-p socket-directory)
246 (chown socket-directory (passwd:uid user) (passwd:gid user))))
247
fe4b8823
MO
248 ;; Create the log directory.
249 (when (string? #$log-directory)
250 (mkdir-p #$log-directory)
251 (chown #$log-directory (passwd:uid user) (passwd:gid user)))
252
0adfe95a
LC
253 ;; Drop privileges and init state directory in a new
254 ;; process. Wait for it to finish before proceeding.
255 (match (primitive-fork)
256 (0
257 ;; Exit with a non-zero status code if an exception is thrown.
258 (dynamic-wind
259 (const #t)
260 (lambda ()
261 (setgid (passwd:gid user))
262 (setuid (passwd:uid user))
e05b780a
CB
263 (primitive-exit
264 (apply system*
265 initdb
266 "-D"
267 #$data-directory
268 initdb-args)))
0adfe95a
LC
269 (lambda ()
270 (primitive-exit 1))))
271 (pid (waitpid pid))))))))
272
d4053c71 273(define postgresql-shepherd-service
0adfe95a 274 (match-lambda
fe4b8823
MO
275 (($ <postgresql-configuration> postgresql port locale config-file
276 log-directory data-directory
277 extension-packages)
5ee4cd69
CL
278 (let* ((pg_ctl-wrapper
279 ;; Wrapper script that switches to the 'postgres' user before
280 ;; launching daemon.
281 (program-file
282 "pg_ctl-wrapper"
283 #~(begin
284 (use-modules (ice-9 match)
285 (ice-9 format))
286 (match (command-line)
287 ((_ mode)
288 (let ((user (getpwnam "postgres"))
fe4b8823
MO
289 (pg_ctl #$(file-append
290 (final-postgresql postgresql
291 extension-packages)
0d57a50a 292 "/bin/pg_ctl"))
5ee4cd69
CL
293 (options (format #f "--config-file=~a -p ~d"
294 #$config-file #$port)))
295 (setgid (passwd:gid user))
296 (setuid (passwd:uid user))
fe4b8823
MO
297 (execl pg_ctl pg_ctl "-D" #$data-directory
298 #$@(if (string? log-directory)
299 (list "-l"
300 (string-append log-directory
301 "/pg_ctl.log"))
302 '())
303 "-o" options
5ee4cd69 304 mode)))))))
ef2dda8e 305 (pid-file (in-vicinity data-directory "postmaster.pid"))
5ee4cd69
CL
306 (action (lambda args
307 #~(lambda _
ef2dda8e
CL
308 (invoke #$pg_ctl-wrapper #$@args)
309 (match '#$args
310 (("start")
311 (call-with-input-file #$pid-file read))
312 (_ #t))))))
d4053c71 313 (list (shepherd-service
0adfe95a
LC
314 (provision '(postgres))
315 (documentation "Run the PostgreSQL daemon.")
9b1cee97 316 (requirement '(user-processes loopback syslogd))
ef2dda8e
CL
317 (modules `((ice-9 match)
318 ,@%default-modules))
5ee4cd69
CL
319 (start (action "start"))
320 (stop (action "stop"))))))))
0adfe95a
LC
321
322(define postgresql-service-type
33687aa3
MO
323 (service-type
324 (name 'postgresql)
325 (extensions
326 (list (service-extension shepherd-root-service-type
327 postgresql-shepherd-service)
328 (service-extension activation-service-type
329 postgresql-activation)
330 (service-extension account-service-type
331 (const %postgresql-accounts))
332 (service-extension
333 profile-service-type
8163f745
MO
334 (compose list postgresql-configuration-postgresql))))
335 (default-value (postgresql-configuration
336 (postgresql postgresql-10)))))
0adfe95a 337
a698df72
CB
338(define-deprecated (postgresql-service #:key (postgresql postgresql)
339 (port 5432)
340 (locale "en_US.utf8")
341 (config-file (postgresql-config-file))
33687aa3
MO
342 (data-directory
343 "/var/lib/postgresql/data")
a698df72
CB
344 (extension-packages '()))
345 postgresql-service-type
33687aa3
MO
346 "Return a service that runs @var{postgresql}, the PostgreSQL database
347server.
105369a4
DT
348
349The PostgreSQL daemon loads its runtime configuration from @var{config-file}
350and stores the database cluster in @var{data-directory}."
0adfe95a
LC
351 (service postgresql-service-type
352 (postgresql-configuration
353 (postgresql postgresql)
2d3d5cc5 354 (port port)
e05b780a 355 (locale locale)
0adfe95a 356 (config-file config-file)
0d57a50a
JL
357 (data-directory data-directory)
358 (extension-packages extension-packages))))
6575183b 359
ec145a2f
MO
360(define-record-type* <postgresql-role>
361 postgresql-role make-postgresql-role
362 postgresql-role?
363 (name postgresql-role-name) ;string
364 (permissions postgresql-role-permissions
365 (default '(createdb login))) ;list
366 (create-database? postgresql-role-create-database? ;boolean
367 (default #f)))
368
369(define-record-type* <postgresql-role-configuration>
370 postgresql-role-configuration make-postgresql-role-configuration
371 postgresql-role-configuration?
372 (host postgresql-role-configuration-host ;string
373 (default "/var/run/postgresql"))
374 (log postgresql-role-configuration-log ;string
375 (default "/var/log/postgresql_roles.log"))
376 (roles postgresql-role-configuration-roles
377 (default '()))) ;list
378
379(define (postgresql-create-roles config)
380 ;; See: https://www.postgresql.org/docs/current/sql-createrole.html for the
381 ;; complete permissions list.
382 (define (format-permissions permissions)
383 (let ((dict '(bypassrls createdb createrole login replication superuser)))
384 (string-join (filter-map (lambda (permission)
385 (and (member permission dict)
386 (string-upcase
387 (symbol->string permission))))
388 permissions)
389 " ")))
390
391 (define (roles->queries roles)
392 (apply mixed-text-file "queries"
393 (append-map
394 (lambda (role)
395 (match-record role <postgresql-role>
396 (name permissions create-database?)
397 `("SELECT NOT(EXISTS(SELECT 1 FROM pg_catalog.pg_roles WHERE \
398rolname = '" ,name "')) as not_exists;\n"
399"\\gset\n"
400"\\if :not_exists\n"
221985ce 401"CREATE ROLE \"" ,name "\""
ec145a2f
MO
402" WITH " ,(format-permissions permissions)
403";\n"
404,@(if create-database?
221985ce
MB
405 `("CREATE DATABASE \"" ,name "\""
406 " OWNER \"" ,name "\";\n")
ec145a2f
MO
407 '())
408"\\endif\n")))
409 roles)))
410
411 (let ((host (postgresql-role-configuration-host config))
412 (roles (postgresql-role-configuration-roles config)))
1a8cfb6d
MO
413 #~(let ((psql #$(file-append postgresql "/bin/psql")))
414 (list psql "-a" "-h" #$host "-f" #$(roles->queries roles)))))
ec145a2f
MO
415
416(define (postgresql-role-shepherd-service config)
417 (match-record config <postgresql-role-configuration>
418 (log)
419 (list (shepherd-service
420 (requirement '(postgres))
421 (provision '(postgres-roles))
422 (one-shot? #t)
1a8cfb6d
MO
423 (start
424 #~(lambda args
425 (let ((pid (fork+exec-command
426 #$(postgresql-create-roles config)
427 #:user "postgres"
428 #:group "postgres"
429 #:log-file #$log)))
430 (zero? (cdr (waitpid pid))))))
ec145a2f
MO
431 (documentation "Create PostgreSQL roles.")))))
432
433(define postgresql-role-service-type
434 (service-type (name 'postgresql-role)
435 (extensions
436 (list (service-extension shepherd-root-service-type
437 postgresql-role-shepherd-service)))
438 (compose concatenate)
439 (extend (lambda (config extended-roles)
440 (match-record config <postgresql-role-configuration>
441 (host roles)
442 (postgresql-role-configuration
443 (host host)
444 (roles (append roles extended-roles))))))
445 (default-value (postgresql-role-configuration))
446 (description "Ensure the specified PostgreSQL roles are
447created after the PostgreSQL database is started.")))
448
6575183b 449\f
119fdd0d
CB
450;;;
451;;; Memcached
452;;;
453
454(define-record-type* <memcached-configuration>
455 memcached-configuration make-memcached-configuration
456 memcached-configuration?
457 (memcached memcached-configuration-memcached ;<package>
458 (default memcached))
459 (interfaces memcached-configuration-interfaces
460 (default '("0.0.0.0")))
461 (tcp-port memcached-configuration-tcp-port
462 (default 11211))
463 (udp-port memcached-configuration-udp-port
464 (default 11211))
465 (additional-options memcached-configuration-additional-options
466 (default '())))
467
468(define %memcached-accounts
469 (list (user-group (name "memcached") (system? #t))
470 (user-account
471 (name "memcached")
472 (group "memcached")
473 (system? #t)
474 (comment "Memcached server user")
475 (home-directory "/var/empty")
476 (shell (file-append shadow "/sbin/nologin")))))
477
6230e155
CB
478(define memcached-activation
479 #~(begin
480 (use-modules (guix build utils))
481 (let ((user (getpwnam "memcached")))
482 (mkdir-p "/var/run/memcached")
483 (chown "/var/run/memcached"
484 (passwd:uid user) (passwd:gid user)))))
485
119fdd0d
CB
486(define memcached-shepherd-service
487 (match-lambda
488 (($ <memcached-configuration> memcached interfaces tcp-port udp-port
489 additional-options)
490 (with-imported-modules (source-module-closure
491 '((gnu build shepherd)))
492 (list (shepherd-service
493 (provision '(memcached))
494 (documentation "Run the Memcached daemon.")
495 (requirement '(user-processes loopback))
496 (modules '((gnu build shepherd)))
497 (start #~(make-forkexec-constructor
498 `(#$(file-append memcached "/bin/memcached")
499 "-l" #$(string-join interfaces ",")
500 "-p" #$(number->string tcp-port)
501 "-U" #$(number->string udp-port)
502 "--daemon"
6230e155
CB
503 ;; Memcached changes to the memcached user prior to
504 ;; writing the pid file, so write it to a directory
505 ;; that memcached owns.
506 "-P" "/var/run/memcached/pid"
119fdd0d
CB
507 "-u" "memcached"
508 ,#$@additional-options)
509 #:log-file "/var/log/memcached"
6230e155 510 #:pid-file "/var/run/memcached/pid"))
119fdd0d
CB
511 (stop #~(make-kill-destructor))))))))
512
513(define memcached-service-type
514 (service-type (name 'memcached)
515 (extensions
516 (list (service-extension shepherd-root-service-type
517 memcached-shepherd-service)
6230e155
CB
518 (service-extension activation-service-type
519 (const memcached-activation))
119fdd0d
CB
520 (service-extension account-service-type
521 (const %memcached-accounts))))
522 (default-value (memcached-configuration))))
523
524\f
5266ff71
CB
525;;;
526;;; MongoDB
527;;;
528
529(define %default-mongodb-configuration-file
530 (plain-file
531 "mongodb.yaml"
532 "# GNU Guix: MongoDB default configuration file
533processManagement:
534 pidFilePath: /var/run/mongodb/pid
535storage:
536 dbPath: /var/lib/mongodb
537"))
538
539
540(define-record-type* <mongodb-configuration>
541 mongodb-configuration make-mongodb-configuration
542 mongodb-configuration?
543 (mongodb mongodb-configuration-mongodb
544 (default mongodb))
545 (config-file mongodb-configuration-config-file
546 (default %default-mongodb-configuration-file))
547 (data-directory mongodb-configuration-data-directory
548 (default "/var/lib/mongodb")))
549
550(define %mongodb-accounts
551 (list (user-group (name "mongodb") (system? #t))
552 (user-account
553 (name "mongodb")
554 (group "mongodb")
555 (system? #t)
556 (comment "Mongodb server user")
557 (home-directory "/var/lib/mongodb")
558 (shell (file-append shadow "/sbin/nologin")))))
559
560(define mongodb-activation
561 (match-lambda
562 (($ <mongodb-configuration> mongodb config-file data-directory)
563 #~(begin
564 (use-modules (guix build utils))
565 (let ((user (getpwnam "mongodb")))
566 (for-each
567 (lambda (directory)
568 (mkdir-p directory)
569 (chown directory
570 (passwd:uid user) (passwd:gid user)))
571 '("/var/run/mongodb" #$data-directory)))))))
572
573(define mongodb-shepherd-service
574 (match-lambda
575 (($ <mongodb-configuration> mongodb config-file data-directory)
576 (shepherd-service
577 (provision '(mongodb))
578 (documentation "Run the Mongodb daemon.")
579 (requirement '(user-processes loopback))
580 (start #~(make-forkexec-constructor
581 `(,(string-append #$mongodb "/bin/mongod")
582 "--config"
583 ,#$config-file)
584 #:user "mongodb"
585 #:group "mongodb"
586 #:pid-file "/var/run/mongodb/pid"
587 #:log-file "/var/log/mongodb.log"))
588 (stop #~(make-kill-destructor))))))
589
590(define mongodb-service-type
591 (service-type
592 (name 'mongodb)
593 (description "Run the MongoDB document database server.")
594 (extensions
595 (list (service-extension shepherd-root-service-type
596 (compose list
597 mongodb-shepherd-service))
598 (service-extension activation-service-type
599 mongodb-activation)
600 (service-extension account-service-type
601 (const %mongodb-accounts))))
602 (default-value
603 (mongodb-configuration))))
604
605\f
6575183b
SB
606;;;
607;;; MySQL.
608;;;
609
610(define-record-type* <mysql-configuration>
611 mysql-configuration make-mysql-configuration
612 mysql-configuration?
4b41febf 613 (mysql mysql-configuration-mysql (default mariadb))
27d7cdbf 614 (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
33ed8296 615 (port mysql-configuration-port (default 3306))
927bf98e 616 (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
e20388ad
MB
617 (extra-content mysql-configuration-extra-content (default ""))
618 (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
6575183b
SB
619
620(define %mysql-accounts
621 (list (user-group
622 (name "mysql")
623 (system? #t))
624 (user-account
625 (name "mysql")
626 (group "mysql")
627 (system? #t)
628 (home-directory "/var/empty")
9e41130b 629 (shell (file-append shadow "/sbin/nologin")))))
6575183b
SB
630
631(define mysql-configuration-file
632 (match-lambda
927bf98e 633 (($ <mysql-configuration> mysql bind-address port socket extra-content)
4b41febf 634 (mixed-text-file "my.cnf" "[mysqld]
6575183b 635datadir=/var/lib/mysql
927bf98e 636socket=" socket "
27d7cdbf 637bind-address=" bind-address "
4b41febf 638port=" (number->string port) "
33ed8296 639" extra-content "
6575183b
SB
640"))))
641
642(define (%mysql-activation config)
643 "Return an activation gexp for the MySQL or MariaDB database server."
644 (let ((mysql (mysql-configuration-mysql config))
645 (my.cnf (mysql-configuration-file config)))
646 #~(begin
647 (use-modules (ice-9 popen)
648 (guix build utils))
649 (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
650 (user (getpwnam "mysql"))
651 (uid (passwd:uid user))
652 (gid (passwd:gid user))
653 (datadir "/var/lib/mysql")
654 (rundir "/run/mysqld"))
655 (mkdir-p datadir)
656 (chown datadir uid gid)
657 (mkdir-p rundir)
658 (chown rundir uid gid)
659 ;; Initialize the database when it doesn't exist.
660 (when (not (file-exists? (string-append datadir "/mysql")))
661 (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
662 ;; For MySQL.
663 (system* mysqld
664 (string-append "--defaults-file=" #$my.cnf)
665 "--initialize"
666 "--user=mysql")
667 ;; For MariaDB.
668 ;; XXX: The 'mysql_install_db' script doesn't work directly
669 ;; due to missing 'mkdir' in PATH.
670 (let ((p (open-pipe* OPEN_WRITE mysqld
671 (string-append
672 "--defaults-file=" #$my.cnf)
673 "--bootstrap"
674 "--user=mysql")))
675 ;; Create the system database, as does by 'mysql_install_db'.
676 (display "create database mysql;\n" p)
677 (display "use mysql;\n" p)
678 (for-each
679 (lambda (sql)
680 (call-with-input-file
5c3d77c3 681 (string-append #$mysql:lib "/share/mysql/" sql)
6575183b
SB
682 (lambda (in) (dump-port in p))))
683 '("mysql_system_tables.sql"
684 "mysql_performance_tables.sql"
685 "mysql_system_tables_data.sql"
686 "fill_help_tables.sql"))
687 ;; Remove the anonymous user and disable root access from
688 ;; remote machines, as does by 'mysql_secure_installation'.
689 (display "
690DELETE FROM user WHERE User='';
691DELETE FROM user WHERE User='root' AND
692 Host NOT IN ('localhost', '127.0.0.1', '::1');
693FLUSH PRIVILEGES;
694" p)
695 (close-pipe p))))))))
696
697(define (mysql-shepherd-service config)
698 (list (shepherd-service
699 (provision '(mysql))
700 (documentation "Run the MySQL server.")
701 (start (let ((mysql (mysql-configuration-mysql config))
702 (my.cnf (mysql-configuration-file config)))
703 #~(make-forkexec-constructor
704 (list (string-append #$mysql "/bin/mysqld")
705 (string-append "--defaults-file=" #$my.cnf))
706 #:user "mysql" #:group "mysql")))
707 (stop #~(make-kill-destructor)))))
708
e20388ad
MB
709(define (mysql-upgrade-wrapper mysql socket-file)
710 ;; The MySQL socket and PID file may appear before the server is ready to
711 ;; accept connections. Ensure the socket is responsive before attempting
712 ;; to run the upgrade script.
713 (program-file
714 "mysql-upgrade-wrapper"
715 #~(begin
716 (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade"))
717 (timeout 10))
718 (begin
719 (let loop ((i 0))
720 (catch 'system-error
721 (lambda ()
722 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
723 (connect sock AF_UNIX #$socket-file)
724 (close-port sock)
725 ;; The socket is ready!
726 (execl mysql-upgrade mysql-upgrade
727 (string-append "--socket=" #$socket-file))))
3cf19b83
MB
728 (lambda args
729 (if (< i timeout)
730 (begin
731 (sleep 1)
732 (loop (+ 1 i)))
733 ;; No luck, give up.
734 (throw 'timeout-error
735 "MySQL server did not appear in time!"))))))))))
e20388ad
MB
736
737(define (mysql-upgrade-shepherd-service config)
738 (list (shepherd-service
739 (provision '(mysql-upgrade))
740 (requirement '(mysql))
741 (one-shot? #t)
742 (documentation "Upgrade MySQL database schemas.")
743 (start (let ((mysql (mysql-configuration-mysql config))
744 (socket (mysql-configuration-socket config)))
745 #~(make-forkexec-constructor
746 (list #$(mysql-upgrade-wrapper mysql socket))
747 #:user "mysql" #:group "mysql"))))))
748
749(define (mysql-shepherd-services config)
750 (if (mysql-configuration-auto-upgrade? config)
751 (append (mysql-shepherd-service config)
752 (mysql-upgrade-shepherd-service config))
753 (mysql-shepherd-service config)))
754
6575183b
SB
755(define mysql-service-type
756 (service-type
757 (name 'mysql)
758 (extensions
759 (list (service-extension account-service-type
760 (const %mysql-accounts))
761 (service-extension activation-service-type
762 %mysql-activation)
763 (service-extension shepherd-root-service-type
e20388ad 764 mysql-shepherd-services)))
e903738f 765 (default-value (mysql-configuration))))
6575183b 766
89b704a4
MB
767(define-deprecated (mysql-service #:key (config (mysql-configuration)))
768 mysql-service-type
6575183b 769 (service mysql-service-type config))
67cadaca
CB
770
771\f
772;;;
773;;; Redis
774;;;
775
776(define-record-type* <redis-configuration>
777 redis-configuration make-redis-configuration
778 redis-configuration?
779 (redis redis-configuration-redis ;<package>
780 (default redis))
781 (bind redis-configuration-bind
782 (default "127.0.0.1"))
783 (port redis-configuration-port
784 (default 6379))
785 (working-directory redis-configuration-working-directory
786 (default "/var/lib/redis"))
787 (config-file redis-configuration-config-file
788 (default #f)))
789
790(define (default-redis.conf bind port working-directory)
791 (mixed-text-file "redis.conf"
792 "bind " bind "\n"
793 "port " (number->string port) "\n"
794 "dir " working-directory "\n"
795 "daemonize no\n"))
796
797(define %redis-accounts
798 (list (user-group (name "redis") (system? #t))
799 (user-account
800 (name "redis")
801 (group "redis")
802 (system? #t)
803 (comment "Redis server user")
804 (home-directory "/var/empty")
805 (shell (file-append shadow "/sbin/nologin")))))
806
807(define redis-activation
808 (match-lambda
809 (($ <redis-configuration> redis bind port working-directory config-file)
810 #~(begin
811 (use-modules (guix build utils)
812 (ice-9 match))
813
814 (let ((user (getpwnam "redis")))
815 (mkdir-p #$working-directory)
816 (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
817
818(define redis-shepherd-service
819 (match-lambda
820 (($ <redis-configuration> redis bind port working-directory config-file)
821 (let ((config-file
822 (or config-file
823 (default-redis.conf bind port working-directory))))
824 (list (shepherd-service
825 (provision '(redis))
826 (documentation "Run the Redis daemon.")
827 (requirement '(user-processes syslogd))
828 (start #~(make-forkexec-constructor
829 '(#$(file-append redis "/bin/redis-server")
830 #$config-file)
831 #:user "redis"
832 #:group "redis"))
833 (stop #~(make-kill-destructor))))))))
834
835(define redis-service-type
836 (service-type (name 'redis)
837 (extensions
838 (list (service-extension shepherd-root-service-type
839 redis-shepherd-service)
840 (service-extension activation-service-type
841 redis-activation)
842 (service-extension account-service-type
bc037c1b
CB
843 (const %redis-accounts))))
844 (default-value (redis-configuration))))