databases.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2015, 2016, 2022 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. ;;; Copyright © 2021 David Larsson <david.larsson@selfhosted.xyz>
  11. ;;;
  12. ;;; This file is part of GNU Guix.
  13. ;;;
  14. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  15. ;;; under the terms of the GNU General Public License as published by
  16. ;;; the Free Software Foundation; either version 3 of the License, or (at
  17. ;;; your option) any later version.
  18. ;;;
  19. ;;; GNU Guix is distributed in the hope that it will be useful, but
  20. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;;; GNU General Public License for more details.
  23. ;;;
  24. ;;; You should have received a copy of the GNU General Public License
  25. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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. postgresql-configuration
  49. postgresql-configuration?
  50. postgresql-configuration-postgresql
  51. postgresql-configuration-port
  52. postgresql-configuration-locale
  53. postgresql-configuration-file
  54. postgresql-configuration-log-directory
  55. postgresql-configuration-data-directory
  56. postgresql-configuration-extension-packages
  57. postgresql-service
  58. postgresql-service-type
  59. postgresql-role
  60. postgresql-role?
  61. postgresql-role-name
  62. postgresql-role-permissions
  63. postgresql-role-create-database?
  64. postgresql-role-configuration
  65. postgresql-role-configuration?
  66. postgresql-role-configuration-host
  67. postgresql-role-configuration-roles
  68. postgresql-role-service-type
  69. memcached-service-type
  70. memcached-configuration
  71. memcached-configuration?
  72. memcached-configuration-memecached
  73. memcached-configuration-interfaces
  74. memcached-configuration-tcp-port
  75. memcached-configuration-udp-port
  76. memcached-configuration-additional-options
  77. mysql-service
  78. mysql-service-type
  79. mysql-configuration
  80. mysql-configuration?
  81. redis-configuration
  82. redis-configuration?
  83. redis-service-type))
  84. ;;; Commentary:
  85. ;;;
  86. ;;; Database services.
  87. ;;;
  88. ;;; Code:
  89. (define %default-postgres-hba
  90. (plain-file "pg_hba.conf"
  91. "
  92. local all all peer
  93. host all all 127.0.0.1/32 md5
  94. host all all ::1/128 md5"))
  95. (define %default-postgres-ident
  96. (plain-file "pg_ident.conf"
  97. "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
  98. (define-record-type* <postgresql-config-file>
  99. postgresql-config-file make-postgresql-config-file
  100. postgresql-config-file?
  101. (log-destination postgresql-config-file-log-destination
  102. (default "syslog"))
  103. (hba-file postgresql-config-file-hba-file
  104. (default %default-postgres-hba))
  105. (ident-file postgresql-config-file-ident-file
  106. (default %default-postgres-ident))
  107. (socket-directory postgresql-config-file-socket-directory
  108. (default "/var/run/postgresql"))
  109. (extra-config postgresql-config-file-extra-config
  110. (default '())))
  111. (define-gexp-compiler (postgresql-config-file-compiler
  112. (file <postgresql-config-file>) system target)
  113. (match file
  114. (($ <postgresql-config-file> log-destination hba-file
  115. ident-file socket-directory
  116. extra-config)
  117. ;; See: https://www.postgresql.org/docs/current/config-setting.html.
  118. (define (format-value value)
  119. (cond
  120. ((boolean? value)
  121. (list (if value "on" "off")))
  122. ((number? value)
  123. (list (number->string value)))
  124. (else
  125. (list "'" value "'"))))
  126. (define contents
  127. (append-map
  128. (match-lambda
  129. ((key) '())
  130. ((key . #f) '())
  131. ((key values ...)
  132. `(,key " = " ,@(append-map format-value values) "\n")))
  133. `(("log_destination" ,log-destination)
  134. ("hba_file" ,hba-file)
  135. ("ident_file" ,ident-file)
  136. ,@(if socket-directory
  137. `(("unix_socket_directories" ,socket-directory))
  138. '())
  139. ,@extra-config)))
  140. (gexp->derivation
  141. "postgresql.conf"
  142. #~(call-with-output-file (ungexp output "out")
  143. (lambda (port)
  144. (display
  145. (string-append #$@contents)
  146. port)))
  147. #:local-build? #t))))
  148. (define-record-type* <postgresql-configuration>
  149. postgresql-configuration make-postgresql-configuration
  150. postgresql-configuration?
  151. (postgresql postgresql-configuration-postgresql) ;file-like
  152. (port postgresql-configuration-port
  153. (default 5432))
  154. (locale postgresql-configuration-locale
  155. (default "en_US.utf8"))
  156. (config-file postgresql-configuration-file
  157. (default (postgresql-config-file)))
  158. (log-directory postgresql-configuration-log-directory
  159. (default "/var/log/postgresql"))
  160. (data-directory postgresql-configuration-data-directory
  161. (default "/var/lib/postgresql/data"))
  162. (extension-packages postgresql-configuration-extension-packages
  163. (default '())))
  164. (define %postgresql-accounts
  165. (list (user-group (name "postgres") (system? #t))
  166. (user-account
  167. (name "postgres")
  168. (group "postgres")
  169. (system? #t)
  170. (comment "PostgreSQL server user")
  171. (home-directory "/var/empty")
  172. (shell (file-append shadow "/sbin/nologin")))))
  173. (define (final-postgresql postgresql extension-packages)
  174. (if (null? extension-packages)
  175. postgresql
  176. (package
  177. (inherit postgresql)
  178. (source #f)
  179. (build-system trivial-build-system)
  180. (arguments
  181. `(#:modules ((guix build utils) (guix build union))
  182. #:builder
  183. (begin
  184. (use-modules (guix build utils) (guix build union) (srfi srfi-26))
  185. (union-build (assoc-ref %outputs "out")
  186. (map (lambda (input) (cdr input))
  187. %build-inputs))
  188. #t)))
  189. (inputs
  190. `(("postgresql" ,postgresql)
  191. ,@(map (lambda (extension) (list "extension" extension))
  192. extension-packages))))))
  193. (define postgresql-activation
  194. (match-lambda
  195. (($ <postgresql-configuration> postgresql port locale config-file
  196. log-directory data-directory
  197. extension-packages)
  198. #~(begin
  199. (use-modules (guix build utils)
  200. (ice-9 match))
  201. (let ((user (getpwnam "postgres"))
  202. (initdb (string-append
  203. #$(final-postgresql postgresql
  204. extension-packages)
  205. "/bin/initdb"))
  206. (initdb-args
  207. (append
  208. (if #$locale
  209. (list (string-append "--locale=" #$locale))
  210. '()))))
  211. ;; Create db state directory.
  212. (mkdir-p #$data-directory)
  213. (chown #$data-directory (passwd:uid user) (passwd:gid user))
  214. ;; Create the socket directory.
  215. (let ((socket-directory
  216. #$(postgresql-config-file-socket-directory config-file)))
  217. (when (string? socket-directory)
  218. (mkdir-p socket-directory)
  219. (chown socket-directory (passwd:uid user) (passwd:gid user))))
  220. ;; Create the log directory.
  221. (when (string? #$log-directory)
  222. (mkdir-p #$log-directory)
  223. (chown #$log-directory (passwd:uid user) (passwd:gid user)))
  224. ;; Drop privileges and init state directory in a new
  225. ;; process. Wait for it to finish before proceeding.
  226. (match (primitive-fork)
  227. (0
  228. ;; Exit with a non-zero status code if an exception is thrown.
  229. (dynamic-wind
  230. (const #t)
  231. (lambda ()
  232. (setgid (passwd:gid user))
  233. (setuid (passwd:uid user))
  234. (primitive-exit
  235. (apply system*
  236. initdb
  237. "-D"
  238. #$data-directory
  239. initdb-args)))
  240. (lambda ()
  241. (primitive-exit 1))))
  242. (pid (waitpid pid))))))))
  243. (define postgresql-shepherd-service
  244. (match-lambda
  245. (($ <postgresql-configuration> postgresql port locale config-file
  246. log-directory data-directory
  247. extension-packages)
  248. (let* ((pg_ctl-wrapper
  249. ;; Wrapper script that switches to the 'postgres' user before
  250. ;; launching daemon.
  251. (program-file
  252. "pg_ctl-wrapper"
  253. #~(begin
  254. (use-modules (ice-9 match)
  255. (ice-9 format))
  256. (match (command-line)
  257. ((_ mode)
  258. (let ((user (getpwnam "postgres"))
  259. (pg_ctl #$(file-append
  260. (final-postgresql postgresql
  261. extension-packages)
  262. "/bin/pg_ctl"))
  263. (options (format #f "--config-file=~a -p ~d"
  264. #$config-file #$port)))
  265. (setgid (passwd:gid user))
  266. (setuid (passwd:uid user))
  267. (execl pg_ctl pg_ctl "-D" #$data-directory
  268. #$@(if (string? log-directory)
  269. (list "-l"
  270. (string-append log-directory
  271. "/pg_ctl.log"))
  272. '())
  273. "-o" options
  274. mode)))))))
  275. (pid-file (in-vicinity data-directory "postmaster.pid"))
  276. (action (lambda args
  277. #~(lambda _
  278. (invoke #$pg_ctl-wrapper #$@args)
  279. (match '#$args
  280. (("start")
  281. (call-with-input-file #$pid-file read))
  282. (_ #t))))))
  283. (list (shepherd-service
  284. (provision '(postgres))
  285. (documentation "Run the PostgreSQL daemon.")
  286. (requirement '(user-processes loopback syslogd))
  287. (modules `((ice-9 match)
  288. ,@%default-modules))
  289. (start (action "start"))
  290. (stop (action "stop"))))))))
  291. (define postgresql-service-type
  292. (service-type
  293. (name 'postgresql)
  294. (extensions
  295. (list (service-extension shepherd-root-service-type
  296. postgresql-shepherd-service)
  297. (service-extension activation-service-type
  298. postgresql-activation)
  299. (service-extension account-service-type
  300. (const %postgresql-accounts))
  301. (service-extension
  302. profile-service-type
  303. (compose list postgresql-configuration-postgresql))))
  304. (default-value (postgresql-configuration
  305. (postgresql postgresql-10)))
  306. (description "Run the PostgreSQL database server.")))
  307. (define-deprecated (postgresql-service #:key (postgresql postgresql)
  308. (port 5432)
  309. (locale "en_US.utf8")
  310. (config-file (postgresql-config-file))
  311. (data-directory
  312. "/var/lib/postgresql/data")
  313. (extension-packages '()))
  314. postgresql-service-type
  315. "Return a service that runs @var{postgresql}, the PostgreSQL database
  316. server.
  317. The PostgreSQL daemon loads its runtime configuration from @var{config-file}
  318. and stores the database cluster in @var{data-directory}."
  319. (service postgresql-service-type
  320. (postgresql-configuration
  321. (postgresql postgresql)
  322. (port port)
  323. (locale locale)
  324. (config-file config-file)
  325. (data-directory data-directory)
  326. (extension-packages extension-packages))))
  327. (define-record-type* <postgresql-role>
  328. postgresql-role make-postgresql-role
  329. postgresql-role?
  330. (name postgresql-role-name) ;string
  331. (permissions postgresql-role-permissions
  332. (default '(createdb login))) ;list
  333. (create-database? postgresql-role-create-database? ;boolean
  334. (default #f)))
  335. (define-record-type* <postgresql-role-configuration>
  336. postgresql-role-configuration make-postgresql-role-configuration
  337. postgresql-role-configuration?
  338. (host postgresql-role-configuration-host ;string
  339. (default "/var/run/postgresql"))
  340. (log postgresql-role-configuration-log ;string
  341. (default "/var/log/postgresql_roles.log"))
  342. (roles postgresql-role-configuration-roles
  343. (default '()))) ;list
  344. (define (postgresql-create-roles config)
  345. ;; See: https://www.postgresql.org/docs/current/sql-createrole.html for the
  346. ;; complete permissions list.
  347. (define (format-permissions permissions)
  348. (let ((dict '(bypassrls createdb createrole login replication superuser)))
  349. (string-join (filter-map (lambda (permission)
  350. (and (member permission dict)
  351. (string-upcase
  352. (symbol->string permission))))
  353. permissions)
  354. " ")))
  355. (define (roles->queries roles)
  356. (apply mixed-text-file "queries"
  357. (append-map
  358. (lambda (role)
  359. (match-record role <postgresql-role>
  360. (name permissions create-database?)
  361. `("SELECT NOT(EXISTS(SELECT 1 FROM pg_catalog.pg_roles WHERE \
  362. rolname = '" ,name "')) as not_exists;\n"
  363. "\\gset\n"
  364. "\\if :not_exists\n"
  365. "CREATE ROLE \"" ,name "\""
  366. " WITH " ,(format-permissions permissions)
  367. ";\n"
  368. ,@(if create-database?
  369. `("CREATE DATABASE \"" ,name "\""
  370. " OWNER \"" ,name "\";\n")
  371. '())
  372. "\\endif\n")))
  373. roles)))
  374. (let ((host (postgresql-role-configuration-host config))
  375. (roles (postgresql-role-configuration-roles config)))
  376. #~(let ((psql #$(file-append postgresql "/bin/psql")))
  377. (list psql "-a" "-h" #$host "-f" #$(roles->queries roles)))))
  378. (define (postgresql-role-shepherd-service config)
  379. (match-record config <postgresql-role-configuration>
  380. (log)
  381. (list (shepherd-service
  382. (requirement '(postgres))
  383. (provision '(postgres-roles))
  384. (one-shot? #t)
  385. (start
  386. #~(lambda args
  387. (let ((pid (fork+exec-command
  388. #$(postgresql-create-roles config)
  389. #:user "postgres"
  390. #:group "postgres"
  391. #:log-file #$log)))
  392. (zero? (cdr (waitpid pid))))))
  393. (documentation "Create PostgreSQL roles.")))))
  394. (define postgresql-role-service-type
  395. (service-type (name 'postgresql-role)
  396. (extensions
  397. (list (service-extension shepherd-root-service-type
  398. postgresql-role-shepherd-service)))
  399. (compose concatenate)
  400. (extend (lambda (config extended-roles)
  401. (match-record config <postgresql-role-configuration>
  402. (host roles)
  403. (postgresql-role-configuration
  404. (host host)
  405. (roles (append roles extended-roles))))))
  406. (default-value (postgresql-role-configuration))
  407. (description "Ensure the specified PostgreSQL roles are
  408. created after the PostgreSQL database is started.")))
  409. ;;;
  410. ;;; Memcached
  411. ;;;
  412. (define-record-type* <memcached-configuration>
  413. memcached-configuration make-memcached-configuration
  414. memcached-configuration?
  415. (memcached memcached-configuration-memcached ;file-like
  416. (default memcached))
  417. (interfaces memcached-configuration-interfaces
  418. (default '("0.0.0.0")))
  419. (tcp-port memcached-configuration-tcp-port
  420. (default 11211))
  421. (udp-port memcached-configuration-udp-port
  422. (default 11211))
  423. (additional-options memcached-configuration-additional-options
  424. (default '())))
  425. (define %memcached-accounts
  426. (list (user-group (name "memcached") (system? #t))
  427. (user-account
  428. (name "memcached")
  429. (group "memcached")
  430. (system? #t)
  431. (comment "Memcached server user")
  432. (home-directory "/var/empty")
  433. (shell (file-append shadow "/sbin/nologin")))))
  434. (define memcached-activation
  435. #~(begin
  436. (use-modules (guix build utils))
  437. (let ((user (getpwnam "memcached")))
  438. (mkdir-p "/var/run/memcached")
  439. (chown "/var/run/memcached"
  440. (passwd:uid user) (passwd:gid user)))))
  441. (define memcached-shepherd-service
  442. (match-lambda
  443. (($ <memcached-configuration> memcached interfaces tcp-port udp-port
  444. additional-options)
  445. (with-imported-modules (source-module-closure
  446. '((gnu build shepherd)))
  447. (list (shepherd-service
  448. (provision '(memcached))
  449. (documentation "Run the Memcached daemon.")
  450. (requirement '(user-processes loopback))
  451. (modules '((gnu build shepherd)))
  452. (start #~(make-forkexec-constructor
  453. `(#$(file-append memcached "/bin/memcached")
  454. "-l" #$(string-join interfaces ",")
  455. "-p" #$(number->string tcp-port)
  456. "-U" #$(number->string udp-port)
  457. "--daemon"
  458. ;; Memcached changes to the memcached user prior to
  459. ;; writing the pid file, so write it to a directory
  460. ;; that memcached owns.
  461. "-P" "/var/run/memcached/pid"
  462. "-u" "memcached"
  463. ,#$@additional-options)
  464. #:log-file "/var/log/memcached"
  465. #:pid-file "/var/run/memcached/pid"))
  466. (stop #~(make-kill-destructor))))))))
  467. (define memcached-service-type
  468. (service-type (name 'memcached)
  469. (extensions
  470. (list (service-extension shepherd-root-service-type
  471. memcached-shepherd-service)
  472. (service-extension activation-service-type
  473. (const memcached-activation))
  474. (service-extension account-service-type
  475. (const %memcached-accounts))))
  476. (default-value (memcached-configuration))
  477. (description "Run @command{memcached}, a daemon that provides
  478. an in-memory caching service, intended for use by dynamic web
  479. applications.")))
  480. ;;;
  481. ;;; MySQL.
  482. ;;;
  483. (define-record-type* <mysql-configuration>
  484. mysql-configuration make-mysql-configuration
  485. mysql-configuration?
  486. (mysql mysql-configuration-mysql (default mariadb))
  487. (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
  488. (port mysql-configuration-port (default 3306))
  489. (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
  490. (extra-content mysql-configuration-extra-content (default ""))
  491. (extra-environment mysql-configuration-extra-environment (default #~'()))
  492. (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
  493. (define %mysql-accounts
  494. (list (user-group
  495. (name "mysql")
  496. (system? #t))
  497. (user-account
  498. (name "mysql")
  499. (group "mysql")
  500. (system? #t)
  501. (home-directory "/var/empty")
  502. (shell (file-append shadow "/sbin/nologin")))))
  503. (define mysql-configuration-file
  504. (match-lambda
  505. (($ <mysql-configuration> mysql bind-address port socket extra-content)
  506. (mixed-text-file "my.cnf" "[mysqld]
  507. datadir=/var/lib/mysql
  508. socket=" socket "
  509. bind-address=" bind-address "
  510. port=" (number->string port) "
  511. " extra-content "
  512. "))))
  513. (define (%mysql-activation config)
  514. "Return an activation gexp for the MySQL or MariaDB database server."
  515. (let ((mysql (mysql-configuration-mysql config))
  516. (my.cnf (mysql-configuration-file config)))
  517. #~(begin
  518. (use-modules (ice-9 popen)
  519. (guix build utils))
  520. (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
  521. (user (getpwnam "mysql"))
  522. (uid (passwd:uid user))
  523. (gid (passwd:gid user))
  524. (datadir "/var/lib/mysql")
  525. (rundir "/run/mysqld"))
  526. (mkdir-p datadir)
  527. (chown datadir uid gid)
  528. (mkdir-p rundir)
  529. (chown rundir uid gid)
  530. ;; Initialize the database when it doesn't exist.
  531. (when (not (file-exists? (string-append datadir "/mysql")))
  532. (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
  533. ;; For MySQL.
  534. (system* mysqld
  535. (string-append "--defaults-file=" #$my.cnf)
  536. "--initialize"
  537. "--user=mysql")
  538. ;; For MariaDB.
  539. ;; XXX: The 'mysql_install_db' script doesn't work directly
  540. ;; due to missing 'mkdir' in PATH.
  541. (let ((p (open-pipe* OPEN_WRITE mysqld
  542. (string-append
  543. "--defaults-file=" #$my.cnf)
  544. "--bootstrap"
  545. "--user=mysql")))
  546. ;; Create the system database, as does by 'mysql_install_db'.
  547. (display "create database mysql;\n" p)
  548. (display "use mysql;\n" p)
  549. (for-each
  550. (lambda (sql)
  551. (call-with-input-file
  552. (string-append #$mysql:lib "/share/mysql/" sql)
  553. (lambda (in) (dump-port in p))))
  554. '("mysql_system_tables.sql"
  555. "mysql_performance_tables.sql"
  556. "mysql_system_tables_data.sql"
  557. "fill_help_tables.sql"))
  558. ;; Remove the anonymous user and disable root access from
  559. ;; remote machines, as does by 'mysql_secure_installation'.
  560. (display "
  561. DELETE FROM user WHERE User='';
  562. DELETE FROM user WHERE User='root' AND
  563. Host NOT IN ('localhost', '127.0.0.1', '::1');
  564. FLUSH PRIVILEGES;
  565. " p)
  566. (close-pipe p))))))))
  567. (define (mysql-shepherd-service config)
  568. (list (shepherd-service
  569. (provision '(mysql))
  570. (documentation "Run the MySQL server.")
  571. (start (let ((mysql (mysql-configuration-mysql config))
  572. (extra-env (mysql-configuration-extra-environment config))
  573. (my.cnf (mysql-configuration-file config)))
  574. #~(make-forkexec-constructor
  575. (list (string-append #$mysql "/bin/mysqld")
  576. (string-append "--defaults-file=" #$my.cnf))
  577. #:user "mysql" #:group "mysql"
  578. #:log-file "/var/log/mysqld.log"
  579. #:environment-variables #$extra-env)))
  580. (stop #~(make-kill-destructor)))))
  581. (define (mysql-upgrade-wrapper mysql socket-file)
  582. ;; The MySQL socket and PID file may appear before the server is ready to
  583. ;; accept connections. Ensure the socket is responsive before attempting
  584. ;; to run the upgrade script.
  585. (program-file
  586. "mysql-upgrade-wrapper"
  587. #~(begin
  588. (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade"))
  589. (timeout 10))
  590. (begin
  591. (let loop ((i 0))
  592. (catch 'system-error
  593. (lambda ()
  594. (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  595. (connect sock AF_UNIX #$socket-file)
  596. (close-port sock)
  597. ;; The socket is ready!
  598. (execl mysql-upgrade mysql-upgrade
  599. (string-append "--socket=" #$socket-file))))
  600. (lambda args
  601. (if (< i timeout)
  602. (begin
  603. (sleep 1)
  604. (loop (+ 1 i)))
  605. ;; No luck, give up.
  606. (throw 'timeout-error
  607. "MySQL server did not appear in time!"))))))))))
  608. (define (mysql-upgrade-shepherd-service config)
  609. (list (shepherd-service
  610. (provision '(mysql-upgrade))
  611. (requirement '(mysql))
  612. (one-shot? #t)
  613. (documentation "Upgrade MySQL database schemas.")
  614. (start (let ((mysql (mysql-configuration-mysql config))
  615. (socket (mysql-configuration-socket config)))
  616. #~(make-forkexec-constructor
  617. (list #$(mysql-upgrade-wrapper mysql socket))
  618. #:user "mysql" #:group "mysql"))))))
  619. (define (mysql-shepherd-services config)
  620. (if (mysql-configuration-auto-upgrade? config)
  621. (append (mysql-shepherd-service config)
  622. (mysql-upgrade-shepherd-service config))
  623. (mysql-shepherd-service config)))
  624. (define mysql-service-type
  625. (service-type
  626. (name 'mysql)
  627. (extensions
  628. (list (service-extension account-service-type
  629. (const %mysql-accounts))
  630. (service-extension activation-service-type
  631. %mysql-activation)
  632. (service-extension shepherd-root-service-type
  633. mysql-shepherd-services)))
  634. (default-value (mysql-configuration))
  635. (description "Run the MySQL or MariaDB database server,
  636. @command{mysqld}.")))
  637. (define-deprecated (mysql-service #:key (config (mysql-configuration)))
  638. mysql-service-type
  639. (service mysql-service-type config))
  640. ;;;
  641. ;;; Redis
  642. ;;;
  643. (define-record-type* <redis-configuration>
  644. redis-configuration make-redis-configuration
  645. redis-configuration?
  646. (redis redis-configuration-redis ;file-like
  647. (default redis))
  648. (bind redis-configuration-bind
  649. (default "127.0.0.1"))
  650. (port redis-configuration-port
  651. (default 6379))
  652. (working-directory redis-configuration-working-directory
  653. (default "/var/lib/redis"))
  654. (config-file redis-configuration-config-file
  655. (default #f)))
  656. (define (default-redis.conf bind port working-directory)
  657. (mixed-text-file "redis.conf"
  658. "bind " bind "\n"
  659. "port " (number->string port) "\n"
  660. "dir " working-directory "\n"
  661. "daemonize no\n"))
  662. (define %redis-accounts
  663. (list (user-group (name "redis") (system? #t))
  664. (user-account
  665. (name "redis")
  666. (group "redis")
  667. (system? #t)
  668. (comment "Redis server user")
  669. (home-directory "/var/empty")
  670. (shell (file-append shadow "/sbin/nologin")))))
  671. (define redis-activation
  672. (match-lambda
  673. (($ <redis-configuration> redis bind port working-directory config-file)
  674. #~(begin
  675. (use-modules (guix build utils)
  676. (ice-9 match))
  677. (let ((user (getpwnam "redis")))
  678. (mkdir-p #$working-directory)
  679. (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
  680. (define redis-shepherd-service
  681. (match-lambda
  682. (($ <redis-configuration> redis bind port working-directory config-file)
  683. (let ((config-file
  684. (or config-file
  685. (default-redis.conf bind port working-directory))))
  686. (list (shepherd-service
  687. (provision '(redis))
  688. (documentation "Run the Redis daemon.")
  689. (requirement '(user-processes syslogd))
  690. (start #~(make-forkexec-constructor
  691. '(#$(file-append redis "/bin/redis-server")
  692. #$config-file)
  693. #:user "redis"
  694. #:group "redis"))
  695. (stop #~(make-kill-destructor))))))))
  696. (define redis-service-type
  697. (service-type (name 'redis)
  698. (extensions
  699. (list (service-extension shepherd-root-service-type
  700. redis-shepherd-service)
  701. (service-extension activation-service-type
  702. redis-activation)
  703. (service-extension account-service-type
  704. (const %redis-accounts))))
  705. (default-value (redis-configuration))
  706. (description "Run Redis, a caching key/value store.")))