From 479c1493e63be4b1b03b7f66538d21b1304429f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20MARTIN?= Date: Mon, 23 Jul 2018 21:48:18 +0200 Subject: [PATCH] Add support for complex Host setup (#102) --- README.md | 36 ++++++++++ manifests/config.pp | 72 ++++++++++++------- manifests/init.pp | 23 ++++-- manifests/install/archive.pp | 2 + manifests/instance.pp | 25 +++++-- templates/common/server.xml/091_hosts.erb | 39 ++++++++++ .../common/server.xml/190_host_close.erb | 2 + templates/common/server.xml/200_footer.erb | 2 - 8 files changed, 165 insertions(+), 36 deletions(-) create mode 100644 templates/common/server.xml/091_hosts.erb create mode 100644 templates/common/server.xml/190_host_close.erb diff --git a/README.md b/README.md index 5a9c1d0..6114fd6 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,32 @@ class { 'tomcat': } ``` +or for more complex setup +```puppet +class { 'tomcat': + … + hosts => [ + { + name => 'www.example.org', + deployonstartup => false, + unpackwars => true, + createDirs => true, + contexts => [{ path => '', docBase => '/home/app', crossContext => true }], + valves => [{ className => 'org.apache.catalina.valves.AccessLogValve', directory => 'logs', prefix => 'app_access_log', suffix => '.txt', pattern => '%h %l %u %t "%r" %s %b'}] + }, + { + name => 'cas.example.org', + deployonstartup => false, + unpackwars => true, + createDirs => true, + contexts => [{ path => '', docBase => '/home/cas', crossContext => true }], + valves => [{ className => 'org.apache.catalina.valves.AccessLogValve', directory => 'logs', prefix => 'cas_access_log', suffix => '.txt', pattern => '%h %l %u %t "%r" %s %b'}] + } + ] + +} +``` + Enable the remote [JMX listener](http://tomcat.apache.org/tomcat-9.0-doc/config/listeners.html#JMX_Remote_Lifecycle_Listener_-_org.apache.catalina.mbeans.JmxRemoteLifecycleListener) and remote JVM monitoring ```puppet @@ -465,9 +491,15 @@ Optional override command for stopping the service. Default depends on the platf ##### `tomcat_user` Tomcat user. Defaults to [`${service_name}`](#service_name) (Debian) / `tomcat` (all other distributions). +##### `tomcat_user_id` +Tomcat user id. Defaults to undef, will be generated at user creation. + ##### `tomcat_group` Tomcat group. Defaults to [`${tomcat_user}`](#tomcat_user). +##### `tomcat_group_id` +Tomcat group id. Defaults to undef, will be generated at group creation. + ##### `file_mode` File mode for certain configuration xml files. Defaults to '0600'. @@ -620,6 +652,10 @@ Name of the default [Host](http://tomcat.apache.org/tomcat-9.0-doc/config/host.h - `host_unpackwars`: whether to unpack web application archive (WAR) files - `host_params`: optional hash of additional attributes/values to put in the Host container +##### `hosts` +An array of `Host` entries. Use this if you need more complex setup. You can nest valves and contexts with their parameters. +See [Host](http://tomcat.apache.org/tomcat-9.0-doc/config/host.html) for the list of possible attributes. + ##### `contexts` An array of custom `Context` entries to be added to the `Host` container. Each entry is to be supplied as a hash of attributes/values for the `Context` XML node. See [Context](http://tomcat.apache.org/tomcat-9.0-doc/config/context.html) for the list of possible attributes. diff --git a/manifests/config.pp b/manifests/config.pp index 2e3f4eb..c2288fa 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -44,6 +44,7 @@ $engine_params_real = $::tomcat::engine_params_real $host_name = $::tomcat::host_name $host_params_real = $::tomcat::host_params_real + $hosts = $::tomcat::hosts $contexts = $::tomcat::contexts $use_simpletcpcluster = $::tomcat::use_simpletcpcluster $cluster_membership_port = $::tomcat::cluster_membership_port @@ -68,6 +69,7 @@ $globalnaming_environments = $::tomcat::globalnaming_environments $globalnaming_resources = $::tomcat::globalnaming_resources $context_params = $::tomcat::context_params + $context_cookieprocessor = $::tomcat::context_cookieprocessor $context_loader = $::tomcat::context_loader $context_manager = $::tomcat::context_manager $context_realm = $::tomcat::context_realm @@ -279,34 +281,51 @@ } } - # Template uses: - # - $host_name - # - $host_params_real - concat::fragment { 'server.xml host': - order => 90, - content => template("${module_name}/common/server.xml/090_host.erb"), - target => 'tomcat server configuration' - } + if (empty($hosts)) { + # Template uses: + # - $host_name + # - $host_params_real + concat::fragment { 'server.xml host': + order => 90, + content => template("${module_name}/common/server.xml/090_host.erb"), + target => 'tomcat server configuration' + } - # Template uses: - # - $contexts - concat::fragment { 'server.xml contexts': - order => 95, - content => template("${module_name}/common/server.xml/095_contexts.erb"), - target => 'tomcat server configuration' - } + # Template uses: + # - $contexts + concat::fragment { 'server.xml contexts': + order => 95, + content => template("${module_name}/common/server.xml/095_contexts.erb"), + target => 'tomcat server configuration' + } - # Template uses: - # - $singlesignon_valve - # - $accesslog_valve - # - $accesslog_valve_pattern - # - $valves - # - $host_name - # - $maj_version - if $singlesignon_valve or $accesslog_valve or ($valves and $valves != []) { - concat::fragment { 'server.xml valves': - order => 100, - content => template("${module_name}/common/server.xml/100_valves.erb"), + # Template uses: + # - $singlesignon_valve + # - $accesslog_valve + # - $accesslog_valve_pattern + # - $valves + # - $host_name + # - $maj_version + if $singlesignon_valve or $accesslog_valve or ($valves and $valves != []) { + concat::fragment { 'server.xml valves': + order => 100, + content => template("${module_name}/common/server.xml/100_valves.erb"), + target => 'tomcat server configuration' + } + } + + # Template uses no variable, just + concat::fragment { 'server.xml host close': + order => 190, + content => template("${module_name}/common/server.xml/190_host_close.erb"), + target => 'tomcat server configuration' + } + } else { + # Template uses : + # - $hosts + concat::fragment { 'server.xml hosts': + order => 91, + content => template("${module_name}/common/server.xml/091_hosts.erb"), target => 'tomcat server configuration' } } @@ -322,6 +341,7 @@ path => "${::tomcat::catalina_base_real}/conf/context.xml", file_mode => $::tomcat::file_mode, params => $context_params, + cookieprocessor => $context_cookieprocessor, loader => $context_loader, manager => $context_manager, realm => $context_realm, diff --git a/manifests/init.pp b/manifests/init.pp index 97373b2..75418f2 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -38,8 +38,12 @@ # override service shutdown command # [*tomcat_user*] # service user +# [*tomcat_user_id*] +# service user id # [*tomcat_group*] # service group +# [*tomcat_group_id*] +# service group id # [*file_mode*] # mode for configuration files # [*tomcat_native*] @@ -120,7 +124,9 @@ $service_start = undef, $service_stop = undef, $tomcat_user = undef, + $tomcat_user_id = undef, $tomcat_group = undef, + $tomcat_group_id = undef, $file_mode = '0600', $tomcat_native = false, $tomcat_native_package_name = $::tomcat::params::tomcat_native_package_name, @@ -261,6 +267,7 @@ $host_undeployoldversions = undef, $host_unpackwars = undef, $host_params = {}, + $hosts = {}, #.................................................................................. # host contexts $contexts = [], @@ -559,13 +566,21 @@ $security_manager_real = $security_manager ? { true => 'yes', default => 'no' - } } else { + } + } else { $security_manager_real = $security_manager } - $engine_defaulthost_real = $engine_defaulthost ? { - undef => $host_name, - default => $engine_defaulthost + if (empty($hosts)) { + $engine_defaulthost_real = $engine_defaulthost ? { + undef => $host_name, + default => $engine_defaulthost + } + } else { + $engine_defaulthost_real = $engine_defaulthost ? { + undef => $hosts[0]['name'], + default => $engine_defaulthost + } } $java_opts_real = join($java_opts, ' ') diff --git a/manifests/install/archive.pp b/manifests/install/archive.pp index 9c228c9..7925764 100644 --- a/manifests/install/archive.pp +++ b/manifests/install/archive.pp @@ -17,6 +17,7 @@ if !defined(Group[$::tomcat::tomcat_group_real]) { group { $::tomcat::tomcat_group_real: ensure => present, + gid => $::tomcat::tomcat_group_id, system => true } } @@ -24,6 +25,7 @@ if !defined(User[$::tomcat::tomcat_user_real]) { user { $::tomcat::tomcat_user_real: ensure => present, + uid => $::tomcat::tomcat_user_id, gid => $::tomcat::tomcat_group_real, home => $::tomcat::catalina_home_real, system => true diff --git a/manifests/instance.pp b/manifests/instance.pp index cc4ade1..ac3b853 100644 --- a/manifests/instance.pp +++ b/manifests/instance.pp @@ -32,8 +32,12 @@ # override service shutdown command # [*tomcat_user*] # service user +# [*tomcat_user_id*] +# service user id # [*tomcat_group*] # service group +# [*tomcat_group_id*] +# service group id # [*file_mode*] # mode for configuration files # [*extras_enable*] @@ -94,7 +98,9 @@ $service_start = undef, $service_stop = undef, $tomcat_user = $::tomcat::tomcat_user_real, + $tomcat_user_id = undef, $tomcat_group = $::tomcat::tomcat_group_real, + $tomcat_group_id = undef, $file_mode = '0600', $extras_enable = false, $extras_source = undef, @@ -235,6 +241,7 @@ $host_undeployoldversions = undef, $host_unpackwars = undef, $host_params = {}, + $hosts = {}, #.................................................................................. # host contexts $contexts = [], @@ -514,13 +521,21 @@ $security_manager_real = $security_manager ? { true => 'yes', default => 'no' - } } else { + } + } else { $security_manager_real = $security_manager } - $engine_defaulthost_real = $engine_defaulthost ? { - undef => $host_name, - default => $engine_defaulthost + if (empty($hosts)) { + $engine_defaulthost_real = $engine_defaulthost ? { + undef => $host_name, + default => $engine_defaulthost + } + } else { + $engine_defaulthost_real = $engine_defaulthost ? { + undef => $hosts[0]['name'], + default => $engine_defaulthost + } } $java_opts_real = join($java_opts, ' ') @@ -655,6 +670,7 @@ if !defined(Group[$tomcat_group]) { group { $tomcat_group: ensure => present, + gid => $tomcat_group_id, system => true } } @@ -662,6 +678,7 @@ if !defined(User[$tomcat_user]) { user { $tomcat_user: ensure => present, + uid => $tomcat_user_id, gid => $tomcat_group, home => $catalina_home_real, system => true diff --git a/templates/common/server.xml/091_hosts.erb b/templates/common/server.xml/091_hosts.erb new file mode 100644 index 0000000..7ebf6c1 --- /dev/null +++ b/templates/common/server.xml/091_hosts.erb @@ -0,0 +1,39 @@ +<%- @hosts.each do |host| %> + <%- host.each_pair do |attrib, value| -%> + <%- if ! ['valves', 'contexts', 'aliases'].include? attrib -%> + <%- if attrib == host.keys.first -%> + =<%= "#{value}".encode(:xml => :attr) -%> + <%- else %> + <%= attrib %>=<%= "#{value}".encode(:xml => :attr) -%> + <%- end -%> + <%- end -%> + <%- end -%>> + <%- if host['aliases'] and ! host['aliases'].empty? -%> + <%- host['aliases'].each do |al| -%> + <%= al %> + <%- end -%> + <%- end -%> + <%- if host['contexts'] and ! host['contexts'].empty? -%> + <%- host['contexts'].each do |context| -%> + <%- context.each_pair do |context_attrib, context_value| %> + <%- if context_attrib == context.keys.first -%> + =<%= "#{context_value}".encode(:xml => :attr) -%> + <%- else -%> + <%= context_attrib %>=<%= "#{context_value}".encode(:xml => :attr) -%> + <%- end -%> + <%- end -%> /> + <%- end -%> + <%- end -%> + <%- if host['valves'] and ! host['valves'].empty? -%> + <%- host['valves'].each do |valve| -%> + <%- valve.each_pair do |valve_attrib, valve_value| %> + <%- if valve_attrib == valve.keys.first -%> + =<%= "#{valve_value}".encode(:xml => :attr) -%> + <%- else -%> + <%= valve_attrib %>=<%= "#{valve_value}".encode(:xml => :attr) -%> + <%- end -%> + <%- end -%> /> + <%- end -%> + <%- end -%> + +<%- end -%> diff --git a/templates/common/server.xml/190_host_close.erb b/templates/common/server.xml/190_host_close.erb new file mode 100644 index 0000000..9e2f614 --- /dev/null +++ b/templates/common/server.xml/190_host_close.erb @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/templates/common/server.xml/200_footer.erb b/templates/common/server.xml/200_footer.erb index 98046e4..bdebe96 100644 --- a/templates/common/server.xml/200_footer.erb +++ b/templates/common/server.xml/200_footer.erb @@ -1,5 +1,3 @@ - -