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,99 +3,88 @@
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";
type = types.string; type = types.string;
description = '' description = ''
The nick name of the irc bot. The nick name of the irc bot.
Defaults to {hostname}|r Defaults to {hostname}|r
''; '';
}; };
overrideConfig = mkOption { overrideConfig = mkOption {
default = null; default = null;
type = types.nullOr types.str; type = types.nullOr types.str;
description = '' description = ''
configuration to be used instead of default ones. configuration to be used instead of default ones.
Reaktor default cfg can be retrieved via `reaktor get-config` Reaktor default cfg can be retrieved via `reaktor get-config`
''; '';
}; };
plugins = mkOption {
default = [pkgs.ReaktorPlugins.nixos-version];
};
extraConfig = mkOption {
default = "";
type = types.string;
description = ''
configuration appended to the default or overridden configuration
'';
};
workdir = mkOption { plugins = mkOption {
default = "/var/lib/Reaktor"; default = [pkgs.ReaktorPlugins.nixos-version];
type = types.str; };
description = ''
Reaktor working directory
'';
};
extraEnviron = mkOption {
default = {};
type = types.attrsOf types.str;
description = ''
Environment to be provided to the service, can be:
REAKTOR_HOST
REAKTOR_PORT
REAKTOR_STATEDIR
debug and nickname can be set separately via the Reaktor api extraConfig = mkOption {
''; default = "";
}; type = types.string;
channels = mkOption { description = ''
default = [ "#krebs" ]; configuration appended to the default or overridden configuration
type = types.listOf types.str; '';
description = '' };
Channels the Reaktor should connect to at startup.
''; extraEnviron = mkOption {
}; default = {};
debug = mkOption { type = types.attrsOf types.str;
default = false; description = ''
description = '' Environment to be provided to the service, can be:
Reaktor debug output REAKTOR_HOST
''; REAKTOR_PORT
}; REAKTOR_STATEDIR
debug and nickname can be set separately via the Reaktor api
'';
};
channels = mkOption {
default = [ "#krebs" ];
type = types.listOf types.str;
description = ''
Channels the Reaktor should connect to at startup.
'';
};
debug = mkOption {
default = false;
description = ''
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,39 +93,52 @@ let
# gid = config.ids.gids.Reaktor; # gid = config.ids.gids.Reaktor;
#}; #};
systemd.services.Reaktor = { systemd.services = mapAttrs' (name: botcfg:
path = with pkgs; [ let
utillinux #flock for tell_on-join ReaktorConfig = pkgs.writeText "config.py" ''
git # for nag ${if (isString botcfg.overrideConfig ) then ''
python # for caps # Overriden Config
]; ${botcfg.overrideConfig}
description = "Reaktor IRC Bot"; '' else ""}
after = [ "network.target" ]; ## Extra Config
wantedBy = [ "multi-user.target" ]; ${concatStringsSep "\n" (map (plug: plug.config) botcfg.plugins)}
environment = { ${botcfg.extraConfig}
GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
REAKTOR_NICKNAME = cfg.nickname;
REAKTOR_DEBUG = (if cfg.debug then "True" else "False");
REAKTOR_CHANNELS = lib.concatStringsSep "," cfg.channels;
state_dir = cfg.workdir;
} // cfg.extraEnviron;
serviceConfig= {
ExecStartPre = pkgs.writeScript "Reaktor-init" ''
#! /bin/sh
${if (isString cfg.overrideConfig) then
''cp ${ReaktorConfig} /tmp/config.py''
else
''(${pkgs.Reaktor}/bin/reaktor get-config;cat "${ReaktorConfig}" ) > /tmp/config.py''
}
''; '';
ExecStart = "${pkgs.Reaktor}/bin/reaktor run /tmp/config.py"; in nameValuePair "Reaktor-${name}" {
PrivateTmp = "true"; path = with pkgs; [
User = "Reaktor"; utillinux #flock for tell_on-join
Restart = "always"; git # for nag
RestartSec= "30" ; python # for caps
];
description = "Reaktor IRC Bot";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
environment = {
GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
REAKTOR_NICKNAME = botcfg.nickname;
REAKTOR_DEBUG = (if botcfg.debug then "True" else "False");
REAKTOR_CHANNELS = lib.concatStringsSep "," botcfg.channels;
state_dir = workdir;
} // botcfg.extraEnviron;
serviceConfig= {
ExecStartPre = pkgs.writeScript "Reaktor-init" ''
#! /bin/sh
${if (isString botcfg.overrideConfig) then
''cp ${ReaktorConfig} /tmp/reaktor-${name}-config.py''
else
''(${pkgs.Reaktor}/bin/reaktor get-config;cat "${ReaktorConfig}" ) > /tmp/reaktor-${name}-config.py''
}
'';
ExecStart = "${pkgs.Reaktor}/bin/reaktor run /tmp/reaktor-${name}-config.py";
PrivateTmp = "true";
User = "Reaktor";
Restart = "always";
RestartSec= "30" ;
}; };
}; }
) cfg;
}; };
in in