@@ -19,11 +19,7 @@ in
1919 options = {
2020
2121 services.tomcat = {
22−
23− enable = mkOption {
24− default = false;
25− description = "Whether to enable Apache Tomcat";
26− };
22+ enable = mkEnableOption "Apache Tomcat";
2723
2824 package = mkOption {
2925 type = types.package;
@@ -36,23 +32,28 @@ in
3632 };
3733
3834 baseDir = mkOption {
35+ type = lib.types.path;
3936 default = "/var/tomcat";
4037 description = "Location where Tomcat stores configuration files, webapplications and logfiles";
4138 };
4239
4340 logDirs = mkOption {
4441 default = [];
42+ type = types.listOf types.path;
4543 description = "Directories to create in baseDir/logs/";
4644 };
4745
4846 extraConfigFiles = mkOption {
4947 default = [];
48+ type = types.listOf types.path;
5049 description = "Extra configuration files to pull into the tomcat conf directory";
5150 };
5251
53− environment = mkOption {
54− default = "";
55− description = "File to be sourced before executing tomcat. Can be used to set environment variables";
52+ extraEnvironment = mkOption {
53+ type = types.listOf types.str;
54+ default = [];
55+ example = [ "ENVIRONMENT=production" ];
56+ description = "Environment Variables to pass to the tomcat service";
5657 };
5758
5859 extraGroups = mkOption {
@@ -62,39 +63,46 @@ in
6263 };
6364
6465 user = mkOption {
66+ type = types.str;
6567 default = "tomcat";
6668 description = "User account under which Apache Tomcat runs.";
6769 };
6870
6971 group = mkOption {
72+ type = types.str;
7073 default = "tomcat";
7174 description = "Group account under which Apache Tomcat runs.";
7275 };
7376
7477 javaOpts = mkOption {
78+ type = types.either (types.listOf types.str) types.str;
7579 default = "";
7680 description = "Parameters to pass to the Java Virtual Machine which spawns Apache Tomcat";
7781 };
7882
7983 catalinaOpts = mkOption {
84+ type = types.either (types.listOf types.str) types.str;
8085 default = "";
8186 description = "Parameters to pass to the Java Virtual Machine which spawns the Catalina servlet container";
8287 };
8388
8489 sharedLibs = mkOption {
90+ type = types.listOf types.str;
8591 default = [];
8692 description = "List containing JAR files or directories with JAR files which are libraries shared by the web applications";
8793 };
8894
8995 serverXml = mkOption {
96+ type = types.lines;
9097 default = "";
9198 description = "
9299 Verbatim server.xml configuration.
93− This is mutualyl exclusive with the virtualHosts options.
100+ This is mutually exclusive with the virtualHosts options.
94101 ";
95102 };
96103
97104 commonLibs = mkOption {
105+ type = types.listOf types.str;
98106 default = [];
99107 description = "List containing JAR files or directories with JAR files which are libraries shared by the web applications and the servlet container";
100108 };
@@ -107,11 +115,21 @@ in
107115 };
108116
109117 virtualHosts = mkOption {
118+ type = types.listOf (types.submodule {
119+ options = {
120+ name = mkOption {
121+ type = types.listOf types.str;
122+ description = "name of the virtualhost";
123+ default = [];
124+ };
125+ };
126+ });
110127 default = [];
111128 description = "List consisting of a virtual host name and a list of web applications to deploy on each virtual host";
112129 };
113130
114131 logPerVirtualHost = mkOption {
132+ type = types.bool;
115133 default = false;
116134 description = "Whether to enable logging per virtual host.";
117135 };
@@ -127,11 +145,13 @@ in
127145
128146 enable = mkOption {
129147 default = false;
148+ type = types.bool;
130149 description = "Whether to enable an Apache Axis2 container";
131150 };
132151
133152 services = mkOption {
134153 default = [];
154+ type = types.listOf types.str;
135155 description = "List containing AAR files or directories with AAR files which are web services to be deployed on Axis2";
136156 };
137157
@@ -163,149 +183,104 @@ in
163183 description = "Apache Tomcat server";
164184 wantedBy = [ "multi-user.target" ];
165185 after = [ "network.target" ];
166− serviceConfig.Type = "oneshot";
167− serviceConfig.RemainAfterExit = true;
168186
169187 preStart = ''
170188 # Create the base directory
171− mkdir -p ${cfg.baseDir}
172−
173− mkdir -p ${cfg.baseDir}
189+ mkdir -p \
190+ ${cfg.baseDir}/{conf,virtualhosts,logs,temp,lib,shared/lib,webapps,work}
191+ chown ${cfg.user}:${cfg.group} \
192+ ${cfg.baseDir}/{conf,virtualhosts,logs,temp,lib,shared/lib,webapps,work}
174193
175194 # Create a symlink to the bin directory of the tomcat component
176195 ln -sfn ${tomcat}/bin ${cfg.baseDir}/bin
177196
178− # Create a conf/ directory
179− mkdir -p ${cfg.baseDir}/conf
180− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/conf
181−
182197 # Symlink the config files in the conf/ directory (except for catalina.properties and server.xml)
183− for i in $(ls ${tomcat}/conf | grep -v catalina.properties | grep -v server.xml)
184− do
185− ln -sfn ${tomcat}/conf/$i ${cfg.baseDir}/conf/`basename $i`
198+ for i in $(ls ${tomcat}/conf | grep -v catalina.properties | grep -v server.xml); do
199+ ln -sfn ${tomcat}/conf/$i ${cfg.baseDir}/conf/`basename $i`
186200 done
187201
188202 ${if cfg.extraConfigFiles != [] then ''
189− for i in ${toString cfg.extraConfigFiles}
190− do
203+ for i in ${toString cfg.extraConfigFiles}; do
191204 ln -sfn $i ${cfg.baseDir}/conf/`basename $i`
192205 done
193206 '' else ""}
194207
195− # Create subdirectory for virtual hosts
196− mkdir -p ${cfg.baseDir}/virtualhosts
197−
198208 # Create a modified catalina.properties file
199209 # Change all references from CATALINA_HOME to CATALINA_BASE and add support for shared libraries
200210 sed -e 's|''${catalina.home}|''${catalina.base}|g' \
201− -e 's|shared.loader=|shared.loader=''${catalina.base}/shared/lib/*.jar|' \
202− ${tomcat}/conf/catalina.properties > ${cfg.baseDir}/conf/catalina.properties
211+ -e 's|shared.loader=|shared.loader=''${catalina.base}/shared/lib/*.jar|' \
212+ ${tomcat}/conf/catalina.properties > ${cfg.baseDir}/conf/catalina.properties
203213
204214 ${if cfg.serverXml != "" then ''
205− cat <<'EOF' > ${cfg.baseDir}/conf/server.xml
206− ${cfg.serverXml}EOF
215+ cp -f ${pkgs.writeTextDir "server.xml" cfg.serverXml}/* ${cfg.baseDir}/conf/
207216 '' else ''
208217 # Create a modified server.xml which also includes all virtual hosts
209218 sed -e "/<Engine name=\"Catalina\" defaultHost=\"localhost\">/a\ ${toString (map (virtualHost: ''<Host name=\"${virtualHost.name}\" appBase=\"virtualhosts/${virtualHost.name}/webapps\" unpackWARs=\"true\" autoDeploy=\"true\" xmlValidation=\"false\" xmlNamespaceAware=\"false\" >${if cfg.logPerVirtualHost then ''<Valve className=\"org.apache.catalina.valves.AccessLogValve\" directory=\"logs/${virtualHost.name}\" prefix=\"${virtualHost.name}_access_log.\" pattern=\"combined\" resolveHosts=\"false\"/>'' else ""}</Host>'') cfg.virtualHosts)}" \
210219 ${tomcat}/conf/server.xml > ${cfg.baseDir}/conf/server.xml
211220 ''
212221 }
213− # Create a logs/ directory
214− mkdir -p ${cfg.baseDir}/logs
215− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs
216− ${if cfg.logDirs != [] then ''
217− for i in ${toString cfg.logDirs}; do
218− mkdir -p ${cfg.baseDir}/logs/$i
219− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/$i
220− done
221− '' else ""}
222− ${if cfg.logPerVirtualHost then
223− toString (map (h: ''
224− mkdir -p ${cfg.baseDir}/logs/${h.name}
225− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/${h.name}
226− '') cfg.virtualHosts) else ''''}
227−
228− # Create a temp/ directory
229− mkdir -p ${cfg.baseDir}/temp
230− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/temp
231−
232− # Create a lib/ directory
233− mkdir -p ${cfg.baseDir}/lib
234− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/lib
235−
236− # Create a shared/lib directory
237− mkdir -p ${cfg.baseDir}/shared/lib
238− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/shared/lib
239−
240− # Create a webapps/ directory
241− mkdir -p ${cfg.baseDir}/webapps
242− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/webapps
222+ ${optionalString (cfg.logDirs != []) ''
223+ for i in ${toString cfg.logDirs}; do
224+ mkdir -p ${cfg.baseDir}/logs/$i
225+ chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/$i
226+ done
227+ ''}
228+ ${optionalString cfg.logPerVirtualHost (toString (map (h: ''
229+ mkdir -p ${cfg.baseDir}/logs/${h.name}
230+ chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/${h.name}
231+ '') cfg.virtualHosts))}
243232
244233 # Symlink all the given common libs files or paths into the lib/ directory
245− for i in ${tomcat} ${toString cfg.commonLibs}
246− do
247− if [ -f $i ]
248− then
249− # If the given web application is a file, symlink it into the common/lib/ directory
250− ln -sfn $i ${cfg.baseDir}/lib/`basename $i`
251− elif [ -d $i ]
252− then
253− # If the given web application is a directory, then iterate over the files
254− # in the special purpose directories and symlink them into the tomcat tree
255−
256− for j in $i/lib/*
257− do
258− ln -sfn $j ${cfg.baseDir}/lib/`basename $j`
259− done
260− fi
234+ for i in ${tomcat} ${toString cfg.commonLibs}; do
235+ if [ -f $i ]; then
236+ # If the given web application is a file, symlink it into the common/lib/ directory
237+ ln -sfn $i ${cfg.baseDir}/lib/`basename $i`
238+ elif [ -d $i ]; then
239+ # If the given web application is a directory, then iterate over the files
240+ # in the special purpose directories and symlink them into the tomcat tree
241+
242+ for j in $i/lib/*; do
243+ ln -sfn $j ${cfg.baseDir}/lib/`basename $j`
244+ done
245+ fi
261246 done
262247
263248 # Symlink all the given shared libs files or paths into the shared/lib/ directory
264− for i in ${toString cfg.sharedLibs}
265− do
266− if [ -f $i ]
267− then
268− # If the given web application is a file, symlink it into the common/lib/ directory
269− ln -sfn $i ${cfg.baseDir}/shared/lib/`basename $i`
270− elif [ -d $i ]
271− then
272− # If the given web application is a directory, then iterate over the files
273− # in the special purpose directories and symlink them into the tomcat tree
274−
275− for j in $i/shared/lib/*
276− do
277− ln -sfn $j ${cfg.baseDir}/shared/lib/`basename $j`
278− done
279− fi
249+ for i in ${toString cfg.sharedLibs}; do
250+ if [ -f $i ]; then
251+ # If the given web application is a file, symlink it into the common/lib/ directory
252+ ln -sfn $i ${cfg.baseDir}/shared/lib/`basename $i`
253+ elif [ -d $i ]; then
254+ # If the given web application is a directory, then iterate over the files
255+ # in the special purpose directories and symlink them into the tomcat tree
256+
257+ for j in $i/shared/lib/*; do
258+ ln -sfn $j ${cfg.baseDir}/shared/lib/`basename $j`
259+ done
260+ fi
280261 done
281262
282263 # Symlink all the given web applications files or paths into the webapps/ directory
283− for i in ${toString cfg.webapps}
284− do
285− if [ -f $i ]
286− then
287− # If the given web application is a file, symlink it into the webapps/ directory
288− ln -sfn $i ${cfg.baseDir}/webapps/`basename $i`
289− elif [ -d $i ]
290− then
291− # If the given web application is a directory, then iterate over the files
292− # in the special purpose directories and symlink them into the tomcat tree
293−
294− for j in $i/webapps/*
295− do
296− ln -sfn $j ${cfg.baseDir}/webapps/`basename $j`
297− done
264+ for i in ${toString cfg.webapps}; do
265+ if [ -f $i ]; then
266+ # If the given web application is a file, symlink it into the webapps/ directory
267+ ln -sfn $i ${cfg.baseDir}/webapps/`basename $i`
268+ elif [ -d $i ]; then
269+ # If the given web application is a directory, then iterate over the files
270+ # in the special purpose directories and symlink them into the tomcat tree
271+
272+ for j in $i/webapps/*; do
273+ ln -sfn $j ${cfg.baseDir}/webapps/`basename $j`
274+ done
298275
299− # Also symlink the configuration files if they are included
300− if [ -d $i/conf/Catalina ]
301− then
302− for j in $i/conf/Catalina/*
303− do
304− mkdir -p ${cfg.baseDir}/conf/Catalina/localhost
305− ln -sfn $j ${cfg.baseDir}/conf/Catalina/localhost/`basename $j`
306− done
307− fi
276+ # Also symlink the configuration files if they are included
277+ if [ -d $i/conf/Catalina ]; then
278+ for j in $i/conf/Catalina/*; do
279+ mkdir -p ${cfg.baseDir}/conf/Catalina/localhost
280+ ln -sfn $j ${cfg.baseDir}/conf/Catalina/localhost/`basename $j`
281+ done
308282 fi
283+ fi
309284 done
310285
311286 ${toString (map (virtualHost: ''
@@ -317,99 +292,79 @@ in
317292
318293 # Symlink all the given web applications files or paths into the webapps/ directory
319294 # of this virtual host
320− for i in "${if virtualHost ? webapps then toString virtualHost.webapps else ""}"
321− do
322− if [ -f $i ]
323− then
324− # If the given web application is a file, symlink it into the webapps/ directory
325− ln -sfn $i ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $i`
326− elif [ -d $i ]
327− then
328− # If the given web application is a directory, then iterate over the files
329− # in the special purpose directories and symlink them into the tomcat tree
330−
331− for j in $i/webapps/*
332− do
333− ln -sfn $j ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $j`
334− done
335−
336− # Also symlink the configuration files if they are included
337− if [ -d $i/conf/Catalina ]
338− then
339− for j in $i/conf/Catalina/*
340− do
341− mkdir -p ${cfg.baseDir}/conf/Catalina/${virtualHost.name}
342− ln -sfn $j ${cfg.baseDir}/conf/Catalina/${virtualHost.name}/`basename $j`
343− done
344− fi
295+ for i in "${if virtualHost ? webapps then toString virtualHost.webapps else ""}"; do
296+ if [ -f $i ]; then
297+ # If the given web application is a file, symlink it into the webapps/ directory
298+ ln -sfn $i ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $i`
299+ elif [ -d $i ]; then
300+ # If the given web application is a directory, then iterate over the files
301+ # in the special purpose directories and symlink them into the tomcat tree
302+
303+ for j in $i/webapps/*; do
304+ ln -sfn $j ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $j`
305+ done
306+
307+ # Also symlink the configuration files if they are included
308+ if [ -d $i/conf/Catalina ]; then
309+ for j in $i/conf/Catalina/*; do
310+ mkdir -p ${cfg.baseDir}/conf/Catalina/${virtualHost.name}
311+ ln -sfn $j ${cfg.baseDir}/conf/Catalina/${virtualHost.name}/`basename $j`
312+ done
345313 fi
314+ fi
346315 done
347−
348− ''
349− ) cfg.virtualHosts) }
350−
351− # Create a work/ directory
352− mkdir -p ${cfg.baseDir}/work
353− chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/work
354−
355− ${if cfg.axis2.enable then
356− ''
357− # Copy the Axis2 web application
358− cp -av ${pkgs.axis2}/webapps/axis2 ${cfg.baseDir}/webapps
359−
360− # Turn off addressing, which causes many errors
361− sed -i -e 's%<module ref="addressing"/>%<!-- <module ref="addressing"/> -->%' ${cfg.baseDir}/webapps/axis2/WEB-INF/conf/axis2.xml
362−
363− # Modify permissions on the Axis2 application
364− chown -R ${cfg.user}:${cfg.group} ${cfg.baseDir}/webapps/axis2
365−
366− # Symlink all the given web service files or paths into the webapps/axis2/WEB-INF/services directory
367− for i in ${toString cfg.axis2.services}
368− do
369− if [ -f $i ]
370− then
371− # If the given web service is a file, symlink it into the webapps/axis2/WEB-INF/services
372− ln -sfn $i ${cfg.baseDir}/webapps/axis2/WEB-INF/services/`basename $i`
373− elif [ -d $i ]
374− then
375− # If the given web application is a directory, then iterate over the files
376− # in the special purpose directories and symlink them into the tomcat tree
377−
378− for j in $i/webapps/axis2/WEB-INF/services/*
379− do
380− ln -sfn $j ${cfg.baseDir}/webapps/axis2/WEB-INF/services/`basename $j`
381− done
382−
383− # Also symlink the configuration files if they are included
384− if [ -d $i/conf/Catalina ]
385− then
386− for j in $i/conf/Catalina/*
387− do
388− ln -sfn $j ${cfg.baseDir}/conf/Catalina/localhost/`basename $j`
389− done
390− fi
391− fi
392− done
393− ''
394− else ""}
395− '';
396−
397− script = ''
398− ${if cfg.environment != "" then ''
399− if [ -r ${cfg.environment} ]; then
400− . ${cfg.environment}
316+ '') cfg.virtualHosts)}
317+
318+ ${optionalString cfg.axis2.enable ''
319+ # Copy the Axis2 web application
320+ cp -av ${pkgs.axis2}/webapps/axis2 ${cfg.baseDir}/webapps
321+
322+ # Turn off addressing, which causes many errors
323+ sed -i -e 's%<module ref="addressing"/>%<!-- <module ref="addressing"/> -->%' ${cfg.baseDir}/webapps/axis2/WEB-INF/conf/axis2.xml
324+
325+ # Modify permissions on the Axis2 application
326+ chown -R ${cfg.user}:${cfg.group} ${cfg.baseDir}/webapps/axis2
327+
328+ # Symlink all the given web service files or paths into the webapps/axis2/WEB-INF/services directory
329+ for i in ${toString cfg.axis2.services}; do
330+ if [ -f $i ]; then
331+ # If the given web service is a file, symlink it into the webapps/axis2/WEB-INF/services
332+ ln -sfn $i ${cfg.baseDir}/webapps/axis2/WEB-INF/services/`basename $i`
333+ elif [ -d $i ]; then
334+ # If the given web application is a directory, then iterate over the files
335+ # in the special purpose directories and symlink them into the tomcat tree
336+
337+ for j in $i/webapps/axis2/WEB-INF/services/*; do
338+ ln -sfn $j ${cfg.baseDir}/webapps/axis2/WEB-INF/services/`basename $j`
339+ done
340+
341+ # Also symlink the configuration files if they are included
342+ if [ -d $i/conf/Catalina ]; then
343+ for j in $i/conf/Catalina/*; do
344+ ln -sfn $j ${cfg.baseDir}/conf/Catalina/localhost/`basename $j`
345+ done
401346 fi
402− '' else ""}
403− ${pkgs.su}/bin/su -s ${pkgs.bash}/bin/sh ${cfg.user} -c 'CATALINA_BASE=${cfg.baseDir} JAVA_HOME=${cfg.jdk} JAVA_OPTS="${cfg.javaOpts}" CATALINA_OPTS="${cfg.catalinaOpts}" ${tomcat}/bin/startup.sh'
404− '';
405−
406− preStop = ''
407− echo "Stopping tomcat..."
408− CATALINA_BASE=${cfg.baseDir} JAVA_HOME=${cfg.jdk} ${pkgs.su}/bin/su -s ${pkgs.bash}/bin/sh ${cfg.user} -c ${tomcat}/bin/shutdown.sh
347+ fi
348+ done
349+ ''}
409350 '';
410351
352+ serviceConfig = {
353+ Type = "forking";
354+ PermissionsStartOnly = true;
355+ PIDFile="/run/tomcat/tomcat.pid";
356+ RuntimeDirectory = "tomcat";
357+ User = cfg.user;
358+ Environment=[
359+ "CATALINA_BASE=${cfg.baseDir}"
360+ "CATALINA_PID=/run/tomcat/tomcat.pid"
361+ "JAVA_HOME='${cfg.jdk}'"
362+ "JAVA_OPTS='${builtins.toString cfg.javaOpts}'"
363+ "CATALINA_OPTS='${builtins.toString cfg.catalinaOpts}'"
364+ ] ++ cfg.extraEnvironment;
365+ ExecStart = "${tomcat}/bin/startup.sh";
366+ ExecStop = "${tomcat}/bin/shutdown.sh";
367+ };
411368 };
412−
413369 };
414−
415370}