admin.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  3. ;;; Copyright © 2016-2022 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. log-cleanup-service-type
  46. log-cleanup-configuration
  47. log-cleanup-configuration?
  48. log-cleanup-configuration-directory
  49. log-cleanup-configuration-expiry
  50. log-cleanup-configuration-schedule
  51. unattended-upgrade-service-type
  52. unattended-upgrade-configuration
  53. unattended-upgrade-configuration?
  54. unattended-upgrade-configuration-operating-system-file
  55. unattended-upgrade-configuration-channels
  56. unattended-upgrade-configuration-schedule
  57. unattended-upgrade-configuration-services-to-restart
  58. unattended-upgrade-configuration-system-expiration
  59. unattended-upgrade-configuration-maximum-duration
  60. unattended-upgrade-configuration-log-file))
  61. ;;; Commentary:
  62. ;;;
  63. ;;; This module implements configuration of rottlog by writing
  64. ;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
  65. ;;;
  66. ;;; (mcron-service)
  67. ;;; (service rottlog-service-type)
  68. ;;;
  69. ;;; Code:
  70. (define-record-type* <log-rotation> log-rotation make-log-rotation
  71. log-rotation?
  72. (files log-rotation-files) ;list of strings
  73. (frequency log-rotation-frequency ;symbol
  74. (default 'weekly))
  75. (post-rotate log-rotation-post-rotate ;#f | gexp
  76. (default #f))
  77. (options log-rotation-options ;list of strings
  78. (default '())))
  79. (define %rotated-files
  80. ;; Syslog files subject to rotation.
  81. '("/var/log/messages" "/var/log/secure" "/var/log/debug"
  82. "/var/log/maillog" "/var/log/mcron.log"))
  83. (define %default-rotations
  84. (list (log-rotation ;syslog files
  85. (files %rotated-files)
  86. (options '(;; Run post-rotate once per rotation
  87. "sharedscripts"
  88. ;; Append .gz to rotated files
  89. "storefile @FILENAME.@COMP_EXT"))
  90. ;; Restart syslogd after rotation.
  91. (post-rotate #~(let ((pid (call-with-input-file "/var/run/syslog.pid"
  92. read)))
  93. (kill pid SIGHUP))))
  94. (log-rotation
  95. (files '("/var/log/guix-daemon.log"))
  96. (options '("rotate 4" ;don't keep too many of them
  97. "storefile @FILENAME.@COMP_EXT")))))
  98. (define (log-rotation->config rotation)
  99. "Return a string-valued gexp representing the rottlog configuration snippet
  100. for ROTATION."
  101. (define post-rotate
  102. (let ((post (log-rotation-post-rotate rotation)))
  103. (and post
  104. (program-file "rottlog-post-rotate.scm" post))))
  105. #~(let ((post #$post-rotate))
  106. (string-append (string-join '#$(log-rotation-files rotation) ",")
  107. " {"
  108. #$(string-join (log-rotation-options rotation)
  109. "\n " 'prefix)
  110. (if post
  111. (string-append "\n postrotate\n " post
  112. "\n endscript\n")
  113. "")
  114. "\n}\n")))
  115. (define (log-rotations->/etc-entries rotations)
  116. "Return the list of /etc entries for ROTATIONS, a list of <log-rotation>."
  117. (define (frequency-file frequency rotations)
  118. (computed-file (string-append "rottlog." (symbol->string frequency))
  119. #~(call-with-output-file #$output
  120. (lambda (port)
  121. (for-each (lambda (str)
  122. (display str port))
  123. (list #$@(map log-rotation->config
  124. rotations)))))))
  125. (let* ((frequencies (delete-duplicates
  126. (map log-rotation-frequency rotations)))
  127. (table (fold (lambda (rotation table)
  128. (vhash-consq (log-rotation-frequency rotation)
  129. rotation table))
  130. vlist-null
  131. rotations)))
  132. (map (lambda (frequency)
  133. `(,(symbol->string frequency)
  134. ,(frequency-file frequency
  135. (vhash-foldq* cons '() frequency table))))
  136. frequencies)))
  137. (define (default-jobs rottlog)
  138. (list #~(job '(next-hour '(0)) ;midnight
  139. #$(file-append rottlog "/sbin/rottlog"))
  140. #~(job '(next-hour '(12)) ;noon
  141. #$(file-append rottlog "/sbin/rottlog"))))
  142. (define-record-type* <rottlog-configuration>
  143. rottlog-configuration make-rottlog-configuration
  144. rottlog-configuration?
  145. (rottlog rottlog-rottlog ;file-like
  146. (default rottlog))
  147. (rc-file rottlog-rc-file ;file-like
  148. (default (file-append rottlog "/etc/rc")))
  149. (rotations rottlog-rotations ;list of <log-rotation>
  150. (default %default-rotations))
  151. (jobs rottlog-jobs ;list of <mcron-job>
  152. (default #f)))
  153. (define (rottlog-etc config)
  154. `(("rottlog"
  155. ,(file-union "rottlog"
  156. (cons `("rc" ,(rottlog-rc-file config))
  157. (log-rotations->/etc-entries
  158. (rottlog-rotations config)))))))
  159. (define (rottlog-jobs-or-default config)
  160. (or (rottlog-jobs config)
  161. (default-jobs (rottlog-rottlog config))))
  162. (define rottlog-service-type
  163. (service-type
  164. (name 'rottlog)
  165. (description
  166. "Periodically rotate log files using GNU@tie{}Rottlog and GNU@tie{}mcron.
  167. Old log files are removed or compressed according to the configuration.")
  168. (extensions (list (service-extension etc-service-type rottlog-etc)
  169. (service-extension mcron-service-type
  170. rottlog-jobs-or-default)
  171. ;; Add Rottlog to the global profile so users can access
  172. ;; the documentation.
  173. (service-extension profile-service-type
  174. (compose list rottlog-rottlog))))
  175. (compose concatenate)
  176. (extend (lambda (config rotations)
  177. (rottlog-configuration
  178. (inherit config)
  179. (rotations (append (rottlog-rotations config)
  180. rotations)))))
  181. (default-value (rottlog-configuration))))
  182. ;;;
  183. ;;; Build log removal.
  184. ;;;
  185. (define-record-type* <log-cleanup-configuration>
  186. log-cleanup-configuration make-log-cleanup-configuration
  187. log-cleanup-configuration?
  188. (directory log-cleanup-configuration-directory) ;string
  189. (expiry log-cleanup-configuration-expiry ;integer (seconds)
  190. (default (* 6 30 24 3600)))
  191. (schedule log-cleanup-configuration-schedule ;string or gexp
  192. (default "30 12 01,08,15,22 * *")))
  193. (define (log-cleanup-program directory expiry)
  194. (program-file "delete-old-logs"
  195. (with-imported-modules '((guix build utils))
  196. #~(begin
  197. (use-modules (guix build utils))
  198. (let* ((now (car (gettimeofday)))
  199. (logs (find-files #$directory
  200. (lambda (file stat)
  201. (> (- now (stat:mtime stat))
  202. #$expiry)))))
  203. (format #t "deleting ~a log files from '~a'...~%"
  204. (length logs) #$directory)
  205. (for-each delete-file logs))))))
  206. (define (log-cleanup-mcron-jobs configuration)
  207. (match-record configuration <log-cleanup-configuration>
  208. (directory expiry schedule)
  209. (list #~(job #$schedule
  210. #$(log-cleanup-program directory expiry)))))
  211. (define log-cleanup-service-type
  212. (service-type
  213. (name 'log-cleanup)
  214. (extensions
  215. (list (service-extension mcron-service-type
  216. log-cleanup-mcron-jobs)))
  217. (description
  218. "Periodically delete old log files.")))
  219. ;;;
  220. ;;; Unattended upgrade.
  221. ;;;
  222. (define-record-type* <unattended-upgrade-configuration>
  223. unattended-upgrade-configuration make-unattended-upgrade-configuration
  224. unattended-upgrade-configuration?
  225. (operating-system-file unattended-upgrade-operating-system-file
  226. (default "/run/current-system/configuration.scm"))
  227. (schedule unattended-upgrade-configuration-schedule
  228. (default "30 01 * * 0"))
  229. (channels unattended-upgrade-configuration-channels
  230. (default #~%default-channels))
  231. (services-to-restart unattended-upgrade-configuration-services-to-restart
  232. (default '(mcron)))
  233. (system-expiration unattended-upgrade-system-expiration
  234. (default (* 3 30 24 3600)))
  235. (maximum-duration unattended-upgrade-maximum-duration
  236. (default 3600))
  237. (log-file unattended-upgrade-configuration-log-file
  238. (default %unattended-upgrade-log-file)))
  239. (define %unattended-upgrade-log-file
  240. "/var/log/unattended-upgrade.log")
  241. (define (unattended-upgrade-mcron-jobs config)
  242. (define channels
  243. (scheme-file "channels.scm"
  244. (unattended-upgrade-configuration-channels config)))
  245. (define log
  246. (unattended-upgrade-configuration-log-file config))
  247. (define services
  248. (unattended-upgrade-configuration-services-to-restart config))
  249. (define expiration
  250. (unattended-upgrade-system-expiration config))
  251. (define config-file
  252. (unattended-upgrade-operating-system-file config))
  253. (define code
  254. (with-imported-modules (source-module-closure '((guix build utils)
  255. (gnu services herd)))
  256. #~(begin
  257. (use-modules (guix build utils)
  258. (gnu services herd)
  259. (srfi srfi-19)
  260. (srfi srfi-34))
  261. (define log
  262. (open-file #$log "a0"))
  263. (define (timestamp)
  264. (date->string (time-utc->date (current-time time-utc))
  265. "[~4]"))
  266. (define (alarm-handler . _)
  267. (format #t "~a time is up, aborting upgrade~%"
  268. (timestamp))
  269. (exit 1))
  270. ;; 'guix time-machine' needs X.509 certificates to authenticate the
  271. ;; Git host.
  272. (setenv "SSL_CERT_DIR"
  273. #$(file-append nss-certs "/etc/ssl/certs"))
  274. ;; Make sure the upgrade doesn't take too long.
  275. (sigaction SIGALRM alarm-handler)
  276. (alarm #$(unattended-upgrade-maximum-duration config))
  277. ;; Redirect stdout/stderr to LOG to save the output of 'guix' below.
  278. (redirect-port log (current-output-port))
  279. (redirect-port log (current-error-port))
  280. (format #t "~a starting upgrade...~%" (timestamp))
  281. (guard (c ((invoke-error? c)
  282. (report-invoke-error c)))
  283. (invoke #$(file-append guix "/bin/guix")
  284. "time-machine" "-C" #$channels
  285. "--" "system" "reconfigure" #$config-file)
  286. ;; 'guix system delete-generations' fails when there's no
  287. ;; matching generation. Thus, catch 'invoke-error?'.
  288. (guard (c ((invoke-error? c)
  289. (report-invoke-error c)))
  290. (invoke #$(file-append guix "/bin/guix")
  291. "system" "delete-generations"
  292. #$(string-append (number->string expiration)
  293. "s")))
  294. (format #t "~a restarting services...~%" (timestamp))
  295. (for-each restart-service '#$services)
  296. ;; XXX: If 'mcron' has been restarted, perhaps this isn't
  297. ;; reached.
  298. (format #t "~a upgrade complete~%" (timestamp))))))
  299. (define upgrade
  300. (program-file "unattended-upgrade" code))
  301. (list #~(job #$(unattended-upgrade-configuration-schedule config)
  302. #$upgrade)))
  303. (define (unattended-upgrade-log-rotations config)
  304. (list (log-rotation
  305. (files
  306. (list (unattended-upgrade-configuration-log-file config))))))
  307. (define unattended-upgrade-service-type
  308. (service-type
  309. (name 'unattended-upgrade)
  310. (extensions
  311. (list (service-extension mcron-service-type
  312. unattended-upgrade-mcron-jobs)
  313. (service-extension rottlog-service-type
  314. unattended-upgrade-log-rotations)))
  315. (description
  316. "Periodically upgrade the system from the current configuration.")
  317. (default-value (unattended-upgrade-configuration))))
  318. ;;; admin.scm ends here