1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
3 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
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.
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.
17 ;;; You should have received a copy of thye GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services admin)
21 #:use-module (gnu packages admin)
22 #:use-module (gnu packages base)
23 #:use-module (gnu packages logging)
24 #:use-module (gnu services)
25 #:use-module (gnu services mcron)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu services web)
28 #:use-module (gnu system shadow)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:use-module (guix packages)
32 #:use-module (guix records)
33 #:use-module (srfi srfi-1)
34 #:use-module (ice-9 vlist)
35 #:use-module (ice-9 match)
36 #:export (%default-rotations
41 log-rotation-frequency
44 log-rotation-post-rotate
47 rottlog-configuration?
51 <tailon-configuration-file>
52 tailon-configuration-file
53 tailon-configuration-file?
54 tailon-configuration-file-files
55 tailon-configuration-file-bind
56 tailon-configuration-file-relative-root
57 tailon-configuration-file-allow-transfers?
58 tailon-configuration-file-follow-names?
59 tailon-configuration-file-tail-lines
60 tailon-configuration-file-allowed-commands
61 tailon-configuration-file-debug?
63 <tailon-configuration>
66 tailon-configuration-config-file
67 tailon-configuration-package
73 ;;; This module implements configuration of rottlog by writing
74 ;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
77 ;;; (service rottlog-service-type)
81 (define-record-type* <log-rotation> log-rotation make-log-rotation
83 (files log-rotation-files) ;list of strings
84 (frequency log-rotation-frequency ;symbol
86 (post-rotate log-rotation-post-rotate ;#f | gexp
88 (options log-rotation-options ;list of strings
91 (define %rotated-files
92 ;; Syslog files subject to rotation.
93 '("/var/log/messages" "/var/log/secure" "/var/log/maillog"))
95 (define %default-rotations
96 (list (log-rotation ;syslog files
97 (files %rotated-files)
99 ;; Restart syslogd after rotation.
100 (options '("sharedscripts"))
101 (post-rotate #~(let ((pid (call-with-input-file "/var/run/syslog.pid"
105 (files '("/var/log/shepherd.log" "/var/log/guix-daemon.log")))))
107 (define (log-rotation->config rotation)
108 "Return a string-valued gexp representing the rottlog configuration snippet
111 (let ((post (log-rotation-post-rotate rotation)))
113 (program-file "rottlog-post-rotate.scm" post))))
115 #~(let ((post #$post-rotate))
116 (string-append (string-join '#$(log-rotation-files rotation) ",")
118 #$(string-join (log-rotation-options rotation)
121 (string-append "\n postrotate\n " post
126 (define (log-rotations->/etc-entries rotations)
127 "Return the list of /etc entries for ROTATIONS, a list of <log-rotation>."
128 (define (frequency-file frequency rotations)
129 (computed-file (string-append "rottlog." (symbol->string frequency))
130 #~(call-with-output-file #$output
132 (for-each (lambda (str)
134 (list #$@(map log-rotation->config
137 (let* ((frequencies (delete-duplicates
138 (map log-rotation-frequency rotations)))
139 (table (fold (lambda (rotation table)
140 (vhash-consq (log-rotation-frequency rotation)
144 (map (lambda (frequency)
145 `(,(symbol->string frequency)
146 ,(frequency-file frequency
147 (vhash-foldq* cons '() frequency table))))
150 (define (default-jobs rottlog)
151 (list #~(job '(next-hour '(0)) ;midnight
153 (system* #$(file-append rottlog "/sbin/rottlog"))))
154 #~(job '(next-hour '(12)) ;noon
156 (system* #$(file-append rottlog "/sbin/rottlog"))))))
158 (define-record-type* <rottlog-configuration>
159 rottlog-configuration make-rottlog-configuration
160 rottlog-configuration?
161 (rottlog rottlog-rottlog ;package
163 (rc-file rottlog-rc-file ;file-like
164 (default (file-append rottlog "/etc/rc")))
165 (rotations rottlog-rotations ;list of <log-rotation>
166 (default %default-rotations))
167 (jobs rottlog-jobs ;list of <mcron-job>
170 (define (rottlog-etc config)
172 ,(file-union "rottlog"
173 (cons `("rc" ,(rottlog-rc-file config))
174 (log-rotations->/etc-entries
175 (rottlog-rotations config)))))))
177 (define (rottlog-jobs-or-default config)
178 (or (rottlog-jobs config)
179 (default-jobs (rottlog-rottlog config))))
181 (define rottlog-service-type
184 (extensions (list (service-extension etc-service-type rottlog-etc)
185 (service-extension mcron-service-type
186 rottlog-jobs-or-default)
188 ;; Add Rottlog to the global profile so users can access
189 ;; the documentation.
190 (service-extension profile-service-type
191 (compose list rottlog-rottlog))))
192 (compose concatenate)
193 (extend (lambda (config rotations)
194 (rottlog-configuration
196 (rotations (append (rottlog-rotations config)
198 (default-value (rottlog-configuration))))
205 (define-record-type* <tailon-configuration-file>
206 tailon-configuration-file make-tailon-configuration-file
207 tailon-configuration-file?
208 (files tailon-configuration-file-files
209 (default '("/var/log")))
210 (bind tailon-configuration-file-bind
211 (default "localhost:8080"))
212 (relative-root tailon-configuration-file-relative-root
214 (allow-transfers? tailon-configuration-file-allow-transfers?
216 (follow-names? tailon-configuration-file-follow-names?
218 (tail-lines tailon-configuration-file-tail-lines
220 (allowed-commands tailon-configuration-file-allowed-commands
221 (default '("tail" "grep" "awk")))
222 (debug? tailon-configuration-file-debug?
225 (define (tailon-configuration-files-string files)
235 (simple-format #f "'~A'" x))
238 (cons (simple-format #f "'~A':" (car x))
240 (lambda (x) (simple-format #f " - '~A'" x))
247 (define-gexp-compiler (tailon-configuration-file-compiler
248 (file <tailon-configuration-file>) system target)
250 (($ <tailon-configuration-file> files bind relative-root
251 allow-transfers? follow-names?
252 tail-lines allowed-commands debug?)
259 ((key . value) (string-append key ": " value "\n")))
261 `(("files" . ,(tailon-configuration-files-string files))
263 ("relative-root" . ,relative-root)
264 ("allow-transfers" . ,(if allow-transfers? "true" "false"))
265 ("follow-names" . ,(if follow-names? "true" "false"))
266 ("tail-lines" . ,(number->string tail-lines))
267 ("commands" . ,(string-append "["
268 (string-join allowed-commands ", ")
270 ,@(if debug? '(("debug" . "true")) '()))))))))
272 (define-record-type* <tailon-configuration>
273 tailon-configuration make-tailon-configuration
274 tailon-configuration?
275 (config-file tailon-configuration-config-file
276 (default (tailon-configuration-file)))
277 (package tailon-configuration-package
280 (define tailon-shepherd-service
282 (($ <tailon-configuration> config-file package)
283 (list (shepherd-service
284 (provision '(tailon))
285 (documentation "Run the tailon daemon.")
286 (start #~(make-forkexec-constructor
287 `(,(string-append #$package "/bin/tailon")
291 (stop #~(make-kill-destructor)))))))
293 (define %tailon-accounts
294 (list (user-group (name "tailon") (system? #t))
300 (home-directory "/var/empty")
301 (shell (file-append shadow "/sbin/nologin")))))
303 (define tailon-service-type
307 (list (service-extension shepherd-root-service-type
308 tailon-shepherd-service)
309 (service-extension account-service-type
310 (const %tailon-accounts))))
311 (compose concatenate)
312 (extend (lambda (parameter files)
313 (tailon-configuration
316 (let ((old-config-file
317 (tailon-configuration-config-file parameter)))
318 (tailon-configuration-file
319 (inherit old-config-file)
320 (files (append (tailon-configuration-file-files old-config-file)
322 (default-value (tailon-configuration))))
324 ;;; admin.scm ends here