Chef-Resources Elementary ingredient..

 

Introduction

Resources are the “elementary ingredient” of Chef house. Before mounting up on the spikes of chef’s elevations, it’s favorable to tailor your suit with basics. Combining common ingredients to make a new play substance is always thrilling!. I wish I had some magic secrets or shortcuts to share, but the truth is that you have to start with the ABC’s and then you can rule over the game.

 

 

Trigger a VM

Git clone our repository dedicated for this blog series.

 

 

Change directory to Chef/centos/chefResourceBackings. Here you find a Vagrantfile which spin up a centos7 machine with chefdk and other tools installed in it.

$ cat Vagrantfile

Launch a new vagrant machine and login.

$ vagrant up

$ vagrant ssh
Your learning environment is ready. Although in this blog we do not focus a lot on practicals, we try to clear our theoretical concepts.

 

Backings of Resources

Chef resources are the statements which define the configuration approach for any element. From the officials of chef, resources are

 

  • Describes the desired state for a configuration item
  • Declares the steps needed to bring that item to the desired state
  • Specifies a resource type—such as package, template, or service
  • Lists additional details (also known as resource properties), as necessary
  • Are grouped into recipes, which describe working configurations

 

Chef works with two basic parts, properties and actions.

Properties

Properties are the attributes and definitions for the target element of any resources. Some common properties for all resources without exception.

ignore_failure

What to do when a resource is fail in its doings i.e. continue or stop. Default value is false or stop.

provider

This is an optional property. You can explicitly define the correct provider for a resource. In common practices this is not mandatory to specified.

retries

Number of attempts that a resource tries in chef-run. Default value is 0.

retry_delay

Time duration between two consecutive tries. Default value is 2.

sensitive

This defines that the data in resource is sensitive and is not logged by chef-client. Default value is false. This property only applies to the execute, file and template resources.

 

Action

Actions differ for distinct resources. A common action for every resource is :nothing. Nothing action stated that resource should not do anything until it is notified by any other resource.

Some Basic Resources

Lets took example of some of the basic resources.
  • Package
This resource manage packages. This install, uninstall and upgrade any package. This resource is also a platform for some other dependent package management resources such as apt_package, dpkg_package, gem_package, yum_package etc. Available actions are :install, :purge, :remove, :upgrade and :nothing.
Example:

 

package ‘tar’ do
 version ‘1.26-29.el7’
 action :install
end
OR
package ‘tar’ do
 action :remove
end
  • File
This is responsible to manage files on a machine. Available actions are :create, :create_if_missing, :delete, :nothing and :touch.

 

Example

 

file ‘/tmp/test.txt’ do
 content ‘This is testing file.’
 mode ‘0755’
 owner ‘root’
 group ‘root’
end
OR

 

file ‘/tmp/test2.txt’ do
 content IO.read(‘/vagrant/resources/string.txt’)
 action :create
end
This will create a test.txt file under /tmp directory and put the contents of string.txt file into it.

 

  • Service

 

Subjected to manage services. Available actions are :disable, :enable, :nothing, :reload, :restart, :start and :stop.

Example

service ‘nginx’  do
 action :start
end

 

OR

service ‘nginx’ do
 supports :status => true, :restart => true, :reload => true
 action [ :enable, :start ]
end
This will ensure enable then start options for nginx service. Before this, install nginx using package resource.
  • Template
This resource calls chef templates to dynamically generate static files. Templates are “.erb” files with some variables and placed under “/templates” directory of cookbook. Available actions are  :create, :create_if_missing, :delete, :nothing and :touch.  Template resource only available with cookbooks, you can not use this resource with chef-apply from command line.

Example

template ‘/tmp/sshd_config’ do
 source ‘sshd_config.erb’
 owner ‘root’
 group ‘root’
 mode ‘0755’
end
OR

 

template ‘/tmp/config.conf’ do
 source ‘config.conf.erb’
 variables(
   :config_var => ‘mytext’
 )
end
This will pass variables to config.conf.erb file to generate config.conf file.

Extended Resource

Have some attention towards less likely used but important resources.
  • Git

This resource manages git repository by interacting with source code management system. Git version 1.6.5 (or higher) is required. Available actions are :checkout, :export, :nothing and :sync.

 

Example

git ‘/opt/mygit/’ do
 repository ‘https://github.com/OpsTree/Chef.git’
 revision ‘master’
 action :sync
end
  • Link

Resource is responsible to manage soft and hard links. Available actions are :create, :delete and :nothing.

Example

link ‘/tmp/myfile’ do
 to ‘/etc/ssh/sshd_config’
end
OR

 

link ‘/tmp/myhardfile’ do
 to ‘/etc/ssh/sshd_config’
 link_type :hard
end
The default link_type is :symbolic.

 

  • Script

This resources is used to execute external script. The supported interpreters are Bash, csh, Perl, Python, or Ruby. Available actions are :run and :nothing.


Example

script ‘extract_module’ do
 interpreter ‘bash’
 cwd ‘/tmp’
 code <<-EOH
   mkdir -p /tmp/mytest
   touch /tmp/mytest/file.txt
   EOH
end
  • Cron

 

To manage your cron jobs use this resource. If a property is not specified then default ‘*’ value is taken. Available actions are :create, :delete and :nothing.

 

Example
cron ‘noop’ do
 hour ‘5’
 minute ‘0’
 command ‘/bin/true’
end

 

OR

 

cron ‘tuesdaycron’ do
 minute ’50’
 hour ’11’
 weekday ‘2’
 command ‘/bin/true’
 action :create
end
This will run the cron job only on tuesday 11:50am.

 

To test all resources available above, go to resource directory under “chefResourceBackings” directory.  All resources are available as bash script.

Custom Resources

It’s an extension of the initially available properties.  Chef allows you to create your own resources. These custom resources extend the basic definitions of the built-in resources. The custom resources resides in the “/resources” directory of any cookbook. Conventionally name of any custom resource is the name of cookbook and name of resource file separated by underscore (_).  Custom resource creation is not an initial task for us so we skip this for now.

 

This is difficult to cover all about the resource(every resource and their properties or actions) in this article. Refer to chef-resources for more information.

 

It’s hard to get this boring stuff exciting in any manner. But it’s a fact that every serious effort makes you closer towards the excellence.

 

“The Expert In Anything Was Once A Beginner. “
Wanna be an adroit of Chef then from now “practice like a devil play like an angel”.

Chef-Resources Easy as pie..


Introduction

Resources are the basal element of chef’s heap. In all the functioning of chef, resources are in crux. These are a very first statement of any chef recipe or cookbook. In this blog we only intensify on the practical approach of  working with resources. This article will help you to compose a primitive awareness towards the execution of chef.

Pie Makes Everybody Happy!! L. H. Anderson

Prerequisites

To follow this article you need a prior information about Git and Vagrant. This blog uses centos7   as platform.

Getting Started

Sooner, probably but it necessitous to talk about chef installation. For this and some of next blogs we use chef-standalone mode. Chef-standalone mode provides a basic learning environment for beginners. Chef-standalone mode allow you to configure a machine directly.
Chef cater a complete  package labeled as Chefdk (Chef Development Kit ). It encloses all the essential thing you need to start with chef.

Install Chefdk

Clone our git repo and spin up a vagrant box with it.


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

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

$ vagrant ssh

Your learning environment is ready. Let’s start with chef resources.

Resource

Lets took our first resource package. As in our blog series we go through a common problem statement of installing nginx and then setup nginx vhost. This time we are going to do this with chef resources only. Chef resource have basic two parts properties and action. We will discuss both of these in our next article. Till then we start playing with  chef resources.

  • Package resource
Package resource is used to manage packages. This is the base resource for other package management resources for different platforms.

    • Add nginx repo by installing epel-release.
$ sudo chef-apply -l info -e “package ‘epel-release'”

    • Install nginx using package resource
$ sudo chef-apply -l info -e “package ‘nginx'”

  • Directory resource
Directory resource is responsible to maintain the directory lane in target node.This resource have multiple attributes to classify the permissions, owner and group.

    • Create home directory for vhosts.
$ sudo chef-apply -l info -e “directory ‘/usr/share/nginx/blog'”
$ sudo chef-apply -l info -e “directory ‘/usr/share/nginx/chef'”

This will create two directories blog and chef under ‘/usr/share/nginx directory’.

  • File resource
This resource is answerable for the file management on the node. This resource holds mode, content, owner, group and path attributes with their respective meaning for a file.

    • Create index files for nginx vhosts.
$ sudo chef-apply -l info -e “file ‘/usr/share/nginx/blog/index.html’ do content ‘Hello from blog server’ end”

$ sudo chef-apply -l info -e “file ‘/usr/share/nginx/chef/index.html’ do content ‘Hello from chef server’ end”

File resource creates a index.html file under chef and blog directories with the respective content available in resource.   

    • Create blog.opstree.com.conf  and chef.opstree.com.conf into the /etc/nginx/conf.d directory with files available for this.
$ sudo chef-apply -l info -e “file ‘/etc/nginx/conf.d/blog.opstree.com.conf’ do content IO.read(‘/vagrant/resources/blog.opstree.com.conf’) end”

$ sudo chef-apply -l info -e “file ‘/etc/nginx/conf.d/chef.opstree.com.conf’ do content IO.read(‘/vagrant/resources/chef.opstree.com.conf’) end”

This file resource creates vhost configuration files for blog.opstree.com and chef.opstree.com. This time file resource uses a ruby function IO.read to read the content of a sample file and paste them into target file.

  • Make entry in /etc/hosts
    • This is possible to make these entry with chef but it is a little complex for this time as we are not so proficient with chef. So we are doing this manually for now, but in our next article we will do this with chef.
$ sudo vim /etc/hosts
127.0.0.1 blog.opstree.com
127.0.0.1 chef.opstree.com

  • Service resource
Service resource is used to manage services. Use this resource to restart the nginx service.

    • Restart your nginx server to make changes effective.

$ sudo chef-apply  -e “service ‘nginx’ do action [:stop, :start] end”

This resource has two defied actions ie. stop and start which run in defined order to stop and then start nginx. You can use restart action of service resource but it is a good habit to use stop and start.

You can find all these resources in our git repo under “Chef/centos/chefResources/resources” directory. Just run the all “.sh” files to run our resources.

So now you start sensing the power of chef and also aware with the basics of resources. We will next come up with the theory behind the resources  and some new exciting examples in our blog.
The Good NEWS Is You’re The Pilot. Michael Altshuler
So stay great as Chef.

Chef Start here with ease..


Introduction

Until I discovered cooking, I was never really interested in anything. Julia Child

Chef, the lead in automation industry has many tickling facet and calibre. Before introducing the potentials of “The Chef”, it’s non negotiable to evade the foresight of its relevance to devops exercises. Chef can take care of server automation, infrastructure environment and continuously deliver your application.


Motive behind this array

With this blog series, we will familiarize you with the concepts of chef and will try to make you comfortable with our hands on blogs. This series of blog contains 15 blogs in a row which will enhance the knowledge and draw your faith in chef.

Always Pre-Heat The Oven Before Putting The Meat In !!

Prerequisites

For all the upcoming blogs we presume that you have basic understanding of Git, Docker,Vagrant and Linux. This blog series is written in consideration with centos as platform, although you can apply them on ubuntu by following some minor changes.


We are going to use our public git repository for all the blogs in this series. We will be using centos7 vagrant box to spin up our testing environment.


We are going to follow a single problem statement in our all blogs to maintain the uniformity and avoid the ambiguity. We are going to install nginx using chef and deploying two virtual host (blog.opstree.com, chef.opstree.com) with it.


Blogs in this series

In this blog we describe Nginx and manually setup the nginx, as per the problem statement and also create two virtual host(blog.opstree.com, chef.opstree.com).
Here we took some example of resources such as package, git, file and service and put our hands to work with chef-apply. We perform some simple task using chef resources.
This blog provides you theoretical concepts about chef resources. In this article  resources and their attributes elaborated.
Chef recipes is in consideration for this edition. Create your first recipe and apply it with chef. Complete doctrine behind the recipes of chef with simplified examples.
Walls of chef house, the cookbook, written from scratch with step to step explanation. Setup of nginx and proxy implementation with sample cookbook.
This blog furnish entire theoretical stuff about cookbooks. This includes command line cookbook generation and handling. One by one description of complete directory structure of a cookbook.  
Installation of chef kitchen. Testing of our nginx cookbook in different environment using docker container. Create, converge, verify and destroy a node with kitchen.
  1. Chef-Kitchen Chefs diagnosis center..
Theory behind the chef kitchen. Complete cycle of kitchen. With in this article elaborated view of .kitchen.yml file, and .kitchen folder provided.
  1. Chef Foodcritic && Chef Rubocop Handle it casually..
Chef lint tools, foodcritic and rubocop requirement. Theory, setup and practice exercises for foodcritic and rubocop.  
  1. Chef-Databags Carry all at once..
Introduction to databags and their need. Division of code and data with databags.  Databags implementation with chef-solo. Setup of mysql password with databags.  
  1. Chef-Roles Club everybody..
Requirement and implementation of chef roles. Clubbing of multiple nodes with chef roles. Complete web stack (webserver, proxy server and database) setup with roles.
  1. Chef-Environment  Organized wisely..
Chef environments for better management of the need of an organization. A complete organizational view with chef to setup different environment. Handle environments with chef-knife.
  1. Chef Server-Client Setup
Complete setup of chef client-server mode. Use of vagrant provisioning only, to spin up chef-server, chef-client and workstation.
  1. Collaboration of Client Server and Workstations
How chef-server, client and workstations work together to automate a complete infrastructure. Chef-server web interface.
  1. Chef Server-Client Work quietly..
Kickoff working with workstation. Chef-client. Install nginx and setup proxies with nginx cookbook on client node.

Chef Journey

I’m starting a blog series on chef where I would be taking you to a journey of managing my current infrastructure using Chef. To start with these are the high level tasks lists that I’ve in mind:

  • User Management : User’s creation or deletion on an environment(Dev/QA/Staging/Production) should be managed by chef, along with kind of access on the environment i.e read-only access, root access, or adding a user to some groups.
  • VPN Setup : Currently we are using openvpnas for managing secured access to our environment, it is manual right now so the vpn set-up will also be done by chef.
  • Apache Setup : We are using apache as web server that sits in front of our app server and also provides SSL.
  • Jar App : We have a SOA based set-up in which we have multiple micro java services, so we would be using chef to manage those jar app i.e deploying those jar app’s, starting/stopping those jar app’s.
  • Tomcat : Another major component type in our application are web apps that are hosted on tomcat server, the tomcat server is not managed as a service instead we create tomcat as an app user along with tomcat management scripts.
  • Mongo : We use replicated mongo as No SQL database in our application.
  • Logstash : For managing logs we are using log stash in a clustered set-up where all the log agents publish the logs to a central server and then served by Kibana, so this complete setup should also be managed by chef
  • ActiveMQ : We are using ActiveMQ for our queuing purpose

This list is not complete surely, I’ll be adding many more tasks in this list as I proceed in setting up my environment using chef as this is the first time I’ll be doing a set-up using Chef, but this list will be a good starting point.

Before jumping into creating the Chef cookbooks, runlists or data bags I’ve to setup the base infrastructure of Chef that is Chef Server to which all chef agents talk to, a chef workstation which would be updating the server with the configurations and a git repo to keep track of all my configuration as shown in the image given below.

In the next blog I’ll talk about how I’ll set-up a chef server. Let me know if you have any inputs for me or suggestion that how I should proceed with the chef set-up.

Chef Solo an Introduction

Introduction

Chef Solo is simple way to begin working with Chef. It is an open source version of the chef-client that allows using cookbooks with nodes without requiring access to a server. Chef Solo runs locally and requires that a cookbook (and any of its dependencies) be on the same physical disk as the node. It is a limited-functionality version of the chef-client and does not support the following:

  • Node data storage
  • Search indexes
  • Centralized distribution of cookbooks
  • A centralized API that interacts with and integrates infrastructure components
  • Authentication or authorization
  • Persistent attributes  

Installing chef-client  (Pre-requisite : curl )
Login to your box and run the following command to install the chef. Make sure that curl program is available on your box.

 curl -L https://www.opscode.com/chef/install.sh | bash  
cropinstall.jpg
To check if the installation was successful check the version of the installed chef-solo by:
 chef-solo -v  

version.jpg                                  

Making Chef Repository
Next step is to setup a file structure that will help organize various Chef files. Opscode, the makers of Chef provide one sample structure. They call it simply the Chef Repository.

 wget http://github.com/opscode/chef-repo/tarball/master  
structure.jpg 
 tar zxf master 
 mv opscode-chef-repo-**** chef-repo/ 
structure1.jpg
Assign cookbook’s path to the newly created cookbook directory inside the Chef Repository which will hold the cookbook

  mkdir .chef  
  echo "cookbook_path ['/root/chef-repo/cookbooks' ]" > .chef/knife.rb   
  knife cookbook site download apt  
.Chef folder

For Chef Solo this directory generally contains only knife.rb file. A knife.rb file is used to specify the chef-repo-specific configuration details for Knife. This file is the default configuration file and is loaded every time this executable is run. The configuration file is located at: ~/.chef/knife.rb. If a
knife.rb file is present in the . chef/knife.rb directory in the chef-repo, the settings contained within that file will override the default configuration settings. Sample content of knife.rb file can be:
 cookbook_path [ '/root/chef-repo/cookbooks' ]  
 role_path [ '/root/chef-repo/roles' ]  
 environment_path [ ' /root/chef-repo/environments ' ]  
 data_bag_path [ ' /root/chef-repo/data_bags ' ]  
Getting Started with Chef Solo
Before we’re able to run Chef Solo on our servers, we will need to add two files to our local Chef repository: solo.rb and node.json.
The solo.rb file tells Chef Solo where to find the cookbooks, roles, and data bags.

The node.json file sets the run list (and any other node-specific attributes if required).

     Create a solo.rb file inside our Chef repository with the following contents:
       current_dir = File.expand_path(File.dirname(__FILE__))  
       file_cache_path "#{current_dir}"  
       cookbook_path "#{current_dir}/cookbooks"  
       role_path "#{current_dir}/roles"  
       data_bag_path "#{current_dir}/data_bags"  
      
      Create a file called node.json inside your Chef repository with the following contents:
       {  
            "run_list": [ "recipe[]" ]  
       }  
      
                  

      Example:- 
      In this example i am going to install apt cookbook and the recipe which i am going to use is apt and here is my solo.rb and node.json files looks like
      solo1.jpg
      Our first Chef run  Goto chef-repo folder and execute following command

       chef-solo -c solo.rb -j node.json  
      run1.jpg
       
      run_last1.jpg

      How it works:

      1. solo.rb configures Chef Solo to look for its cookbooks, roles, and data bags   inside the current directory: the Chef repository.
           2. Chef Solo takes its node configuration from a JSON file, in our example we simply        called it node.json. If we’re going to manage multiple servers, we’ll need a separate    &nbsp &nbsp &nbsp file.
      1. Then, Chef Solo just executes a Chef run based on the configuration data found in
                 solo.rb and node.json