databases.scm 29 KB

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