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