blob: 210a4ea1bf24b6e176eb3e0950538c25da4953d0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
{ config, lib, pkgs, ... }:
let
inherit (builtins)
attrNames isBool isString replaceStrings ;
inherit (lib)
concatMapStringsSep concatStringsSep escape filterAttrs foldAttrs foldl
hasPrefix mapAttrs mapAttrs' mapAttrsToList mkOption nameValuePair optionalString
unique ;
inherit (lib.types)
attrsOf submodule ;
explicit = filterAttrs (n: v: n != "_module" && v != null);
instances = explicit config.nixsap.apps.jenkins;
users = mapAttrsToList (_: i: i.user) instances;
maybeFile = name: cnt:
let norm = replaceStrings [" "] ["-"] name;
in if hasPrefix "/" cnt
then "${cnt}"
else pkgs.writeXML norm cnt;
configFiles = name: cfg: mapAttrs (n: v: maybeFile "jenkins-${name}-${n}" v) cfg.config;
jobFiles = name: cfg: mapAttrs (n: v: maybeFile "jenkins-${name}-job-${n}.xml" v) cfg.jobs;
nodeFiles = name: cfg: mapAttrs (n: v: maybeFile "jenkins-${name}-node-${n}.xml" v) cfg.nodes;
keyrings =
let
# This requires read-write mode of evaluation:
keys = n: i: import (pkgs.xinclude2nix (
(mapAttrsToList (_: f: f) (configFiles n i))
++ (unique (mapAttrsToList (_: f: f) (jobFiles n i)))
));
ik = mapAttrsToList (n: i: { "${i.user}" = keys n i; } ) instances;
in foldAttrs (l: r: l ++ r) [] ik;
mkService = name: cfg:
let
inherit (cfg.jre) properties;
mkOpt = n: v: if isBool v then (if v then "--${n}" else "")
else if isString v then "--${n}='${v}'"
else "--${n}=${toString v}";
path = ".war.path";
start = pkgs.writeBashScriptBin "jenkins-${name}" ''
set -euo pipefail
umask 0027
export HOME='${cfg.home}'
export SHELL='${pkgs.bash}/bin/bash'
cd '${cfg.home}'
find . -maxdepth 1 \( \
-iname '*.xml' \
-o -iname '*.bak' \
-o -iname '*.log' \
-o -iname '*.tmp' \
-o -iname '*.txt' \
\) -delete
${concatStringsSep "\n" (
mapAttrsToList (n: p:
# XXX Jenkins does not support XInclude
# XXX We use XInclude to include secret files (keys)
''
${pkgs.libxml2}/bin/xmllint --xinclude --format '${p}' > '${n}'
'') (configFiles name cfg)
)}
if [ -d jobs ]; then
find jobs -maxdepth 1 -mindepth 1 -type d \
${concatMapStringsSep " " (k: "-not -name '${escape [ "[" ] k}'") (attrNames cfg.jobs)} \
-print0 | xargs -0 --verbose --no-run-if-empty rm -rf
fi
${concatStringsSep "\n" (
mapAttrsToList (n: p: ''
mkdir -p -- 'jobs/${n}'
rm -rf -- 'jobs/${n}/config.xml'
${pkgs.libxml2}/bin/xmllint --xinclude --format '${p}' > 'jobs/${n}/config.xml'
'') (jobFiles name cfg)
)}
if [ -d nodes ]; then
find nodes -maxdepth 1 -mindepth 1 -type d \
${concatMapStringsSep " " (k: "-not -name '${escape [ "[" ] k}'") (attrNames cfg.nodes)} \
-print0 | xargs -0 --verbose --no-run-if-empty rm -rf
fi
${concatStringsSep "\n" (
mapAttrsToList (n: p: ''
mkdir -p -- 'nodes/${n}'
rm -rf -- 'nodes/${n}/config.xml'
${pkgs.libxml2}/bin/xmllint --xinclude --format '${p}' > 'nodes/${n}/config.xml'
'') (nodeFiles name cfg)
)}
mkdir -p secrets
echo ${if cfg.master-access-control then "false" else "true"} > secrets/slave-to-master-security-kill-switch
if [ -f ${path} ]; then
old=$(cat ${path})
else
old=
fi
# FIXME: make sure old content is flushed
if [ '${cfg.war}' != "$old" ]; then
rm -rf war plugins
echo '${cfg.war}' > ${path}
fi
rm -rf -- '${cfg.jre.properties.java.io.tmpdir}'
mkdir -p -- '${cfg.jre.properties.java.io.tmpdir}'
# TODO: generalize properties, maybe put in a file:
exec ${cfg.jre.package}/bin/java \
-DJENKINS_HOME='${cfg.home}' \
${optionalString (properties.hudson.model.DirectoryBrowserSupport.CSP != null)
''-Dhudson.model.DirectoryBrowserSupport.CSP="${properties.hudson.model.DirectoryBrowserSupport.CSP}"''} \
${optionalString (properties.java.util.logging.config.file != null)
"-Djava.util.logging.config.file='${properties.java.util.logging.config.file}'"} \
-Djava.io.tmpdir='${properties.java.io.tmpdir}' \
-jar '${cfg.war}' \
${concatStringsSep " \\\n " (mapAttrsToList mkOpt (explicit cfg.options))}
'';
in {
"jenkins-${name}" = {
description = "Jenkins (${name}) automation server";
wantedBy = [ "multi-user.target" ];
after = [ "keys.target" "network.target" "local-fs.target" ];
inherit (cfg) path;
preStart = ''
mkdir -p -- '${cfg.home}'
# XXX ignore potentially dangling symlinks
# XXX like lastStable -> builds/lastStableBuild.
# XXX chmod/chown fail on them
find '${cfg.home}' -not -type l \( \
-not -user '${cfg.user}' \
-or -not -group '${cfg.user}' \
-or \( -type d -not -perm -u=wrx,g=rx \) \
-or \( -type f -not -perm -u=rw,g=r \) \
\) \
-exec chown -c -- '${cfg.user}:${cfg.user}' {} + \
-exec chmod -c -- u=rwX,g=rX,o= {} +
'';
serviceConfig = {
ExecStart = "${start}/bin/jenkins-${name}";
KillMode = "mixed";
PermissionsStartOnly = true;
Restart = "always";
TimeoutSec = 0;
User = cfg.user;
};
};
};
in {
options.nixsap.apps.jenkins = mkOption {
description = "Jenkins instances";
default = {};
type = attrsOf (submodule (import ./instance.nix pkgs));
};
config = {
systemd.services = foldl (a: b: a//b) {} (mapAttrsToList mkService instances);
nixsap.deployment.keyrings = keyrings;
nixsap.system.users.daemons = users;
# Although jenkins user is a daemon, many tools require proper home
# directory and ignore $HOME (e. g. Maven). This assumes each Jenkins
# instance has its own user (this is true because i.user is read-only):
users.users = mapAttrs' (_: i: nameValuePair i.user {home = i.home;}) instances;
};
}
|