Monday, September 4, 2017

chef-kitchen

Test environment for chef:

Chef supports kitchen,where you can test stuff locally.

But it demands,couple of things:
One is:
1)virtualBox
2)Vagrant

.kitchen.yml will determine you test environment.
The next important file is metadata.rb,this file should be present.

These two are important to get started.


---
driver:
  name: vagrant

provisioner:
  name: chef_solo
  roles_path: ../../roles
  data_bags_path: ../../data_bags
  environments_path: ../../environments
  cookbook_path: ../../cookbooks

platforms:
  - name: centos-7.2

suites:
  - name: default
    run_list:
      - role[]
      - recipe[cookbookname::default]
      - recipe[cookbookname::recipefile]
    verifier:
      inspec_tests:
        - test/recipes
    attributes: {"vmnameprovider(ec2)": {"placement_availability_zone": "xxxxxxxxx"}}



Vagrant:
Google search > public vagrant boxes.

(this url,can help us to search,which boxes we require).


(we can see,a lot of kitchen stuff in this).


Cookbook:: firsttest
Recipe:: default

Package ‘tree’ do
  Action :install
End

----------------------------------------------------------------------------------------------------------------------------

driver:
  name: vagrant

provisioner:
  name: chef_zero
  #roles_path: ../../roles
 # data_bags_path: ../../data_bags
  #environments_path: ../../environments
  #cookbook_path: ../../cookbooks

platforms:
  - name: Ubuntu-14.04
     driver:
        box:Ubuntu/trusty64

suites:
  - name: default
    run_list:
      - recipe[firsttest::default]
    verifier:
      inspec_tests:
        - test/recipes
    attributes:
-----------------------------------------------------------------------------------------------------------------
.kicthen.yml:
this is the place where,we defined how your test environment be.

Note:
Kitchen has a concept called the Driver.
(Driver is,what should be a machine,created for you).
In our case,we are saying vagrant to create a machine for you.

Note:
Most easiest Driver is vagrant.
“this is for mainly local dev environement”

Note:
first ,we have to come to .kicthen.yml and find out your end-user environment.

In this case,my end-user environment is Ubuntu/trusty64.
Or
Ubuntu-14.04

Important stuff,we have to do,before executing the kitchen:
(before kitchen to be tested).

1)Go to the cookbook folder:
in our case it is :

 >>>> firsttest   >  berks install

(berkshelf tool,which is a dependency manager for chef).

Note:
(Whenever you change,anything in the metadata.rb,we need to do the berks install).

Command to check the kitchen stuff on machine:  kitchen converge
(kitchen converge(setup+start the machine) is one command,which will do everything for us).
(kitchen create will do all the setup required) ** rarely used.
(kitchen destroy to remove the virtual machine).
(kitchen login to login in to the virtual machine).


What happends,when we do kitchen converge?
1)kitchen launches Ubuntu machine
2)it provisions with the help chef-zero
3)on that chef-zero,it will try to apply the cookbook.

Do kitchen login and check the “tree” got package installed or not.

Note:Things to be noticed,

1)During/after this process,we can notice that,there is directory called the .kitchen
   In that,we can see:
   Kitchen-vagrant file(that means automatically a vagrant file is generated).
2) In that,we can see logs directory also.
     Default-ubuntu -1404.log
      Kitchen.log
(you have see,all the logs are stored at this location).
(if something is not working,the failures of that recipe,we can see in this).

for example:
Tomcat landing page/apache landing page:

To test this,a virtual machine needs an ipaddress.

I would be giving,something called as private ipaddress.

Remember,In virtual machines

We have two kinds of networking:
1)private networking
2)public networking.

Generally,
Public networking means,the network which as exposing connection with  theinternet.

Note:
But incase of virtual machine there is slight different:

Incase of virtualmachine,private networking means:where do we install virtualmachine software,we have installed on our laptop,so the network established between,mylaptop and the virtualmachine installed with in mylaptop is private network.

This is not to every network,this is virtual machine scenario.

What is private network interms of virtualmachine?
The network established between you and the virtual machine launched by you and this is in your machine only,not on the other machine

There is a concept called as public network:


Public networking means,let us assume that,I am(laptop) connecting to the internet cable via wifi router,Making my virtual machine connected to the network,which I am connected to,is called the public network.

For example:

Let us assume that,we are all working in an office and I created a kitchen-vagrant machine,where my Jenkins is installed and you want to use that machine,you used a virtual machine,which I have created on my machine from your machine ,both are different machines,in one machine,we have a virtualbox and in that virtual machine we  have a kitchen-vagrant,if I just,have a private networking configured,it is,I would say not possible(it is not directly possible).
          But,if we configured a public network, your virtual machine will get a ipaddress,which belongs to the network ipaddress,where you belong.

So,that’s why the other person,can be logging in to that.

If your still in private networking between the virtualmachine and your machine,if you want to give access to the external person,there is a concept called the port forwarding,which we need to do.

Note:
Between you and your machines,always there should be private networking.
But,you need to expose your virtual machine,to the external world.
The concept here,we used is port forwarding.

So,what I will do is  “80” on virtual machine  and it will be mapped to “8081” on my machine.
To the external world,when someone wants to communicate the virtual machine,I would give my machine ipaddress:8081,8081 of my machine is mapped to the port 80 of virtual machine.
The traffic will get routed,this is done interms of private network.

In public networking,your virtual machine will get the ipaddress.

Most of the time,we use the private networking.

Note:
Public,we don’t do it,because,we don’t have a DHCP server.

What is a DHCP server?
You login in a machine in your office and your getting an ipaddress and  you restart the machine,it is not guarantee,that you get a same ipaddress,so there is someone,who is allocating the ipaddress right and that component with in our network,use as ipadress is nothing but the DHCP server and in general incase of home networks,we don’t have DHCP server.
But incase of offices, we have DHCP SERVER, things will be smoother, when compared to, public network overhere.
Because, there is an external component, which tries to give the ipaddress.

We mostly use private networking,that does not mean that,we wont prefer public networking,just public networking cannot be simulated here.

Cookbook:: firsttest
Recipe:: default
      
           package ‘tree’  do
                 action  :install
            end

    ##  include_recipe ‘firsttest::install_apache’  (just an option)

(we can also,do this)


Cookbook:: firsttest
Recipe:: install_apache

(this is a Ubuntu machine,but I want to write a package,which works on both).

If node[‘platform’] == “Ubuntu”
    Apt_update ‘package Updates’ do
            Action  :update
       End
End

 Package_name = node[‘firsttest’][‘webserver’][‘name’]
   (because this will be part of my node object)

       package package_name  do
                 action  :install
        end
    
       service package_name do
                   action  [:enable, :start]
        end

(sometimes,we require,more than one thing to be done in service)
---------------------------------------------------------------------------------------------------
Attribute.rb

[Here we can write ruby code,that does not mean that,you write a recipe].
[we can write if conditions and all the logical stuff,but not the execution stuff].
[this is supposed to have the variable definitions].

If node[“platform”] == “ubuntu”
       default[‘firsttest’][‘webserver’][‘name’]= “apache2”
If I think,I can give much more,webserver values.
     elsif  node[“platform”] == “RedHat”
       default[‘firsttest’][‘webserver’][‘name’]= “httpd”
  End

To use this,in my code (recipe),what I need to copy [‘firsttest’][‘webserver’][‘name’]
I have to copy this structure.
------------------------------------------------------------------------------------------------------------------
Reference:
Google  >> chef resource service

We can pass the service stuff,action in the array also
[:enable ,  :start]

How to copy,this stuff to the run_list of kitchen machine?
We have to go to the kitchen.yml file.
            run_list:
-       recipe[firsttest::default]
-       recipe[firsttest::install_apache]

-----------------------------------------------------------------------------------------------------------------------------

driver:
  name: vagrant

provisioner:
  name: chef_zero
  #roles_path: ../../roles
 # data_bags_path: ../../data_bags
  #environments_path: ../../environments
  #cookbook_path: ../../cookbooks

platforms:
  - name: Ubuntu-14.04
     driver:
        box:Ubuntu/trusty64
        network :
-       [“private_network” , {ip: “192.168.33.34”}]
suites:
  - name: default
    run_list:
      - recipe[firsttest::default]
      - recipe[firsttest::install_apache]
    verifier:
      inspec_tests:
        - test/recipes
    attributes::
-----------------------------------------------------------------------------------------------------


Reference for kitchen files:


No comments:

Post a Comment