]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/modules/buildbot/default.nix
Move secrets module outside of nixops
[perso/Immae/Config/Nix.git] / nixops / modules / buildbot / default.nix
CommitLineData
587b9e34 1{ lib, pkgs, config, myconfig, mylibs, ... }:
9fb4205e
IB
2let
3 varDir = "/var/lib/buildbot";
4 buildslist_src = mylibs.fetchedGitPrivate ./buildslist.json;
33aa7e5c 5 buildslist_yarn = mylibs.yarn2nixPackage.mkYarnModules rec {
9fb4205e 6 name = "buildslist-yarn-modules";
33aa7e5c
IB
7 pname = name;
8 inherit (pkgs.buildbot-pkg) version;
9fb4205e
IB
9 packageJSON = "${buildslist_src.src}/package.json";
10 yarnLock = "${buildslist_src.src}/yarn.lock";
167b8165
IB
11 pkgConfig = {
12 all = { buildInputs = [ mylibs.yarn2nixPackage.src ]; };
13 };
9fb4205e 14 };
caa08508 15 buildslist_bower = pkgs.buildBowerComponents {
9fb4205e
IB
16 name = "buildslist";
17 generated = ./bower.nix;
18 src = "${buildslist_src.src}/guanlecoja/";
19 };
20
caa08508 21 buildslist = pkgs.python3Packages.buildPythonPackage rec {
9fb4205e 22 pname = "buildbot-buildslist";
caa08508 23 inherit (pkgs.buildbot-pkg) version;
9fb4205e
IB
24
25 preConfigure = ''
26 export HOME=$PWD
27 cp -a ${buildslist_yarn}/node_modules .
28 chmod -R u+w node_modules
29 cp -a ${buildslist_bower}/bower_components ./libs
30 chmod -R u+w libs
31 '';
caa08508 32 propagatedBuildInputs = with pkgs.python3Packages; [
9fb4205e
IB
33 (klein.overridePythonAttrs(old: { checkPhase = ""; }))
34 buildbot-pkg
35 ];
caa08508 36 nativeBuildInputs = with pkgs; [ yarn nodejs ];
9fb4205e
IB
37 buildInputs = [ buildslist_yarn buildslist_bower ];
38
39 doCheck = false;
40 src = buildslist_src.src;
41 };
caa08508 42 buildbot_common = pkgs.python3Packages.buildPythonPackage rec {
e2b96bf5
IB
43 name = "buildbot_common";
44 src = ./common;
9fb4205e
IB
45 format = "other";
46 installPhase = ''
caa08508
IB
47 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
48 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common
9fb4205e 49 '';
e2b96bf5 50 };
caa08508 51 buildbot = pkgs.python3Packages.buildbot-full.withPlugins ([ buildslist ]);
9fb4205e
IB
52in
53{
54 options = {
55 services.buildbot.enable = lib.mkOption {
56 type = lib.types.bool;
57 default = false;
58 description = ''
59 Whether to enable buildbot.
60 '';
61 };
62 };
63
64 config = lib.mkIf config.services.buildbot.enable {
65 ids.uids.buildbot = myconfig.env.buildbot.user.uid;
66 ids.gids.buildbot = myconfig.env.buildbot.user.gid;
67
68 users.groups.buildbot.gid = config.ids.gids.buildbot;
69 users.users.buildbot = {
70 name = "buildbot";
71 uid = config.ids.uids.buildbot;
72 group = "buildbot";
73 description = "Buildbot user";
74 home = varDir;
6984f454 75 extraGroups = [ "keys" ];
9fb4205e
IB
76 };
77
78 services.myWebsites.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
79 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
80 RewriteEngine On
81 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
82 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
83 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
84 <Location /buildbot/${project.name}/>
85 Use LDAPConnect
e2b96bf5 86 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
9fb4205e
IB
87
88 SetEnvIf X-Url-Scheme https HTTPS=1
89 ProxyPreserveHost On
90 </Location>
91 <Location /buildbot/${project.name}/change_hook/base>
85817848
IB
92 <RequireAny>
93 Require local
94 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
ca330baa 95 Include /var/secrets/buildbot/${project.name}/webhook-httpd-include
85817848 96 </RequireAny>
9fb4205e
IB
97 </Location>
98 '') myconfig.env.buildbot.projects;
99
100 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
101 deps = [ "users" "wrappers" ];
6984f454
IB
102 text = ''
103 install -m 0755 -o buildbot -g buildbot -d /run/buildbot/
104 install -m 0755 -o buildbot -g buildbot -d ${varDir}
105 ${project.activationScript}
106 '';
107 }) myconfig.env.buildbot.projects;
108
1a718805 109 secrets.keys = (
6984f454
IB
110 lib.lists.flatten (
111 lib.attrsets.mapAttrsToList (k: project:
112 lib.attrsets.mapAttrsToList (k: v:
ca330baa 113 {
6984f454
IB
114 permissions = "0600";
115 user = "buildbot";
116 group = "buildbot";
117 text = v;
ca330baa 118 dest = "buildbot/${project.name}/${k}";
6984f454
IB
119 }
120 ) project.secrets
121 ++ [
ca330baa 122 {
6984f454
IB
123 permissions = "0600";
124 user = "wwwrun";
125 group = "wwwrun";
126 text = lib.optionalString (lib.attrsets.hasAttr "webhookTokens" project) ''
127 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
128 '';
ca330baa
IB
129 dest = "buildbot/${project.name}/webhook-httpd-include";
130 }
6984f454
IB
131 ]
132 ) myconfig.env.buildbot.projects
133 )
ca330baa
IB
134 ) ++ [
135 {
6984f454
IB
136 permissions = "0600";
137 user = "buildbot";
138 group = "buildbot";
139 text = myconfig.env.buildbot.ldap.password;
ca330baa
IB
140 dest = "buildbot/ldap";
141 }
142 {
6984f454
IB
143 permissions = "0600";
144 user = "buildbot";
145 group = "buildbot";
146 text = builtins.readFile "${myconfig.privateFiles}/buildbot_ssh_key";
ca330baa
IB
147 dest = "buildbot/ssh_key";
148 }
149 ];
6984f454
IB
150
151 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
152 description = "Buildbot Continuous Integration Server ${project.name}.";
ca330baa 153 after = [ "network-online.target" ];
6984f454
IB
154 wantedBy = [ "multi-user.target" ];
155 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
156 preStart = let
caa08508 157 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
e2b96bf5
IB
158 tac_file = pkgs.writeText "buildbot.tac" ''
159 import os
160
161 from twisted.application import service
162 from buildbot.master import BuildMaster
163
164 basedir = '${varDir}/${project.name}'
165 rotateLength = 10000000
166 maxRotatedFiles = 10
167 configfile = '${master-cfg}'
168
169 # Default umask for server
170 umask = None
171
172 # if this is a relocatable tac file, get the directory containing the TAC
173 if basedir == '.':
174 import os
175 basedir = os.path.abspath(os.path.dirname(__file__))
176
177 # note: this line is matched against to check that this is a buildmaster
178 # directory; do not edit it.
179 application = service.Application('buildmaster')
180 from twisted.python.logfile import LogFile
181 from twisted.python.log import ILogObserver, FileLogObserver
182 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
183 maxRotatedFiles=maxRotatedFiles)
184 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
185
186 m = BuildMaster(basedir, configfile, umask)
187 m.setServiceParent(application)
188 m.log_rotation.rotateLength = rotateLength
189 m.log_rotation.maxRotatedFiles = maxRotatedFiles
190 '';
9fb4205e 191 in ''
9fb4205e 192 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
6984f454 193 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
9fb4205e 194 rm -f ${varDir}/${project.name}/master.cfg.sample
e2b96bf5 195 rm -f ${varDir}/${project.name}/buildbot.tac
9fb4205e 196 fi
e2b96bf5 197 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
ca330baa
IB
198 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
199 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true
9fb4205e 200 buildbot_secrets=${varDir}/${project.name}/secrets
6984f454 201 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
ca330baa 202 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap
9fb4205e 203 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
ca330baa 204 (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets
9fb4205e 205 )}
9fb4205e 206 '';
9fb4205e
IB
207 environment = let
208 project_env = lib.attrsets.mapAttrs' (k: v: lib.attrsets.nameValuePair "BUILDBOT_${k}" v) project.environment;
caa08508 209 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
9fb4205e 210 name = "buildbot_config-${project.name}";
e2b96bf5 211 src = ./projects + "/${project.name}";
9fb4205e
IB
212 format = "other";
213 installPhase = ''
caa08508
IB
214 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
215 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
9fb4205e
IB
216 '';
217 });
218 HOME = "${varDir}/${project.name}";
caa08508 219 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
256d607c 220 pkgs.python3Packages.wokkel
caa08508
IB
221 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
222 pkgs.python3Packages.buildbot-worker
9fb4205e 223 buildbot_common buildbot_config
caa08508 224 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
9fb4205e
IB
225 in project_env // { inherit PYTHONPATH HOME; };
226
227 serviceConfig = {
228 Type = "forking";
229 User = "buildbot";
230 Group = "buildbot";
6984f454 231 SupplementaryGroups = "keys";
9fb4205e
IB
232 WorkingDirectory = "${varDir}/${project.name}";
233 ExecStart = "${buildbot}/bin/buildbot start";
234 };
235 }) myconfig.env.buildbot.projects;
236 };
237}