DevOps Classroom Series – Chef – 24/Nov/2019

Uploading Cookbooks to chef server

  • Cookbooks can be uploaded to chef server using
    • Knife
      • Same cookbook can be uploaded multiple times and every time it overrites the existing cookbook
    • berkshelf
      • Allows cookbook with one version uploaded only once.

Run List

  • List of Recipes to be applied/executed on the node.
  • This list mainitained by chef-server and during convergence passed to node.
  • Each Recipe in the runlist has the following format
<cookbook-name>::<recipe-name>

## examples
exercise1::default
exercise1::redhat
  • Just writing a cookbook name in runlist is equivalent to calling default recipe
exercise1
or
exercise1::default

Developing a Cookbook for Java Application Deployment.

  • Manual Steps in Ubuntu
sudo apt-get update
sudo apt-get install openjdk-8-jdk -y
wget https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar
java -jar spring-petclinic.jar
  • Execute the following command chef generate cookbook -b springpetclinic
  • Create a new recipe for java installation and updating ubuntu packages
chef generate recipe . java8
  • Refer Here for package resource.
  • Please refer below for java8 recipe
#
# Cookbook:: .
# Recipe:: java8
#
# Copyright:: 2019, The Authors, All Rights Reserved.

# updating ubuntu packages
apt_update 'update' do
    ignore_failure true
    action :update
end


package 'openjdk-8-jdk' do
    action :install
end

  • Now to download the spring petclinic and execute it lets create a new recipe called as spc with following content
#
# Cookbook:: .
# Recipe:: spc
#
# Copyright:: 2019, The Authors, All Rights Reserved.

remote_file '/home/ubuntu/spring-petclinic.jar' do
    source 'https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar'
    action :create
end

## todo automation for java -jar spring-petclinic.jar

  • Also change default.rb in recipes folder to call the recipes in the execution order (so that user can include default recipe in run list and whole cookbook executes)
#
# Cookbook:: springpetclinic
# Recipe:: default
#
# Copyright:: 2019, The Authors, All Rights Reserved.

include_recipe 'springpetclinic::java8'
include_recipe 'springpetclinic::spc'
  • Upload the cookbook to chef server using berks upload
  • Change the runlist of the ubuntu node and wait for convergence (or manually force it using sudo chef-client)

Developing a cookbook for lamp stack

sudo apt-get update
sudo apt-get install apache2 -y
sudo systemctl restart apache2
sudo apt-get install php libapache2-mod-php php-mysql php-cli -y
sudo systemctl restart apache2
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>

sudo systemctl restart apache2

  • Create a cookbook called as apachewithphp
chef generate cookbook -b apachewithphp
  • Change metadata.rb file with your information
name 'apachewithphp'
maintainer 'DirectDevOps'
maintainer_email 'qtdevops@gmail.com'
license 'All Rights Reserved'
description 'Installs/Configures apache and php'
long_description 'Installs/Configures apachewithphp'
version '0.1.0'
chef_version '>= 14.0'
  • Create a recipe to install apache with php
chef generate recipe . apachephp
  • Ensure your recipe has the following content in it
#
# Cookbook:: .
# Recipe:: apachephp
#
# Copyright:: 2019, The Authors, All Rights Reserved.

apt_update 'update pacakges' do
    ignore_failure true
    action :update
end

package 'apache2' do
    action :install
end

service 'apache2' do
    action [:enable, :restart]
end


php_packages = ["php", "libapache2-mod-php",  "php-mysql", "php-cli"]

php_packages.each do |package_name|
  package package_name do
      action :install
  end
end

service 'mypache2' do
    service_name 'apache2'
    action :restart
end

remote_file '/var/www/html/info.php' do
    source 'https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/info.php'
    action :create
end

service 'mypache2again' do
    service_name 'apache2'
    action :restart         
end


  • Problems in this recipe so far are
    • service is appearing multiple times.
    • File info.php is downloaded but not created.
    • Service is restarted multiple times from second convergence.
  • First lets try to resolve multipler services. For that we would use notifications in chef. Refer Here for more info.
  • Using notifies updated recipe as shown below
#
# Cookbook:: .
# Recipe:: apachephp
#
# Copyright:: 2019, The Authors, All Rights Reserved.

apt_update 'update pacakges' do
    ignore_failure true
    action :update
end

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

php_packages = ["php", "libapache2-mod-php",  "php-mysql", "php-cli"]

php_packages.each do |package_name|
  package package_name do
      action :install
  end
end

remote_file '/var/www/html/info.php' do
    source 'https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/info.php'
    action :create
    notifies :restart, 'service[apache2]'
end

service 'apache2' do
    action :nothing
end

  • Change the version. Upload the cookbook using berks upload
  • Now lets try to create a file info.php rather than downloading it.
  • Generate a file in your cookbook using chef generate file . info.php and you can observe a folder called files gets created in your cookbook with a file info.php. In this file add the contents to info.php
<?php
phpinfo();
?>
  • To copy this file to your node, use chef resource cookbook_file. This changes the recipe as shown below
#
# Cookbook:: .
# Recipe:: apachephp
#
# Copyright:: 2019, The Authors, All Rights Reserved.

apt_update 'update pacakges' do
    ignore_failure true
    action :update
end

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

php_packages = ["php", "libapache2-mod-php",  "php-mysql", "php-cli"]

php_packages.each do |package_name|
  package package_name do
      action :install
  end
end

#remote_file '/var/www/html/info.php' do
#    source 'https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/info.php'
#    action :create
#    notifies :restart, 'service[apache2]'
#end

cookbook_file '/var/www/html/info.php' do
    source 'info.php'
    action :create
    notifies :restart, 'service[apache2]'
end

service 'apache2' do
    action :nothing
end

  • Change the version of the cookbook and upload the recipe and manually force convergence.

  • The problems still open are

    • uploading multiple versions of under developed cookbooks to chef server.
  • Next Steps:

    • Doing the developement for the same lampstack on centos
    • Avoiding uploading of under development cookbooks.
    • Uploading only stable versions to Chef Server.

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner