Tomcat service restarts Tomcat on each chef-client run

Ohai Chefs,

I have a cookbook that installs Tomcat7, sets up an /etc/init.d service to start, stop, or restart Tomcat, and provides a LWRP to deploy applications to the instance from our Artifactory setup.

It all works with one caveat: Tomcat gets restarted every time the chef-client daemon runs, or every 15 minutes in this case.

Here is the default recipe, which installs Tomcat and sets up the service:

Create a tomcat user

user node[‘tomcat’][‘tomcat_user’] do
comment 'tomcat user’
system true
shell "/bin/false"
end

Create home for Tomcat

directory “#{node[‘tomcat’][‘tomcat_home’]}” do
owner node[‘tomcat’][‘tomcat_user’]
group node[‘tomcat’][‘tomcat_user’]
mode "0755"
recursive true
action :create
end

use ark to extract the archive

ark “#{node[‘tomcat’][‘tomcat_version’]}” do
path node[‘tomcat’][‘tomcat_home’]
url node[‘tomcat’][‘tomcat_url’]
version node[‘tomcat’][‘tomcat_version’]
owner node[‘tomcat’][‘tomcat_user’]
checksum node[‘tomcat’][‘checksum’]
action [ :put ]
end

Create /etc/init.d entry for Tomcat

template “#{node[‘tomcat’][‘service’]}” do
source "tomcat7.erb"
mode "0755"
owner "root"
group "root"
end

Tomcat Service

Control whether Tomcat restarts automatically or not

if node[‘tomcat’][‘autostart’]
service ‘tomcat’ do

service_name “tomcat#{node[‘tomcat’][‘base_version’]}”

service_name "tomcat7"
supports :restart => false, :reload => false, :start => true, :stop => true
action [ :start, :enable ]
notifies :run, 'execute[wait for tomcat]', :immediately
retries 4
retry_delay 30

end
end

execute ‘wait for tomcat’ do
command 'sleep 5’
action :nothing
end

I recently added the conditional if node[‘tomcat’][‘autostart’] around the service. Even with that attribute set to false the service restarts with every daemon run.

For completeness sake, here is the template I’m using for the /etc/init.d script.

#!/bin/bash

BEGIN INIT INFO

Provides: tomcat7

Required-Start: $network

Required-Stop: $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Start/Stop Tomcat server

END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

if [ -f <%= node[“tomcat”][“tomcat_conf”] %>/catalina.sh.includes ]; then
source <%= node[“tomcat”][“tomcat_conf”] %>/catalina.sh.includes
fi

start() {
sh <%= node[“tomcat”][“tomcat_bin”] %>/startup.sh
}

stop() {
sh <%= node[“tomcat”][“tomcat_bin”] %>/catalina.sh stop -force
}

showconfig() {
echo ${JAVA_OPTS}
}

case $1 in
start|stop|showconfig) $1;;
restart) stop; sleep 2; start;;
*) echo “Run as $0 <start|stop|restart>”; exit 1;;
esac

Any ideas as to what I’m doing that is causing Tomcat to be restarted with each daemon run?

Thanks in advance,
Mark

Usually this happens when your chef-client is changing some file in two
separate places, such as if you've templated a config file in your app
cookbook and it's already templated in community cookbook. In that case one
cookbook changes the file, the other cookbook un-does the change,
chef-client recognizes that change took place and restarts any services
that the file resource notifies.

On Wed, Jul 30, 2014 at 5:51 AM, Mark Nichols chef@zanshin.net wrote:

Ohai Chefs,

I have a cookbook that installs Tomcat7, sets up an /etc/init.d service
to start, stop, or restart Tomcat, and provides a LWRP to deploy
applications to the instance from our Artifactory setup.

It all works with one caveat: Tomcat gets restarted every time the
chef-client daemon runs, or every 15 minutes in this case.

Here is the default recipe, which installs Tomcat and sets up the
service:

Create a tomcat user

user node['tomcat']['tomcat_user'] do
comment 'tomcat user'
system true
shell "/bin/false"
end

Create home for Tomcat

directory "#{node['tomcat']['tomcat_home']}" do
owner node['tomcat']['tomcat_user']
group node['tomcat']['tomcat_user']
mode "0755"
recursive true
action :create
end

use ark to extract the archive

ark "#{node['tomcat']['tomcat_version']}" do
path node['tomcat']['tomcat_home']
url node['tomcat']['tomcat_url']
version node['tomcat']['tomcat_version']
owner node['tomcat']['tomcat_user']
checksum node['tomcat']['checksum']
action [ :put ]
end

Create /etc/init.d entry for Tomcat

template "#{node['tomcat']['service']}" do
source "tomcat7.erb"
mode "0755"
owner "root"
group "root"
end

Tomcat Service

Control whether Tomcat restarts automatically or not

if node['tomcat']['autostart']
service 'tomcat' do

service_name "tomcat#{node['tomcat']['base_version']}"

service_name "tomcat7"
supports :restart => false, :reload => false, :start => true, :stop =>

true
action [ :start, :enable ]
notifies :run, 'execute[wait for tomcat]', :immediately
retries 4
retry_delay 30
end
end

execute 'wait for tomcat' do
command 'sleep 5'
action :nothing
end

I recently added the conditional if node[‘tomcat’][‘autostart’] around
the service. Even with that attribute set to false the service restarts
with every daemon run.

For completeness sake, here is the template I’m using for the
/etc/init.d script.

#!/bin/bash

BEGIN INIT INFO

Provides: tomcat7

Required-Start: $network

Required-Stop: $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Start/Stop Tomcat server

END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

if [ -f <%= node["tomcat"]["tomcat_conf"] %>/catalina.sh.includes ]; then
source <%= node["tomcat"]["tomcat_conf"] %>/catalina.sh.includes
fi

start() {
sh <%= node["tomcat"]["tomcat_bin"] %>/startup.sh
}

stop() {
sh <%= node["tomcat"]["tomcat_bin"] %>/catalina.sh stop -force
}

showconfig() {
echo ${JAVA_OPTS}
}

case $1 in
start|stop|showconfig) $1;;
restart) stop; sleep 2; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

Any ideas as to what I’m doing that is causing Tomcat to be restarted with
each daemon run?

Thanks in advance,
Mark

--
Best regards, Dmitriy V.

Hi Mark,
I would start by implementing also a status command in init.d script and
then declaring also
"supports :status => true"
in your chef resource service. Otherwise I think chef can't manage
idempotence on service resource by testing if service is already running.
Cheers
Marco
Il 30/lug/2014 14:51 "Mark Nichols" chef@zanshin.net ha scritto:

Ohai Chefs,

I have a cookbook that installs Tomcat7, sets up an /etc/init.d service
to start, stop, or restart Tomcat, and provides a LWRP to deploy
applications to the instance from our Artifactory setup.

It all works with one caveat: Tomcat gets restarted every time the
chef-client daemon runs, or every 15 minutes in this case.

Here is the default recipe, which installs Tomcat and sets up the
service:

Create a tomcat user

user node['tomcat']['tomcat_user'] do
comment 'tomcat user'
system true
shell "/bin/false"
end

Create home for Tomcat

directory "#{node['tomcat']['tomcat_home']}" do
owner node['tomcat']['tomcat_user']
group node['tomcat']['tomcat_user']
mode "0755"
recursive true
action :create
end

use ark to extract the archive

ark "#{node['tomcat']['tomcat_version']}" do
path node['tomcat']['tomcat_home']
url node['tomcat']['tomcat_url']
version node['tomcat']['tomcat_version']
owner node['tomcat']['tomcat_user']
checksum node['tomcat']['checksum']
action [ :put ]
end

Create /etc/init.d entry for Tomcat

template "#{node['tomcat']['service']}" do
source "tomcat7.erb"
mode "0755"
owner "root"
group "root"
end

Tomcat Service

Control whether Tomcat restarts automatically or not

if node['tomcat']['autostart']
service 'tomcat' do

service_name "tomcat#{node['tomcat']['base_version']}"

service_name "tomcat7"
supports :restart => false, :reload => false, :start => true, :stop =>

true
action [ :start, :enable ]
notifies :run, 'execute[wait for tomcat]', :immediately
retries 4
retry_delay 30
end
end

execute 'wait for tomcat' do
command 'sleep 5'
action :nothing
end

I recently added the conditional if node[‘tomcat’][‘autostart’] around
the service. Even with that attribute set to false the service restarts
with every daemon run.

For completeness sake, here is the template I’m using for the
/etc/init.d script.

#!/bin/bash

BEGIN INIT INFO

Provides: tomcat7

Required-Start: $network

Required-Stop: $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Start/Stop Tomcat server

END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

if [ -f <%= node["tomcat"]["tomcat_conf"] %>/catalina.sh.includes ]; then
source <%= node["tomcat"]["tomcat_conf"] %>/catalina.sh.includes
fi

start() {
sh <%= node["tomcat"]["tomcat_bin"] %>/startup.sh
}

stop() {
sh <%= node["tomcat"]["tomcat_bin"] %>/catalina.sh stop -force
}

showconfig() {
echo ${JAVA_OPTS}
}

case $1 in
start|stop|showconfig) $1;;
restart) stop; sleep 2; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

Any ideas as to what I’m doing that is causing Tomcat to be restarted with
each daemon run?

Thanks in advance,
Mark