Tbpgr Blog

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

Chef | cookbook | sqlite向けcookbookの作成

概要

sqlite向けcookbookの作成

詳細

sqlite向けcookbookの作成をします。

手順

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

# Vagrantfile生成
vagrant init

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

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

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

# vagrant起動
vagrant up
vagrant provision

# 動作確認
vagrant ssh
vagrant@precise64:~$ sqlite3 --version
3.7.9 2011-11-01 00:52:41 c7c6050ef060877ebe77b41d959e9df13f8c9b5e

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 = "sqlite"
  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 "sqlite"
  end
end

site-cookbooks/sqlite/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/sqlite/recipe/default.rb

packages = %w{libsqlite3-dev sqlite3}
execute_with_log("install packages =  #{packages.inspect}") do
  packages.each do |each_package|
    execute_with_log("#{each_package} install") do
      package each_package do
        action :install
        options "--force-yes"
      end
    end
  end
end