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