databases.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. ;;; 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. (define-deprecated (postgresql-service #:key (postgresql postgresql)
  307. (port 5432)
  308. (locale "en_US.utf8")
  309. (config-file (postgresql-config-file))
  310. (data-directory
  311. "/var/lib/postgresql/data")
  312. (extension-packages '()))
  313. postgresql-service-type
  314. "Return a service that runs @var{postgresql}, the PostgreSQL database
  315. server.
  316. The PostgreSQL daemon loads its runtime configuration from @var{config-file}
  317. and stores the database cluster in @var{data-directory}."
  318. (service postgresql-service-type
  319. (postgresql-configuration
  320. (postgresql postgresql)
  321. (port port)
  322. (locale locale)
  323. (config-file config-file)
  324. (data-directory data-directory)
  325. (extension-packages extension-packages))))
  326. (define-record-type* <postgresql-role>
  327. postgresql-role make-postgresql-role
  328. postgresql-role?
  329. (name postgresql-role-name) ;string
  330. (permissions postgresql-role-permissions
  331. (default '(createdb login))) ;list
  332. (create-database? postgresql-role-create-database? ;boolean
  333. (default #f)))
  334. (define-record-type* <postgresql-role-configuration>
  335. postgresql-role-configuration make-postgresql-role-configuration
  336. postgresql-role-configuration?
  337. (host postgresql-role-configuration-host ;string
  338. (default "/var/run/postgresql"))
  339. (log postgresql-role-configuration-log ;string
  340. (default "/var/log/postgresql_roles.log"))
  341. (roles postgresql-role-configuration-roles
  342. (default '()))) ;list
  343. (define (postgresql-create-roles config)
  344. ;; See: https://www.postgresql.org/docs/current/sql-createrole.html for the
  345. ;; complete permissions list.
  346. (define (format-permissions permissions)
  347. (let ((dict '(bypassrls createdb createrole login replication superuser)))
  348. (string-join (filter-map (lambda (permission)
  349. (and (member permission dict)
  350. (string-upcase
  351. (symbol->string permission))))
  352. permissions)
  353. " ")))
  354. (define (roles->queries roles)
  355. (apply mixed-text-file "queries"
  356. (append-map
  357. (lambda (role)
  358. (match-record role <postgresql-role>
  359. (name permissions create-database?)
  360. `("SELECT NOT(EXISTS(SELECT 1 FROM pg_catalog.pg_roles WHERE \
  361. rolname = '" ,name "')) as not_exists;\n"
  362. "\\gset\n"
  363. "\\if :not_exists\n"
  364. "CREATE ROLE \"" ,name "\""
  365. " WITH " ,(format-permissions permissions)
  366. ";\n"
  367. ,@(if create-database?
  368. `("CREATE DATABASE \"" ,name "\""
  369. " OWNER \"" ,name "\";\n")
  370. '())
  371. "\\endif\n")))
  372. roles)))
  373. (let ((host (postgresql-role-configuration-host config))
  374. (roles (postgresql-role-configuration-roles config)))
  375. #~(let ((psql #$(file-append postgresql "/bin/psql")))
  376. (list psql "-a" "-h" #$host "-f" #$(roles->queries roles)))))
  377. (define (postgresql-role-shepherd-service config)
  378. (match-record config <postgresql-role-configuration>
  379. (log)
  380. (list (shepherd-service
  381. (requirement '(postgres))
  382. (provision '(postgres-roles))
  383. (one-shot? #t)
  384. (start
  385. #~(lambda args
  386. (let ((pid (fork+exec-command
  387. #$(postgresql-create-roles config)
  388. #:user "postgres"
  389. #:group "postgres"
  390. #:log-file #$log)))
  391. (zero? (cdr (waitpid pid))))))
  392. (documentation "Create PostgreSQL roles.")))))
  393. (define postgresql-role-service-type
  394. (service-type (name 'postgresql-role)
  395. (extensions
  396. (list (service-extension shepherd-root-service-type
  397. postgresql-role-shepherd-service)))
  398. (compose concatenate)
  399. (extend (lambda (config extended-roles)
  400. (match-record config <postgresql-role-configuration>
  401. (host roles)
  402. (postgresql-role-configuration
  403. (host host)
  404. (roles (append roles extended-roles))))))
  405. (default-value (postgresql-role-configuration))
  406. (description "Ensure the specified PostgreSQL roles are
  407. created after the PostgreSQL database is started.")))
  408. ;;;
  409. ;;; Memcached
  410. ;;;
  411. (define-record-type* <memcached-configuration>
  412. memcached-configuration make-memcached-configuration
  413. memcached-configuration?
  414. (memcached memcached-configuration-memcached ;file-like
  415. (default memcached))
  416. (interfaces memcached-configuration-interfaces
  417. (default '("0.0.0.0")))
  418. (tcp-port memcached-configuration-tcp-port
  419. (default 11211))
  420. (udp-port memcached-configuration-udp-port
  421. (default 11211))
  422. (additional-options memcached-configuration-additional-options
  423. (default '())))
  424. (define %memcached-accounts
  425. (list (user-group (name "memcached") (system? #t))
  426. (user-account
  427. (name "memcached")
  428. (group "memcached")
  429. (system? #t)
  430. (comment "Memcached server user")
  431. (home-directory "/var/empty")
  432. (shell (file-append shadow "/sbin/nologin")))))
  433. (define memcached-activation
  434. #~(begin
  435. (use-modules (guix build utils))
  436. (let ((user (getpwnam "memcached")))
  437. (mkdir-p "/var/run/memcached")
  438. (chown "/var/run/memcached"
  439. (passwd:uid user) (passwd:gid user)))))
  440. (define memcached-shepherd-service
  441. (match-lambda
  442. (($ <memcached-configuration> memcached interfaces tcp-port udp-port
  443. additional-options)
  444. (with-imported-modules (source-module-closure
  445. '((gnu build shepherd)))
  446. (list (shepherd-service
  447. (provision '(memcached))
  448. (documentation "Run the Memcached daemon.")
  449. (requirement '(user-processes loopback))
  450. (modules '((gnu build shepherd)))
  451. (start #~(make-forkexec-constructor
  452. `(#$(file-append memcached "/bin/memcached")
  453. "-l" #$(string-join interfaces ",")
  454. "-p" #$(number->string tcp-port)
  455. "-U" #$(number->string udp-port)
  456. "--daemon"
  457. ;; Memcached changes to the memcached user prior to
  458. ;; writing the pid file, so write it to a directory
  459. ;; that memcached owns.
  460. "-P" "/var/run/memcached/pid"
  461. "-u" "memcached"
  462. ,#$@additional-options)
  463. #:log-file "/var/log/memcached"
  464. #:pid-file "/var/run/memcached/pid"))
  465. (stop #~(make-kill-destructor))))))))
  466. (define memcached-service-type
  467. (service-type (name 'memcached)
  468. (extensions
  469. (list (service-extension shepherd-root-service-type
  470. memcached-shepherd-service)
  471. (service-extension activation-service-type
  472. (const memcached-activation))
  473. (service-extension account-service-type
  474. (const %memcached-accounts))))
  475. (default-value (memcached-configuration))))
  476. ;;;
  477. ;;; MySQL.
  478. ;;;
  479. (define-record-type* <mysql-configuration>
  480. mysql-configuration make-mysql-configuration
  481. mysql-configuration?
  482. (mysql mysql-configuration-mysql (default mariadb))
  483. (bind-address mysql-configuration-bind-address (default "127.0.0.1"))
  484. (port mysql-configuration-port (default 3306))
  485. (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock"))
  486. (extra-content mysql-configuration-extra-content (default ""))
  487. (extra-environment mysql-configuration-extra-environment (default #~'()))
  488. (auto-upgrade? mysql-configuration-auto-upgrade? (default #t)))
  489. (define %mysql-accounts
  490. (list (user-group
  491. (name "mysql")
  492. (system? #t))
  493. (user-account
  494. (name "mysql")
  495. (group "mysql")
  496. (system? #t)
  497. (home-directory "/var/empty")
  498. (shell (file-append shadow "/sbin/nologin")))))
  499. (define mysql-configuration-file
  500. (match-lambda
  501. (($ <mysql-configuration> mysql bind-address port socket extra-content)
  502. (mixed-text-file "my.cnf" "[mysqld]
  503. datadir=/var/lib/mysql
  504. socket=" socket "
  505. bind-address=" bind-address "
  506. port=" (number->string port) "
  507. " extra-content "
  508. "))))
  509. (define (%mysql-activation config)
  510. "Return an activation gexp for the MySQL or MariaDB database server."
  511. (let ((mysql (mysql-configuration-mysql config))
  512. (my.cnf (mysql-configuration-file config)))
  513. #~(begin
  514. (use-modules (ice-9 popen)
  515. (guix build utils))
  516. (let* ((mysqld (string-append #$mysql "/bin/mysqld"))
  517. (user (getpwnam "mysql"))
  518. (uid (passwd:uid user))
  519. (gid (passwd:gid user))
  520. (datadir "/var/lib/mysql")
  521. (rundir "/run/mysqld"))
  522. (mkdir-p datadir)
  523. (chown datadir uid gid)
  524. (mkdir-p rundir)
  525. (chown rundir uid gid)
  526. ;; Initialize the database when it doesn't exist.
  527. (when (not (file-exists? (string-append datadir "/mysql")))
  528. (if (string-prefix? "mysql-" (strip-store-file-name #$mysql))
  529. ;; For MySQL.
  530. (system* mysqld
  531. (string-append "--defaults-file=" #$my.cnf)
  532. "--initialize"
  533. "--user=mysql")
  534. ;; For MariaDB.
  535. ;; XXX: The 'mysql_install_db' script doesn't work directly
  536. ;; due to missing 'mkdir' in PATH.
  537. (let ((p (open-pipe* OPEN_WRITE mysqld
  538. (string-append
  539. "--defaults-file=" #$my.cnf)
  540. "--bootstrap"
  541. "--user=mysql")))
  542. ;; Create the system database, as does by 'mysql_install_db'.
  543. (display "create database mysql;\n" p)
  544. (display "use mysql;\n" p)
  545. (for-each
  546. (lambda (sql)
  547. (call-with-input-file
  548. (string-append #$mysql:lib "/share/mysql/" sql)
  549. (lambda (in) (dump-port in p))))
  550. '("mysql_system_tables.sql"
  551. "mysql_performance_tables.sql"
  552. "mysql_system_tables_data.sql"
  553. "fill_help_tables.sql"))
  554. ;; Remove the anonymous user and disable root access from
  555. ;; remote machines, as does by 'mysql_secure_installation'.
  556. (display "
  557. DELETE FROM user WHERE User='';
  558. DELETE FROM user WHERE User='root' AND
  559. Host NOT IN ('localhost', '127.0.0.1', '::1');
  560. FLUSH PRIVILEGES;
  561. " p)
  562. (close-pipe p))))))))
  563. (define (mysql-shepherd-service config)
  564. (list (shepherd-service
  565. (provision '(mysql))
  566. (documentation "Run the MySQL server.")
  567. (start (let ((mysql (mysql-configuration-mysql config))
  568. (extra-env (mysql-configuration-extra-environment config))
  569. (my.cnf (mysql-configuration-file config)))
  570. #~(make-forkexec-constructor
  571. (list (string-append #$mysql "/bin/mysqld")
  572. (string-append "--defaults-file=" #$my.cnf))
  573. #:user "mysql" #:group "mysql"
  574. #:log-file "/var/log/mysqld.log"
  575. #:environment-variables #$extra-env)))
  576. (stop #~(make-kill-destructor)))))
  577. (define (mysql-upgrade-wrapper mysql socket-file)
  578. ;; The MySQL socket and PID file may appear before the server is ready to
  579. ;; accept connections. Ensure the socket is responsive before attempting
  580. ;; to run the upgrade script.
  581. (program-file
  582. "mysql-upgrade-wrapper"
  583. #~(begin
  584. (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade"))
  585. (timeout 10))
  586. (begin
  587. (let loop ((i 0))
  588. (catch 'system-error
  589. (lambda ()
  590. (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  591. (connect sock AF_UNIX #$socket-file)
  592. (close-port sock)
  593. ;; The socket is ready!
  594. (execl mysql-upgrade mysql-upgrade
  595. (string-append "--socket=" #$socket-file))))
  596. (lambda args
  597. (if (< i timeout)
  598. (begin
  599. (sleep 1)
  600. (loop (+ 1 i)))
  601. ;; No luck, give up.
  602. (throw 'timeout-error
  603. "MySQL server did not appear in time!"))))))))))
  604. (define (mysql-upgrade-shepherd-service config)
  605. (list (shepherd-service
  606. (provision '(mysql-upgrade))
  607. (requirement '(mysql))
  608. (one-shot? #t)
  609. (documentation "Upgrade MySQL database schemas.")
  610. (start (let ((mysql (mysql-configuration-mysql config))
  611. (socket (mysql-configuration-socket config)))
  612. #~(make-forkexec-constructor
  613. (list #$(mysql-upgrade-wrapper mysql socket))
  614. #:user "mysql" #:group "mysql"))))))
  615. (define (mysql-shepherd-services config)
  616. (if (mysql-configuration-auto-upgrade? config)
  617. (append (mysql-shepherd-service config)
  618. (mysql-upgrade-shepherd-service config))
  619. (mysql-shepherd-service config)))
  620. (define mysql-service-type
  621. (service-type
  622. (name 'mysql)
  623. (extensions
  624. (list (service-extension account-service-type
  625. (const %mysql-accounts))
  626. (service-extension activation-service-type
  627. %mysql-activation)
  628. (service-extension shepherd-root-service-type
  629. mysql-shepherd-services)))
  630. (default-value (mysql-configuration))))
  631. (define-deprecated (mysql-service #:key (config (mysql-configuration)))
  632. mysql-service-type
  633. (service mysql-service-type config))
  634. ;;;
  635. ;;; Redis
  636. ;;;
  637. (define-record-type* <redis-configuration>
  638. redis-configuration make-redis-configuration
  639. redis-configuration?
  640. (redis redis-configuration-redis ;file-like
  641. (default redis))
  642. (bind redis-configuration-bind
  643. (default "127.0.0.1"))
  644. (port redis-configuration-port
  645. (default 6379))
  646. (working-directory redis-configuration-working-directory
  647. (default "/var/lib/redis"))
  648. (config-file redis-configuration-config-file
  649. (default #f)))
  650. (define (default-redis.conf bind port working-directory)
  651. (mixed-text-file "redis.conf"
  652. "bind " bind "\n"
  653. "port " (number->string port) "\n"
  654. "dir " working-directory "\n"
  655. "daemonize no\n"))
  656. (define %redis-accounts
  657. (list (user-group (name "redis") (system? #t))
  658. (user-account
  659. (name "redis")
  660. (group "redis")
  661. (system? #t)
  662. (comment "Redis server user")
  663. (home-directory "/var/empty")
  664. (shell (file-append shadow "/sbin/nologin")))))
  665. (define redis-activation
  666. (match-lambda
  667. (($ <redis-configuration> redis bind port working-directory config-file)
  668. #~(begin
  669. (use-modules (guix build utils)
  670. (ice-9 match))
  671. (let ((user (getpwnam "redis")))
  672. (mkdir-p #$working-directory)
  673. (chown #$working-directory (passwd:uid user) (passwd:gid user)))))))
  674. (define redis-shepherd-service
  675. (match-lambda
  676. (($ <redis-configuration> redis bind port working-directory config-file)
  677. (let ((config-file
  678. (or config-file
  679. (default-redis.conf bind port working-directory))))
  680. (list (shepherd-service
  681. (provision '(redis))
  682. (documentation "Run the Redis daemon.")
  683. (requirement '(user-processes syslogd))
  684. (start #~(make-forkexec-constructor
  685. '(#$(file-append redis "/bin/redis-server")
  686. #$config-file)
  687. #:user "redis"
  688. #:group "redis"))
  689. (stop #~(make-kill-destructor))))))))
  690. (define redis-service-type
  691. (service-type (name 'redis)
  692. (extensions
  693. (list (service-extension shepherd-root-service-type
  694. redis-shepherd-service)
  695. (service-extension activation-service-type
  696. redis-activation)
  697. (service-extension account-service-type
  698. (const %redis-accounts))))
  699. (default-value (redis-configuration))))