Vagrantとchef-soloでお気楽環境構築 このエントリをはてなブックマークに登録

2013年03月19日

ishizimaishizima

Engine Yardさんの勉強会に行ってきた

皆さんこんにちは。
2/22に開催されたChefの勉強会に行ってきました。

事務所の引っ越しなどでバタバタしていたので時間が経ってしまいましたが、参加して分かったことを元に、本エントリーではvagrantの仮想環境をchef-soloで設定できるようにしてみたいと思います。
というかはてブのトップがこのネタばかりで、なんというか旬を逃した感が拭えないんですけど…、よろしくおつきあいください。

どうしてchefとvagrant?

以前からちょいちょい動作確認環境のをvagrantで用意していました。で、毎回環境作るのがめんどいぞと。頻度がそんなになかったんで、ある意味我慢してました。一方で、社内の環境を chef-solo 使って環境構築することはあったんですけど、管理する対象はvagrantで構築したのとは別の環境だったり。。。ちょっとちぐはぐです。
ちょうどこの勉強会に参加したら、やっぱりVagrantの話が結構出ていました。AWSもガッツリ使ってるのでOpsWorksも試したかったんですけど、まずは目先のVagrant周りを楽にしたいってことで備忘録代わりにこのエントリーを書きました。

chefのインストール

まず、何はなくともchefを用意するところから始めます。勉強会ではローカルに環境を作るよりVM内に用意する方が無難ということでした。
自分はローカルでchef-soloのレシピを用意してvagrantの構成を管理したいのと、ローカルにrubyの環境ができあがってるので、手元のMacで環境を用意します。
MacはOS X 10.8.2、rubyはrvmで1.8.7を使うので事前に rvm use 1.8.7 しておきましょう。

$ rvm 1.8.7 do gem install chef --no-rdoc --no-ri
Successfully installed chef-11.4.0
1 gem installed

これで chef のインストールが完了しました。今回使う chef-solo はchefに同梱されています。
入れておいてなんですが、今回Macでは chef-solo を使いません。レシピの管理を行う knife だけです。まぁ一応 chef-solo の起動程度は確認しておきます。

$ chef-solo -v
Chef: 11.4.0

knife の初期設定

knifeはchefの設定情報(cookbooks)の管理ツールです。

$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/Users/user/.chef/knife.rb]
Please enter the chef server URL: [http://user.local:4000]
Please enter an existing username or clientname for the API: [user]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem]
Please enter the path to a chef repository (or leave blank):
*****

You must place your client key in:
  /Users/user/.chef/user.pem
Before running commands with Knife!

*****

You must place your validation key in:
  /etc/chef/validation.pem
Before generating instance data with Knife!

*****
Configuration file written to /Users/ishizima/.chef/knife.rb

chef-solo の構成データ(cookbooks)はデフォルトで /var/chef 以下に作成されるようなので、作成します。また、chef-soloの設定ファイルが /etc/chef/solo.rb として必要になります。

file_cache_path '/var/chef'
cookbook_path ['/var/chef/cookbooks']

という内容で用意します。

その後、下記のコマンドでcookbookが作成できます。ここでは sample という名前にします。

$ knife cookbook create sample
** Creating cookbook sample
** Creating README for cookbook: sample
** Creating CHANGELOG for cookbook: sample
** Creating metadata for cookbook: sample

VirtualBox と Vagrant を用意する

構成管理ツールである chef に対して、vagrantはVirtualBoxのVMを操作するCUIのフロントエンドです。

VirtualBox

まずは管理される対象のVirtualBoxが必要になります。以下のURLからよしなに。
https://www.virtualbox.org/wiki/Downloads

Vagrant

gemでインストールします。

$ rvm 1.8.7 do gem install vagrant --no-rdoc --no-ri
Successfully installed vagrant-1.0.6
1 gem installed

一撃ですね。
※ 書いてる間にver 1.1が出ちゃいました。
http://docs.vagrantup.com/v2/installation/index.html
ここに書いてある通り、gemでのインストールは廃止です。各OS毎にパッケージが用意されてるので、(1.0系がインストールされている場合はgemを削除してから)それらをインストールします。

仮想マシンの準備

boxのダウンロード

VirtualBoxとVagrantが入ったら、起動する仮想マシン環境(box)を用意しなければなりません。様々なものが公開されているので、その中のものを使うのが手っ取り早いでしょう。。
http://www.vagrantbox.es/
↑に色々あります。ここではCentOS6.3を例に進めます。

$ vagrant box add centos6.3 https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

boxの初期化

$ vagrant init centos6.3
vagrant init centos6.3
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.

仮想マシンの起動

$ vagrant up
[default] Importing base box 'centos6.3'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and mええore to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.1.18
VirtualBox Version: 4.2.6
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Booting VM...
/////////////////3[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant

無事実行できました。VMが動いているかどうかは以下のように確認できます。

$ vagrant status
Current VM states:

default                  running

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

vagrantとchef-soloの連動

vagrantで起動したVMから chef-solo のcookbookが見えないとどうにもなりません。
ホストOS側の ~ がVMでは /vagrant としてマウントされるので、cookbooksディレクトリを /var/chef 以下から ~ 以下へ移動すればよさそうです。

VM側のchef-soloの設定を合わせる

/etc/chef/solo.rbの内容を↓こんな感じに。

file_cache_path '/var/chef'
cookbook_path ['/vagrant/cookbooks']

レシピの用意

先ほど作成した sample に内容を記述します。cookbookの recipes/default.rb を下記のように編集してみます。
勉強会でディストリビューションの差異は吸収してくるっぽい話でしたので、debian系の名前をそのまま書いちゃいました(後述しますが、これが失敗の元)。。

#
# Cookbook Name:: sample
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

log 'Hello Chef'
package 'apache2' do
  action :install
end

service 'apache2' do
  action :start
end

レシピの実行

vagrant ssh したら、以下のように実行します。

$ sudo chef-solo -o sample
[2013-03-07T03:10:54+01:00] INFO: *** Chef 10.12.0 ***
[2013-03-07T03:10:54+01:00] WARN: Run List override has been provided.
[2013-03-07T03:10:54+01:00] WARN: Original Run List: []
[2013-03-07T03:10:54+01:00] WARN: Overridden Run List: [recipe[sample]]
[2013-03-07T03:10:54+01:00] INFO: Run List is [recipe[sample]]
[2013-03-07T03:10:54+01:00] INFO: Run List expands to [sample]
[2013-03-07T03:10:54+01:00] INFO: Starting Chef Run for localhost
[2013-03-07T03:10:54+01:00] INFO: Running start handlers
[2013-03-07T03:10:54+01:00] INFO: Start handlers complete.
[2013-03-07T03:10:54+01:00] INFO: Processing log[Hello Chef] action write (sample::default line 10)
[2013-03-07T03:10:54+01:00] INFO: Hello Chef
[2013-03-07T03:10:54+01:00] INFO: Processing package[apache2] action install (sample::default line 11)
[2013-03-07T03:11:27+01:00] ERROR: package[apache2] (sample::default line 11) has had an error
[2013-03-07T03:11:27+01:00] ERROR: package[apache2] (/vagrant/cookbooks/sample/recipes/default.rb:11:in `from_file') had an error:
package[apache2] (sample::default line 11) had an error: Chef::Exceptions::Package: No version specified, and no candidate version available for apache2
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/provider/package.rb:50:in `action_install'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource.rb:454:in `send'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource.rb:454:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:49:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `each'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection.rb:94:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection.rb:92:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:80:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/client.rb:330:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/client.rb:163:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:207:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:195:in `loop'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:195:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application.rb:70:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/chef-solo:25
/usr/bin/chef-solo:19:in `load'
/usr/bin/chef-solo:19
[2013-03-07T03:11:27+01:00] ERROR: Running exception handlers
[2013-03-07T03:11:27+01:00] ERROR: Exception handlers complete
[2013-03-07T03:11:27+01:00] FATAL: Stacktrace dumped to /var/chef/chef-stacktrace.out
[2013-03-07T03:11:27+01:00] FATAL: Chef::Exceptions::Package: package[apache2] (sample::default line 11) had an error: Chef::Exceptions::Package: No version specified, and no candidate version available for apache2

……、コケましたね。
apache2 が見つからないと言っているので、recipes/default.rb のapache2をcentosのpackage名である httpd に書き替えてみます。

$ sudo chef-solo -o sample
[2013-03-07T03:20:03+01:00] INFO: *** Chef 10.12.0 ***
[2013-03-07T03:20:04+01:00] WARN: Run List override has been provided.
[2013-03-07T03:20:04+01:00] WARN: Original Run List: []
[2013-03-07T03:20:04+01:00] WARN: Overridden Run List: [recipe[sample]]
[2013-03-07T03:20:04+01:00] INFO: Run List is [recipe[sample]]
[2013-03-07T03:20:04+01:00] INFO: Run List expands to [sample]
[2013-03-07T03:20:04+01:00] INFO: Starting Chef Run for localhost
[2013-03-07T03:20:04+01:00] INFO: Running start handlers
[2013-03-07T03:20:04+01:00] INFO: Start handlers complete.
[2013-03-07T03:20:04+01:00] INFO: Processing log[Hello Chef] action write (sample::default line 10)
[2013-03-07T03:20:04+01:00] INFO: Hello Chef
[2013-03-07T03:20:04+01:00] INFO: Processing package[httpd] action install (sample::default line 11)
[2013-03-07T03:20:05+01:00] INFO: package[httpd] installing httpd-2.2.15-15.el6.centos.1 from base repository
[2013-03-07T03:20:15+01:00] INFO: package[httpd] installed version 2.2.15-15.el6.centos.1
[2013-03-07T03:20:15+01:00] INFO: Processing service[httpd] action start (sample::default line 15)
[2013-03-07T03:20:15+01:00] INFO: service[httpd] started
[2013-03-07T03:20:15+01:00] INFO: Chef Run complete in 11.062676 seconds
[2013-03-07T03:20:15+01:00] INFO: Running report handlers
[2013-03-07T03:20:15+01:00] INFO: Report handlers complete

成功しました。やっぱり。ディストリビューション毎に異なるパッケージ管理システムの違いは吸収するけど、パッケージ名までは知らないよ、自分で分岐するなりしろってことですね。

結果を確認してみましょう。

$ sudo /etc/init.d/httpd status
httpd (pid  1885) を実行中...

動いてます。やった!

cookbookを自動適用する

いちいちログインして chef-solo を実行するのは面倒なので、自動実行できるようにしてみます。
まずさっき作ったVMを破棄します。好きに作業をやり直せるのがvagrantのいいところ。

$ vagrant halt
[default] Attempting graceful shutdown of VM...
$ vagrant destroy
Are you sure you want to destroy the 'default' VM? [Y/N] Y
[default] Destroying VM and associated drives...

Vagrantfileに設定を追加

Vagrantの設定ファイル ~/Vagrantfile に以下の行を追加します。
provisionのとこで実行するrecipeを指定しています。
host_nameとnetwork設定は直接今回の件とは関係なくて、単に同じLAN内の他のhostから参照したかっただけです。

  config.vm.host_name = "chef-solo-sample"
  config.vm.network :bridged
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe "sample"
    chef.json = {:nameservers => ["10.0.0.1"]}
  end

upと同時にVM側でchef-soloを実行

$ vagrant up
[default] Importing base box 'centos6.3'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.1.18
VirtualBox Version: 4.2.6
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Available bridged network interfaces:
1) en1: Wi-Fi (AirPort)
2) en0: Ethernet
3) p2p0
What interface should the network bridge to? What interface should the network bridge to? What interface should the network bridge to? What interface should the network bridge to? What interface should the network bridge to? 1
[default] Preparing network interfaces based on configuration...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Setting host name...
[default] Mounting shared folders...
[default] -- v-root: /vagrant
[default] -- v-csc-1: /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[2013-03-14T10:52:30+01:00] INFO: *** Chef 10.12.0 ***
[2013-03-14T10:52:30+01:00] INFO: Setting the run_list to ["recipe[sample]"] from JSON
[2013-03-14T10:52:30+01:00] INFO: Run List is [recipe[sample]]
[2013-03-14T10:52:30+01:00] INFO: Run List expands to [sample]
[2013-03-14T10:52:30+01:00] INFO: Starting Chef Run for chef-solo-sample
[2013-03-14T10:52:30+01:00] INFO: Running start handlers
[2013-03-14T10:52:30+01:00] INFO: Start handlers complete.
[2013-03-14T10:52:30+01:00] INFO: Processing log[Hello Chef] action write (sample::default line 10)
[2013-03-14T10:52:30+01:00] INFO: Hello Chef
[2013-03-14T10:52:30+01:00] INFO: Processing package[httpd] action install (sample::default line 11)
[2013-03-14T10:52:58+01:00] INFO: package[httpd] installing httpd-2.2.15-26.el6.centos from base repository
[2013-03-14T10:53:32+01:00] INFO: package[httpd] installed version 2.2.15-26.el6.centos
[2013-03-14T10:53:32+01:00] INFO: Processing service[httpd] action start (sample::default line 15)
[2013-03-14T10:53:33+01:00] INFO: service[httpd] started
[2013-03-14T10:53:33+01:00] INFO: Chef Run complete in 62.226153 seconds
[2013-03-14T10:53:33+01:00] INFO: Running report handlers
[2013-03-14T10:53:33+01:00] INFO: Report handlers complete

うまくいきました。便利!

コミュニティのcookbookを使う

自分で作った何かより誰かが作った何かの方が大抵の場合よくできているものです。ということで、世に公開されて実績のあるcookbookを使ってみます。

まずcookbookの在処ですけど、オフィシャルサイトにたくさんあります。
http://community.opscode.com/cookbooks/
色々探せば他にもあるんですけど、勉強会ではchefとかknifeとかrecipeとか一般名詞だらけでググり難いねって話になってました。そうかも。
で、ここではmysqlのcookbookを試してみることにします。理由は一番上にあったから。
まずは https://github.com/opscode-cookbooks/mysql からcloneしたものをcookbooksの中に置きます。公式については knife site でダウンロードした方が依存関係を見てくれたりと、問題が少ないそうです。というか、既に openssl をダウンロードしないとならなかったのでサラっと追加してます。世間の言うことは従った方がいいです。

で、~/Vagrantfile を以下のように書き替えて、mysqlの設定を追加します。ここではrootユーザのPWを。
debianじゃないしレプリケーションするつもりもないんですけど、server_debian_password と server_debian_password はセットで必須のようなので指定しました。

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe "sample"
    chef.add_recipe "openssl"
    chef.add_recipe "mysql::server"
    chef.json = {
      :nameservers => ["10.0.0.1", "192.168.91.1"],
      :mysql => {
        :server_root_password => "p@ssw0rd",
        :server_repl_password => "p@ssw0rd",
        :server_debian_password => "p@ssw0rd"
      }
    }
  end

で、一回destroyしたboxをもっかいup。

[default] Importing base box 'centos6.3'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.1.18
VirtualBox Version: 4.2.6
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Available bridged network interfaces:
1) en1: Wi-Fi (AirPort)
2) en0: Ethernet
3) p2p0
What interface should the network bridge to? 1
[default] Preparing network interfaces based on configuration...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Setting host name...
[default] Mounting shared folders...
[default] -- v-root: /vagrant
[default] -- v-csc-1: /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[2013-03-14T12:35:17+01:00] INFO: *** Chef 10.12.0 ***
[2013-03-14T12:35:17+01:00] INFO: Setting the run_list to ["recipe[sample]", "recipe[openssl]", "recipe[mysql::server]"] from JSON
[2013-03-14T12:35:17+01:00] INFO: Run List is [recipe[sample], recipe[openssl], recipe[mysql::server]]
[2013-03-14T12:35:17+01:00] INFO: Run List expands to [sample, openssl, mysql::server]
[2013-03-14T12:35:17+01:00] INFO: Starting Chef Run for chef-solo-sample
[2013-03-14T12:35:17+01:00] INFO: Running start handlers
[2013-03-14T12:35:17+01:00] INFO: Start handlers complete.
[2013-03-14T12:35:17+01:00] INFO: Could not find previously defined grants.sql resource
[2013-03-14T12:35:17+01:00] INFO: Processing log[Hello Chef] action write (sample::default line 10)
[2013-03-14T12:35:17+01:00] INFO: Hello Chef
[2013-03-14T12:35:17+01:00] INFO: Processing package[httpd] action install (sample::default line 11)
[2013-03-14T12:35:40+01:00] INFO: package[httpd] installing httpd-2.2.15-26.el6.centos from base repository
[2013-03-14T12:35:46+01:00] INFO: package[httpd] installed version 2.2.15-26.el6.centos
[2013-03-14T12:35:46+01:00] INFO: Processing service[httpd] action start (sample::default line 15)
[2013-03-14T12:35:46+01:00] INFO: service[httpd] started
[2013-03-14T12:35:46+01:00] INFO: Processing package[mysql] action install (mysql::client line 46)
[2013-03-14T12:35:46+01:00] INFO: package[mysql] installing mysql-5.1.67-1.el6_3 from updates repository
[2013-03-14T12:35:54+01:00] INFO: package[mysql] installed version 5.1.67-1.el6_3
[2013-03-14T12:35:54+01:00] INFO: Processing package[mysql-devel] action install (mysql::client line 46)
[2013-03-14T12:35:54+01:00] INFO: package[mysql-devel] installing mysql-devel-5.1.67-1.el6_3 from updates repository
[2013-03-14T12:35:55+01:00] INFO: package[mysql-devel] installed version 5.1.67-1.el6_3
[2013-03-14T12:35:55+01:00] INFO: Processing package[mysql-server] action install (mysql::server line 94)
[2013-03-14T12:35:56+01:00] INFO: package[mysql-server] installing mysql-server-5.1.67-1.el6_3 from updates repository
[2013-03-14T12:36:18+01:00] INFO: package[mysql-server] installed version 5.1.67-1.el6_3
[2013-03-14T12:36:18+01:00] INFO: package[mysql-server] sending start action to service[mysql] (immediate)
[2013-03-14T12:36:18+01:00] INFO: Processing service[mysql] action start (mysql::server line 218)
[2013-03-14T12:36:20+01:00] INFO: service[mysql] started
[2013-03-14T12:36:20+01:00] INFO: Processing directory[/var/run/mysqld] action create (mysql::server line 108)
[2013-03-14T12:36:20+01:00] ERROR: directory[/var/run/mysqld] (mysql::server line 108) has had an error
[2013-03-14T12:36:20+01:00] ERROR: directory[/var/run/mysqld] (/tmp/vagrant-chef-1/chef-solo-1/cookbooks/mysql/recipes/server.rb:108:in `from_file') had an error:
directory[/var/run/mysqld] (mysql::server line 108) had an error: Chef::Exceptions::GroupIDNotFound: cannot determine group id for 'mysql', does the group exist on this system?
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/file_access_control/unix.rb:79:in `target_gid'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/file_access_control/unix.rb:83:in `set_group'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/file_access_control/unix.rb:31:in `set_all'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/mixin/enforce_ownership_and_permissions.rb:33:in `enforce_ownership_and_permissions'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/provider/directory.rb:51:in `action_create'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource.rb:454:in `send'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource.rb:454:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:49:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `each'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:85:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection.rb:94:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/resource_collection.rb:92:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/runner.rb:80:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/client.rb:330:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/client.rb:163:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:207:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:195:in `loop'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application/solo.rb:195:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/../lib/chef/application.rb:70:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-10.12.0/bin/chef-solo:25
/usr/bin/chef-solo:19:in `load'
/usr/bin/chef-solo:19
[2013-03-14T12:36:20+01:00] ERROR: Running exception handlers
[2013-03-14T12:36:20+01:00] ERROR: Exception handlers complete
[2013-03-14T12:36:20+01:00] FATAL: Stacktrace dumped to /tmp/vagrant-chef-1/chef-stacktrace.out
[2013-03-14T12:36:20+01:00] FATAL: Chef::Exceptions::GroupIDNotFound: directory[/var/run/mysqld] (mysql::server line 108) had an error: Chef::Exceptions::GroupIDNotFound: cannot determine group id for 'mysql', does the group exist on this system?
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

こけました。調べたら、使っているboxに含まれる chef-solo のバージョンの問題らしく……。
http://tickets.opscode.com/browse/COOK-1854
もう一回実行するとうまくいくそうなので、もう一回やります。

$ vagrant provision
[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[2013-03-14T12:53:22+01:00] INFO: *** Chef 10.12.0 ***
[2013-03-14T12:53:23+01:00] INFO: Setting the run_list to ["recipe[sample]", "recipe[openssl]", "recipe[mysql::server]"] from JSON
[2013-03-14T12:53:23+01:00] INFO: Run List is [recipe[sample], recipe[openssl], recipe[mysql::server]]
[2013-03-14T12:53:23+01:00] INFO: Run List expands to [sample, openssl, mysql::server]
[2013-03-14T12:53:23+01:00] INFO: Starting Chef Run for chef-solo-sample
[2013-03-14T12:53:23+01:00] INFO: Running start handlers
[2013-03-14T12:53:23+01:00] INFO: Start handlers complete.
[2013-03-14T12:53:23+01:00] INFO: Could not find previously defined grants.sql resource
[2013-03-14T12:53:23+01:00] INFO: Processing log[Hello Chef] action write (sample::default line 10)
[2013-03-14T12:53:23+01:00] INFO: Hello Chef
[2013-03-14T12:53:23+01:00] INFO: Processing package[httpd] action install (sample::default line 11)
[2013-03-14T12:53:24+01:00] INFO: Processing service[httpd] action start (sample::default line 15)
[2013-03-14T12:53:25+01:00] INFO: Processing package[mysql] action install (mysql::client line 46)
[2013-03-14T12:53:25+01:00] INFO: Processing package[mysql-devel] action install (mysql::client line 46)
[2013-03-14T12:53:25+01:00] INFO: Processing package[mysql-server] action install (mysql::server line 94)
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/var/run/mysqld] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/var/log/mysql] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: directory[/var/log/mysql] created directory /var/log/mysql
[2013-03-14T12:53:25+01:00] INFO: directory[/var/log/mysql] owner changed to 27
[2013-03-14T12:53:25+01:00] INFO: directory[/var/log/mysql] group changed to 27
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/etc/mysql/conf.d] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: directory[/etc/mysql/conf.d] created directory /etc/mysql/conf.d
[2013-03-14T12:53:25+01:00] INFO: directory[/etc/mysql/conf.d] owner changed to 27
[2013-03-14T12:53:25+01:00] INFO: directory[/etc/mysql/conf.d] group changed to 27
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/etc/mysql/conf.d] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/var/lib/mysql] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: Processing directory[/var/lib/mysql] action create (mysql::server line 108)
[2013-03-14T12:53:25+01:00] INFO: Processing execute[mysql-install-db] action run (mysql::server line 148)
[2013-03-14T12:53:25+01:00] INFO: Processing service[mysql] action enable (mysql::server line 154)
[2013-03-14T12:53:25+01:00] INFO: service[mysql] enabled
[2013-03-14T12:53:25+01:00] INFO: Processing execute[assign-root-password] action run (mysql::server line 166)
[2013-03-14T12:53:25+01:00] INFO: execute[assign-root-password] ran successfully
[2013-03-14T12:53:25+01:00] INFO: Processing template[/etc/mysql_grants.sql] action create (mysql::server line 179)
[2013-03-14T12:53:25+01:00] INFO: template[/etc/mysql_grants.sql] updated content
[2013-03-14T12:53:25+01:00] INFO: template[/etc/mysql_grants.sql] sending run action to execute[mysql-install-privileges] (immediate)
[2013-03-14T12:53:25+01:00] INFO: Processing execute[mysql-install-privileges] action run (mysql::server line 195)
[2013-03-14T12:53:25+01:00] INFO: execute[mysql-install-privileges] ran successfully
[2013-03-14T12:53:25+01:00] INFO: Processing execute[mysql-install-privileges] action nothing (mysql::server line 195)
[2013-03-14T12:53:25+01:00] INFO: Processing template[/etc/my.cnf] action create (mysql::server line 202)
[2013-03-14T12:53:25+01:00] INFO: template[/etc/my.cnf] backed up to /var/chef/backup/etc/my.cnf.chef-20130314125325
[2013-03-14T12:53:25+01:00] INFO: template[/etc/my.cnf] mode changed to 644
[2013-03-14T12:53:25+01:00] INFO: template[/etc/my.cnf] updated content
[2013-03-14T12:53:25+01:00] INFO: template[/etc/my.cnf] sending restart action to service[mysql] (immediate)
[2013-03-14T12:53:25+01:00] INFO: Processing service[mysql] action restart (mysql::server line 218)
[2013-03-14T12:53:32+01:00] INFO: service[mysql] restarted
[2013-03-14T12:53:32+01:00] INFO: Processing service[mysql] action start (mysql::server line 218)
[2013-03-14T12:53:32+01:00] INFO: Chef Run complete in 9.352482 seconds
[2013-03-14T12:53:32+01:00] INFO: Running report handlers
[2013-03-14T12:53:32+01:00] INFO: Report handlers complete

今度はOKです。chef-solo のバージョンが 10.16 だと一発で成功するそうなので、新しいバージョンのchefが含まれるboxがあれば、そちらを使う方がいいでしょう。
ともかく、動作を確認してみます。

$ vagrant ssh
Last login: Tue Jul 10 22:56:01 2012 from 10.0.2.2
[vagrant@chef-solo-sample ~]$ mysql -u root -p
Enter password: {Vagrantfileに書いたパスワード}
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.67-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

設定した通りに動いています。これで開発環境の自動構築が最低限ながら試せました。
実際に開発で使うにはまだまだやること山積ですけど、VagranとChef-soloによる環境の構成制御の最初に一歩が踏み出せたのではないでしょうか。

関連記事

クレイについてもっと知りたい方は…

  1. クレイの3つの強みを見てみる。
  2. WEBシステムのことなら何でもご相談ください。

「いいね!」で応援よろしくお願いします!

このエントリーに対するコメント

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


トラックバック
  1. [VM] Vagrant1.1の機能が楽しそうです | Roguer2013/03/22, 6:00 AM

    […] Vagrantとchef-soloでお気楽環境構築 | KRAY Inc […]

  2. vagrantとchef-soloを体験してみるpilog | pilog2013/04/15, 4:52 PM

    […] Vagrantとchef-soloでお気楽環境構築 | KRAY Inc […]

we use!!Ruby on RailsAmazon Web Services

このページの先頭へ