]>
Commit | Line | Data |
---|---|---|
1 | { lib, pkgs, config, ... }: | |
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 | myServices.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.myServices.buildbot.enable { | |
27 | services.duplyBackup.profiles.buildbot = { | |
28 | rootDir = varDir; | |
29 | remotes = [ "eriomem" "ovh" ]; | |
30 | }; | |
31 | ids.uids.buildbot = config.myEnv.buildbot.user.uid; | |
32 | ids.gids.buildbot = config.myEnv.buildbot.user.gid; | |
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; | |
41 | extraGroups = [ "keys" ]; | |
42 | }; | |
43 | ||
44 | services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList | |
45 | (k: project: "/var/secrets/buildbot/${project.name}/webhook-httpd-include") | |
46 | config.myEnv.buildbot.projects; | |
47 | ||
48 | services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: '' | |
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 | |
56 | Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu | |
57 | ||
58 | SetEnvIf X-Url-Scheme https HTTPS=1 | |
59 | ProxyPreserveHost On | |
60 | </Location> | |
61 | <Location /buildbot/${project.name}/change_hook/base> | |
62 | <RequireAny> | |
63 | Require local | |
64 | Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu | |
65 | Include /var/secrets/buildbot/${project.name}/webhook-httpd-include | |
66 | </RequireAny> | |
67 | </Location> | |
68 | '') config.myEnv.buildbot.projects; | |
69 | ||
70 | system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
71 | deps = [ "users" "wrappers" ]; | |
72 | text = '' | |
73 | install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name} | |
74 | ||
75 | ${project.activationScript} | |
76 | ''; | |
77 | }) config.myEnv.buildbot.projects; | |
78 | ||
79 | secrets.keys = ( | |
80 | lib.lists.flatten ( | |
81 | lib.attrsets.mapAttrsToList (k: project: | |
82 | lib.attrsets.mapAttrsToList (k: v: | |
83 | { | |
84 | permissions = "0600"; | |
85 | user = "buildbot"; | |
86 | group = "buildbot"; | |
87 | text = v; | |
88 | dest = "buildbot/${project.name}/${k}"; | |
89 | } | |
90 | ) project.secrets | |
91 | ++ [ | |
92 | { | |
93 | permissions = "0600"; | |
94 | user = "wwwrun"; | |
95 | group = "wwwrun"; | |
96 | text = lib.optionalString (project.webhookTokens != null) '' | |
97 | Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }" | |
98 | ''; | |
99 | dest = "buildbot/${project.name}/webhook-httpd-include"; | |
100 | } | |
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 | { | |
111 | BUILDBOT_PROJECT_DIR = ./projects + "/${project.name}"; | |
112 | BUILDBOT_WORKER_PORT = builtins.toString project.workerPort; | |
113 | BUILDBOT_HOST = config.hostEnv.fqdn; | |
114 | BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system"; | |
115 | }; | |
116 | in builtins.concatStringsSep "\n" | |
117 | (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env); | |
118 | } | |
119 | ] | |
120 | ) config.myEnv.buildbot.projects | |
121 | ) | |
122 | ) ++ [ | |
123 | { | |
124 | permissions = "0600"; | |
125 | user = "buildbot"; | |
126 | group = "buildbot"; | |
127 | text = config.myEnv.buildbot.ldap.password; | |
128 | dest = "buildbot/ldap"; | |
129 | } | |
130 | { | |
131 | permissions = "0600"; | |
132 | user = "buildbot"; | |
133 | group = "buildbot"; | |
134 | text = config.myEnv.buildbot.workerPassword; | |
135 | dest = "buildbot/worker_password"; | |
136 | } | |
137 | { | |
138 | permissions = "0600"; | |
139 | user = "buildbot"; | |
140 | group = "buildbot"; | |
141 | text = config.myEnv.buildbot.ssh_key.private; | |
142 | dest = "buildbot/ssh_key"; | |
143 | } | |
144 | ]; | |
145 | ||
146 | services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
147 | restart = true; | |
148 | paths = [ | |
149 | "/var/secrets/buildbot/ldap" | |
150 | "/var/secrets/buildbot/worker_password" | |
151 | "/var/secrets/buildbot/ssh_key" | |
152 | "/var/secrets/buildbot/${project.name}/environment_file" | |
153 | ] ++ lib.attrsets.mapAttrsToList (k: v: "/var/secrets/buildbot/${project.name}/${k}") project.secrets; | |
154 | }) config.myEnv.buildbot.projects; | |
155 | ||
156 | systemd.slices.buildbot = { | |
157 | description = "buildbot slice"; | |
158 | }; | |
159 | ||
160 | networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects; | |
161 | systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
162 | description = "Buildbot Continuous Integration Server ${project.name}."; | |
163 | after = [ "network-online.target" ]; | |
164 | wantedBy = [ "multi-user.target" ]; | |
165 | path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs); | |
166 | preStart = let | |
167 | master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg"; | |
168 | tac_file = pkgs.writeText "buildbot.tac" '' | |
169 | import os | |
170 | ||
171 | from twisted.application import service | |
172 | from buildbot.master import BuildMaster | |
173 | ||
174 | basedir = '${varDir}/${project.name}' | |
175 | rotateLength = 10000000 | |
176 | maxRotatedFiles = 10 | |
177 | configfile = '${master-cfg}' | |
178 | ||
179 | # Default umask for server | |
180 | umask = None | |
181 | ||
182 | # if this is a relocatable tac file, get the directory containing the TAC | |
183 | if basedir == '.': | |
184 | import os | |
185 | basedir = os.path.abspath(os.path.dirname(__file__)) | |
186 | ||
187 | # note: this line is matched against to check that this is a buildmaster | |
188 | # directory; do not edit it. | |
189 | application = service.Application('buildmaster') | |
190 | from twisted.python.logfile import LogFile | |
191 | from twisted.python.log import ILogObserver, FileLogObserver | |
192 | logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength, | |
193 | maxRotatedFiles=maxRotatedFiles) | |
194 | application.setComponent(ILogObserver, FileLogObserver(logfile).emit) | |
195 | ||
196 | m = BuildMaster(basedir, configfile, umask) | |
197 | m.setServiceParent(application) | |
198 | m.log_rotation.rotateLength = rotateLength | |
199 | m.log_rotation.maxRotatedFiles = maxRotatedFiles | |
200 | ''; | |
201 | in '' | |
202 | if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then | |
203 | ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}" | |
204 | rm -f ${varDir}/${project.name}/master.cfg.sample | |
205 | rm -f ${varDir}/${project.name}/buildbot.tac | |
206 | fi | |
207 | ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac | |
208 | # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race | |
209 | install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true | |
210 | buildbot_secrets=${varDir}/${project.name}/secrets | |
211 | install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets | |
212 | install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap | |
213 | install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/worker_password $buildbot_secrets/worker_password | |
214 | ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList | |
215 | (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets | |
216 | )} | |
217 | ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name} | |
218 | ''; | |
219 | environment = let | |
220 | buildbot_config = pkgs.python3Packages.buildPythonPackage (rec { | |
221 | name = "buildbot_config-${project.name}"; | |
222 | src = ./projects + "/${project.name}"; | |
223 | format = "other"; | |
224 | installPhase = '' | |
225 | mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages} | |
226 | cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config | |
227 | ''; | |
228 | }); | |
229 | HOME = "${varDir}/${project.name}"; | |
230 | PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [ | |
231 | pkgs.python3Packages.libvirt | |
232 | pkgs.python3Packages.wokkel | |
233 | pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot | |
234 | pkgs.python3Packages.buildbot-worker | |
235 | buildbot_common buildbot_config | |
236 | ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}"; | |
237 | in { inherit PYTHONPATH HOME; }; | |
238 | ||
239 | serviceConfig = { | |
240 | Slice = "buildbot.slice"; | |
241 | Type = "forking"; | |
242 | User = "buildbot"; | |
243 | Group = "buildbot"; | |
244 | RuntimeDirectory = "buildbot"; | |
245 | RuntimeDirectoryPreserve = "yes"; | |
246 | StateDirectory = "buildbot"; | |
247 | SupplementaryGroups = "keys"; | |
248 | WorkingDirectory = "${varDir}/${project.name}"; | |
249 | ExecStart = "${buildbot}/bin/buildbot start"; | |
250 | EnvironmentFile = "/var/secrets/buildbot/${project.name}/environment_file"; | |
251 | }; | |
252 | }) config.myEnv.buildbot.projects; | |
253 | }; | |
254 | } |