| 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 | ;;; |
| 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) |
| 24 | #:use-module (gnu services shepherd) |
| 25 | #:use-module (gnu system shadow) |
| 26 | #:use-module (gnu packages admin) |
| 27 | #:use-module (gnu packages databases) |
| 28 | #:use-module (guix modules) |
| 29 | #:use-module (guix records) |
| 30 | #:use-module (guix gexp) |
| 31 | #:use-module (ice-9 match) |
| 32 | #:export (postgresql-configuration |
| 33 | postgresql-configuration? |
| 34 | postgresql-service |
| 35 | postgresql-service-type |
| 36 | |
| 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 | |
| 47 | mysql-service |
| 48 | mysql-service-type |
| 49 | mysql-configuration |
| 50 | mysql-configuration? |
| 51 | |
| 52 | redis-configuration |
| 53 | redis-configuration? |
| 54 | redis-service-type)) |
| 55 | |
| 56 | ;;; Commentary: |
| 57 | ;;; |
| 58 | ;;; Database services. |
| 59 | ;;; |
| 60 | ;;; Code: |
| 61 | |
| 62 | (define-record-type* <postgresql-configuration> |
| 63 | postgresql-configuration make-postgresql-configuration |
| 64 | postgresql-configuration? |
| 65 | (postgresql postgresql-configuration-postgresql ;<package> |
| 66 | (default postgresql)) |
| 67 | (port postgresql-configuration-port |
| 68 | (default 5432)) |
| 69 | (locale postgresql-configuration-locale |
| 70 | (default "en_US.utf8")) |
| 71 | (config-file postgresql-configuration-file) |
| 72 | (data-directory postgresql-configuration-data-directory)) |
| 73 | |
| 74 | (define %default-postgres-hba |
| 75 | (plain-file "pg_hba.conf" |
| 76 | " |
| 77 | local all all trust |
| 78 | host all all 127.0.0.1/32 trust |
| 79 | host all all ::1/128 trust")) |
| 80 | |
| 81 | (define %default-postgres-ident |
| 82 | (plain-file "pg_ident.conf" |
| 83 | "# MAPNAME SYSTEM-USERNAME PG-USERNAME")) |
| 84 | |
| 85 | (define %default-postgres-config |
| 86 | (mixed-text-file "postgresql.conf" |
| 87 | "log_destination = 'syslog'\n" |
| 88 | "hba_file = '" %default-postgres-hba "'\n" |
| 89 | "ident_file = '" %default-postgres-ident "'\n")) |
| 90 | |
| 91 | (define %postgresql-accounts |
| 92 | (list (user-group (name "postgres") (system? #t)) |
| 93 | (user-account |
| 94 | (name "postgres") |
| 95 | (group "postgres") |
| 96 | (system? #t) |
| 97 | (comment "PostgreSQL server user") |
| 98 | (home-directory "/var/empty") |
| 99 | (shell (file-append shadow "/sbin/nologin"))))) |
| 100 | |
| 101 | (define postgresql-activation |
| 102 | (match-lambda |
| 103 | (($ <postgresql-configuration> postgresql port locale config-file data-directory) |
| 104 | #~(begin |
| 105 | (use-modules (guix build utils) |
| 106 | (ice-9 match)) |
| 107 | |
| 108 | (let ((user (getpwnam "postgres")) |
| 109 | (initdb (string-append #$postgresql "/bin/initdb")) |
| 110 | (initdb-args |
| 111 | (append |
| 112 | (if #$locale |
| 113 | (list (string-append "--locale=" #$locale)) |
| 114 | '())))) |
| 115 | ;; Create db state directory. |
| 116 | (mkdir-p #$data-directory) |
| 117 | (chown #$data-directory (passwd:uid user) (passwd:gid user)) |
| 118 | |
| 119 | ;; Drop privileges and init state directory in a new |
| 120 | ;; process. Wait for it to finish before proceeding. |
| 121 | (match (primitive-fork) |
| 122 | (0 |
| 123 | ;; Exit with a non-zero status code if an exception is thrown. |
| 124 | (dynamic-wind |
| 125 | (const #t) |
| 126 | (lambda () |
| 127 | (setgid (passwd:gid user)) |
| 128 | (setuid (passwd:uid user)) |
| 129 | (primitive-exit |
| 130 | (apply system* |
| 131 | initdb |
| 132 | "-D" |
| 133 | #$data-directory |
| 134 | initdb-args))) |
| 135 | (lambda () |
| 136 | (primitive-exit 1)))) |
| 137 | (pid (waitpid pid)))))))) |
| 138 | |
| 139 | (define postgresql-shepherd-service |
| 140 | (match-lambda |
| 141 | (($ <postgresql-configuration> postgresql port locale config-file data-directory) |
| 142 | (let ((start-script |
| 143 | ;; Wrapper script that switches to the 'postgres' user before |
| 144 | ;; launching daemon. |
| 145 | (program-file "start-postgres" |
| 146 | #~(let ((user (getpwnam "postgres")) |
| 147 | (postgres (string-append #$postgresql |
| 148 | "/bin/postgres"))) |
| 149 | (setgid (passwd:gid user)) |
| 150 | (setuid (passwd:uid user)) |
| 151 | (system* postgres |
| 152 | (string-append "--config-file=" |
| 153 | #$config-file) |
| 154 | "-p" (number->string #$port) |
| 155 | "-D" #$data-directory))))) |
| 156 | (list (shepherd-service |
| 157 | (provision '(postgres)) |
| 158 | (documentation "Run the PostgreSQL daemon.") |
| 159 | (requirement '(user-processes loopback syslogd)) |
| 160 | (start #~(make-forkexec-constructor #$start-script)) |
| 161 | (stop #~(make-kill-destructor)))))))) |
| 162 | |
| 163 | (define postgresql-service-type |
| 164 | (service-type (name 'postgresql) |
| 165 | (extensions |
| 166 | (list (service-extension shepherd-root-service-type |
| 167 | postgresql-shepherd-service) |
| 168 | (service-extension activation-service-type |
| 169 | postgresql-activation) |
| 170 | (service-extension account-service-type |
| 171 | (const %postgresql-accounts)))))) |
| 172 | |
| 173 | (define* (postgresql-service #:key (postgresql postgresql) |
| 174 | (port 5432) |
| 175 | (locale "en_US.utf8") |
| 176 | (config-file %default-postgres-config) |
| 177 | (data-directory "/var/lib/postgresql/data")) |
| 178 | "Return a service that runs @var{postgresql}, the PostgreSQL database server. |
| 179 | |
| 180 | The PostgreSQL daemon loads its runtime configuration from @var{config-file} |
| 181 | and stores the database cluster in @var{data-directory}." |
| 182 | (service postgresql-service-type |
| 183 | (postgresql-configuration |
| 184 | (postgresql postgresql) |
| 185 | (port port) |
| 186 | (locale locale) |
| 187 | (config-file config-file) |
| 188 | (data-directory data-directory)))) |
| 189 | |
| 190 | \f |
| 191 | ;;; |
| 192 | ;;; Memcached |
| 193 | ;;; |
| 194 | |
| 195 | (define-record-type* <memcached-configuration> |
| 196 | memcached-configuration make-memcached-configuration |
| 197 | memcached-configuration? |
| 198 | (memcached memcached-configuration-memcached ;<package> |
| 199 | (default memcached)) |
| 200 | (interfaces memcached-configuration-interfaces |
| 201 | (default '("0.0.0.0"))) |
| 202 | (tcp-port memcached-configuration-tcp-port |
| 203 | (default 11211)) |
| 204 | (udp-port memcached-configuration-udp-port |
| 205 | (default 11211)) |
| 206 | (additional-options memcached-configuration-additional-options |
| 207 | (default '()))) |
| 208 | |
| 209 | (define %memcached-accounts |
| 210 | (list (user-group (name "memcached") (system? #t)) |
| 211 | (user-account |
| 212 | (name "memcached") |
| 213 | (group "memcached") |
| 214 | (system? #t) |
| 215 | (comment "Memcached server user") |
| 216 | (home-directory "/var/empty") |
| 217 | (shell (file-append shadow "/sbin/nologin"))))) |
| 218 | |
| 219 | (define memcached-shepherd-service |
| 220 | (match-lambda |
| 221 | (($ <memcached-configuration> memcached interfaces tcp-port udp-port |
| 222 | additional-options) |
| 223 | (with-imported-modules (source-module-closure |
| 224 | '((gnu build shepherd))) |
| 225 | (list (shepherd-service |
| 226 | (provision '(memcached)) |
| 227 | (documentation "Run the Memcached daemon.") |
| 228 | (requirement '(user-processes loopback)) |
| 229 | (modules '((gnu build shepherd))) |
| 230 | (start #~(make-forkexec-constructor |
| 231 | `(#$(file-append memcached "/bin/memcached") |
| 232 | "-l" #$(string-join interfaces ",") |
| 233 | "-p" #$(number->string tcp-port) |
| 234 | "-U" #$(number->string udp-port) |
| 235 | "--daemon" |
| 236 | "-P" "/var/run/memcached.pid" |
| 237 | "-u" "memcached" |
| 238 | ,#$@additional-options) |
| 239 | #:log-file "/var/log/memcached" |
| 240 | #:pid-file "/var/run/memcached.pid")) |
| 241 | (stop #~(make-kill-destructor)))))))) |
| 242 | |
| 243 | (define memcached-service-type |
| 244 | (service-type (name 'memcached) |
| 245 | (extensions |
| 246 | (list (service-extension shepherd-root-service-type |
| 247 | memcached-shepherd-service) |
| 248 | (service-extension account-service-type |
| 249 | (const %memcached-accounts)))) |
| 250 | (default-value (memcached-configuration)))) |
| 251 | |
| 252 | \f |
| 253 | ;;; |
| 254 | ;;; MySQL. |
| 255 | ;;; |
| 256 | |
| 257 | (define-record-type* <mysql-configuration> |
| 258 | mysql-configuration make-mysql-configuration |
| 259 | mysql-configuration? |
| 260 | (mysql mysql-configuration-mysql (default mariadb)) |
| 261 | (port mysql-configuration-port (default 3306))) |
| 262 | |
| 263 | (define %mysql-accounts |
| 264 | (list (user-group |
| 265 | (name "mysql") |
| 266 | (system? #t)) |
| 267 | (user-account |
| 268 | (name "mysql") |
| 269 | (group "mysql") |
| 270 | (system? #t) |
| 271 | (home-directory "/var/empty") |
| 272 | (shell (file-append shadow "/sbin/nologin"))))) |
| 273 | |
| 274 | (define mysql-configuration-file |
| 275 | (match-lambda |
| 276 | (($ <mysql-configuration> mysql port) |
| 277 | (mixed-text-file "my.cnf" "[mysqld] |
| 278 | datadir=/var/lib/mysql |
| 279 | socket=/run/mysqld/mysqld.sock |
| 280 | port=" (number->string port) " |
| 281 | ")))) |
| 282 | |
| 283 | (define (%mysql-activation config) |
| 284 | "Return an activation gexp for the MySQL or MariaDB database server." |
| 285 | (let ((mysql (mysql-configuration-mysql config)) |
| 286 | (my.cnf (mysql-configuration-file config))) |
| 287 | #~(begin |
| 288 | (use-modules (ice-9 popen) |
| 289 | (guix build utils)) |
| 290 | (let* ((mysqld (string-append #$mysql "/bin/mysqld")) |
| 291 | (user (getpwnam "mysql")) |
| 292 | (uid (passwd:uid user)) |
| 293 | (gid (passwd:gid user)) |
| 294 | (datadir "/var/lib/mysql") |
| 295 | (rundir "/run/mysqld")) |
| 296 | (mkdir-p datadir) |
| 297 | (chown datadir uid gid) |
| 298 | (mkdir-p rundir) |
| 299 | (chown rundir uid gid) |
| 300 | ;; Initialize the database when it doesn't exist. |
| 301 | (when (not (file-exists? (string-append datadir "/mysql"))) |
| 302 | (if (string-prefix? "mysql-" (strip-store-file-name #$mysql)) |
| 303 | ;; For MySQL. |
| 304 | (system* mysqld |
| 305 | (string-append "--defaults-file=" #$my.cnf) |
| 306 | "--initialize" |
| 307 | "--user=mysql") |
| 308 | ;; For MariaDB. |
| 309 | ;; XXX: The 'mysql_install_db' script doesn't work directly |
| 310 | ;; due to missing 'mkdir' in PATH. |
| 311 | (let ((p (open-pipe* OPEN_WRITE mysqld |
| 312 | (string-append |
| 313 | "--defaults-file=" #$my.cnf) |
| 314 | "--bootstrap" |
| 315 | "--user=mysql"))) |
| 316 | ;; Create the system database, as does by 'mysql_install_db'. |
| 317 | (display "create database mysql;\n" p) |
| 318 | (display "use mysql;\n" p) |
| 319 | (for-each |
| 320 | (lambda (sql) |
| 321 | (call-with-input-file |
| 322 | (string-append #$mysql "/share/mysql/" sql) |
| 323 | (lambda (in) (dump-port in p)))) |
| 324 | '("mysql_system_tables.sql" |
| 325 | "mysql_performance_tables.sql" |
| 326 | "mysql_system_tables_data.sql" |
| 327 | "fill_help_tables.sql")) |
| 328 | ;; Remove the anonymous user and disable root access from |
| 329 | ;; remote machines, as does by 'mysql_secure_installation'. |
| 330 | (display " |
| 331 | DELETE FROM user WHERE User=''; |
| 332 | DELETE FROM user WHERE User='root' AND |
| 333 | Host NOT IN ('localhost', '127.0.0.1', '::1'); |
| 334 | FLUSH PRIVILEGES; |
| 335 | " p) |
| 336 | (close-pipe p)))))))) |
| 337 | |
| 338 | (define (mysql-shepherd-service config) |
| 339 | (list (shepherd-service |
| 340 | (provision '(mysql)) |
| 341 | (documentation "Run the MySQL server.") |
| 342 | (start (let ((mysql (mysql-configuration-mysql config)) |
| 343 | (my.cnf (mysql-configuration-file config))) |
| 344 | #~(make-forkexec-constructor |
| 345 | (list (string-append #$mysql "/bin/mysqld") |
| 346 | (string-append "--defaults-file=" #$my.cnf)) |
| 347 | #:user "mysql" #:group "mysql"))) |
| 348 | (stop #~(make-kill-destructor))))) |
| 349 | |
| 350 | (define mysql-service-type |
| 351 | (service-type |
| 352 | (name 'mysql) |
| 353 | (extensions |
| 354 | (list (service-extension account-service-type |
| 355 | (const %mysql-accounts)) |
| 356 | (service-extension activation-service-type |
| 357 | %mysql-activation) |
| 358 | (service-extension shepherd-root-service-type |
| 359 | mysql-shepherd-service))))) |
| 360 | |
| 361 | (define* (mysql-service #:key (config (mysql-configuration))) |
| 362 | "Return a service that runs @command{mysqld}, the MySQL or MariaDB |
| 363 | database server. |
| 364 | |
| 365 | The optional @var{config} argument specifies the configuration for |
| 366 | @command{mysqld}, which should be a @code{<mysql-configuration>} object." |
| 367 | (service mysql-service-type config)) |
| 368 | |
| 369 | \f |
| 370 | ;;; |
| 371 | ;;; Redis |
| 372 | ;;; |
| 373 | |
| 374 | (define-record-type* <redis-configuration> |
| 375 | redis-configuration make-redis-configuration |
| 376 | redis-configuration? |
| 377 | (redis redis-configuration-redis ;<package> |
| 378 | (default redis)) |
| 379 | (bind redis-configuration-bind |
| 380 | (default "127.0.0.1")) |
| 381 | (port redis-configuration-port |
| 382 | (default 6379)) |
| 383 | (working-directory redis-configuration-working-directory |
| 384 | (default "/var/lib/redis")) |
| 385 | (config-file redis-configuration-config-file |
| 386 | (default #f))) |
| 387 | |
| 388 | (define (default-redis.conf bind port working-directory) |
| 389 | (mixed-text-file "redis.conf" |
| 390 | "bind " bind "\n" |
| 391 | "port " (number->string port) "\n" |
| 392 | "dir " working-directory "\n" |
| 393 | "daemonize no\n")) |
| 394 | |
| 395 | (define %redis-accounts |
| 396 | (list (user-group (name "redis") (system? #t)) |
| 397 | (user-account |
| 398 | (name "redis") |
| 399 | (group "redis") |
| 400 | (system? #t) |
| 401 | (comment "Redis server user") |
| 402 | (home-directory "/var/empty") |
| 403 | (shell (file-append shadow "/sbin/nologin"))))) |
| 404 | |
| 405 | (define redis-activation |
| 406 | (match-lambda |
| 407 | (($ <redis-configuration> redis bind port working-directory config-file) |
| 408 | #~(begin |
| 409 | (use-modules (guix build utils) |
| 410 | (ice-9 match)) |
| 411 | |
| 412 | (let ((user (getpwnam "redis"))) |
| 413 | (mkdir-p #$working-directory) |
| 414 | (chown #$working-directory (passwd:uid user) (passwd:gid user))))))) |
| 415 | |
| 416 | (define redis-shepherd-service |
| 417 | (match-lambda |
| 418 | (($ <redis-configuration> redis bind port working-directory config-file) |
| 419 | (let ((config-file |
| 420 | (or config-file |
| 421 | (default-redis.conf bind port working-directory)))) |
| 422 | (list (shepherd-service |
| 423 | (provision '(redis)) |
| 424 | (documentation "Run the Redis daemon.") |
| 425 | (requirement '(user-processes syslogd)) |
| 426 | (start #~(make-forkexec-constructor |
| 427 | '(#$(file-append redis "/bin/redis-server") |
| 428 | #$config-file) |
| 429 | #:user "redis" |
| 430 | #:group "redis")) |
| 431 | (stop #~(make-kill-destructor)))))))) |
| 432 | |
| 433 | (define redis-service-type |
| 434 | (service-type (name 'redis) |
| 435 | (extensions |
| 436 | (list (service-extension shepherd-root-service-type |
| 437 | redis-shepherd-service) |
| 438 | (service-extension activation-service-type |
| 439 | redis-activation) |
| 440 | (service-extension account-service-type |
| 441 | (const %redis-accounts)))))) |