k 3 Reaktor: allow multiple Reaktor configurations

This commit is contained in:
lassulus 2017-01-21 21:55:59 +01:00
parent c7f0bc2800
commit 0ff2496de4

View file

@ -3,29 +3,18 @@
with import <stockholm/lib>; with import <stockholm/lib>;
let let
ReaktorConfig = pkgs.writeText "config.py" ''
${if (isString cfg.overrideConfig ) then ''
# Overriden Config
${cfg.overrideConfig}
'' else ""}
## Extra Config
${concatStringsSep "\n" (map (plug: plug.config) cfg.plugins)}
${cfg.extraConfig}
'';
cfg = config.krebs.Reaktor; cfg = config.krebs.Reaktor;
workdir = "/var/lib/Reaktor";
out = { out = {
options.krebs.Reaktor = api; options.krebs.Reaktor = api;
config = lib.mkIf cfg.enable imp; config = imp;
}; };
api = { api = mkOption {
enable = mkOption { default = {};
default = false; type = with types; attrsOf (submodule ({ options = {
description = ''
Start Reaktor at system boot
'';
};
nickname = mkOption { nickname = mkOption {
default = config.krebs.build.host.name + "|r"; default = config.krebs.build.host.name + "|r";
@ -44,9 +33,11 @@ let
Reaktor default cfg can be retrieved via `reaktor get-config` Reaktor default cfg can be retrieved via `reaktor get-config`
''; '';
}; };
plugins = mkOption { plugins = mkOption {
default = [pkgs.ReaktorPlugins.nixos-version]; default = [pkgs.ReaktorPlugins.nixos-version];
}; };
extraConfig = mkOption { extraConfig = mkOption {
default = ""; default = "";
type = types.string; type = types.string;
@ -55,13 +46,6 @@ let
''; '';
}; };
workdir = mkOption {
default = "/var/lib/Reaktor";
type = types.str;
description = ''
Reaktor working directory
'';
};
extraEnviron = mkOption { extraEnviron = mkOption {
default = {}; default = {};
type = types.attrsOf types.str; type = types.attrsOf types.str;
@ -74,6 +58,7 @@ let
debug and nickname can be set separately via the Reaktor api debug and nickname can be set separately via the Reaktor api
''; '';
}; };
channels = mkOption { channels = mkOption {
default = [ "#krebs" ]; default = [ "#krebs" ];
type = types.listOf types.str; type = types.listOf types.str;
@ -81,21 +66,25 @@ let
Channels the Reaktor should connect to at startup. Channels the Reaktor should connect to at startup.
''; '';
}; };
debug = mkOption { debug = mkOption {
default = false; default = false;
description = '' description = ''
Reaktor debug output Reaktor debug output
''; '';
}; };
};}));
}; };
imp = { imp = {
# TODO get user per configured bot
# TODO get home from api
# for reaktor get-config # for reaktor get-config
users.extraUsers = singleton rec { users.extraUsers = singleton rec {
name = "Reaktor"; name = "Reaktor";
uid = genid name; uid = genid name;
description = "Reaktor user"; description = "Reaktor user";
home = cfg.workdir; home = workdir;
createHome = true; createHome = true;
}; };
@ -104,7 +93,18 @@ let
# gid = config.ids.gids.Reaktor; # gid = config.ids.gids.Reaktor;
#}; #};
systemd.services.Reaktor = { systemd.services = mapAttrs' (name: botcfg:
let
ReaktorConfig = pkgs.writeText "config.py" ''
${if (isString botcfg.overrideConfig ) then ''
# Overriden Config
${botcfg.overrideConfig}
'' else ""}
## Extra Config
${concatStringsSep "\n" (map (plug: plug.config) botcfg.plugins)}
${botcfg.extraConfig}
'';
in nameValuePair "Reaktor-${name}" {
path = with pkgs; [ path = with pkgs; [
utillinux #flock for tell_on-join utillinux #flock for tell_on-join
git # for nag git # for nag
@ -115,28 +115,30 @@ let
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
environment = { environment = {
GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
REAKTOR_NICKNAME = cfg.nickname; REAKTOR_NICKNAME = botcfg.nickname;
REAKTOR_DEBUG = (if cfg.debug then "True" else "False"); REAKTOR_DEBUG = (if botcfg.debug then "True" else "False");
REAKTOR_CHANNELS = lib.concatStringsSep "," cfg.channels; REAKTOR_CHANNELS = lib.concatStringsSep "," botcfg.channels;
state_dir = cfg.workdir; state_dir = workdir;
} // cfg.extraEnviron; } // botcfg.extraEnviron;
serviceConfig= { serviceConfig= {
ExecStartPre = pkgs.writeScript "Reaktor-init" '' ExecStartPre = pkgs.writeScript "Reaktor-init" ''
#! /bin/sh #! /bin/sh
${if (isString cfg.overrideConfig) then ${if (isString botcfg.overrideConfig) then
''cp ${ReaktorConfig} /tmp/config.py'' ''cp ${ReaktorConfig} /tmp/reaktor-${name}-config.py''
else else
''(${pkgs.Reaktor}/bin/reaktor get-config;cat "${ReaktorConfig}" ) > /tmp/config.py'' ''(${pkgs.Reaktor}/bin/reaktor get-config;cat "${ReaktorConfig}" ) > /tmp/reaktor-${name}-config.py''
} }
''; '';
ExecStart = "${pkgs.Reaktor}/bin/reaktor run /tmp/config.py"; ExecStart = "${pkgs.Reaktor}/bin/reaktor run /tmp/reaktor-${name}-config.py";
PrivateTmp = "true"; PrivateTmp = "true";
User = "Reaktor"; User = "Reaktor";
Restart = "always"; Restart = "always";
RestartSec= "30" ; RestartSec= "30" ;
}; };
}; }
) cfg;
}; };
in in