Wednesday, 30 March 2016

Chef-Cookbooks Roast it perfectly..




Introduction

This is our first major step towards our learnings. We are now quite sound with the ABC’s of chef. Now we announce the most compelling facet of chef. Cookbooks are the most crucial segment of the chef’s kingdom. Nothing worth having comes easy, so kickoff with more efforts.

A Real Test Of a Good Chef Is, Perfectly Roasted Chicken. julia child

Wait wait wait !! fire your all cylinders but remember to breath.

Prerequisites

This article assumes that you are aware with the basics of Git and Vagrant. You know the basic functioning of chef and its recipes and resources. This article is written with centos7 platform. To know about chef follow our previous blogs of this series Chef Start here with ease.. .

Get started

Clone our git repo and fire up a vagrant box with this.


  • Go to Chef/centos/chefCookbooks directory. This directory contains a Vagrantfile, which can launch a centos7 vagrant machine with Chefdk and other essential tools installed.
$ cat Vagrantfile


  • Download Chefdk using below available command

$ cd Chef/centos/chefCookbooks

$ wget https://opscode-omnibus-packages.s3.amazonaws.com/el/7/x86_64/chefdk-0.11.2-1.el7.x86_64.rpm

  • This directory also includes a knife.rb file which sets the cookbook folder path and default editor for the virtual machine.

  • Launch a new vagrant machine and login into it.
$ vagrant up

$ vagrant ssh

Your working environment is ready. Let's start with chef cookbooks.

Create your first cookbook

First create a dedicated directory for our cookbooks. As in knife.rb file it will be created by following command.

$ mkdir /vagrant/cookbooks

Chef manage its cookbooks using a version control system so next we initialize and also make our first commit for /vagrant/cookbooks directory. Provide your name and email for git configuration.
$ mkdir /vagrant/cookbooks
$ cd /vagrant/cookbooks
$ git init
$ git add .
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
$ git commit -m "Initial Commit"

Now you are ready to start with the creation of your first cookbook.

Call up with knife

We are using knife to generate are cookbooks. Generate your cookbook for installing nginx and to set a virtual hosts blog.opstree.com and chef.opstree.com with this.

Generate our first cookbook using below written command. This command setup copyright, email, license, and readme format options for your cookbook.
$ knife cookbook create nginxVhost -C "Saurabh Vajpayee" -m "myemail@email.com" -I nginxv1 -r md

Let's create a recipe

Create the default recipe with below provided command and put below available content.

$ vim /vagrant/cookbooks/nginxVhost/recipes/default.rb

package 'epel-release' do
action :install
end

package 'nginx' do
action :install
end

directory "#{node['nginx']['webroot']}" do
 recursive true
end


template "/etc/nginx/conf.d/#{node['nginx']['conffile']}" do
 source 'chefmanagedconf.conf.erb'
 variables(
   :port => "#{node['nginx']['port']}",
   :servername => "#{node['nginx']['servername']}",
   :webroot => "#{node['nginx']['webroot']}"
 )
end


template "#{node['nginx']['webroot']}/index.html" do
 source 'index.html.erb'
 variables(
   :servername => "#{node['nginx']['servername']}"
 )
end

line = "127.0.0.1 #{node['nginx']['servername']}"
file = Chef::Util::FileEdit.new('/etc/hosts')
file.insert_line_if_no_match(/#{line}/, line)
file.write_file

service 'iptables' do
 action :stop
end

service 'nginx' do
action :restart
end

This file includes multiple chef resources and some variable like 'webroot', conffile, port and servername. These variables have their default values under attribute directory and used in the templates which are next to create.

Create attribute file

This file contains the default value of your variables used in recipes. Create a default.rb file and place these values.

$ chef generate attribute /vagrant/cookbooks/nginxVhost/ default

$ vim /vagrant/cookbooks/nginxVhost/attributes/default.rb

default['nginx']['port'] = "80"
default['nginx']['webroot'] = "/usr/share/nginx/blog"
default['nginx']['servername'] = "blog.opstree.com"
default['nginx']['conffile'] = "blog.opstree.com.conf"

Create templates

Create template file to provide dynamic touch to your files. First create template for “configuration” files, and put below provided content.

$ chef generate template /vagrant/cookbooks/nginxVhost/ chefmanagedconf.conf

$ vim /vagrant/cookbooks/nginxVhost/templates/default/chefmanagedconf.conf.erb

server
{
   listen       <%= @port  %>;
   server_name  <%= @servername  %>;

   location / {
       root   <%= @webroot  %>;
       index  index.html index.htm;
   }

   error_page  404              /404.html;
   location = /404.html {
       root   <%= @webroot  %>;
   }

   # redirect server error pages to the static page /50x.html
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   <%= @webroot  %>;
        }
  }

And next place template for index.html file with following content.

$ chef generate template /vagrant/cookbooks/nginxVhost/ index.html

$ vim /vagrant/cookbooks/nginxVhost/templates/default/index.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
       <title>Test Page for the Opstree Server </title>
   </head>

   <body>
       <h1>Welcome to <strong> <%= @servername %> </strong></h1>
   </body>
</html>

Your cookbook is ready for initial workings. Let’s run it.

Run and feel like a million bucks

Run your cookbook with below command, and relax for a while.

$ sudo chef-client --local-mode  --runlist 'recipe[nginxVhost]'
By default this cookbook setup blog.opstree.com vhost if you want to setup another vhost chef.opstree.com then overiride default values of variables. To do this create a json file which declares the new values for variables.

$ vim /vagrant/runlist.json

{
"nginxVhost": {
"webroot" : "/usr/share/nginx/chef",
"servername" : "chef.opstree.com",
"conffile": "chef.opstree.com.conf"
},

"run_list": [
"recipe[nginxVhost]"
]

}

And run chef-client once again with following commands.

$ sudo chef-client --local-mode  -j /vagrant/runlist.json

Now you  have power to create as many vhost automatically with chef.

Verify the vhost

$ curl blog.opstree.com
$ curl chef.opstree.com


From now we have to work hard to match the expectation of the industry. You are now developing into a chef proficient. Do some experiments and play hard.

Thursday, 24 March 2016

Chef-Recipes Bake it calmly..


(source: google.com)

Introduction

This is very crucial state of your learnings with chef. It's not easy to deposit that much of attention continuously so be calm and reassemble all your amplitude for the next bout. Straightway we are going to clash with chef-recipes. We will try to maintain the balance so we didn't feel overfed.
Never Eat More Then You Can Lift. Miss Piggy
(source: google.com)

Prerequisites

This article is written in consideration with centos as platform. We assume that you have basic understanding of Vagrant, Git and Chef-Resources. To know about chef resources follow our previous blogs of this series Chef Start here with ease.. .

Get started

Clone our git repository to create your learning vagrant environment.


  • Change your current directory to Chef/centos/chefRecipes. A vagrant file is present here with some bash provisioning to provide you a complete chef learning environment.
$ cat Vagrantfile
Trigger a vm using following commands.  

$ vagrant up

$ vagrant ssh
This will spin up and login into a centos7 vagrant machine with chef installed in it.

Recipe

A recipe is the first significant element of a cookbook to manage any environment. A recipe club together multiple resources (built-in and custom )and some ruby code. The recipes are ruby files with “.rb” extension. These are generally part of a cookbook. A recipe may be dependent over any other recipe. The recipe ensure the legitimate use of templates and files.

Recipes also contains attributes for different resources. These attributes help chef to maintain the desired state of any machine under chef’s attention.

Shape your first set of recipes

Let’s start with our first recipe. We deal with our common problem statement for all articles. We are installing nginx web server and then create two vhost chef.opstree.com and blog.opstree.com using a set of recipes. With this you can gently start feeling the power of chef and automation.   

  • Create a nginxInstall.rb file


Create nginxInstall.rb using below command and put the following content in that file.
$ vim nginxInstall.rb

package 'epel-release' do
action :install
end

package 'nginx' do
action :install
end

service 'nginx' do
action [ :enable, :start ]
end

This will install nginx on your machine with default configuration, and also start it.
  • Create nginxVhost.rb file

Create a nginxvhost.rb file to configure your vhost. Paste the following content in it.  

$ vim nginxVhost.rb

directory '/usr/share/nginx/blog' do
   recursive true
end

directory '/usr/share/nginx/chef' do
  recursive true
end

file  '/usr/share/nginx/blog/index.html' do
 content '<html><title>Chef Session</title><body><h1>Hello this is Blog from Opstree !!</h1></body></html>'
end

file  '/usr/share/nginx/chef/index.html' do
 content '<html><title>Chef Session</title><body><h1>Hello this is Chef Opstree !!</h1></body></html>'
end

file '/etc/nginx/conf.d/blog.opstree.com.conf' do
 content IO.read('/vagrant/recipes/blog.opstree.com.conf')
end

file '/etc/nginx/conf.d/chef.opstree.com.conf' do
 content IO.read('/vagrant/recipes/chef.opstree.com.conf')
end

service 'nginx' do
 action [:stop, :start]
end

Download the content of files blog.opstree.com.conf and chef.opstree.com.conf .

This recipe configure the two desired vhost chef.opstree.com and blog.opstree.com for you. This recipe includes directory, file and service resources. It also includes IO.read ruby function to read files.

Directory resource creates complete data directory structure for chef and blog vhost. File resource maintain content for index.html and .conf files of chef.opstree.com and blog.opstree.com.
  • Make entry in /etc/hosts


This is also possible with chef but for for now we are doing it manually.
$ sudo vim /etc/hosts

127.0.0.1 blog.opstree.com
127.0.0.1 chef.opstree.com
  • Get set go


Now run your recipes one by one and sense the fascination with chef.

$ sudo chef-apply nginxInstall.rb

$ sudo chef-apply nginxVhost.rb

Now all is set, your virtual host are ready to visit.

$ curl chef.opstree.com

$ curl blog.opstree.com
Recipes are your first step towards chef expertise. Start behaving like flier, clench the nut bolts and start practising with chef-recipes. Took some basic problems and resolve them with recipes. More you dig into the sea more you get.

The Great Recipes For Success Is To Work And Always Work. Leon Gambetta

You are now going to be addict of chef and automation, be aware of your demands .
Dear stomach you're bored not hungry, So shut up !!
(source: google.com)