Merge pull request #107980 from lukegb/grafana-plugins

Add Nix packages for Grafana plugins and allow declarative installation
This commit is contained in:
WilliButz 2021-01-01 16:00:17 +01:00 committed by GitHub
commit a4960d450e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 128 additions and 2 deletions

View file

@ -5,10 +5,11 @@ with lib;
let
cfg = config.services.grafana;
opt = options.services.grafana;
declarativePlugins = pkgs.linkFarm "grafana-plugins" (builtins.map (pkg: { name = pkg.pname; path = pkg; }) cfg.declarativePlugins);
envOptions = {
PATHS_DATA = cfg.dataDir;
PATHS_PLUGINS = "${cfg.dataDir}/plugins";
PATHS_PLUGINS = if builtins.isNull cfg.declarativePlugins then "${cfg.dataDir}/plugins" else declarativePlugins;
PATHS_LOGS = "${cfg.dataDir}/log";
SERVER_PROTOCOL = cfg.protocol;
@ -260,6 +261,12 @@ in {
defaultText = "pkgs.grafana";
type = types.package;
};
declarativePlugins = mkOption {
type = with types; nullOr (listOf path);
default = null;
description = "If non-null, then a list of packages containing Grafana plugins to install. If set, plugins cannot be manually installed.";
example = literalExample "with pkgs.grafanaPlugins; [ grafana-piechart-panel ]";
};
dataDir = mkOption {
description = "Data directory.";

View file

@ -17,6 +17,10 @@ let
};
extraNodeConfs = {
declarativePlugins = {
services.grafana.declarativePlugins = [ pkgs.grafanaPlugins.grafana-clock-panel ];
};
postgresql = {
services.grafana.database = {
host = "127.0.0.1:5432";
@ -52,7 +56,7 @@ let
nameValuePair dbName (mkMerge [
baseGrafanaConf
(extraNodeConfs.${dbName} or {})
])) [ "sqlite" "postgresql" "mysql" ]);
])) [ "sqlite" "declarativePlugins" "postgresql" "mysql" ]);
in {
name = "grafana";
@ -66,6 +70,14 @@ in {
testScript = ''
start_all()
with subtest("Declarative plugins installed"):
declarativePlugins.wait_for_unit("grafana.service")
declarativePlugins.wait_for_open_port(3000)
declarativePlugins.succeed(
"curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/plugins | grep -q grafana-clock-panel"
)
declarativePlugins.shutdown()
with subtest("Successful API query as admin user with sqlite db"):
sqlite.wait_for_unit("grafana.service")
sqlite.wait_for_open_port(3000)

View file

@ -0,0 +1,7 @@
{ newScope, pkgs }:
let
callPackage = newScope (pkgs // plugins);
plugins = import ./plugins.nix { inherit callPackage; };
in
plugins

View file

@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:
grafanaPlugin rec {
pname = "grafana-clock-panel";
version = "1.1.1";
zipHash = "sha256-SvZyg7r+XG6i7jqYwxpPn6ZzJc7qmtfPtyphYppURDk=";
meta = with lib; {
description = "Clock panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}

View file

@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:
grafanaPlugin rec {
pname = "grafana-piechart-panel";
version = "1.6.1";
zipHash = "sha256-64K/efoBKuBFp8Jw79hTdMyTurTZsL0qfgPDcUWz2jg=";
meta = with lib; {
description = "Pie chart panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}

View file

@ -0,0 +1,28 @@
{ stdenvNoCC, fetchurl, unzip }:
{ pname, version, zipHash, meta ? {}, passthru ? {}, ... }@args:
stdenvNoCC.mkDerivation ({
inherit pname version;
src = fetchurl {
name = "${pname}-${version}.zip";
url = "https://grafana.com/api/plugins/${pname}/versions/${version}/download";
hash = zipHash;
};
nativeBuildInputs = [ unzip ];
installPhase = ''
cp -R "." "$out"
chmod -R a-w "$out"
chmod u+w "$out"
'';
passthru = {
updateScript = [ ./update-grafana-plugin.sh pname ];
} // passthru;
meta = {
homepage = "https://grafana.com/grafana/plugins/${pname}";
} // meta;
} // (builtins.removeAttrs args [ "pname" "version" "sha256" "meta" ]))

View file

@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:
grafanaPlugin rec {
pname = "grafana-polystat-panel";
version = "1.2.2";
zipHash = "sha256-HWQdhstnrDuXPithZ8kOG2ZtSeAT215MJ1ftMCt6/tc=";
meta = with lib; {
description = "Hexagonal multi-stat panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}

View file

@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:
grafanaPlugin rec {
pname = "grafana-worldmap-panel";
version = "0.3.2";
zipHash = "sha256-MGAJzS9X91x6wt305jH1chLoW3zd7pIYDwRnPg9qrgE=";
meta = with lib; {
description = "World Map panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}

View file

@ -0,0 +1,11 @@
{ callPackage }:
{
inherit callPackage;
grafanaPlugin = callPackage ./grafana-plugin.nix { };
grafana-clock-panel = callPackage ./grafana-clock-panel { };
grafana-piechart-panel = callPackage ./grafana-piechart-panel { };
grafana-polystat-panel = callPackage ./grafana-polystat-panel { };
grafana-worldmap-panel = callPackage ./grafana-worldmap-panel { };
}

View file

@ -0,0 +1,8 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq common-updater-scripts
set -eu -o pipefail
readonly plugin_name="$1"
readonly latest_version="$(curl "https://grafana.com/api/plugins/${plugin_name}" | jq -r .version)"
update-source-version "grafanaPlugins.${plugin_name}" "$latest_version"

View file

@ -17203,6 +17203,7 @@ in
gofish = callPackage ../servers/gopher/gofish { };
grafana = callPackage ../servers/monitoring/grafana { };
grafanaPlugins = dontRecurseIntoAttrs (callPackage ../servers/monitoring/grafana/plugins { });
grafana-loki = callPackage ../servers/monitoring/loki { };