あいつの日誌β

働きながら旅しています。

Vagrant + Chef 2015 with bugs

あらすじ

めっちゃレガシーな開発環境に出会ったので後続の部隊の為に chef が欲しくなったんだけど後続の部隊のために chef-repo を残している偉大なエンジニアがいた!

だけどうまく動かない...

準備

ツールをインストールしておく

vagrant の plugin はこのような感じです

% vagrant plugin list
vagrant-berkshelf (4.0.4)
vagrant-chef-zero (1.0.1)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.3, system)
vagrant-vbguest (0.10.0)
vagrant-vbox-snapshot (0.0.9)

作業用ディレクトリを作成

% mkdir -p ~/vagrant/vm.test.jp && cd $_

chefdk に内包されているコマンドを使うために環境変数を準備します。 .zshrc に追記しておいても良いと思います。

% echo 'eval "$(chef shell-init `basename $SHELL`)"' >> .envrc
% direnv allow

以下の表示がされていれば OK です。

% which ruby
/opt/chefdk/embedded/bin/ruby

knife-solo, knife-zero を使いたい場合は以下のようにしておくとよいでしょう。

% chef gem install knife-solo knife-zero

Vagrantfile を用意

% touch Vagrantfile

edit 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.box = "vm.test.jp"
  config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box"
  config.vm.network "private_network", ip: "192.168.33.21" # 他の vm と IP が重複しないように注意
  config.vm.hostname = "vm.test.jp"

  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
  end
end

おもむろに起動する

% vagrant up

もしかするとこのようなエラーが表示されてるかもしれません。

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

これはゲスト OS にインストールされている Guest Additions のバージョンが ホスト OS の VirtualBox のバージョンより低いらしいです。

% vagrant ssh
$ sudo yum update -y kernel
$ sudo yum -y install kernel-devel kernel-headers dkms gcc gcc-c++
$ exit
% vagrant halt
% vagrant up

解決しなかったらごめん。

ntp で練習

edit 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.box = "vm.test.jp"
  config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box"
  config.vm.network "private_network", ip: "192.168.33.21" # 他の vm と IP が重複しないように注意
  config.vm.hostname = "vm.test.jp"
+  config.omnibus.chef_version = :latest

  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
  end

+  config.vm.provision "chef_zero" do |chef|
+    chef.cookbooks_path = ["chef-repo/cookbooks"]
+    chef.add_recipe "ntp"
+  end
end
% cd chef-repo/
% touch Berksfile

edit Berksfile:

source "https://supermarket.chef.io"
cookbook 'ntp'

local に cookbooks を配備しよう。レッツトライ。

% vagrant provision

とりあえず以下のような表示がされたらバグらしいです。

Shared folders that Chef requires are missing on the virtual machine.
This is usually due to configuration changing after already booting the
machine. The fix is to run a `vagrant reload` so that the proper shared
folders will be prepared and mounted on the VM.

こうしましょう。

% rm .vagrant/machines/default/virtualbox/synced_folders
% vagrant reload --provision

改めて再挑戦

% vagrant provision

確認する

% vagrant ssh -c 'ps aux | grep ntp'
D, [2015-07-15T16:29:55.878694 #30152] DEBUG -- : Celluloid 0.17.0 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
ntp       2842  0.0  0.1  30720  2104 ?        Ss   07:29   0:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
vagrant   2848  1.0  0.0 106060  1480 pts/0    Ss+  07:29   0:00 bash -l -c ps aux | grep ntp
vagrant   2862  0.0  0.0 107460   936 pts/0    S+   07:29   0:00 grep ntp

できた

まとめ

おおまかにいうと 2015年7月に私が vagrant + chef を使おうとしたところ以下のバグに遭遇したので気をつけましょう。

  • Guest Additions の version が違う問題(よくわかっていない)
  • Chef が 共有フォルダで使う cache を使ってはいけない問題(よくわかっていない)

おしまい