From e862b7a5b28de96c426b2c105718519caf204039 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Sun, 25 Aug 2024 19:11:54 +0200 Subject: Send mail output for some mcron jobs For mcron jobs that only output text when they fail, mail that output. --- tw/services/dns.scm | 54 +++++++++++++-------------- tw/services/nextcloud.scm | 13 ++++--- tw/services/personal-data-exporter.scm | 6 ++- tw/services/restic.scm | 68 +++++++++++++++++----------------- 4 files changed, 72 insertions(+), 69 deletions(-) diff --git a/tw/services/dns.scm b/tw/services/dns.scm index ba4da6fe..675984e7 100644 --- a/tw/services/dns.scm +++ b/tw/services/dns.scm @@ -27,34 +27,32 @@ Guile @code{read} calls.") (define (mythic-dynamic-dns-cronjob config api-host) (match-record config (schedule host-name credential-file) #~(job #$schedule - #$(program-file (string-append "dynamic-dns-" api-host "-command") - (with-extensions (list guile-json-4 guile-gnutls) ; guile-gnutls needed by (web client) - #~(begin - (use-modules (srfi srfi-11) ; `let*-values' - (ice-9 iconv) - (web client) - (web response) - (web uri) - (json)) - (let*-values - (((update-url) - (format #f "https://~a/dns/v2/dynamic/~a" #$api-host #$host-name)) - ((auth-body) - (call-with-input-file #$credential-file - (lambda (port) - (string-append "username=" (uri-encode (read port)) - "&password=" (uri-encode (read port)))))) - ((response body) - (http-post update-url #:body auth-body #:decode-body? #t - #:headers '((content-type application/x-www-form-urlencoded)))) - ;; For some reason, the body is not decoded, even with `#:decode-body? #t'. - ((decoded-body) - (and body (bytevector->string body "utf-8")))) - (if (= 200 (response-code response)) - (format (current-error-port) "Success: ~a\n" - (assoc-ref (json-string->scm decoded-body) "message")) - (format (current-error-port) "Got error response ~a: ~a\n" - response decoded-body))))))))) + (with-mail-out + #$(program-file (string-append "dynamic-dns-" api-host "-command") + (with-extensions (list guile-json-4 guile-gnutls) ; guile-gnutls needed by (web client) + #~(begin + (use-modules (srfi srfi-11) ; `let*-values' + (ice-9 iconv) + (web client) + (web response) + (web uri) + (json)) + (let*-values + (((update-url) + (format #f "https://~a/dns/v2/dynamic/~a" #$api-host #$host-name)) + ((auth-body) + (call-with-input-file #$credential-file + (lambda (port) + (string-append "username=" (uri-encode (read port)) + "&password=" (uri-encode (read port)))))) + ((response body) + (http-post update-url #:body auth-body #:decode-body? #t + #:headers '((content-type application/x-www-form-urlencoded)))) + ;; For some reason, the body is not decoded, even with `#:decode-body? #t'. + ((decoded-body) + (and body (bytevector->string body "utf-8")))) + (unless (= 200 (response-code response)) + (error "Got error response:" response decoded-body)))))))))) (define (mythic-dynamic-dns-cronjobs config) (match-record config (ipv4? ipv6?) diff --git a/tw/services/nextcloud.scm b/tw/services/nextcloud.scm index 177af535..f4170a31 100644 --- a/tw/services/nextcloud.scm +++ b/tw/services/nextcloud.scm @@ -311,12 +311,13 @@ opcache.revalidate_freq=120 ;; Nextcloud cron (simple-service 'nextcloud-cron mcron-service-type (list #~(job "*/5 * * * *" - #$(program-file "nextcloud-cron-command" - #~(begin - ;; Nextcloud News needs this to fetch HTTPS feeds. - (setenv "SSL_CERT_DIR" #$(file-append nss-certs "/etc/ssl/certs")) - (execl #$(file-append php "/bin/php") "php" - "-c" #$%nextcloud-php.ini "/var/www/nextcloud/cron.php"))) + (with-mail-out + #$(program-file "nextcloud-cron-command" + #~(begin + ;; Nextcloud News needs this to fetch HTTPS feeds. + (setenv "SSL_CERT_DIR" #$(file-append nss-certs "/etc/ssl/certs")) + (execl #$(file-append php "/bin/php") "php" + "-c" #$%nextcloud-php.ini "/var/www/nextcloud/cron.php")))) #:user "php-fpm") ;; TODO: try `with-mail-out' from `(mcron redirect)'? diff --git a/tw/services/personal-data-exporter.scm b/tw/services/personal-data-exporter.scm index 80e85622..ff3cbe80 100644 --- a/tw/services/personal-data-exporter.scm +++ b/tw/services/personal-data-exporter.scm @@ -121,7 +121,7 @@ create table if not exists \"conso_max_power\" (\"time\" timestamp primary key, '((#$(file-append postgresql "/bin/pg_dump") "-wU" #$user "-t" "conso_*" #$user) (#$(file-append restic "/bin/restic") "backup" - "--no-cache" "--stdin" "--stdin-filename=conso.sql"))) + "--quiet" "--no-cache" "--stdin" "--stdin-filename=conso.sql"))) (close to) (do ((char (read-char from) (read-char from))) @@ -160,6 +160,7 @@ create table if not exists \"conso_max_power\" (\"time\" timestamp primary key, ;; Ledgerplot uses the Boerse Frankfurt API, so run after markets close there. ;; According to https://www.boerse.de/handelszeiten/, it's 22:00 CET/CEST. (list #~(job "5 22 * * mon-fri" ; weekdays after market close + ;; TODO: use `with-mail-out' once ledgerplot doesn't output anything on success #$(program-file "ledgerplot-exchange-rates-command" #~(begin (setenv "LEDGER_FILE" #$ledger-file) @@ -168,12 +169,13 @@ create table if not exists \"conso_max_power\" (\"time\" timestamp primary key, #:user #$user) ;; Process the previous day's data during the night. + ;; TODO: use `with-mail-out', but this command has output on success #~(job "0 4 * * *" #$(conso-fetch-command config) #:user #$user) ;; Back up electricity consumption database. The other data can ;; be easily recreated from the source data, but this data ;; becomes inaccessible after 2 years via the API. - #~(job "0 5 * * *" #$(conso-backup-command config) #:user #$user)))) + #~(job "0 5 * * *" (with-mail-out #$(conso-backup-command config)) #:user #$user)))) (define (personal-data-shepherd-services config) (match-record config (user group ledger-file ledger-locale) diff --git a/tw/services/restic.scm b/tw/services/restic.scm index f23da013..2ea8c084 100644 --- a/tw/services/restic.scm +++ b/tw/services/restic.scm @@ -249,24 +249,25 @@ true." prune? keep-last keep-hourly keep-daily keep-weekly keep-monthly keep-yearly keep-within) #~(job #$schedule - #$(program-file - ;; Make cron commands for different repos easier to distinguish. - (string-append "restic-cleanup-" (repo-path-basename repo) "-command") - #~(begin - #$(set-restic-variables repo password) - (execl #$(file-append restic "/bin/restic") - "restic" "forget" - #$(restic-cache-policy repo) - #$@(arg-with-value "--prune" prune?) - #$@(arg-with-value "--host" snapshot-host) - #$@(arg-with-value "--path" snapshot-paths) - #$@(arg-with-value "--keep-within" keep-within) - #$@(arg-with-value "--keep-last" keep-last) - #$@(arg-with-value "--keep-hourly" (norm-keep keep-hourly)) - #$@(arg-with-value "--keep-daily" (norm-keep keep-daily)) - #$@(arg-with-value "--keep-weekly" (norm-keep keep-weekly)) - #$@(arg-with-value "--keep-monthly" (norm-keep keep-monthly)) - #$@(arg-with-value "--keep-yearly" (norm-keep keep-yearly))))) + (with-mail-out + #$(program-file + ;; Make cron commands for different repos easier to distinguish. + (string-append "restic-cleanup-" (repo-path-basename repo) "-command") + #~(begin + #$(set-restic-variables repo password) + (execl #$(file-append restic "/bin/restic") + "restic" "forget" "--quiet" + #$(restic-cache-policy repo) + #$@(arg-with-value "--prune" prune?) + #$@(arg-with-value "--host" snapshot-host) + #$@(arg-with-value "--path" snapshot-paths) + #$@(arg-with-value "--keep-within" keep-within) + #$@(arg-with-value "--keep-last" keep-last) + #$@(arg-with-value "--keep-hourly" (norm-keep keep-hourly)) + #$@(arg-with-value "--keep-daily" (norm-keep keep-daily)) + #$@(arg-with-value "--keep-weekly" (norm-keep keep-weekly)) + #$@(arg-with-value "--keep-monthly" (norm-keep keep-monthly)) + #$@(arg-with-value "--keep-yearly" (norm-keep keep-yearly)))))) #$@(if (maybe-value-set? user) (list #:user user) '())))) (map cronjob configs)) @@ -314,21 +315,22 @@ must be given. Leading @code{~/} are replaced with @code{$HOME}.") (define (cronjob config) (match-record config (schedule paths repo password tags restic) #~(job #$schedule - #$(program-file "restic-backup-command" - #~(begin - (use-modules (ice-9 popen) - (ice-9 textual-ports) - (srfi srfi-1)) - (define (replace-home path) - (if (string-prefix? "~/" path) - (string-replace path (getenv "HOME") 0 1) - path)) - #$(set-restic-variables repo password) - (apply execl #$(file-append restic "/bin/restic") - "restic" "backup" - #$(restic-cache-policy repo) - #$@(append-map (lambda (tag) (list "--tag" tag)) tags) - (map replace-home '#$paths))))))) + (with-mail-out + #$(program-file "restic-backup-command" + #~(begin + (use-modules (ice-9 popen) + (ice-9 textual-ports) + (srfi srfi-1)) + (define (replace-home path) + (if (string-prefix? "~/" path) + (string-replace path (getenv "HOME") 0 1) + path)) + #$(set-restic-variables repo password) + (apply execl #$(file-append restic "/bin/restic") + "restic" "backup" "--quiet" + #$(restic-cache-policy repo) + #$@(append-map (lambda (tag) (list "--tag" tag)) tags) + (map replace-home '#$paths)))))))) (map cronjob configs)) (define home-restic-backup-service-type -- cgit v1.2.3