aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/jenkins/default.nix
blob: 4fbaf1bad005efb910ca4110ea7ef8bbafd356bf (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
{ config, lib, pkgs, ... }:

let

  inherit (builtins)
    attrNames isBool isString ;

  inherit (lib)
    concatMapStringsSep concatStringsSep escape filterAttrs foldAttrs foldl
    hasPrefix 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 = n: c: if hasPrefix "/" c then c else pkgs.writeXML n c;
  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;

  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

      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";
      tmpdir = "${cfg.home}/tmp";

      startJenkins = pkgs.writeBashScript "jenkins-${name}-start" ''
        set -euo pipefail
        umask 0027
        export HOME='${cfg.home}'

        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 [ -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 -- '${tmpdir}'
        mkdir -p -- '${tmpdir}'
        exec ${cfg.jre}/bin/java \
          -DJENKINS_HOME='${cfg.home}' \
          -Djava.io.tmpdir='${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 = startJenkins;
          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;
  };

}