services: postgresql-roles: Fix race condition.
[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 ;;; Copyright © 2020 Marius Bakke <marius@gnu.org>
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)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu system shadow)
30 #:use-module (gnu packages admin)
31 #:use-module (gnu packages databases)
32 #:use-module (guix build-system trivial)
33 #:use-module (guix build union)
34 #:use-module (guix deprecation)
35 #:use-module (guix modules)
36 #:use-module (guix packages)
37 #:use-module (guix records)
38 #:use-module (guix gexp)
39 #:use-module (srfi srfi-1)
40 #:use-module (ice-9 match)
41 #:export (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-socket-directory
47 postgresql-config-file-extra-config
48
49 postgresql-configuration
50 postgresql-configuration?
51 postgresql-configuration-postgresql
52 postgresql-configuration-port
53 postgresql-configuration-locale
54 postgresql-configuration-file
55 postgresql-configuration-log-directory
56 postgresql-configuration-data-directory
57
58 postgresql-service
59 postgresql-service-type
60
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
73 memcached-service-type
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
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
89 mysql-service
90 mysql-service-type
91 mysql-configuration
92 mysql-configuration?
93
94 redis-configuration
95 redis-configuration?
96 redis-service-type))
97
98 ;;; Commentary:
99 ;;;
100 ;;; Database services.
101 ;;;
102 ;;; Code:
103
104 (define %default-postgres-hba
105 (plain-file "pg_hba.conf"
106 "
107 local all all peer
108 host all all 127.0.0.1/32 md5
109 host all all ::1/128 md5"))
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?
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 '())))
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
133 ident-file socket-directory
134 extra-config)
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)
156 ,@(if socket-directory
157 `(("unix_socket_directories" ,socket-directory))
158 '())
159 ,@extra-config)))
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
170 (define-record-type* <postgresql-configuration>
171 postgresql-configuration make-postgresql-configuration
172 postgresql-configuration?
173 (postgresql postgresql-configuration-postgresql) ;<package>
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)))
180 (log-directory postgresql-configuration-log-directory
181 (default "/var/log/postgresql"))
182 (data-directory postgresql-configuration-data-directory
183 (default "/var/lib/postgresql/data"))
184 (extension-packages postgresql-configuration-extension-packages
185 (default '())))
186
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")
195 (shell (file-append shadow "/sbin/nologin")))))
196
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))
209 (union-build (assoc-ref %outputs "out")
210 (map (lambda (input) (cdr input))
211 %build-inputs))
212 #t)))
213 (inputs
214 `(("postgresql" ,postgresql)
215 ,@(map (lambda (extension) (list "extension" extension))
216 extension-packages))))))
217
218 (define postgresql-activation
219 (match-lambda
220 (($ <postgresql-configuration> postgresql port locale config-file
221 log-directory data-directory
222 extension-packages)
223 #~(begin
224 (use-modules (guix build utils)
225 (ice-9 match))
226
227 (let ((user (getpwnam "postgres"))
228 (initdb (string-append
229 #$(final-postgresql postgresql
230 extension-packages)
231 "/bin/initdb"))
232 (initdb-args
233 (append
234 (if #$locale
235 (list (string-append "--locale=" #$locale))
236 '()))))
237 ;; Create db state directory.
238 (mkdir-p #$data-directory)
239 (chown #$data-directory (passwd:uid user) (passwd:gid user))
240
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
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
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))
263 (primitive-exit
264 (apply system*
265 initdb
266 "-D"
267 #$data-directory
268 initdb-args)))
269 (lambda ()
270 (primitive-exit 1))))
271 (pid (waitpid pid))))))))
272
273 (define postgresql-shepherd-service
274 (match-lambda
275 (($ <postgresql-configuration> postgresql port locale config-file
276 log-directory data-directory
277 extension-packages)
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"))
289 (pg_ctl #$(file-append
290 (final-postgresql postgresql
291 extension-packages)
292 "/bin/pg_ctl"))
293 (options (format #f "--config-file=~a -p ~d"
294 #$config-file #$port)))
295 (setgid (passwd:gid user))
296 (setuid (passwd:uid user))
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
304 mode)))))))
305 (pid-file (in-vicinity data-directory "postmaster.pid"))
306 (action (lambda args
307 #~(lambda _
308 (invoke #$pg_ctl-wrapper #$@args)
309 (match '#$args
310 (("start")
311 (call-with-input-file #$pid-file read))
312 (_ #t))))))
313 (list (shepherd-service
314 (provision '(postgres))
315 (documentation "Run the PostgreSQL daemon.")
316 (requirement '(user-processes loopback syslogd))
317 (modules `((ice-9 match)
318 ,@%default-modules))
319 (start (action "start"))
320 (stop (action "stop"))))))))
321
322 (define postgresql-service-type
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
334 (compose list postgresql-configuration-postgresql))))
335 (default-value (postgresql-configuration
336 (postgresql postgresql-10)))))
337
338 (define-deprecated (postgresql-service #:key (postgresql postgresql)
339 (port 5432)
340 (locale "en_US.utf8")
341 (config-file (postgresql-config-file))
342 (data-directory
343 "/var/lib/postgresql/data")
344 (extension-packages '()))
345 postgresql-service-type
346 "Return a service that runs @var{postgresql}, the PostgreSQL database
347 server.
348
349 The PostgreSQL daemon loads its runtime configuration from @var{config-file}
350 and stores the database cluster in @var{data-directory}."
351 (service postgresql-service-type
352 (postgresql-configuration
353 (postgresql postgresql)
354 (port port)
355 (locale locale)
356 (config-file config-file)
357 (data-directory data-directory)
358 (extension-packages extension-packages))))
359
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 \
398 rolname = '" ,name "')) as not_exists;\n"
399 "\\gset\n"
400 "\\if :not_exists\n"
401 "CREATE ROLE \"" ,name "\""
402 " WITH " ,(format-permissions permissions)
403 ";\n"
404 ,@(if create-database?
405 `("CREATE DATABASE \"" ,name "\""
406 " OWNER \"" ,name "\";\n")
407 '())
408 "\\endif\n")))
409 roles)))
410
411 (let ((host (postgresql-role-configuration-host config))
412 (roles (postgresql-role-configuration-roles config)))
413 #~(let ((psql #$(file-append postgresql "/bin/psql")))
414 (list psql "-a" "-h" #$host "-f" #$(roles->queries roles)))))
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)
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))))))
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
447 created after the PostgreSQL database is started.")))
448
449 \f
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
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
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"
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"
507 "-u" "memcached"
508 ,#$@additional-options)
509 #:log-file "/var/log/memcached"
510 #:pid-file "/var/run/memcached/pid"))
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)
518 (service-extension activation-service-type
519 (const memcached-activation))
520 (service-extension account-service-type
521 (const %memcached-accounts))))
522 (default-value (memcached-configuration))))
523
524 \f
525 ;;;
526 ;;; MongoDB
527 ;;;
528
529 (define %default-mongodb-configuration-file
530 (plain-file
531 "mongodb.yaml"
532 "# GNU Guix: MongoDB default configuration file
533 processManagement:
534 pidFilePath: /var/run/mongodb/pid
535 storage:
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
606 ;;;
607 ;;; MySQL.
608 ;;;
609
610 (define-record-type* <mysql-configuration>
611 mysql-configuration make-mysql-configuration
612 mysql-configuration?
613 (mysql mysql-configuration-mysql (default mariadb))
614 (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
615 (port mysql-configuration-port (default 3306))
616 (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
617 (extra-content mysql-configuration-extra-content (default ""))
618 (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
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")
629 (shell (file-append shadow "/sbin/nologin")))))
630
631 (define mysql-configuration-file
632 (match-lambda
633 (($ <mysql-configuration> mysql bind-address port socket extra-content)
634 (mixed-text-file "my.cnf" "[mysqld]
635 datadir=/var/lib/mysql
636 socket=" socket "
637 bind-address=" bind-address "
638 port=" (number->string port) "
639 " extra-content "
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
681 (string-append #$mysql:lib "/share/mysql/" sql)
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 "
690 DELETE FROM user WHERE User='';
691 DELETE FROM user WHERE User='root' AND
692 Host NOT IN ('localhost', '127.0.0.1', '::1');
693 FLUSH 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
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))))
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!"))))))))))
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
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
764 mysql-shepherd-services)))
765 (default-value (mysql-configuration))))
766
767 (define-deprecated (mysql-service #:key (config (mysql-configuration)))
768 mysql-service-type
769 (service mysql-service-type config))
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
843 (const %redis-accounts))))
844 (default-value (redis-configuration))))