diff options
| author | wrobell <wrobell@riseup.net> | 2025-06-18 21:41:36 +0100 |
|---|---|---|
| committer | Sharlatan Hellseher <sharlatanus@gmail.com> | 2025-07-19 01:47:50 +0100 |
| commit | c65da197cfb21ef1d2cb16c78c737a814fb1e43c (patch) | |
| tree | b2ff16fb32d3ce3b3a58eadac2e473b36eb73bec /gnu/services | |
| parent | 27c3c7c4ed6c2ad027d0a45c1a8cd77abe984f41 (diff) | |
services: Add rabbitmq service.
* gnu/services/high-availability.scm (<rabbitmq-configuration>): New record.
(rabbitmq-shepherd-service): New procedure.
(rabbitmq-service-type): New variable.
* gnu/tests/high-availability.scm (run-rabbitmq-test): New procedure.
(%rabbitmq-os, %tests-rabbitmq): New variables.
* doc/gnu.texi (High Availability Services): Document it.
Change-Id: I53e9f2881b6340e1ed314785e4c5529b81381a3b
Co-authored-by: Christopher Baines <mail@cbaines.net>
Reviewed-by: Ludovic Courtès <ludo@gnu.org>
Signed-off-by: Sharlatan Hellseher <sharlatanus@gmail.com>
Diffstat (limited to 'gnu/services')
| -rw-r--r-- | gnu/services/high-availability.scm | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/gnu/services/high-availability.scm b/gnu/services/high-availability.scm new file mode 100644 index 00000000000..0bca1111442 --- /dev/null +++ b/gnu/services/high-availability.scm | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | ||
| 2 | ;;; Copyright © 2018 Christopher Baines <mail@cbaines.net> | ||
| 3 | ;;; Copyright © 2025 Artur Wroblewski <wrobell@riseup.net> | ||
| 4 | ;;; | ||
| 5 | ;;; This file is part of GNU Guix. | ||
| 6 | ;;; | ||
| 7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | ||
| 8 | ;;; under the terms of the GNU General Public License as published by | ||
| 9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | ||
| 10 | ;;; your option) any later version. | ||
| 11 | ;;; | ||
| 12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | ||
| 13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;;; GNU General Public License for more details. | ||
| 16 | ;;; | ||
| 17 | ;;; You should have received a copy of the GNU General Public License | ||
| 18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | (define-module (gnu services high-availability) | ||
| 21 | #:use-module (gnu packages admin) | ||
| 22 | #:use-module (gnu packages high-availability) | ||
| 23 | #:use-module (gnu services) | ||
| 24 | #:use-module (gnu services shepherd) | ||
| 25 | #:use-module (gnu system shadow) | ||
| 26 | #:use-module (guix gexp) | ||
| 27 | #:use-module (guix modules) | ||
| 28 | #:use-module (guix records) | ||
| 29 | #:use-module (ice-9 match) | ||
| 30 | #:use-module (ice-9 format) | ||
| 31 | |||
| 32 | #:export (rabbitmq-configuration rabbitmq-configuration? | ||
| 33 | rabbitmq-configuration-rabbitmq | ||
| 34 | rabbitmq-configuration-config-file | ||
| 35 | rabbitmq-configuration-plugins | ||
| 36 | rabbitmq-service-type)) | ||
| 37 | |||
| 38 | ;; By default, start on local ipv4 and ipv6 interfaces only, see also: | ||
| 39 | ;; | ||
| 40 | ;; https://www.rabbitmq.com/docs/networking | ||
| 41 | ;; | ||
| 42 | ;; NOTE: How to enable plugins to listen on localhost only? | ||
| 43 | (define %default-rabbitmq-config-file | ||
| 44 | (plain-file "rabbitmq.conf" " | ||
| 45 | listeners.tcp.1 = 127.0.0.1:5672 | ||
| 46 | listeners.tcp.2 = ::1:5672 | ||
| 47 | ")) | ||
| 48 | |||
| 49 | (define-record-type* <rabbitmq-configuration> rabbitmq-configuration | ||
| 50 | make-rabbitmq-configuration | ||
| 51 | rabbitmq-configuration? | ||
| 52 | (rabbitmq rabbitmq-configuration-rabbitmq | ||
| 53 | (default rabbitmq)) | ||
| 54 | (config-file rabbitmq-configuration-config-file | ||
| 55 | (default %default-rabbitmq-config-file)) | ||
| 56 | ;; It can be a mnesia database or a khepri database, so use "data" instead | ||
| 57 | ;; of the traditional "mnesia". | ||
| 58 | (data-directory rabbitmq-configuration-data-directory | ||
| 59 | (default "/var/lib/rabbitmq/data")) | ||
| 60 | (plugins rabbitmq-configuration-plugins | ||
| 61 | (default '()))) | ||
| 62 | |||
| 63 | (define %rabbitmq-accounts | ||
| 64 | (list (user-group | ||
| 65 | (name "rabbitmq") | ||
| 66 | (system? #t)) | ||
| 67 | (user-account | ||
| 68 | (name "rabbitmq") | ||
| 69 | (group "rabbitmq") | ||
| 70 | (system? #t) | ||
| 71 | (comment "RabbitMQ server user") | ||
| 72 | (home-directory "/var/lib/rabbitmq") | ||
| 73 | (shell (file-append shadow "/sbin/nologin"))))) | ||
| 74 | |||
| 75 | (define (rabbitmq-activation config) | ||
| 76 | (let* ((data-directory (rabbitmq-configuration-data-directory config)) | ||
| 77 | (plugins (string-join (rabbitmq-configuration-plugins config) ","))) | ||
| 78 | (with-imported-modules '((guix build utils)) | ||
| 79 | #~(begin | ||
| 80 | (use-modules (guix build utils)) | ||
| 81 | (let ((user (getpwnam "rabbitmq")) | ||
| 82 | (srv-directories (list | ||
| 83 | "/var/lib/rabbitmq" | ||
| 84 | "/var/log/rabbitmq" | ||
| 85 | "/var/run/rabbitmq" | ||
| 86 | #$data-directory))) | ||
| 87 | (for-each (lambda (directory) | ||
| 88 | (mkdir-p directory) | ||
| 89 | (chown directory | ||
| 90 | (passwd:uid user) | ||
| 91 | (passwd:gid user))) | ||
| 92 | srv-directories) | ||
| 93 | |||
| 94 | ;; Create file with the enabled plugins. | ||
| 95 | (with-output-to-file (string-append #$data-directory | ||
| 96 | "/enabled_plugins") | ||
| 97 | (lambda () (display (format #f "[~a]." #$plugins)))) | ||
| 98 | (chown (string-append #$data-directory "/enabled_plugins") | ||
| 99 | (passwd:uid user) | ||
| 100 | (passwd:gid user))))))) | ||
| 101 | |||
| 102 | (define (rabbitmq-shepherd-service config) | ||
| 103 | (match-record config <rabbitmq-configuration> | ||
| 104 | (rabbitmq data-directory config-file plugins) | ||
| 105 | (with-imported-modules | ||
| 106 | (source-module-closure '((gnu build shepherd))) | ||
| 107 | (list | ||
| 108 | (shepherd-service | ||
| 109 | (provision '(rabbitmq)) | ||
| 110 | (documentation "Run the RabbitMQ daemon.") | ||
| 111 | (requirement '(user-processes loopback)) | ||
| 112 | (modules '((gnu build shepherd))) | ||
| 113 | (start | ||
| 114 | #~(make-forkexec-constructor | ||
| 115 | `(#$(file-append rabbitmq "/sbin/rabbitmq-server")) | ||
| 116 | #:pid-file "/var/run/rabbitmq/pid" | ||
| 117 | #:user "rabbitmq" | ||
| 118 | #:group "rabbitmq" | ||
| 119 | #:environment-variables | ||
| 120 | (append | ||
| 121 | (list | ||
| 122 | (string-append "RABBITMQ_CONFIG_FILE=" #$config-file) | ||
| 123 | "RABBITMQ_PID_FILE=/var/run/rabbitmq/pid" | ||
| 124 | "RABBITMQ_CONF_ENV_FILE=/run/current-system/profile/etc/rabbitmq/rabbitmq-env.conf" | ||
| 125 | (string-append | ||
| 126 | "RABBITMQ_ENABLED_PLUGINS_FILE=" | ||
| 127 | #$data-directory | ||
| 128 | "/enabled_plugins") | ||
| 129 | (string-append | ||
| 130 | "RABBITMQ_MNESIA_BASE=" | ||
| 131 | #$data-directory) | ||
| 132 | "RABBITMQ_LOG_BASE=/var/log/rabbitmq") | ||
| 133 | (environ)))) | ||
| 134 | (stop #~(make-kill-destructor))))))) | ||
| 135 | |||
| 136 | (define rabbitmq-service-type | ||
| 137 | (service-type (name 'rabbitmq) | ||
| 138 | (description "Run the RabbitMQ message broker service.") | ||
| 139 | (extensions (list (service-extension | ||
| 140 | shepherd-root-service-type | ||
| 141 | rabbitmq-shepherd-service) | ||
| 142 | (service-extension activation-service-type | ||
| 143 | rabbitmq-activation) | ||
| 144 | (service-extension account-service-type | ||
| 145 | (const %rabbitmq-accounts)))) | ||
| 146 | (default-value (rabbitmq-configuration)))) | ||
