sohatach's blog

http://github.com/soha

WEB+DB PRESS No.75を参考にChefを動かしてみた。

Windows8へのChef+Vagrant環境構築

インストールする必要のあるもの

Ruby(1.9以上の最新のもの)
VirtualBox
・Vagrant
・Box(VirtualBox上で動かすOSイメージ、既にChefの入ったものを使う)
・knife-solo(ローカルPCに入れ、リモートでChefを操作する)
・(Cygwin

1-1. Rubyのインストール

Rubyインストーラをダウンロードします。ここでは無難に1.9(32bit)を入れます。
rubyinstaller-1.9.3-p448.exeをダウンロードしました。
http://rubyinstaller.org/downloads/
Rubyの実行ファイルへ環境変数PATHを設定する」
にチェックを入れ、環境変数PATHに入れます。

加えてRubyの追加パッケージを入れる際にコンパイル環境が必要になることがあることに備え
DEVELOPMENT KITもインストールします。
同じく
http://rubyinstaller.org/downloads/
より、DevKit-tdm-32-4.5.2-20111229-1559-sfx.exeをダウンロードします。
ダブルクリックし、展開するとインストール先を求められるので、
ここでは、C:\DevKitにインストールします。

インストールには、C:\DevKitにて以下を実行します。

ruby dk.rb init
ruby dk.rb install

参考
DevKitのインストール
http://www.rubylife.jp/railsinstall/rails/index4.html

Ruby1.9には、DevKit-tdm-32-4.5.2-20111229-1559-sfx.exeが対応するそうです。
https://github.com/rubygems/rubygems/issues/594


あとでbundleを使うためこれも入れます。

gem install bundler

bundlerとは、Rubyのパッケージ管理ツールで、必要なライブラリ(Gemファイル)の種類やバージョンを指定するものです。
Gemfileというファイルに記載します。(あとで出てきます)

参考
Bundlerを使ったGemパッケージの管理
http://www.rubylife.jp/rails/ini/index2.html

ツールを使いたいだけの人のための bundler 入門 (例: vagrant + veewee)
http://qiita.com/znz@github/items/5471e5826fde29fa9a80

1-2. VirtualBoxのインストール

https://www.virtualbox.org/wiki/Downloads
Windows用のインストーラをダウンロードし、インストールします。

1-3. Vagrantのインストール

http://downloads.vagrantup.com/
こちらもWindows用の最新インストーラをダウンロードし、インストールします。

1-4. Box(OSイメージ)のインストール

http://www.vagrantbox.es/
にいろいろなイメージファイルがありますが、
ここでは、Chef入りの以下のものを使用します。
https://s3.amazonaws.com/itmat-public/centos-6.3-chef-10.14.2.box

vagrant box add centos https://s3.amazonaws.com/itmat-public/centos-6.3-chef-10.14.2.box

でインストールします。


インストール関連は以上で終わり。
次からは、その後の準備作業になります。


2-1. Vagrant環境の構築

vagrant init centos
を実行することで先ほどインストールしたBoxのcentosの環境が構築されます。
実行時のディレクトリ下に設定ファイルが展開されるため、適当にディレクトリを掘って実行します。

2-2. Vagrant環境の起動

vagrant up
2-1で環境を構築したディレクトリで上記コマンドを実行することで、OSが起動します。

OSが起動せず途中で止まってしまう場合
BIOSで仮想化支援機能が無効になっている可能性が考えられます。
BIOSの設定を見直してください。

OSが起動するとSSHでログインできるようになっているはずです。
(デフォルトでlocalhostの2222ポート?)

vagrant ssh

でログインできることを確認する。
TeraTermなどのツールでもSSHログインできます。(接続情報は画面上のメッセージ参照)

.ssh/configの設定(この設定によりホスト名を指定して簡単にSSH出来るようになります)

cd %USERPROFILE%
mkdir .ssh
vagrant ssh-config --host centos > %USERPROFILE%\.ssh\config

set HOME=%USERPROFILE%

環境変数HOMEを設定しておかないとsshコマンドがconfigが参照できない可能性があるため設定しておく。

2-3. chef-soloを試す。(以下本章はcentos上で実行する)

試しにnginxをインストールしてみます。まずは

sudo rpm -qa | grep nginx

でインストールされていないことを確認します。

centossshログインし、

sudo knife cookbook create nginx

でレシピファイルを生成します。

sudo vi /var/chef/cookbooks/nginx/recipes/default.rb

で中身を以下のように書き換えます。

package "nginx" do
action :install
end

sudo chef-solo -o nginx

これでnginxがインストールされます。

sudo rpm -qa | grep nginx

でインストールされたことがわかります。


3-1. knife-soloのインストール(ここからWindows上の作業に戻る)

vagrant upを実行したディレクトリに以下のGemfileを追加する。

source 'https://rubygems.org'
gem 'knife-solo', '~> 0.3.0.pre5'

bundle install

でインストール。

bundle exec knife configure

で初期化する。

以下のようなエラーが出ました。

C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api.rb:20:in `require': cannot load such file -- ffi (LoadError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api.rb:20:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api/security.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api/security.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/security.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/security.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control/windows.rb:20:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control/windows.rb:20:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:30:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:30:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:27:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:22:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/mixin/enforce_ownership_and_permissions.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/mixin/enforce_ownership_and_permissions.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider.rb:23:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider.rb:23:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/platform.rb:25:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/platform.rb:25:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife/core/ui.rb:22:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife/core/ui.rb:22:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife.rb:26:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife.rb:26:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/application/knife.rb:18:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/application/knife.rb:18:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/bin/knife:23:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/bin/knife:23:in `'
from C:/Ruby193/bin/knife:23:in `load'
from C:/Ruby193/bin/knife:23:in `

'

ffiパッケージがロードできないとのエラーです。
その場合は、Gemfileに以下を追加します。

gem 'ffi'

そして再度

bundle install
bundle exec knife configure

以下のようにいろいろ聞かれますが、すべてデフォルトで問題なしです。

c:\Users\you\centos>bundle exec knife configure
WARNING: No knife configuration file found
Where should I put the config file? [C:/Users/you/.chef/knife.rb]
Please enter the chef server URL: http://localhost:4000
Please enter an existing username or clientname for the API: [you]
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:
C:/Users/you/.chef/you.pem
Before running commands with Knife!

****

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

****

Configuration file written to C:/Users/you/.chef/knife.rb

knife-soloでChef用レポジトリを作る

bundle exec knife solo init chef-repo

treeコマンドで確認すると以下のようなディレクトリが作成されてます。

c:\Users\you\centos>tree chef-repo
フォルダー パスの一覧
ボリューム シリアル番号は 00000092 A027:637A です
C:\USERS\YOU\CENTOS\CHEF-REPO
├─.chef
├─cookbooks
├─data_bags
├─nodes
├─roles
└─site-cookbooks

knife solo prepareでノードにChef Soloを入れる。

C:\Users\you\centos>bundle exec knife solo prepare centos
Bootstrapping Chef...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6510 100 6510 0 0 973 0 0:00:06 0:00:06 --:--:-- 20666
Downloading Chef 11.4.4 for el...
Installing Chef 11.4.4
warning: /tmp/tmp.8yNJ4RIq/chef-11.4.4.x86_64.rpm: Header V4 DSA/SHA1 Signature,
key ID 83ef826a: NOKEY
Preparing... ########################################### [100%]
1:chef ########################################### [100%]
Thank you for installing Chef!
Generating node config 'nodes/centos.json'...

もともと既にChefが入っていましたが、バージョンアップしているのがわかります。



以上で準備作業が終わりとなります。
次はクックブックの作成。

3-2. site-cookbooksにクックブックを作る。

C:\Users\you\centos\chef-repo>bundle exec knife cookbook create nginx -o site-cookbooks

Creating cookbook nginx

Creating README for cookbook: nginx

Creating CHANGELOG for cookbook: nginx

Creating metadata for cookbook: nginx

レシピの編集。

C:\Users\you\centos\chef-repo>notepad site-cookbooks\nginx\recipes\default.rb

デフォルトは空なので、以下を追記する。

package "nginx" do
action :install
end


centosへのnginxレシピの適用。

bundle exec knife solo cook centos -o nginx

本来は上記で適用されるはずですが、Windows環境のためかrsyncまわりでエラーが出ました。

C:\Users\you\centos\chef-repo>bundle exec knife solo cook centos -o nginx
Running Chef on centos...
Checking Chef version...
Generating node config 'nodes/centos.json'...
Uploading the kitchen...
ERROR: RuntimeError: Failed to launch command rsync -rl --chmod=ugo=rwX --rsh="ssh vagrant@centos" --delete --exclude 'revision-deploys' --exclude 'tmp' --exclude '.git' --exclude '.hg' --exclude '.svn' --exclude '.bzr' /cygdrive/C/Users/you/centos/chef-repo/cookbooks/ :~/chef-solo/cookbooks-1

https://www.itefix.no/i2/content/cwrsync-free-edition
からWindows用Rsyncをダウンロードします。(cwRsync_4.0.5_Installer.zipを落としました)
C:\Program Files (x86)\cwRsync\binをPATHに通します。
sshコマンドが被るのでGit for Windowsなどが入っている場合は、それより前にPATHを通してください。(Gitのsshコマンドだと動かないようです)

>Bad owner or permissions on /cygdrive/c/Users/you/.ssh/config
といったエラーが出る場合、configファイルのパーミッションを600に設定する必要があります。
cwRsyncの中にchmodコマンドは入っていますが、Windows8環境では正常に動かない(設定しても反映されない)ことがあるようです。
その際は、Cygwinを入れて以下のコマンドを実行します。
chgrp -R Users ~/.ssh
ファイルの所有グループを設定することでchmodが効くようになります。
(chgrpコマンドはcwRsyncに入ってないのでわざわざCygwinを入れる必要があります)
http://superuser.com/questions/397288/using-cygwin-in-windows-8-chmod-600-does-not-work-as-expected

C:\Users\you\centos\chef-repo>bundle exec knife solo cook centos -o nginx
Running Chef on centos...
Checking Chef version...
Uploading the kitchen...
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Generating solo config...
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Running Chef...
Starting Chef Client, version 11.4.4[0m
[2013-07-21T10:33:43+02:00] WARN: Run List override has been provided.
[2013-07-21T10:33:43+02:00] WARN: Original Run List: []
[2013-07-21T10:33:43+02:00] WARN: Overridden Run List: [recipe[nginx]]
Compiling Cookbooks...[0m
Converging 1 resources[0m
Recipe: nginx::default[0m
* package[nginx] action install[0m (up to date)[0m
Chef Client finished, 0 resources updated[0m

警告がたくさん出ています。この警告を消すには、
CYGWINという環境変数に以下を設定すると良いようです。

nodosfilewarning

3-3. 新たなクックブックの追加。

クックブックの名前は任意だが、ここではbase_packegesという名前で作成。

bundle exec knife cookbook create base_packages -o site-cookbooks

C:\Users\you\centos\chef-repo>notepad site-cookbooks\base_packages\recipes\default.rb

%w{gcc make git readline readline-devel}.each do |pkg|
package pkg do
action :install
end
end

bundle exec knife solo cook centos -o base_packages

としても良いが、Node単位で適用するクックブックを定義できるため、そちらを使用する。

chef-repo\nodes\centos.jsonに以下を記載する。

{
"run_list":[
"recipe[base_packages]",
"recipe[nginx]"
]
}

3-4. クックブックの適用

bundle exec knife solo cook centos