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