Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Manifest for Listener #57

Merged
merged 4 commits into from Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Expand Up @@ -19,6 +19,7 @@
- [tomcat::config::server::connector](#tomcatconfigserverconnector)
- [tomcat::config::server::engine](#tomcatconfigserverengine)
- [tomcat::config::server::host](#tomcatconfigserverhost)
- [tomcat::config::server::listener](#tomcatconfigserverlistener)
- [tomcat::config::server::service](#tomcatconfigserverservice)
- [tomcat::config::server::valve](#tomcatconfigservervalve)
- [tomcat::instance](#tomcatinstance)
Expand Down Expand Up @@ -196,6 +197,7 @@ tomcat::config::server::connector { 'tomcat8-jsvc':
* `tomcat::config::server::connector`: Configures [Connector](http://tomcat.apache.org/tomcat-8.0-doc/connectors.html) elements in $CATALINA_BASE/conf/server.xml.
* `tomcat::config::server::engine`: Configures [Engine](http://tomcat.apache.org/tomcat-8.0-doc/config/engine.html#Introduction) elements in $CATALINA_BASE/conf/server.xml.
* `tomcat::config::server::host`: Configures [Host](http://tomcat.apache.org/tomcat-8.0-doc/config/host.html) elements in $CATALINA_BASE/conf/server.xml.
* `tomcat::config::server::listener`: Configures [Listener](http://tomcat.apache.org/tomcat-8.0-doc/config/listeners.html) elements in $CATALINA_BASE/conf/server.xml.
* `tomcat::config::server::service`: Configures a [Service](http://tomcat.apache.org/tomcat-8.0-doc/config/service.html) element nested in the Server element in $CATALINA_BASE/conf/server.xml.
* `tomcat::config::server::valve`: Configures a [Valve](http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html) element in $CATALINA_BASE/conf/server.xml.
* `tomcat::instance`: Installs a Tomcat instance.
Expand Down Expand Up @@ -382,6 +384,40 @@ be a hash of the format 'attribute' => 'value'. This parameter is optional
Specifies any attributes to remove from the Host. Should
be an array of the format 'attribute' => 'value'. This parameter is optional.

####tomcat::config::server::listener

#####`$catalina_base`

Specifies the base directory for the Tomcat installation.

#####`$listener_ensure`

Specifies whether to add or remove Listener XML element in configuration file. Valid values are 'true', 'false', 'present', and 'absent'. Defaults to 'present'.

#####`$class_name`

Specifies the Java class name of the implementation to use, and maps to the className XML attribute of a Listener Element. Defaults to '[name]' passed in the define.

#####`$parent_service`

Specifies the Service element this Listener should be nested beneath. Only valid if `$parent_engine` or `$parent_host` is specified. Defaults to 'Catalina' if `$parent_host` or `$parent_engine` is specified.

#####`$parent_engine`

Specifies which Engine element this Listener should be nested beneath. Needs to be the `name` attribute for the Engine.

#####`$parent_host`

Specifies which Host element this Listener should be nested beneath. Needs to be the `name` attribute for the Host.

#####`$additional_attributes`

Specifies any additional attributes to add to the Listener. Should be a hash of the format 'attribute' => 'value'. This parameter is optional.

#####`$attributes_to_remove`

Specifies any attributes to remove from the Listener. Should be a hash of the format 'attribute' => 'value'. This parameter is optional.

####tomcat::config::server::service

#####`$catalina_base`
Expand Down
18 changes: 18 additions & 0 deletions examples/instance_with_listener.pp
@@ -0,0 +1,18 @@
# This code fragment downloads tomcat 7.0.53, creates an instance and adds a listener
#
class { 'tomcat': }
class { 'java': }

tomcat::instance { 'mycat':
catalina_base => '/opt/apache-tomcat/mycat',
source_url => 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.tar.gz',
}->
tomcat::config::server::listener { 'mycat-jmx':
catalina_base => '/opt/apache-tomcat/mycat',
listener_ensure => present,
class_name => 'org.apache.catalina.mbeans.JmxRemoteLifecycleListener',
additional_attributes => {
'rmiRegistryPortPlatform' => '10001',
'rmiServerPortPlatform' => '10002',
},
}
91 changes: 91 additions & 0 deletions manifests/config/server/listener.pp
@@ -0,0 +1,91 @@
# Definition: tomcat::config::server::listener
#
# Configure Listener elements in $CATALINA_BASE/conf/server.xml
#
# Parameters:
# - $catalina_base is the base directory for the Tomcat installation.
# - $listener_ensure specifies whether you are trying to add or remove the
# Listener element. Valid values are 'true', 'false', 'present', and
# 'absent'. Defaults to 'present'.
# - $class_name is the Java class name of the implementation to use.
# Defaults to $name.
# - $parent_service is the Service element this Listener should be nested
# beneath. Only valid if $parent_host or $parent_engine is specified. Defaults
# to 'Catalina' if $parent_host or $parent_engine was specified.
# - $parent_engine is the `name` attribute to the Engine element this Listener
# should be nested beneath.
# - $parent_host is the `name` attribute to the Engine element this Listener
# should be nested beneath.
# - An optional hash of $additional_attributes to add to the Listener. Should
# be of the format 'attribute' => 'value'.
# - An optional array of $attributes_to_remove from the Listener.
define tomcat::config::server::listener (
$catalina_base = $::tomcat::catalina_home,
$listener_ensure = 'present',
$class_name = undef,
$parent_service = undef,
$parent_engine = undef,
$parent_host = undef,
$additional_attributes = {},
$attributes_to_remove = [],
) {
if versioncmp($::augeasversion, '1.0.0') < 0 {
fail('Server configurations require Augeas >= 1.0.0')
}

validate_re($listener_ensure, '^(present|absent|true|false)$')
validate_hash($additional_attributes)
validate_array($attributes_to_remove)

if $parent_service and ! ($parent_host or $parent_engine) {
warning('listener elements cannot be nested directly under service elements, ignoring $parent_service')
}

if ! $parent_service and ($parent_engine or $parent_host) {
$_parent_service = 'Catalina'
} else {
$_parent_service = $parent_service
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this block can we have $class_name default to $name? (see https://github.com/puppetlabs/puppetlabs-tomcat/blob/master/manifests/config/server/host.pp#L34-L38)


if $class_name {
$_class_name = $class_name
} else {
$_class_name = $name
}

if $parent_engine and ! $parent_host {
$path = "Server/Service[#attribute/name='${_parent_service}']/Engine[#attribute/name='${parent_engine}']/Listener[#attribute/className='${_class_name}']"
} elsif $parent_engine and $parent_host {
$path = "Server/Service[#attribute/name='${_parent_service}']/Engine[#attribute/name='${parent_engine}']/Host[#attribute/name='${parent_host}']/Listener[#attribute/className='${_class_name}']"
} elsif $parent_host {
$path = "Server/Service[#attribute/name='${_parent_service}']/Engine/Host[#attribute/name='${parent_host}']/Listener[#attribute/className='${_class_name}']"
} else {
$path = "Server/Listener[#attribute/className='${_class_name}']"
}

if $listener_ensure =~ /^(absent|false)$/ {
$augeaschanges = "rm ${path}"
} else {
$listener = "set ${path}/#attribute/className ${_class_name}"

if ! empty($additional_attributes) {
$_additional_attributes = prefix(join_keys_to_values($additional_attributes, ' '), "set ${path}/#attribute/")
} else {
$_additional_attributes = undef
}

if ! empty(any2array($attributes_to_remove)) {
$_attributes_to_remove = prefix(any2array($attributes_to_remove), "rm ${path}/#attribute/")
} else {
$_attributes_to_remove = undef
}

$augeaschanges = delete_undef_values(flatten([$listener, $_additional_attributes, $_attributes_to_remove]))
}

augeas { "${catalina_base}-${_parent_service}-${parent_engine}-${parent_host}-listener-${name}":
lens => 'Xml.lns',
incl => "${catalina_base}/conf/server.xml",
changes => $augeaschanges,
}
}
228 changes: 228 additions & 0 deletions spec/defines/config/server/listener_spec.rb
@@ -0,0 +1,228 @@
require 'spec_helper'

describe 'tomcat::config::server::listener', :type => :define do
let :pre_condition do
'class { "tomcat": }'
end
let :facts do
{
:osfamily => 'Debian',
:augeasversion => '1.0.0'
}
end
let :title do
'JmxRemoteLifecycleListener'
end
context 'Add Listener' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:class_name => 'org.apache.catalina.mbeans.JmxRemoteLifecycleListener',
:listener_ensure => 'present',
:additional_attributes => {
'rmiRegistryPortPlatform' => '10001',
'rmiServerPortPlatform' => '10002',
},
:attributes_to_remove => [
'foo',
'bar',
'baz',
],
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test----listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/className org.apache.catalina.mbeans.JmxRemoteLifecycleListener',
'set Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/rmiRegistryPortPlatform 10001',
'set Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/rmiServerPortPlatform 10002',
'rm Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/foo',
'rm Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/bar',
'rm Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']/#attribute/baz',
]
)
}
end
context 'No class_name' do
let :title do
'org.apache.catalina.core.AprLifecycleListener'
end
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test----listener-org.apache.catalina.core.AprLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context '$parent_engine, no $parent_service' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
:parent_engine => 'default',
:class_name => 'org.apache.catalina.core.AprLifecycleListener',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test-Catalina-default--listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Service[#attribute/name=\'Catalina\']/Engine[#attribute/name=\'default\']/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context '$parent_engine' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
:parent_engine => 'default',
:parent_service => 'Catalina2',
:class_name => 'org.apache.catalina.core.AprLifecycleListener',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test-Catalina2-default--listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Service[#attribute/name=\'Catalina2\']/Engine[#attribute/name=\'default\']/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context '$parent_host, no $parent_engine or $parent_service' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
:parent_host => 'localhost',
:class_name => 'org.apache.catalina.core.AprLifecycleListener',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test-Catalina--localhost-listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Service[#attribute/name=\'Catalina\']/Engine/Host[#attribute/name=\'localhost\']/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context '$parent_host, no $parent_engine' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
:parent_host => 'localhost',
:parent_service => 'Catalina2',
:class_name => 'org.apache.catalina.core.AprLifecycleListener',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test-Catalina2--localhost-listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Service[#attribute/name=\'Catalina2\']/Engine/Host[#attribute/name=\'localhost\']/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context '$parent_host' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:listener_ensure => 'present',
:parent_engine => 'default',
:parent_host => 'localhost',
:parent_service => 'Catalina2',
:class_name => 'org.apache.catalina.core.AprLifecycleListener',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test-Catalina2-default-localhost-listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'set Server/Service[#attribute/name=\'Catalina2\']/Engine[#attribute/name=\'default\']/Host[#attribute/name=\'localhost\']/Listener[#attribute/className=\'org.apache.catalina.core.AprLifecycleListener\']/#attribute/className org.apache.catalina.core.AprLifecycleListener',
]
)
}
end
context 'Remove Listener' do
let :params do
{
:catalina_base => '/opt/apache-tomcat/test',
:class_name => 'org.apache.catalina.mbeans.JmxRemoteLifecycleListener',
:listener_ensure => 'absent',
}
end
it { is_expected.to contain_augeas('/opt/apache-tomcat/test----listener-JmxRemoteLifecycleListener').with(
'lens' => 'Xml.lns',
'incl' => '/opt/apache-tomcat/test/conf/server.xml',
'changes' => [
'rm Server/Listener[#attribute/className=\'org.apache.catalina.mbeans.JmxRemoteLifecycleListener\']'
]
)
}
end
context 'Failing Tests' do
context 'Bad listener_ensure' do
let :params do
{
:listener_ensure => 'foo',
}
end
it do
expect {
is_expected.to compile
}. to raise_error(Puppet::Error, /does not match/)
end
end
context 'Bad additional_attributes' do
let :params do
{
:additional_attributes => 'foo',
}
end
it do
expect {
is_expected.to compile
}. to raise_error(Puppet::Error, /is not a Hash/)
end
end
context 'Bad attributes_to_remove' do
let :params do
{
:attributes_to_remove => 'foo',
}
end
it do
expect {
is_expected.to compile
}. to raise_error(Puppet::Error, /is not an Array/)
end
end
context 'old augeas' do
let :facts do
{
:osfamily => 'Debian',
:augeasversion => '0.10.0'
}
end
it do
expect {
is_expected.to compile
}.to raise_error(Puppet::Error, /configurations require Augeas/)
end
end
end
end