From aa72ac94a3223faa287c01557874c3840219e000 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Thu, 7 Dec 2023 23:14:38 +0100 Subject: Generalise Docker service and use it to run Grafana --- tw/services/grafana.scm | 60 ++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 28 deletions(-) (limited to 'tw/services/grafana.scm') diff --git a/tw/services/grafana.scm b/tw/services/grafana.scm index a796ba19..2fdb9695 100644 --- a/tw/services/grafana.scm +++ b/tw/services/grafana.scm @@ -1,13 +1,12 @@ (define-module (tw services grafana) #:use-module (gnu) #:use-module ((gnu packages admin) #:select (shadow)) - #:use-module ((gnu packages docker) #:select (docker-cli)) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services databases) - #:use-module (gnu services shepherd) #:use-module (guix records) #:use-module (tw services web) + #:use-module (tw services docker) #:export (grafana-service-type grafana-configuration)) @@ -42,31 +41,36 @@ variables.")) (home-directory (grafana-configuration-data-path config)) (shell (file-append shadow "/sbin/nologin"))))) -(define (grafana-shepherd-service config) - (list (shepherd-service - (provision '(grafana)) - (requirement '(networking)) - (documentation "Run a Grafana instance using Docker.") - (start #~(make-forkexec-constructor - ;; https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/ - (list #$(file-append docker-cli "/bin/docker") "run" "--rm" "--network=host" - "--name" "grafana" "--user" '#$%grafana-user - "-v" '#$(format #f "~a:/var/lib/grafana" (grafana-configuration-data-path config)) - "--env-file" '#$(grafana-configuration-metrics-credentials-file config) - ;; https://grafana.com/docs/grafana/latest/setup-grafana/configure-docker/ - "-e" "GF_SERVER_PROTOCOL=http" ; use Wireguard for encryption - "-e" '#$(format #f "GF_SERVER_HTTP_ADDR=~a" (grafana-configuration-bind-address config)) - "-e" '#$(format #f "GF_SERVER_HTTP_PORT=~a" (grafana-configuration-host-port config)) - "-e" "GF_SERVER_ENABLE_GZIP=true" ; recommended by docs - "-e" "GF_ANALYTICS_REPORTING_ENABLED=false" - "-e" "GF_ANALYTICS_CHECK_FOR_UPDATES=false" - "-e" "GF_ANALYTICS_CHECK_FOR_PLUGIN_UPDATES=false" - "-e" "GF_SNAPSHOTS_ENABLED=false" ; disable publishing dashboard snapshots to the internet - "-e" "GF_DATE_FORMATS_INTERVAL_HOUR=DD.MM. HH:mm" ; use sensible date format - "-e" "GF_DATE_FORMATS_INTERVAL_DAY=DD.MM." ; use sensible date format - ;; TODO: https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#smtp - '#$(grafana-configuration-container config)))) - (stop #~(make-kill-destructor))))) +(define (grafana-environment config) + (match-record config (bind-address host-port) + (mixed-text-file "grafana.env" "\ +# https://grafana.com/docs/grafana/latest/setup-grafana/configure-docker/ +GF_SERVER_PROTOCOL=http +GF_SERVER_HTTP_ADDR=" bind-address " +GF_SERVER_HTTP_PORT=" host-port " +# gzip compression is recommended by docs, but is not the default +GF_SERVER_ENABLE_GZIP=true +GF_ANALYTICS_REPORTING_ENABLED=false +GF_ANALYTICS_CHECK_FOR_UPDATES=false +GF_ANALYTICS_CHECK_FOR_PLUGIN_UPDATES=false +# disable publishing dashboard snapshots to the internet +GF_SNAPSHOTS_ENABLED=false +# use sensible date and time formats +GF_DATE_FORMATS_INTERVAL_HOUR=DD.MM. HH:mm +GF_DATE_FORMATS_INTERVAL_DAY=DD.MM. +# TODO: https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#smtp +"))) + +(define (grafana-docker-service config) + (match-record config (container data-path metrics-credentials-file) + (list (docker-container-configuration + (name "grafana") + (user %grafana-user) + (image container) + (volumes `((,data-path "/var/lib/grafana" #t))) + (environment-files + (list (grafana-environment config) metrics-credentials-file)) + (network-type "host"))))) (define (grafana-reverse-proxy config) (match-record config (domain bind-address host-port) @@ -83,7 +87,7 @@ variables.")) (service-type (name 'grafana) (extensions - (list (service-extension shepherd-root-service-type grafana-shepherd-service) + (list (service-extension docker-container-service-type grafana-docker-service) (service-extension account-service-type grafana-accounts) (service-extension https-reverse-proxy-service-type grafana-reverse-proxy))) (default-value (grafana-configuration)) -- cgit v1.2.3