admin.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; 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 certs)
  23. #:use-module (gnu packages package-management)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services mcron)
  26. #:use-module (gnu services shepherd)
  27. #:use-module (guix gexp)
  28. #:use-module (guix modules)
  29. #:use-module (guix packages)
  30. #:use-module (guix records)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (ice-9 vlist)
  33. #:export (%default-rotations
  34. %rotated-files
  35. log-rotation
  36. log-rotation?
  37. log-rotation-frequency
  38. log-rotation-files
  39. log-rotation-options
  40. log-rotation-post-rotate
  41. rottlog-configuration
  42. rottlog-configuration?
  43. rottlog-service
  44. rottlog-service-type
  45. unattended-upgrade-service-type
  46. unattended-upgrade-configuration
  47. unattended-upgrade-configuration?
  48. unattended-upgrade-configuration-operating-system-file
  49. unattended-upgrade-configuration-channels
  50. unattended-upgrade-configuration-schedule
  51. unattended-upgrade-configuration-services-to-restart
  52. unattended-upgrade-configuration-system-expiration
  53. unattended-upgrade-configuration-maximum-duration
  54. unattended-upgrade-configuration-log-file))
  55. ;;; Commentary:
  56. ;;;
  57. ;;; This module implements configuration of rottlog by writing
  58. ;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
  59. ;;;
  60. ;;; (mcron-service)
  61. ;;; (service rottlog-service-type)
  62. ;;;
  63. ;;; Code:
  64. (define-record-type* <log-rotation> log-rotation make-log-rotation
  65. log-rotation?
  66. (files log-rotation-files) ;list of strings
  67. (frequency log-rotation-frequency ;symbol
  68. (default 'weekly))
  69. (post-rotate log-rotation-post-rotate ;#f | gexp
  70. (default #f))
  71. (options log-rotation-options ;list of strings
  72. (default '())))
  73. (define %rotated-files
  74. ;; Syslog files subject to rotation.
  75. '("/var/log/messages" "/var/log/secure" "/var/log/debug"
  76. "/var/log/maillog" "/var/log/mcron.log"))
  77. (define %default-rotations
  78. (list (log-rotation ;syslog files
  79. (files %rotated-files)
  80. (options '(;; Run post-rotate once per rotation
  81. "sharedscripts"
  82. ;; Append .gz to rotated files
  83. "storefile @FILENAME.@COMP_EXT"))
  84. ;; Restart syslogd after rotation.
  85. (post-rotate #~(let ((pid (call-with-input-file "/var/run/syslog.pid"
  86. read)))
  87. (kill pid SIGHUP))))
  88. (log-rotation
  89. (files '("/var/log/guix-daemon.log")))))
  90. (define (log-rotation->config rotation)
  91. "Return a string-valued gexp representing the rottlog configuration snippet
  92. for ROTATION."
  93. (define post-rotate
  94. (let ((post (log-rotation-post-rotate rotation)))
  95. (and post
  96. (program-file "rottlog-post-rotate.scm" post))))
  97. #~(let ((post #$post-rotate))
  98. (string-append (string-join '#$(log-rotation-files rotation) ",")
  99. " {"
  100. #$(string-join (log-rotation-options rotation)
  101. "\n " 'prefix)
  102. (if post
  103. (string-append "\n postrotate\n " post
  104. "\n endscript\n")
  105. "")
  106. "\n}\n")))
  107. (define (log-rotations->/etc-entries rotations)
  108. "Return the list of /etc entries for ROTATIONS, a list of <log-rotation>."
  109. (define (frequency-file frequency rotations)
  110. (computed-file (string-append "rottlog." (symbol->string frequency))
  111. #~(call-with-output-file #$output
  112. (lambda (port)
  113. (for-each (lambda (str)
  114. (display str port))
  115. (list #$@(map log-rotation->config
  116. rotations)))))))
  117. (let* ((frequencies (delete-duplicates
  118. (map log-rotation-frequency rotations)))
  119. (table (fold (lambda (rotation table)
  120. (vhash-consq (log-rotation-frequency rotation)
  121. rotation table))
  122. vlist-null
  123. rotations)))
  124. (map (lambda (frequency)
  125. `(,(symbol->string frequency)
  126. ,(frequency-file frequency
  127. (vhash-foldq* cons '() frequency table))))
  128. frequencies)))
  129. (define (default-jobs rottlog)
  130. (list #~(job '(next-hour '(0)) ;midnight
  131. #$(file-append rottlog "/sbin/rottlog"))
  132. #~(job '(next-hour '(12)) ;noon
  133. #$(file-append rottlog "/sbin/rottlog"))))
  134. (define-record-type* <rottlog-configuration>
  135. rottlog-configuration make-rottlog-configuration
  136. rottlog-configuration?
  137. (rottlog rottlog-rottlog ;package
  138. (default rottlog))
  139. (rc-file rottlog-rc-file ;file-like
  140. (default (file-append rottlog "/etc/rc")))
  141. (rotations rottlog-rotations ;list of <log-rotation>
  142. (default %default-rotations))
  143. (jobs rottlog-jobs ;list of <mcron-job>
  144. (default #f)))
  145. (define (rottlog-etc config)
  146. `(("rottlog"
  147. ,(file-union "rottlog"
  148. (cons `("rc" ,(rottlog-rc-file config))
  149. (log-rotations->/etc-entries
  150. (rottlog-rotations config)))))))
  151. (define (rottlog-jobs-or-default config)
  152. (or (rottlog-jobs config)
  153. (default-jobs (rottlog-rottlog config))))
  154. (define rottlog-service-type
  155. (service-type
  156. (name 'rottlog)
  157. (description
  158. "Periodically rotate log files using GNU@tie{}Rottlog and GNU@tie{}mcron.
  159. Old log files are removed or compressed according to the configuration.")
  160. (extensions (list (service-extension etc-service-type rottlog-etc)
  161. (service-extension mcron-service-type
  162. rottlog-jobs-or-default)
  163. ;; Add Rottlog to the global profile so users can access
  164. ;; the documentation.
  165. (service-extension profile-service-type
  166. (compose list rottlog-rottlog))))
  167. (compose concatenate)
  168. (extend (lambda (config rotations)
  169. (rottlog-configuration
  170. (inherit config)
  171. (rotations (append (rottlog-rotations config)
  172. rotations)))))
  173. (default-value (rottlog-configuration))))
  174. ;;;
  175. ;;; Unattended upgrade.
  176. ;;;
  177. (define-record-type* <unattended-upgrade-configuration>
  178. unattended-upgrade-configuration make-unattended-upgrade-configuration
  179. unattended-upgrade-configuration?
  180. (operating-system-file unattended-upgrade-operating-system-file
  181. (default "/run/current-system/configuration.scm"))
  182. (schedule unattended-upgrade-configuration-schedule
  183. (default "30 01 * * 0"))
  184. (channels unattended-upgrade-configuration-channels
  185. (default #~%default-channels))
  186. (services-to-restart unattended-upgrade-configuration-services-to-restart
  187. (default '(mcron)))
  188. (system-expiration unattended-upgrade-system-expiration
  189. (default (* 3 30 24 3600)))
  190. (maximum-duration unattended-upgrade-maximum-duration
  191. (default 3600))
  192. (log-file unattended-upgrade-configuration-log-file
  193. (default %unattended-upgrade-log-file)))
  194. (define %unattended-upgrade-log-file
  195. "/var/log/unattended-upgrade.log")
  196. (define (unattended-upgrade-mcron-jobs config)
  197. (define channels
  198. (scheme-file "channels.scm"
  199. (unattended-upgrade-configuration-channels config)))
  200. (define log
  201. (unattended-upgrade-configuration-log-file config))
  202. (define services
  203. (unattended-upgrade-configuration-services-to-restart config))
  204. (define expiration
  205. (unattended-upgrade-system-expiration config))
  206. (define config-file
  207. (unattended-upgrade-operating-system-file config))
  208. (define code
  209. (with-imported-modules (source-module-closure '((guix build utils)
  210. (gnu services herd)))
  211. #~(begin
  212. (use-modules (guix build utils)
  213. (gnu services herd)
  214. (srfi srfi-19)
  215. (srfi srfi-34))
  216. (define log
  217. (open-file #$log "a0"))
  218. (define (timestamp)
  219. (date->string (time-utc->date (current-time time-utc))
  220. "[~4]"))
  221. (define (alarm-handler . _)
  222. (format #t "~a time is up, aborting upgrade~%"
  223. (timestamp))
  224. (exit 1))
  225. ;; 'guix time-machine' needs X.509 certificates to authenticate the
  226. ;; Git host.
  227. (setenv "SSL_CERT_DIR"
  228. #$(file-append nss-certs "/etc/ssl/certs"))
  229. ;; Make sure the upgrade doesn't take too long.
  230. (sigaction SIGALRM alarm-handler)
  231. (alarm #$(unattended-upgrade-maximum-duration config))
  232. ;; Redirect stdout/stderr to LOG to save the output of 'guix' below.
  233. (redirect-port log (current-output-port))
  234. (redirect-port log (current-error-port))
  235. (format #t "~a starting upgrade...~%" (timestamp))
  236. (guard (c ((invoke-error? c)
  237. (report-invoke-error c)))
  238. (invoke #$(file-append guix "/bin/guix")
  239. "time-machine" "-C" #$channels
  240. "--" "system" "reconfigure" #$config-file)
  241. ;; 'guix system delete-generations' fails when there's no
  242. ;; matching generation. Thus, catch 'invoke-error?'.
  243. (guard (c ((invoke-error? c)
  244. (report-invoke-error c)))
  245. (invoke #$(file-append guix "/bin/guix")
  246. "system" "delete-generations"
  247. #$(string-append (number->string expiration)
  248. "s")))
  249. (format #t "~a restarting services...~%" (timestamp))
  250. (for-each restart-service '#$services)
  251. ;; XXX: If 'mcron' has been restarted, perhaps this isn't
  252. ;; reached.
  253. (format #t "~a upgrade complete~%" (timestamp))))))
  254. (define upgrade
  255. (program-file "unattended-upgrade" code))
  256. (list #~(job #$(unattended-upgrade-configuration-schedule config)
  257. #$upgrade)))
  258. (define (unattended-upgrade-log-rotations config)
  259. (list (log-rotation
  260. (files
  261. (list (unattended-upgrade-configuration-log-file config))))))
  262. (define unattended-upgrade-service-type
  263. (service-type
  264. (name 'unattended-upgrade)
  265. (extensions
  266. (list (service-extension mcron-service-type
  267. unattended-upgrade-mcron-jobs)
  268. (service-extension rottlog-service-type
  269. unattended-upgrade-log-rotations)))
  270. (description
  271. "Periodically upgrade the system from the current configuration.")
  272. (default-value (unattended-upgrade-configuration))))
  273. ;;; admin.scm ends here