写経環境を、vagrant-berkshelf から Vagrant + knife-solo + Berkshelf へと変更した

以前、GDG信州でvagrant-berkshelfを使った写経環境構築の発表をしましたが、その数日後にvagrant-berkshelfが非推奨になってしまいました*1
今のところは使えるものの、将来的なことも考えて、手元にある写経用の環境を Vagrant + knife-solo + Berkshelf へと更新しました。

環境


ちなみに、VMへのCookbook転送については、

のようにいくつか方法があったものの、今回はVagrantに任せることにしました。


なお、WindowsでのCookbook転送については、knife-soloのGitHubのIssueにMilestone(0.5.0)と書かれていました。
Provide better help if rsync isn't available · Issue #167 · matschaffer/knife-solo · GitHub


また、構築したものはGitHubにて公開しておきました。
thinkAmi/syakyo_env-ruby · GitHub



流れ

knife soloとBerkshelfのインストール

knife-soloとBerkshelfはBundlerで入れることにするため、Gemfileを作成します。
d:\hoge\Gemfile

source 'https://rubygems.org'

gem "knife-solo"
gem "berkshelf"

gem "win32-api", "~> 1.5.1", platforms: [:mingw, :x64_mingw]


なお、knife-solo 0.4.1の場合、何も指定しないとwin32-apiが1.4.8で入りますが、そのままではknife-solo実行時に以下のエラーが出ます。そのため、明示的にwin32-apiのバージョンを指定しておきます。

d:\hoge\fuga>bundle exec knife solo init chef-repo
DL is deprecated, please use Fiddle
d:/Develop/env/knife/vendor/bundle/ruby/2.0.0/gems/win32-api-1.4.8-x86-mingw32/lib/win32/api.rb:2:in `require': 126: 指定されたモジュールが見つかりません。   - d:/Develop/env/knife/vendor/bundle/ruby/2.0.0/gems/win32-api-1.4.8-x86-mingw32/lib/win32/ruby19/win32/api.so (LoadError)

また、念のためplatformsの指定もしておきました。platformsに指定するシンボル名は、以下を参考にしました。
Gemfile(5) - A format for describing gem dependencies for Ruby programs



Bundlerでパスを指定してローカルインストールします。

d:\hoge>bundle install --path vendor/bundle
DL is deprecated, please use Fiddle
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
〜略〜
Installing win32-api (1.5.1)
〜略〜
Installing knife-solo (0.4.1)
Using bundler (1.5.3)
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from knife-solo:
Thanks for installing knife-solo!
If you run into any issues please let us know at:
  https://github.com/matschaffer/knife-solo/issues

If you are upgrading knife-solo please uninstall any old versions by running `gem clean knife-solo` to avoid any errors.

See http://bit.ly/CHEF-3255 for more information on the knife bug that causes this.


knifeの設定をします。色々と聞かれますが、今回はすべてEnterで進めます。

d:\hoge>bundle exec knife configure
DL is deprecated, please use Fiddle
WARNING: No knife configuration file found
〜略〜
Configuration file written to C:/Users//.chef/knife.rb
写経環境向けのCookbook作成

knifeを使ってリポジトリを作成します。knifeはBerkshelfと連携ができるようなので*2、同時にBerksfileも作成されます。

d:\hoge>mkdir fuga
d:\hoge>cd fuga
d:\hoge\fuga>bundle exec knife solo init chef-repo
DL is deprecated, please use Fiddle
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
DL is deprecated, please use Fiddle
Setting up Berkshelf...


knifeを使ってCookbookのひな形を作ります。今回は、以下の内容でひな形を作ります。

  • Cookbook名: base
  • Cookbookの保存先: site-cookbooksディレクトリの下
d:\hoge\fuga>pushd chef-repo
d:\hoge\fuga\chef-repo>bundle exec knife cookbook create base -o site-cookbooks
DL is deprecated, please use Fiddle
** Creating cookbook base
** Creating README for cookbook: base
** Creating CHANGELOG for cookbook: base
** Creating metadata for cookbook: base


続いて環境用のレシピを編集します。内容は以下の通りです。

  • Ubuntuのみの設定
  • Gitの最新版をPPAからインストールする*3
  • Node.js、SQLiteBrowserをインストールする
  • Gitのブランチをターミナルに表示する
  • .gemrcを作成する

d:\hoge\fuga\chef-repo\site-cookbooks\base\recipes\default.rb

return unless node['platform'] == "ubuntu"

apt_repository 'git' do
    uri 'http://ppa.launchpad.net/git-core/ppa/ubuntu'
    distribution node[:lsb][:codename]
    components [:main]
    keyserver 'keyserver.ubuntu.com'
    key 'E1DF1F24'
    notifies :run, 'execute[apt-get update]', :immediately
end
package 'git'

package "nodejs"

package "sqlitebrowser"


script "show_git_branch" do
  interpreter "bash"
  user        "root"
  code <<-'EOL'
    wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -P /home/vagrant
    chmod -R 777 /home/vagrant/git-completion.bash
    echo source /home/vagrant/git-completion.bash >> /home/vagrant/.bashrc
    echo GIT_PS1_SHOWDIRTYSTATE=true >> /home/vagrant/.bashrc

    echo -n export PS1=\' >> /home/vagrant/.bashrc
    echo -n '\[\033[01;32m\]\u@\[\033[01;33m\] \w$(__git_ps1) \[\033[01;34m\]\$\[\033[00m\] ' >> /home/vagrant/.bashrc
    echo \' >> /home/vagrant/.bashrc

    source /home/vagrant/.bashrc
  EOL
end


script "create_gemrc" do
  interpreter "bash"
  user        "root"
  code <<-'EOL'
    echo 'install: --no-rdoc --no-ri' >> /home/vagrant/.gemrc
    echo -n 'update:  --no-rdoc --no-ri' >> /home/vagrant/.gemrc
    chmod -R 777 /home/vagrant/.gemrc
  EOL
end


上記の設定の他、サードパーティのCookbookを利用するため、knife solo init で作成したBerksfileを編集します。
d:\hoge\fuga\chef-repo\Berksfile

site :opscode

cookbook 'apt'
cookbook 'ruby_build'
cookbook 'rbenv', github: "fnichol/chef-rbenv"
cookbook 'sublime-text', github: 'ephess/sublime-text'


vagrant-berkshelfプラグインが無いため、事前にBerkshelfを実行してサードパーティのCookbookを取得しておきます。

d:\hoge\fuga\chef-repo>bundle exec berks install --path cookbooks
DL is deprecated, please use Fiddle
DL is deprecated, please use Fiddle
Installing apt (2.3.8) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing ruby_build (0.8.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing rbenv (0.7.3) from github: 'fnichol/chef-rbenv' with branch: 'master' over protocol: 'git'
Installing sublime-text (0.1.0) from github: 'ephess/sublime-text' with branch:'master' over protocol: 'git'
Installing java (1.19.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Vagrant設定

Vagrantfileを作成します。

d:\hoge\fuga\chef-repo>popd
d:\hoge\fuga>vagrant init
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.


作成したVagrantfileを編集します。chef.cookbooks_pathで、Cookbookがある相対パスを指定します。
d:\hoge\fuga\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.hostname = "foo"
  
  config.vm.box = "ubuntu120403ja"
  config.vm.network :private_network, ip: "192.168.33.10"

  config.vm.provider :virtualbox do |vb|
    vb.gui = true
  end

  config.omnibus.chef_version = :latest


  config.vm.provision :chef_solo do |chef|
    chef.json = {
      rbenv: {
        user_installs: [{
          user: "vagrant",
          rubies: ["2.0.0-p451"],
          global: "2.0.0-p451",
          gems: {
            "2.0.0-p451" => [
              { name: "bundler"
              }
            ]
          }
        }]
      }
    }

    chef.cookbooks_path = ["./chef-repo/cookbooks", "./chef-repo/site-cookbooks"]


    chef.run_list = [
      "apt",
      "base",
      "ruby_build",
      "rbenv::user",
      "sublime-text"
    ]
  end
end
起動と確認

vagrant upで環境を構築します。

d:\hoge\fuga>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
〜略〜
[2014-02-26T08:27:53+00:00] INFO: execute[apt-get update] sending run action to
execute[apt-cache gencaches] (immediate)
[2014-02-26T08:27:53+00:00] INFO: execute[apt-cache gencaches] ran successfully
[2014-02-26T08:28:09+00:00] INFO: Chef Run complete in 841.560946263 seconds
[2014-02-26T08:28:09+00:00] INFO: Running report handlers
[2014-02-26T08:28:09+00:00] INFO: Report handlers complete


ターミナルで環境が構築されているかを確認します。

vagrant@ ~ $ rbenv versions list
  system
* 2.0.0-p451 (set by /home/vagrant/.rbenv/version)
vagrant@ ~ $ git --version
git version 1.9.0
vagrant@ ~ $ subl --version
Sublime Text 2 Build 2221


以上で完成です。