読者です 読者をやめる 読者になる 読者になる

masayuki5160's diary

名古屋でWeb系エンジニアしてます

VagrantでChef Soloテスト用のboxを作って、レシピをためす環境をつくる

Vagrantつかっていろいろやってますが、
目的はChef Soloのレシピをばしばしためしたいわけで。

Chef Solo実行環境をつくる手間はめんどくさいのでboxにしてしまいました。
とくにむずかしくもなく、最近ちょろちょろ記事に書いていたことをまとめてやったらあっさりできました。
簡単ですがログだけまとめておきます。



1. ひとまずchef soloの実行環境をつくっとく


tanaka-no-MacBook-Air:chefTest masayuki5160$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos2'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$ vagrant ssh
Last login: Wed May 8 23:59:54 2013 from 10.0.2.2
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ ls -l
合計 0
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/home/vagrant/.chef/knife.rb]
Please enter the chef server URL: http://localhost.localdomain:4000
Please enter an existing username or clientname for the API: [vagrant]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem]
Please enter the path to a chef repository (or leave blank):

****

You must place your client key in:
/home/vagrant/.chef/vagrant.pem
Before running commands with Knife!

****

You must place your validation key in:
/etc/chef/validation.pem
Before generating instance data with Knife!

****

Configuration file written to /home/vagrant/.chef/knife.rb
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

(省略)

2013-05-26 03:28:28 (16.6 KB/s) - `rpmforge-release-0.5.2-2.el5.rf.i386.rpm' へ保存完了 [12680/12680]

[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ ls -l
合計 20

  • rw-rw-r-- 1 vagrant vagrant 12680 11月 13 2010 rpmforge-release-0.5.2-2.el5.rf.i386.rpm

[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ su
パスワード:
[root@vagrant-c5-x86_64 vagrant]# rpm -Uvh rpmforge-release-0.5.2-2.el5.rf.i386.rpm

(省略)

Installed:
git.x86_64 0:1.7.11.3-1.el5.rf

Dependency Installed:
apr.x86_64 0:1.2.7-11.el5_6.5 apr-util.x86_64 0:1.2.7-11.el5_5.2 neon.x86_64 0:0.25.5-10.el5_4.1 perl.x86_64 4:5.8.8-40.el5_9 perl-DBI.x86_64 0:1.52-2.el5
perl-Git.x86_64 0:1.7.11.3-1.el5.rf perl-URI.noarch 0:1.35-3 perl-YAML.noarch 0:0.72-1.el5.rf postgresql-libs.x86_64 0:8.1.23-6.el5_8 rsync.x86_64 0:3.0.6-4.el5_7.1
subversion.x86_64 0:1.6.11-11.el5_9 subversion-perl.x86_64 0:1.6.11-11.el5_9

Complete!
[root@vagrant-c5-x86_64 vagrant]# git
git git-cvsserver git-receive-pack git-shell git-upload-archive git-upload-pack
[root@vagrant-c5-x86_64 vagrant]# git
git git-cvsserver git-receive-pack git-shell git-upload-archive git-upload-pack
[root@vagrant-c5-x86_64 vagrant]# pwd
/home/vagrant
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]# mkdir kitchen
[root@vagrant-c5-x86_64 vagrant]# ls -l
合計 28
drwxr-xr-x 2 root root 4096 5月 26 03:30 kitchen

  • rw-rw-r-- 1 vagrant vagrant 12680 11月 13 2010 rpmforge-release-0.5.2-2.el5.rf.i386.rpm

[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]# git clone https://github.com/masayuki5160/chef-repo.git kitchen
Cloning into 'kitchen'...
remote: Counting objects: 202, done.
remote: Compressing objects: 100% (122/122), done.
remote: Total 202 (delta 72), reused 165 (delta 47)
Receiving objects: 100% (202/202), 35.59 KiB, done.
Resolving deltas: 100% (72/72), done.
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]#
[root@vagrant-c5-x86_64 vagrant]# ls -l kitchen/
.git/ .gitignore LICENSE README.md Rakefile certificates/ chefignore config/ cookbooks/ data_bags/ environments/ roles/
[root@vagrant-c5-x86_64 vagrant]# ls -l kitchen/
.git/ .gitignore LICENSE README.md Rakefile certificates/ chefignore config/ cookbooks/ data_bags/ environments/ roles/
[root@vagrant-c5-x86_64 vagrant]# ls -l kitchen/
.git/ .gitignore LICENSE README.md Rakefile certificates/ chefignore config/ cookbooks/ data_bags/ environments/ roles/
[root@vagrant-c5-x86_64 vagrant]# ls -l kitchen/
.git/ .gitignore LICENSE README.md Rakefile certificates/ chefignore config/ cookbooks/ data_bags/ environments/ roles/
[root@vagrant-c5-x86_64 vagrant]#vi solo.rb
[root@vagrant-c5-x86_64 vagrant]#vi chefsolo.json


solo.rbは下記

# solo.rb
file_cache_path "/tmp/chef-solo"
cookbook_path ["/home/vagrant/kitchen/cookbooks"]

chefsolo.jsonは下記

{
"run_list" :[
//"recipe[vim]"
]
}

これでchef solo をためす準備はok
ちなみにOSはcentOS 5.9。



2. パッケージ化してboxとして追加

もちろん仮想サーバからでて作業をすること。
仮想サーバは起動中でもパッケージは作成できます。

tanaka-no-MacBook-Air:chefTest masayuki5160$ ls -l
total 16

  • rw-r--r-- 1 masayuki5160 staff 4342 5 21 08:29 Vagrantfile

tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$ pwd
/Users/masayuki5160/vagrant/chefTest
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$
tanaka-no-MacBook-Air:chefTest masayuki5160$ vagrant package
[default] Attempting graceful shutdown of VM...
[default] Clearing any previously set forwarded ports...
[default] Creating temporary directory for export...
[default] Exporting VM...
[default] Compressing package to: /Users/masayuki5160/vagrant/chefTest/package.box
tanaka-no-MacBook-Air:chefTest masayuki5160$ ls -l
total 802752

  • rw-r--r-- 1 masayuki5160 staff 4342 5 21 08:29 Vagrantfile
  • rw-r--r-- 1 masayuki5160 staff 410996784 5 26 12:41 package.box



3. boxを追加してサーバ起動

tanaka-no-MacBook-Air:vagrant masayuki5160$ mkdir chefSoloTest
tanaka-no-MacBook-Air:vagrant masayuki5160$
tanaka-no-MacBook-Air:vagrant masayuki5160$
tanaka-no-MacBook-Air:vagrant masayuki5160$
tanaka-no-MacBook-Air:vagrant masayuki5160$
tanaka-no-MacBook-Air:vagrant masayuki5160$ cd chefSoloTest/
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ vagrant box add chefsolo ../chefTest/package.box
Downloading or copying the box...
Extracting box...te: 389M/s, Estimated time remaining: 0:00:01)
Successfully added box 'chefsolo' with provider 'virtualbox'!
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ vagrant box list
centos-5.9-x86-64-minimal (virtualbox)
centos2 (virtualbox)
chefsolo (virtualbox)
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ ls -l
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ vagrant init chefsolo
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ ls -l
total 16

  • rw-r--r-- 1 masayuki5160 staff 4347 5 26 12:46 Vagrantfile

tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'chefsolo'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
tanaka-no-MacBook-Air:chefSoloTest masayuki5160$ vagrant ssh


Last login: Sun May 26 03:22:05 2013 from 10.0.2.2


[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$
[vagrant@vagrant-c5-x86_64 ~]$ ls -l
合計 28
drwxr-xr-x 9 root root 4096 5月 26 03:30 kitchen

  • rw-rw-r-- 1 vagrant vagrant 12680 11月 13 2010 rpmforge-release-0.5.2-2.el5.rf.i386.rpm

[vagrant@vagrant-c5-x86_64 ~]$



4. cookbook作成

かりにapacheをyumでインストールして起動させるレシピをつくってみます。

[root@vagrant-c5-x86_64 kitchen]# knife cookbook create apache -o cookbooks/

Creating cookbook apache

Creating README for cookbook: apache

Creating CHANGELOG for cookbook: apache

Creating metadata for cookbook: apache

root@vagrant-c5-x86_64 kitchen]# vi cookbooks/apache/recipes/default.rb
[root@vagrant-c5-x86_64 kitchen]# vi chefsolo.json
[root@vagrant-c5-x86_64 kitchen]# vi solo.rb
[root@vagrant-c5-x86_64 kitchen]#
[root@vagrant-c5-x86_64 kitchen]# sudo chef-solo -c solo.rb -j ./chefsolo.json
[2013-05-26T08:18:10+00:00] INFO: *** Chef 10.16.2 ***
[2013-05-26T08:18:10+00:00] INFO: Setting the run_list to ["recipe[apache]"] from JSON
[2013-05-26T08:18:10+00:00] INFO: Run List is [recipe[apache]]
[2013-05-26T08:18:10+00:00] INFO: Run List expands to [apache]
[2013-05-26T08:18:10+00:00] INFO: Starting Chef Run for localhost.localdomain
[2013-05-26T08:18:10+00:00] INFO: Running start handlers
[2013-05-26T08:18:10+00:00] INFO: Start handlers complete.
[2013-05-26T08:18:10+00:00] INFO: Processing package[httpd] action install (apache::default line 10)
[2013-05-26T08:18:18+00:00] INFO: package[httpd] installing httpd-2.2.3-78.el5.centos from updates repository
[2013-05-26T08:19:53+00:00] INFO: Processing service[httpd] action start (apache::default line 14)
[2013-05-26T08:19:53+00:00] INFO: service[httpd] started
[2013-05-26T08:19:53+00:00] INFO: Chef Run complete in 102.47374 seconds
[2013-05-26T08:19:53+00:00] INFO: Running report handlers
[2013-05-26T08:19:53+00:00] INFO: Report handlers complete
[root@vagrant-c5-x86_64 kitchen]#
[root@vagrant-c5-x86_64 kitchen]# ps -aux | grep httpd
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
root 2606 0.0 0.6 174696 3236 ? Ss 08:19 0:00 /usr/sbin/httpd
apache 2607 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2608 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2609 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2610 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2611 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2612 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2614 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
apache 2615 0.0 0.4 174696 2388 ? S 08:19 0:00 /usr/sbin/httpd
root 2629 0.0 0.1 61176 776 pts/0 R+ 08:48 0:00 grep httpd
[root@vagrant-c5-x86_64 kitchen]#

cookbooks/apache/recipes/default.rbは下記

#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

package "httpd" do
action :install
end

service "httpd" do
action :start
end


chefsolo.jsonは下記

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


solo.rbは下記

# solo.rb
file_cache_path "/tmp/chef-solo"
cookbook_path ["/home/vagrant/kitchen/cookbooks"]


これでなんどもレシピをつくって試せる環境ができました。
やっとやりたかったことができる、て感じです。