summary refs log tree commit diff
path: root/nixos/modules/services/continuous-integration/gocd-server/default.nix
blob: 3540656f934480fed6e7a3c5dc49a5216a2a584a (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{ config, lib, options, pkgs, ... }:

with lib;

let
  cfg = config.services.gocd-server;
  opt = options.services.gocd-server;
in {
  options = {
    services.gocd-server = {
      enable = mkEnableOption "gocd-server";

      user = mkOption {
        default = "gocd-server";
        type = types.str;
        description = ''
          User the Go.CD server should execute under.
        '';
      };

      group = mkOption {
        default = "gocd-server";
        type = types.str;
        description = ''
          If the default user "gocd-server" is configured then this is the primary group of that user.
        '';
      };

      extraGroups = mkOption {
        default = [ ];
        type = types.listOf types.str;
        example = [ "wheel" "docker" ];
        description = ''
          List of extra groups that the "gocd-server" user should be a part of.
        '';
      };

      listenAddress = mkOption {
        default = "0.0.0.0";
        example = "localhost";
        type = types.str;
        description = ''
          Specifies the bind address on which the Go.CD server HTTP interface listens.
        '';
      };

      port = mkOption {
        default = 8153;
        type = types.int;
        description = ''
          Specifies port number on which the Go.CD server HTTP interface listens.
        '';
      };

      sslPort = mkOption {
        default = 8154;
        type = types.int;
        description = ''
          Specifies port number on which the Go.CD server HTTPS interface listens.
        '';
      };

      workDir = mkOption {
        default = "/var/lib/go-server";
        type = types.str;
        description = ''
          Specifies the working directory in which the Go.CD server java archive resides.
        '';
      };

      packages = mkOption {
        default = [ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ];
        defaultText = literalExpression "[ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ]";
        type = types.listOf types.package;
        description = ''
          Packages to add to PATH for the Go.CD server's process.
        '';
      };

      initialJavaHeapSize = mkOption {
        default = "512m";
        type = types.str;
        description = ''
          Specifies the initial java heap memory size for the Go.CD server's java process.
        '';
      };

      maxJavaHeapMemory = mkOption {
        default = "1024m";
        type = types.str;
        description = ''
          Specifies the java maximum heap memory size for the Go.CD server's java process.
        '';
      };

      startupOptions = mkOption {
        type = types.listOf types.str;
        default = [
          "-Xms${cfg.initialJavaHeapSize}"
          "-Xmx${cfg.maxJavaHeapMemory}"
          "-Dcruise.listen.host=${cfg.listenAddress}"
          "-Duser.language=en"
          "-Djruby.rack.request.size.threshold.bytes=30000000"
          "-Duser.country=US"
          "-Dcruise.config.dir=${cfg.workDir}/conf"
          "-Dcruise.config.file=${cfg.workDir}/conf/cruise-config.xml"
          "-Dcruise.server.port=${toString cfg.port}"
          "-Dcruise.server.ssl.port=${toString cfg.sslPort}"
        ];
        defaultText = literalExpression ''
          [
            "-Xms''${config.${opt.initialJavaHeapSize}}"
            "-Xmx''${config.${opt.maxJavaHeapMemory}}"
            "-Dcruise.listen.host=''${config.${opt.listenAddress}}"
            "-Duser.language=en"
            "-Djruby.rack.request.size.threshold.bytes=30000000"
            "-Duser.country=US"
            "-Dcruise.config.dir=''${config.${opt.workDir}}/conf"
            "-Dcruise.config.file=''${config.${opt.workDir}}/conf/cruise-config.xml"
            "-Dcruise.server.port=''${toString config.${opt.port}}"
            "-Dcruise.server.ssl.port=''${toString config.${opt.sslPort}}"
          ]
        '';

        description = ''
          Specifies startup command line arguments to pass to Go.CD server
          java process.
        '';
      };

      extraOptions = mkOption {
        default = [ ];
        type = types.listOf types.str;
        example = [
          "-X debug"
          "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
          "-verbose:gc"
          "-Xloggc:go-server-gc.log"
          "-XX:+PrintGCTimeStamps"
          "-XX:+PrintTenuringDistribution"
          "-XX:+PrintGCDetails"
          "-XX:+PrintGC"
        ];
        description = ''
          Specifies additional command line arguments to pass to Go.CD server's
          java process.  Example contains debug and gcLog arguments.
        '';
      };

      environment = mkOption {
        default = { };
        type = with types; attrsOf str;
        description = ''
          Additional environment variables to be passed to the gocd-server process.
          As a base environment, gocd-server receives NIX_PATH from
          <option>environment.sessionVariables</option>, NIX_REMOTE is set to
          "daemon".
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    users.groups = optionalAttrs (cfg.group == "gocd-server") {
      gocd-server.gid = config.ids.gids.gocd-server;
    };

    users.users = optionalAttrs (cfg.user == "gocd-server") {
      gocd-server = {
        description = "gocd-server user";
        createHome = true;
        home = cfg.workDir;
        group = cfg.group;
        extraGroups = cfg.extraGroups;
        useDefaultShell = true;
        uid = config.ids.uids.gocd-server;
      };
    };

    systemd.services.gocd-server = {
      description = "GoCD Server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      environment =
        let
          selectedSessionVars =
            lib.filterAttrs (n: v: builtins.elem n [ "NIX_PATH" ])
              config.environment.sessionVariables;
        in
          selectedSessionVars //
            { NIX_REMOTE = "daemon";
            } //
            cfg.environment;

      path = cfg.packages;

      script = ''
        ${pkgs.git}/bin/git config --global --add http.sslCAinfo /etc/ssl/certs/ca-certificates.crt
        ${pkgs.jre}/bin/java -server ${concatStringsSep " " cfg.startupOptions} \
                               ${concatStringsSep " " cfg.extraOptions}  \
                              -jar ${pkgs.gocd-server}/go-server/go.jar
      '';

      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.workDir;
      };
    };
  };
}