Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

Chef | cookbook | vagrant向けcookbookの作成

概要

vagrant向けcookbookの作成

詳細

vagrant向けcookbookの作成をします。
VagrantからChefを使ってUbuntuVagrantをインストールします。

手順

# kitchenの作成
mkdir vagrant
cd vagrant
knife solo init .

# Vagrantfile生成
vagrant init

# Vagrantfile編集 ※詳細は後述
vi Vagrantfile

# cookbookのテンプレート生成
knife cookbook create vagrant -o site-cookbooks/

# レシピの編集 ※詳細は後述

# vagrant起動
vagrant up
vagrant provision

# 動作確認
vagrant ssh

# vagrantのインストールバージョン確認
vagrant@precise64:~$ vagrant -v
Vagrant 1.3.5

Vagrantfile

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provider :virtualbox do |vb|
    vb.name = "vagrant"
    vb.customize ["modifyvm", :id, "--memory", 512]
  end

  config.vm.box = "ubuntu-12.04-x64"
  config.omnibus.chef_version = "11.6.0"
  config.vm.network :private_network, ip: "192.168.33.22"
  
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.add_recipe "vagrant"
  end
end

site-cookbooks/vagrant/libraries/helper.rb

class Chef
  class Recipe
    def execute_with_log(name, &block)
      log "start #{name}"
      block.call
      log "end   #{name}"
    end
  end
end

site-cookbooks/vagrant/recipe/default.rb

execute_with_log("download vagrant1.3.5 deb") do
  bash "install vagrant" do
    user "root"
    code <<-EOS
    cd /tmp
    wget http://files.vagrantup.com/packages/a40522f5fabccb9ddabad03d836e120ff5d14093/vagrant_1.3.5_x86_64.deb
    EOS
  end
end

execute_with_log("install vagrant1.3.5") do
  dpkg_package "vagrant_1.3.5_x86_64.deb" do
    source "/tmp/vagrant_1.3.5_x86_64.deb"
    action :install
  end
end