1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.gocd-agent;
7in {
8 options = {
9 services.gocd-agent = {
10 enable = mkEnableOption "gocd-agent";
11
12 user = mkOption {
13 default = "gocd-agent";
14 type = types.str;
15 description = ''
16 User the Go.CD agent should execute under.
17 '';
18 };
19
20 group = mkOption {
21 default = "gocd-agent";
22 type = types.str;
23 description = ''
24 If the default user "gocd-agent" is configured then this is the primary
25 group of that user.
26 '';
27 };
28
29 extraGroups = mkOption {
30 type = types.listOf types.str;
31 default = [ ];
32 example = [ "wheel" "docker" ];
33 description = ''
34 List of extra groups that the "gocd-agent" user should be a part of.
35 '';
36 };
37
38 packages = mkOption {
39 default = [ pkgs.stdenv pkgs.jre pkgs.git config.programs.ssh.package pkgs.nix ];
40 type = types.listOf types.package;
41 description = ''
42 Packages to add to PATH for the Go.CD agent process.
43 '';
44 };
45
46 agentConfig = mkOption {
47 default = "";
48 type = types.str;
49 example = ''
50 agent.auto.register.resources=ant,java
51 agent.auto.register.environments=QA,Performance
52 agent.auto.register.hostname=Agent01
53 '';
54 description = ''
55 Agent registration configuration.
56 '';
57 };
58
59 goServer = mkOption {
60 default = "https://127.0.0.1:8154/go";
61 type = types.str;
62 description = ''
63 URL of the GoCD Server to attach the Go.CD Agent to.
64 '';
65 };
66
67 workDir = mkOption {
68 default = "/var/lib/go-agent";
69 type = types.str;
70 description = ''
71 Specifies the working directory in which the Go.CD agent java archive resides.
72 '';
73 };
74
75 initialJavaHeapSize = mkOption {
76 default = "128m";
77 type = types.str;
78 description = ''
79 Specifies the initial java heap memory size for the Go.CD agent java process.
80 '';
81 };
82
83 maxJavaHeapMemory = mkOption {
84 default = "256m";
85 type = types.str;
86 description = ''
87 Specifies the java maximum heap memory size for the Go.CD agent java process.
88 '';
89 };
90
91 startupOptions = mkOption {
92 default = [
93 "-Xms${cfg.initialJavaHeapSize}"
94 "-Xmx${cfg.maxJavaHeapMemory}"
95 "-Djava.io.tmpdir=/tmp"
96 "-Dcruise.console.publish.interval=10"
97 "-Djava.security.egd=file:/dev/./urandom"
98 ];
99 description = ''
100 Specifies startup command line arguments to pass to Go.CD agent
101 java process. Example contains debug and gcLog arguments.
102 '';
103 };
104
105 extraOptions = mkOption {
106 default = [ ];
107 example = [
108 "-X debug"
109 "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006"
110 "-verbose:gc"
111 "-Xloggc:go-agent-gc.log"
112 "-XX:+PrintGCTimeStamps"
113 "-XX:+PrintTenuringDistribution"
114 "-XX:+PrintGCDetails"
115 "-XX:+PrintGC"
116 ];
117 description = ''
118 Specifies additional command line arguments to pass to Go.CD agent
119 java process. Example contains debug and gcLog arguments.
120 '';
121 };
122
123 environment = mkOption {
124 default = { };
125 type = with types; attrsOf str;
126 description = ''
127 Additional environment variables to be passed to the Go.CD agent process.
128 As a base environment, Go.CD agent receives NIX_PATH from
129 <option>environment.sessionVariables</option>, NIX_REMOTE is set to
130 "daemon".
131 '';
132 };
133 };
134 };
135
136 config = mkIf cfg.enable {
137 users.extraGroups = optional (cfg.group == "gocd-agent") {
138 name = "gocd-agent";
139 gid = config.ids.gids.gocd-agent;
140 };
141
142 users.extraUsers = optional (cfg.user == "gocd-agent") {
143 name = "gocd-agent";
144 description = "gocd-agent user";
145 createHome = true;
146 home = cfg.workDir;
147 group = cfg.group;
148 extraGroups = cfg.extraGroups;
149 useDefaultShell = true;
150 uid = config.ids.uids.gocd-agent;
151 };
152
153 systemd.services.gocd-agent = {
154 description = "GoCD Agent";
155 after = [ "network.target" ];
156 wantedBy = [ "multi-user.target" ];
157
158 environment =
159 let
160 selectedSessionVars =
161 lib.filterAttrs (n: v: builtins.elem n [ "NIX_PATH" ])
162 config.environment.sessionVariables;
163 in
164 selectedSessionVars //
165 {
166 NIX_REMOTE = "daemon";
167 AGENT_WORK_DIR = cfg.workDir;
168 AGENT_STARTUP_ARGS = ''${concatStringsSep " " cfg.startupOptions}'';
169 LOG_DIR = cfg.workDir;
170 LOG_FILE = "${cfg.workDir}/go-agent-start.log";
171 } //
172 cfg.environment;
173
174 path = cfg.packages;
175
176 script = ''
177 MPATH="''${PATH}";
178 source /etc/profile
179 export PATH="''${MPATH}:''${PATH}";
180
181 if ! test -f ~/.nixpkgs/config.nix; then
182 mkdir -p ~/.nixpkgs/
183 echo "{ allowUnfree = true; }" > ~/.nixpkgs/config.nix
184 fi
185
186 mkdir -p config
187 rm -f config/autoregister.properties
188 ln -s "${pkgs.writeText "autoregister.properties" cfg.agentConfig}" config/autoregister.properties
189
190 ${pkgs.git}/bin/git config --global --add http.sslCAinfo /etc/ssl/certs/ca-certificates.crt
191 ${pkgs.jre}/bin/java ${concatStringsSep " " cfg.startupOptions} \
192 ${concatStringsSep " " cfg.extraOptions} \
193 -jar ${pkgs.gocd-agent}/go-agent/agent-bootstrapper.jar \
194 -serverUrl ${cfg.goServer}
195 '';
196
197 serviceConfig = {
198 User = cfg.user;
199 WorkingDirectory = cfg.workDir;
200 RestartSec = 30;
201 Restart = "on-failure";
202 };
203 };
204 };
205}