Chef; Membuat Cookbook Sederhana dan Ujicoba Cookbook dengan Test-Kitchen

Chef Software Inc. company logo

Melanjutkan tulisan sebelumnya.
Prasyarat tool dan kawan-kawannya yang digunakan:

  1. ChefDK dan Ruby
  2. Text Editor
  3. Vagrant
  4. Internet kenceng
  5. Test-Kitchen

Kali ini rencananya mau bikin cookbook nginx untuk lingkungan deb (debian, ubuntu, etc). Mari dimulai membuat cookbook dan ujicoba di lokal:

  1. Buat lingkungan kerja
    $ mkdir -p ~/chef-repo/cookbook && cd ~/chef-repo
  2. Generate cookbook kosong dengan perintah chef
    $ chef generate cookbook cookbooks/nginx
  3. Edit resep, tambahkan perintah untuk memasang paket nginx dan dependensinya
    $ vim cookbooks/nginx/recipes/default.rb
    #
    # Cookbook Name:: nginx
    # Recipe:: default
    #
    # Copyright (c) 2016 The Authors, All Rights Reserved.
    
    include_recipe "apt"
    
    # memasang nginx
    package 'nginx' do
      action :install
    end
    
    service 'nginx' do
      action [:enable, :start]
    end
    
    # mengubah conten index.html dgn isi lain
    template "/usr/share/nginx/html/index.html" do
    	source "nginx.erb"
    	mode "0644"
    end
    $ vim cookbooks/nginx/metadata.rb
    
    name 'nginx'
    maintainer 'The Authors'
    maintainer_email 'you@example.com'
    license 'all_rights'
    description 'Installs/Configures nginx'
    long_description 'Installs/Configures nginx'
    version '0.1.0'
    
    depends 'apt'
  4. Generate template halaman index.html
    $ chef generate template cookbooks/nginx nginx
    $ vim cookbooks/nginx/templates/default/nginx.erb
    nginx.erb
  5. Mari kita uji resepnya lewat kitchen
    $ cd cookbooks/nginx
    $ vim .kitchen.yml
    ---
    driver:
      name: vagrant
    
    provisioner:
      name: chef_zero
    
    platforms:
      - name: ubuntu/trusty64
    
    suites:
      - name: default
        run_list:
          - recipe[nginx::default]
        attributes:
    $ kitchen create
    $ kitchen converge
  6. Tambahkan unit testing dengan bantuan serverspec
    $ vim cookbooks/nginx/test/integration/default/serverspec/default_spec.rb
    require 'spec_helper'
    
    # Serverspec examples can be found at
    # http://serverspec.org/resource_types.html
    
    nginx_port = 80
    
    describe 'nginx' do
    
      describe package('nginx') do
      	it { should be_installed}
      end
    
      describe service('nginx') do
      	it { should be_running}
      end
    
      describe service('nginx') do
      	it { should be_enabled}
      end
    
      describe port(nginx_port) do
        it { should be_listening}
      end
    end
    
  7. Lakukan testing-kitchen
    $ kitchen verify
  8. Proses ini akan memakan waktu lama, tergantung internet. Jika tanpa error maka cookbook sudah benar. Selamat!
    Login ke mesin dan lakukan curl

    $ kitchen login
    $ curl localhost
  9. Hancurkan instansce kitchen setiap selesai testing.
    $ kitchen destroy

Sekian, tulisan selanjutnya akan membahas knife bootstrap.

*kode sumber cookbook ini dapat dilihat di akun github saya.

Bacaan:

[0] https://learn.chef.io/learn-the-basics/rhel/configure-a-package-and-service/
[1] https://learn.chef.io/local-development/ubuntu/get-started-with-test-kitchen/
[2] https://www.digitalocean.com/community/tutorials/how-to-create-simple-chef-cookbooks-to-manage-infrastructure-on-ubuntu

Estu, yang lagi sibuk di dapur