DevOps Classroom Series – 21/Apr/2020

Practical Explanation (Tomcat Installation)

  • In this we would understand the concept of templates
  • If we write a install recipe to install tomcat8 it looks as shown below
#
# Cookbook:: .
# Recipe:: install
#
# Copyright:: 2020, The Authors, All Rights Reserved.

apt_update 'updatepackage' do
    ignore_failure true
    action :update
end

package 'tomcat8' do
    action :install
    notifies :enable, 'service[tomcat8]'
end

service 'tomcat8' do
    action :nothing
end

other_packages = ['tomcat8-docs', 'tomcat8-admin', 'tomcat8-examples']

other_packages.each do |tomcat_package|
    package tomcat_package do
        action :install
        notifies :restart, 'service[tomcat8]'
    end
end


  • After this we need to change tomcat-users.xml in /etc/tomcat8/tomcat-users.xml and for this we have to deal with dynamic contents. For Dynamic contents in CM tools we use templates, so in Chef we use templates which have the format of erb. Template docs and template resource
  • To understand this concept lets try to create a text file with content
os: <osname>

# so for ubuntu
os: ubuntu

# so for centos
os : centos
  • Lets create a recipe
chef generate recipe . configure
chef generate template . platform.txt

  • When we generate a template, a new folder called as templates is created.
  • Now make changes in configure recipe
template '/home/ubuntu/platform.txt' do
    source 'platform.txt.erb'
    action :create
end
  • upload, execute and cross check
  • Now lets try to create a template for the tomcat-users.xml and also one attribute file to hold attributes
 chef generate template . tomcat-users.xml
 chef generate attribute . default
  • In the attributes/default.rb file add the following
default['mytomcat']['username'] = 'admin'
default['mytomcat']['password'] = 'password'
default['mytomcat']['roles'] = 'manager-gui,admin-gui'
  • Now in the templates/tomcat-users.xml.erb make the following changes
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You 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-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
    <user username="<%= node['mytomcat']['username'] %>" password="<%= node['mytomcat']['password'] %>" roles="<%=  node['mytomcat']['roles'] %>"/>
</tomcat-users>
  • Lets add resource in recipe configure to copy this file
#
# Cookbook:: .
# Recipe:: configure
#
# Copyright:: 2020, The Authors, All Rights Reserved.
template '/home/ubuntu/platform.txt' do
    source 'platform.txt.erb'
    action :create
end

template '/etc/tomcat8/tomcat-users.xml' do
    source 'tomcat-users.xml.erb'
    action :create
    notifies :restart, 'service[tomcat8]'
end
  • upload, verify the file after convergance
  • Use templates to change configuration files of your application

Exercise: 1

  • Create a chef cookbook which will create a file called a readme.txt with the following information
free memory: <free memory value>
swap size: <swap size value>
operating system: <os value>
ip address: <ip value>

Practical Explanation to understand supermarket and berks

  • This will help in making us understand supermarket cookbook & berkshelf
  • Lets assume we need to install tomcat, I have following approaches
    • write the whole cookbook to automate all the manual steps
    • Use the cookbook which is already written by some one <your devops team or community>
  • The idea here to reuse cookbooks, to use some other cookbook in your cookbook we call that as dependency and berkshelf (berks) is a tool designed to resolve dependencies
  • In chef cookbook to describe, you depend on some one elses cookbook, we need to use metadata.rb file and use the depends statement.
  • So lets create a cookbook, which installs java (from supermarket)
  • Lets create a cookbook called supermarketdemo
chef generate cookbook -b supermarketdemo 
  • Search for someone elses work on supermarket and i would be using supermarket java Preview Preview
  • Add the lines in metadata.rb to depend on java cookbook Preview
  • Now Execute berks install to download the dependencies
  • Now after reading the documentation i’m using a custom resource defined as shown below
#
# Cookbook:: supermarketdemo
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.

openjdk_install '11'
  • Now upload the cookbook using “`berks upload“ and then look into the chef servers cookbooks section and add the supermarketdemo to runlist of some node

Exercise

  • Write a chef cookbook to install
    • mysql
    • nodejs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin