LoginSignup

(一)Trend
(二)Qiita Engineer Festa 2024
(三)Question
(四)Official Event
(五)Official Column
(六)Career
(七)Organization
 



226
Go to list of users who liked
231








More than 5 years have passed since last update.

@kasaharu(Wataru KASAHARA)inClassi

 Chef 

chef
knife-solo
serverspec
test-kitchen
Last updated at Posted at 2014-06-11



 Chef 

 knife-solo 使Serverspec  test-kitchen 使

Chef



 Infastructure as Code









Vagrant 

Chef 



Chef Chef-Server/Client Chef-Solo 

Chef-Solo  ( Chef-Solo 使)

Chef-Server/Client 



Chef-Server, Node, Workstation 3

 

Chef-Server



Node 

Cookbook 

Client 

Node



 Chef-Client 

Chef-Server Cookbook 

Workstation



Cookbook 

Cookbook  Chef-Server 

Chef-Solo 



Server  Client 

 Chef 

Chef-solo


$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
# インストール確認
$ chef-solo -v
Chef: 11.14.0.alpha.3

Recipe ()



Resource ()



Ruby 

git-core  apt  yum 

git-core 

OS Chef ()





default.rb

package "git-core" do
  action :install
end


cookbook ()



Recipe 


# cookbook のディレクトリ構成
.
├── attributes
├── definitions
├── files
│   └── default
├── libraries
├── providers
├── recipes
├── resources
└── templates
    └── default

kitchen ()





Chef 


# kitchen のディレクトリ構成
# 自分で cookbook を作成した場合は、基本的に site-cookbooks/ 下に置くことになる
.
├── cookbooks
├── data_bags
├── environments
├── nodes
├── roles
└── site-cookbooks

knife-solo



knife 使 Chef-Solo 



OS Vagrant  Chef 

OS cookbook OS knife  Chef-Solo 


# インストールはホストOS側で行う
$ gem install knife-solo
# インストール確認
$ gem list | grep knife
knife-solo (0.4.2)

kitchen 


$ knife solo init <kitchen 名>

cookbook 


# 作成した kitchen 下でおこなう
$ knife cookbook create <cookbook 名> -o site-cookbooks

Recipe 



cookbook  site-cookbooks//recipes/default.rb ( Recipe )

Node 



Node (Node)  Recipe 

sample1 git  Recipe 



nodes/sample1.json

{
  "run_list":[
    "recipe[git]"
  ]
}




 sample1 OS Chef-Solo Node 

OS Chef-Solo 


$ knife solo prepare sample1



 Node  Recipe 


$ knife solo cook sample1



Chef 使

 SSH 便

Serverspec



(Chef )

RSpec 


$ gem install serverspec
# インストール確認
$ gem list | grep server                          
serverspec (1.7.0)



 "git-core" 



git_spec.rb

require 'spec_helper'

describe package('git-core') do
  it { should be_installed }
end


test-kitchen



 Serverspec  -> Recipe  -> 


$ gem install test-kitchen
# インストール確認
$ gem list | grep kitchen
test-kitchen (1.2.1)



Vagrant vim  .vimrc  knife-solo 

kitchen  Recipe 



kitchen 


# kitchen 名は "qiita0611" とする
$ knife solo init qiita0611
Creating kitchen...
Creating knife.rb inkitchen...
Creating cupboards...



cookbook 


# cookbook 名は vim
$ knife cookbook create vim -o site-cookbooks
** Creating cookbook vim
** Creating README for cookbook: vim
** Creating CHANGELOG for cookbook: vim
** Creating metadata for cookbook: vim



Recipe 



site-cookbooks/vim/recipes/default.rb

package "vim" do
  action :install
end

template "/home/vagrant/.vimrc" do
  source "vimrc.erb"
  owner  "vagrant"
  group  "vagrant"
  mode 0644
end




 .vimrc  site-cookbooks/vim/templates/default/vim.rc.erb 

 test-kitchen 使()Node 

test-kitchen 



test 


$ kitchen init
      create  .kitchen.yml
      create  test/integration/default
         run  gem install kitchen-vagrant from "."
Successfully installed kitchen-vagrant-0.15.0
Parsing documentation for kitchen-vagrant-0.15.0
Done installing documentation for kitchen-vagrant after 0 seconds



.kitchen.yml 

suites:run_list  Node 





.kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: chef_solo

platforms:
  - name: ubuntu-12.04

suites:
  - name: default
    run_list:
      - recipe[vim::default]
    attributes:


Serverspec 



test/integration/default/ 


serverspec-init
Select OS type:

  1)UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: y
Auto-configure Vagrant from Vagrantfile? y/n: n
Input vagrant instance name: 
 + spec/
 + spec//httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile



spec_helper.rb 



spec_helper.rb

require 'serverspec'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
  c.before :all do
    c.path = '/sbin:/usr/sbin'
  end
end




spec/httpd_spec.rb 

httpd_spec.rb -> vim_spec.rb 





spec/vim_spec.rb

require 'spec_helper'

describe package('vim') do
  it { should be_installed }
end

describe file('/home/vagrant/.vimrc') do
  it { should be_file }
end




test/integration/default/spec/  test/integration/default/serverspec/ 

qiita0611/ 


$ kitchen test

....
Finished in0.08399 seconds       
2 examples, 0 failures       
       Finished verifying <default-ubuntu-1204> (0m1.42s).
-----> Destroying <default-ubuntu-1204>...
       [default] Forcing shutdown of VM...
       [default] Destroying VM and associated drives...
       Vagrant instance <default-ubuntu-1204> destroyed.
       Finished destroying <default-ubuntu-1204> (0m2.45s).
       Finished testing <default-ubuntu-1204> (2m28.58s).
-----> Kitchen is finished. (2m28.85s)



https://github.com/kasaharu/sample_for_testkitchen



Chef

Kitchen CI

Serverspec

Chef 



226
Go to list of users who liked
231
0
Go to list of comments
Register as a new user and use Qiita more conveniently
(一)You get articles that match your needs
(二)You can efficiently read back useful information
(三)You can use dark theme
What you can do with signing up
Sign upLogin


226
Go to list of users who liked
231

 


How developers code is here.
© 2011-2024Qiita Inc.

Guide & Help
About
Terms
Privacy
Guideline
Design Guideline
Feedback
Help
Advertisement
Contents
Release Note
Official Event
Official Column
Advent Calendar
Qiita Award
API
Career
SNS
X(Twitter)@Qiita
X(Twitter)@qiita_milestone
X(Twitter)@qiitapoi
Facebook@Qiita
Our service
Qiita Team
Qiita Zine
Official Shop
Company
About Us
Careers
Qiita Blog