How to setup Tomcat V8 using chef
Here in this article we will try to setup a node with Tomcat web container instance using chef.
Test Environment
Hosted Chef Server – https://manage.chef.io/
desktop1.example.com – Used for preparing cookbooks and recipes
server1.example.com – Managed using hosted chef server
Procedure
Step1: Generate a cookbook
As a first step let’s try to generate a cookbook template as shown below.
[root@desktop1 cookbooks]# chef generate cookbook tomcat_v8
Generating cookbook tomcat_v8
- Ensuring correct cookbook file content
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
Your cookbook is ready. Type `cd tomcat_v8` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.
Why not start by writing a test? Tests for the default recipe are stored at:
test/smoke/default/default_test.rb
If you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb
Step2: Define the attributes to be used
Now define the default attributes that will be further used in the recipe as shown below.
[root@desktop1 tomcat_v8]# cat attributes/default.rb
#
# Cookbook Name:: tomcat_latest
# Attributes:: default
#
# Copyright 2013, Sudhir B
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
default['tomcat_v8']['tomcat_url']="http://archive.apache.org/dist/tomcat/tomcat-8/"
default['tomcat_v8']['tomcat_version']="8.5.15"
default['tomcat_v8']['tomcat_install_dir']="/opt/apache/tomcat"
default['tomcat_v8']['tomcat_user']="tomcat"
default['tomcat_v8']['tomcat_auto_start']="true"
Step3: Update the Berksfile to include the dependency cookbook and upload the cookbook to chef server
We will now upload the updated cookbook by defining it in the Berksfile and uploading it using berks utility as shown below.
[root@desktop1 tomcat_v8]# cat Berksfile
source 'https://supermarket.chef.io'
cookbook 'java_se', '~> 8.131.0'
metadata
[root@desktop1 tomcat_v8]# berks upload
Skipping java_se (8.131.0) (frozen)
Uploaded tomcat_v8 (1.5.0) to: 'https://api.chef.io:443/organizations/<org_name>
Step4: Update the default.rb recipe as below
Now, it’s time prepare our recipe which will do the setup of tomcat server with the attributes that were defined a shown below.
[root@desktop1 tomcat_v8]# cat recipes/default.rb
#
# Cookbook:: tomcat_v8
# Recipe:: default
#
# Copyright:: 2017, Sudhir B, All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
tomcat_url=node['tomcat_v8']['tomcat_url']
tomcat_version=node['tomcat_v8']['tomcat_version']
tomcat_install_dir=node['tomcat_v8']['tomcat_install_dir']
tomcat_user=node['tomcat_v8']['tomcat_user']
tomcat_auto_start=node['tomcat_v8']['tomcat_auto_start']
group "tomcat"
user "tomcat" do
group "tomcat"
system true
shell "/bin/bash"
end
directory "#{tomcat_install_dir}" do
owner 'tomcat'
group 'tomcat'
mode '0755'
recursive true
action :create
end
## Include the dependencies
include_recipe "java_se"
## Download the tomcat package
download_url="#{tomcat_url}"+"v#{tomcat_version}"+"/bin/apache-tomcat-#{tomcat_version}.tar.gz"
script "Download Apache Tomcat version #{tomcat_version}" do
interpreter "bash"
user "#{tomcat_user}"
cwd "/tmp"
code <<-EOH
wget "#{download_url}" -O "/tmp/apache-tomcat-#{tomcat_version}.tar.gz"
#mkdir -p #{tomcat_install_dir}
EOH
end
## Extract the package
script "Extracting the Apache Tomcat Package" do
interpreter "bash"
user "#{tomcat_user}"
cwd "/tmp"
code <<-EOH
tar -zxvf /tmp/apache-tomcat-#{tomcat_version}.tar.gz -C #{tomcat_install_dir}
EOH
end
## Move the unzipped package to /opt/apache/tomcat/apache-tomcat
script "Move the package" do
interpreter "bash"
user "#{tomcat_user}"
cwd "/tmp"
code <<-EOH
if [[ ! -d /opt/apache/tomcat/apache-tomcat ]] ; then
cd #{tomcat_install_dir} ; mv apache-tomcat-* apache-tomcat
else
echo "Directory already exists"
fi
EOH
end
## Start the tomcat instance
script "Start the tomcat instance" do
interpreter "bash"
user "#{tomcat_user}"
cwd "/tmp"
code <<-EOH
/opt/apache/tomcat/apache-tomcat/bin/startup.sh
EOH
End
Step5: Install the Recipe on node using below knife command
It’s time to execute our cookbook on the managed node.
[root@desktop1 tomcat_v8]# knife ssh 'name:node_name' 'sudo chef-client' --ssh-user user --ssh-password 'password' --attribute ipaddress
After running the above cookbook, your node should be setup with Tomcat installed at /opt/apache/tomcat/apache-tomcat and listening on HTTP port 8080. Test the tomcat instance using below URL.
URL : http://<FQDN>:8080/
Hope you enjoyed reading this article. Thank you..
Leave a Reply
You must be logged in to post a comment.