Chefにゅうもん2

嫁が起きない。
半日以上は寝てるってばよ。

前回はNode側でvimをインストールしてみた。
今回は自分のマシンからvagrant upした仮想マシンをいじくってみよう。

vagrant init

まずVagrantboxで適当なboxファイルを探しきて、Vagrantfileを作る。

$ vagrant box add centos6 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

$ mkdir ~/vms/centos6 && cd ~/vms/centos6

$ vagrant init centos6

Vagrantfile

こんな感じで

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vbguest.auto_update = false
  config.vm.box = "centos6"
  config.vm.network "private_network", ip: '192.168.33.10'
end

Gemfile

# Gemfileはこんな感じで
$ cat Gemfile

source 'https://rubygems.org'

gem 'chef'
gem 'knife-solo'
gem 'berkshelf'

# bundle execって打つのめんどくさいから--binstubsオプションつける
# 自分はrbhev使ってるからこれ参考にしたよ
# http://qiita.com/naoty_k/items/9000280b3c3a0e74a618
$ bundle install --binstubs

Chefリポジトリの作成

$ bundle exec knife solo init .
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...

なんか色々ファイルが出来た。

centos6
├── Berksfile      # 誰かが公開しているCookBookをBundlerみたいに管理する為の設定ファイル
├── Gemfile        # Gemの依存関係を管理するファイル
├── Gemfile.lock   # 依存gemのversionと取得先
├── Vagrantfile    # 仮想マシン起動の為の設定情報
├── cookbooks      # 誰かが公開しているcookbookを置いとく場所
├── data_bags      # cookbook内で利用したいデータを格納する場所らしい
├── environments   # 開発用とか本番用とか環境によって設定を分けたい時に使うデータを格納する場所
├── nodes          # 開発用とか本番用とか環境によって設定を分けたい時に使うデータを格納する場所
├── roles          # Nodeの状態を定義しておくもの置き場
└── site-cookbooks # 自分で作ったcookbookはここに置いとく

Chef Soloのインストール

# 仮想マシン起動
vagrant up

# SSHの接続設定
vagrant ssh-config --host cos6 >> ~/.ssh/config

# Chef Soloインストール
knife solo bootstrap cos6

途中でWARNING: Local cookbook_path '/Users/nob/vms/centos6/cookbooks' does not existみたいな警告が出るけど、適用するcookbookは今のところ未設定なのでムシ!!

Cookbookを作成してrun_listに追加してみる

$ knife cookbook create dstat -o site-cookbooks

$ tree site-cookbooks/dstat

# なんかいっぱい出来た
site-cookbooks/dstat
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│   └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│   └── default.rb
├── resources
└── templates
    └── default

# こんな感じで「dstatをインストールするレシピ」を書く
$ cat site-cookbooks/dstat/recipes/default.rb
package "dstat" do
  action :install
end

# run_list(適用するレシピ集)に作成したdstatを追加する
$ knife node run_list add cos6 dstat -z
cos6:
  run_list: recipe[dstat]

プロビジョニングの実行

# プロビジョニングしてみる
$ knife solo cook cos6
Running Chef on cos6...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
WARNING: Local cookbook_path '/Users/nob/vms/centos6/cookbooks' does not exist
Generating solo config...
Running Chef...
Starting Chef Client, version 12.0.3
Compiling Cookbooks...
Converging 1 resources
Recipe: dstat::default
  * yum_package[dstat] action install
    - install version 0.7.0-1.el6 of package dstat

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 4.7559155 seconds

DEKITA!!
起きない嫁を起こします。

参考

bundle execを使わずに済む方法(rbenv編)