[Vagrant & VirtualBox] Chef: インストールから Cookbook を作って動かすまで

配布物の内容を理解できるようにアウトラインを確認しておきます。
いわゆる chef-solo, knife solo & Vagrant の構成となります。

[markdown]
> * [Mac OS XでVagrantとChefを使った環境構築のまとめ – Qiita](http://qiita.com/hamichamp/items/e27a0ecacc33482936c8)
> * [Chef入門 (全14回) – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_chef)

“`prettyprinted
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
Vagrant 1.6.2
chef (11.16.0)
knife-solo (0.4.2)
“`

## Workstation を設定する

Mac 側の設定です。

### インストール

chef をインストールします。

“`prettyprinted
% gem install chef
“`

knife-solo をインストールします。

“`prettyprinted
% gem install knife-solo
“`

ただし、バージョンコントロールしておいた方が良さそうです。

> * [Chef: vagrant-omnibus や knife solo prepare を利用すると wget が 404 error | deadwood](https://www.d-wood.com/blog/2014/09/17_6943.html)

### 設定

knife の設定をします。全てリターンで進めます。

“`prettyprinted
% knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/Users/****/.chef/knife.rb]
Please enter the chef server URL: [https://mbp.local:443]
Please enter an existing username or clientname for the API: [****]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem]
Please enter the path to a chef repository (or leave blank):
*****
You must place your client key in:
/Users/****/.chef/****.pem
Before running commands with Knife!
*****
You must place your validation key in:
/etc/chef-server/chef-validator.pem
Before generating instance data with Knife!
*****
Configuration file written to /Users/****/.chef/knife.rb
“`

pem 関連の記述があります。
このあたりは調べようと思いましたが、力尽きました。
いつかやるかも。

## Node を設定する

Vagrant VM 側の設定です。

### 仮想マシンの作成

ディレクトリを作成し、ローカルにある vagrant box を確認します。

“`prettyprinted
% mkdir vagrant
% cd vagrant
% vagrant box list
chef/centos-6.5 (virtualbox, 1.0.0)
chef/centos-6.5-i386 (virtualbox, 1.0.0)
precise64 (virtualbox, 0)
vccw_box (virtualbox, 0)
wp_box (virtualbox, 0)
“`

chef/centos-6.5 を使うことにします。
なければ box add をします。

> * [chef](https://vagrantcloud.com/chef)
> * [Vagrant: + VirtualBox で仮想マシンを作成する | deadwood](https://www.d-wood.com/blog/2013/09/20_4688.html#vagrant_box_list)

“`prettyprinted
% vagrant init chef/centos-6.5
“`

Vagrantfile を編集して接続設定を有効化しておきます。

“`ruby:VagrantFile
config.vm.box = “chef/centos-6.5”
config.vm.network “private_network”, ip: “192.168.33.10”
“`

仮想マシンを起動します。

“`prettyprinted
% vagrant up
“`

### ssh の設定

ホストに名前を付けて ssh config 設定を追記しておきます。

> * [Vagrant 仮想マシンに接続するための ssh config 設定の方法 | deadwood](https://www.d-wood.com/blog/2014/08/22_6712.html)

“`prettyprinted
% vagrant ssh-config –host zend >> ~/.ssh/config
“`

ここでは zend というホスト名にしました。

## knife-solo コマンドで chef を実行する

knife-solo コマンドを使って操作していきます。

> * [knife-soloによるChefの実行 – Qiita](http://qiita.com/kidachi_/items/b222fb2892e6108c46d5)

### chef リポジトリを作成する

リポジトリを作成します。

“`prettyprinted
% cd ../
% knife solo init chef-repo
“`

ファイル構成はこのようになりました。

“`prettyprinted
.
├── chef-repo
│   ├── cookbooks
│   ├── data_bags
│   ├── environments
│   ├── nodes
│   ├── roles
│   └── site-cookbooks
└── vagrant
└── Vagrantfile
“`

site-cookbooks 以下にレシピを作成していきます。
なお、cookbooks には配布されているクックブックを入れる慣習とのことです。

### 仮想マシンに chef をインストールする

ホスト zend へ chef をインストールします。

“`prettyprinted
% knife solo prepare zend
“`

### Cookbook を作成する

scaffold します。

“`prettyprinted
% cd chef-repo
% knife cookbook create zend -o site-cookbooks
** Creating cookbook zend
** Creating README for cookbook: zend
** Creating CHANGELOG for cookbook: zend
** Creating metadata for cookbook: zend
“`

このようなファイル構成になりました。

“`prettyprinted
chef-repo
├── cookbooks
├── data_bags
├── environments
├── nodes
├── roles
└── site-cookbooks
└── zend
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│   └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│   └── default.rb
├── resources
└── templates
└── default
“`

site-cookbooks/zend/recipes/default.rb を編集して、vim をインストールしてみます。

“`ruby
package “vim-enhanced” do
action :install
end
“`

nodes/zend.json を作成し、実行時のレシピを登録します。

“`json
{
“run_list”: [
“recipe[zend]”
]
}
“`

実行します。

“`prettyprinted
% knife solo cook zend
“`

このように package や service といった resource を使いながらレシピを書いていくようです。

> * [Resources and Providers Reference — Chef Single-page Topics](http://docs.getchef.com/chef/resources.html)

ただ、このままでは新規に `vagrant up` した後、毎回手動でプロビジョニングする必要があります。

“`prettyprinted
% vagrant up
% knife solo prepare zend
% knife solo cook zend
“`

次回にこのあたりを解決したいと思います。

## ファイル構成について

今回、プロジェクトディレクトリの構成については、ドットインストールのレッスンに対応する形で作ってみました。
ただ `vagrant up` や `knife solo` コマンドの実行、設定ファイルのパスの記述などで不便なことが多かったので、同一ディレクトリにまとめた方が使い勝手が良さそうです。

“`prettyprinted
.
├── Vagrantfile
├── cookbooks
├── data_bags
├── environments
├── nodes
│   └── zend.json
├── roles
└── site-cookbooks
└── zend
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│   └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│   └── default.rb
├── resources
└── templates
└── default
└── httpd.conf.erb
“`

例えば、次回からはこんな形でプロジェクトを開始しようと考えています。

“`prettyprinted
% knife solo init foo
% cd foo
% vagrant init chef/centos-6.5
% vagrant up
% vagrant ssh-config –host foo >> ~/.ssh/config
“`
[/markdown]