]>
Commit | Line | Data |
---|---|---|
ab8f306d | 1 | { lib, pkgs, config, ... }: |
9fb4205e IB |
2 | let |
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 |
14 | in |
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 | } | |
6984f454 | 101 | ] |
ab8f306d | 102 | ) config.myEnv.buildbot.projects |
6984f454 | 103 | ) |
ca330baa IB |
104 | ) ++ [ |
105 | { | |
6984f454 IB |
106 | permissions = "0600"; |
107 | user = "buildbot"; | |
108 | group = "buildbot"; | |
ab8f306d | 109 | text = config.myEnv.buildbot.ldap.password; |
ca330baa IB |
110 | dest = "buildbot/ldap"; |
111 | } | |
112 | { | |
6984f454 IB |
113 | permissions = "0600"; |
114 | user = "buildbot"; | |
115 | group = "buildbot"; | |
ab8f306d | 116 | text = builtins.readFile "${config.myEnv.privateFiles}/buildbot_ssh_key"; |
ca330baa IB |
117 | dest = "buildbot/ssh_key"; |
118 | } | |
119 | ]; | |
6984f454 | 120 | |
17f6eae9 IB |
121 | services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { |
122 | restart = true; | |
123 | paths = [ | |
124 | "/var/secrets/buildbot/ldap" | |
125 | "/var/secrets/buildbot/ssh_key" | |
126 | ] ++ lib.attrsets.mapAttrsToList (k: v: "/var/secrets/buildbot/${project.name}/${k}") project.secrets; | |
ab8f306d | 127 | }) config.myEnv.buildbot.projects; |
17f6eae9 | 128 | |
6984f454 IB |
129 | systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { |
130 | description = "Buildbot Continuous Integration Server ${project.name}."; | |
ca330baa | 131 | after = [ "network-online.target" ]; |
6984f454 IB |
132 | wantedBy = [ "multi-user.target" ]; |
133 | path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs); | |
134 | preStart = let | |
caa08508 | 135 | master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg"; |
e2b96bf5 IB |
136 | tac_file = pkgs.writeText "buildbot.tac" '' |
137 | import os | |
138 | ||
139 | from twisted.application import service | |
140 | from buildbot.master import BuildMaster | |
141 | ||
142 | basedir = '${varDir}/${project.name}' | |
143 | rotateLength = 10000000 | |
144 | maxRotatedFiles = 10 | |
145 | configfile = '${master-cfg}' | |
146 | ||
147 | # Default umask for server | |
148 | umask = None | |
149 | ||
150 | # if this is a relocatable tac file, get the directory containing the TAC | |
151 | if basedir == '.': | |
152 | import os | |
153 | basedir = os.path.abspath(os.path.dirname(__file__)) | |
154 | ||
155 | # note: this line is matched against to check that this is a buildmaster | |
156 | # directory; do not edit it. | |
157 | application = service.Application('buildmaster') | |
158 | from twisted.python.logfile import LogFile | |
159 | from twisted.python.log import ILogObserver, FileLogObserver | |
160 | logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength, | |
161 | maxRotatedFiles=maxRotatedFiles) | |
162 | application.setComponent(ILogObserver, FileLogObserver(logfile).emit) | |
163 | ||
164 | m = BuildMaster(basedir, configfile, umask) | |
165 | m.setServiceParent(application) | |
166 | m.log_rotation.rotateLength = rotateLength | |
167 | m.log_rotation.maxRotatedFiles = maxRotatedFiles | |
168 | ''; | |
9fb4205e | 169 | in '' |
9fb4205e | 170 | if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then |
6984f454 | 171 | ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}" |
9fb4205e | 172 | rm -f ${varDir}/${project.name}/master.cfg.sample |
e2b96bf5 | 173 | rm -f ${varDir}/${project.name}/buildbot.tac |
9fb4205e | 174 | fi |
e2b96bf5 | 175 | ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac |
ca330baa IB |
176 | # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race |
177 | install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true | |
9fb4205e | 178 | buildbot_secrets=${varDir}/${project.name}/secrets |
6984f454 | 179 | install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets |
ca330baa | 180 | install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap |
9fb4205e | 181 | ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList |
ca330baa | 182 | (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets |
9fb4205e | 183 | )} |
5400b9b6 | 184 | ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name} |
9fb4205e | 185 | ''; |
9fb4205e | 186 | environment = let |
0012da0f IB |
187 | project_env = with lib.attrsets; |
188 | mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" v) project.environment // | |
60d328e2 IB |
189 | mapAttrs' (k: v: nameValuePair "BUILDBOT_PATH_${k}" (v pkgs)) (attrByPath ["builderPaths"] {} project) // |
190 | { BUILDBOT_PROJECT_DIR = ./projects + "/${project.name}"; }; | |
caa08508 | 191 | buildbot_config = pkgs.python3Packages.buildPythonPackage (rec { |
9fb4205e | 192 | name = "buildbot_config-${project.name}"; |
e2b96bf5 | 193 | src = ./projects + "/${project.name}"; |
9fb4205e IB |
194 | format = "other"; |
195 | installPhase = '' | |
caa08508 IB |
196 | mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages} |
197 | cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config | |
9fb4205e IB |
198 | ''; |
199 | }); | |
200 | HOME = "${varDir}/${project.name}"; | |
caa08508 | 201 | PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [ |
256d607c | 202 | pkgs.python3Packages.wokkel |
caa08508 IB |
203 | pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot |
204 | pkgs.python3Packages.buildbot-worker | |
9fb4205e | 205 | buildbot_common buildbot_config |
caa08508 | 206 | ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}"; |
9fb4205e IB |
207 | in project_env // { inherit PYTHONPATH HOME; }; |
208 | ||
209 | serviceConfig = { | |
210 | Type = "forking"; | |
211 | User = "buildbot"; | |
212 | Group = "buildbot"; | |
81b9ff89 IB |
213 | RuntimeDirectory = "buildbot"; |
214 | RuntimeDirectoryPreserve = "yes"; | |
215 | StateDirectory = "buildbot"; | |
6984f454 | 216 | SupplementaryGroups = "keys"; |
9fb4205e IB |
217 | WorkingDirectory = "${varDir}/${project.name}"; |
218 | ExecStart = "${buildbot}/bin/buildbot start"; | |
219 | }; | |
ab8f306d | 220 | }) config.myEnv.buildbot.projects; |
9fb4205e IB |
221 | }; |
222 | } |