ほわいとぼーど

ぷろぐらまのメモ帳

knife-solo用boxをchefで作ってみた

以前、「Vagrantでknife-solo+Serverspecなboxを作っていた話」でした作業を
Chef使ってるのに手作業はなかろうとChefのレシピに落とし込んでみました。

作ったレシピは以下。

[site-cookbooks/knifesolobox/recipes/default.rb]

execute "install-chef" do
  user "root"
  command "curl -L https://www.opscode.com/chef/install.sh | bash"
end

%w{knife-solo rake serverspec}.each do |gem|
  gem_package "#{gem}" do
    gem_binary("/opt/chef/embedded/bin/gem")
    options("--no-ri --no-rdoc")
  end
end

%w{rake rspec}.each do |bin|
  link "/usr/sbin/#{bin}" do
    to "/opt/chef/embedded/bin/#{bin}"
  end
end

directory "/home/vagrant/.chef" do
  owner "vagrant"
  group "vagrant"
  mode  "0775"
end

cookbook_file "/home/vagrant/.chef/knife.rb" do
  source "knife.rb"
  owner "vagrant"
  group "vagrant"
  mode  "0644"
end

%w{insecure_private_key config}.each do |file|
  cookbook_file "/home/vagrant/.ssh/#{file}" do
    source "#{file}"
    owner "vagrant"
    group "vagrant"
    mode  "0600"
  end
end

link "/home/vagrant/chef-repo" do
  to "/vagrant"
end

[site-cookbooks/knifesolobox/files/default/knife.rb]

log_level                :info
log_location             STDOUT
node_name                'vagrant'
client_key               '/home/vagrant/.chef/vagrant.pem'
validation_client_name   'chef-validator'
validation_key           '/etc/chef-server/chef-validator.pem'
chef_server_url          'https://vagrant-ubuntu-precise-64:443'
syntax_check_cache_path  '/home/vagrant/.chef/syntax_check_cache'
knife[:solo_path] = '/tmp/chef-solo'

余り冪等性とかはこだわってない。まずはレシピ化が重要。
.ssh/configと.ssh/insecure_private_keyは前回記事を参照のこと。

Vagrantfileは以下。

Vagrant.configure("2") do |config|

  config.vm.box = "precise64official"
  
  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "knifesolobox"
    chef.cookbooks_path = "/path/to/chef-repo/site-cookbooks"
  end
end


実行してみます。

C:\vagrant\work\createbox>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise64official'...
[0K[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Fixed port collision for 22 => 2222. Now on port 2200.
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2200 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.1.12
VirtualBox Version: 4.2
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: chef_solo...
The chef binary (either `chef-solo` or `chef-client`) was not found on
the VM and is required for chef provisioning. Please verify that chef
is installed and that the binary is available on the PATH.

おうふ、、、
ChefでProvisioningするのにBoxに事前にchef-solo必要なんですね。
普段、knife-soloしかしてなかったので知らなかった。
そこでRyuzeeさんが紹介されているvagrant-omnibus pluginを使うことにしました。
Ryuzeeさんも書かれてますが、
knife-solo用のBoxを作るのにchef-soloを手で入れるとかなんか違うし、
じゃぁ、シェルでProvisioningして入れてから、、、と思ったけど
cookbookを途中でrsync出来るんだっけ?とか思って断念しました。


pluginインストール。

C:\vagrant\work\createbox>vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Installed the plugin 'vagrant-omnibus (1.1.1)'!


これでChefはインストールされるはずなので、
前のレシピの最初のexecuteは削っておきます。

それから、Vagrantfileに以下を追加。

  config.omnibus.chef_version = :latest


実行してみます。

C:\vagrant\work\createbox>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise64official'...
[0K[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.1.12
VirtualBox Version: 4.2
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Installing Chef 11.6.0 Omnibus package...
[default] Running provisioner: chef_solo...
Generating chef JSON and uploading...
Running chef-solo...
stdin: is not a tty
[2013-09-15T22:09:31+00:00] INFO: Forking chef instance to converge...
[2013-09-15T22:09:31+00:00] INFO: *** Chef 11.6.0 ***
[2013-09-15T22:09:31+00:00] INFO: Setting the run_list to ["recipe[knifesolobox]"] from JSON
[2013-09-15T22:09:31+00:00] INFO: Run List is [recipe[knifesolobox]]
[2013-09-15T22:09:31+00:00] INFO: Run List expands to [knifesolobox]
[2013-09-15T22:09:31+00:00] INFO: Starting Chef Run for vagrant-ubuntu-precise-64
[2013-09-15T22:09:31+00:00] INFO: Running start handlers
[2013-09-15T22:09:31+00:00] INFO: Start handlers complete.
[2013-09-15T22:10:14+00:00] INFO: link[/usr/sbin/rake] created
[2013-09-15T22:10:14+00:00] INFO: link[/usr/sbin/rspec] created
[2013-09-15T22:10:14+00:00] INFO: directory[/home/vagrant/.chef] created directory /home/vagrant/.chef
[2013-09-15T22:10:14+00:00] INFO: directory[/home/vagrant/.chef] owner changed to 1001
[2013-09-15T22:10:14+00:00] INFO: directory[/home/vagrant/.chef] group changed to 1001
[2013-09-15T22:10:14+00:00] INFO: directory[/home/vagrant/.chef] mode changed to 775
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.chef/knife.rb] created file /home/vagrant/.chef/knife.rb
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.chef/knife.rb] updated file contents /home/vagrant/.chef/knife.rb
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.chef/knife.rb] owner changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.chef/knife.rb] group changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.chef/knife.rb] mode changed to 644
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/insecure_private_key] created file /home/vagrant/.ssh/insecure_private_key
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/insecure_private_key] updated file contents /home/vagrant/.ssh/insecure_private_key
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/insecure_private_key] owner changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/insecure_private_key] group changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/insecure_private_key] mode changed to 600
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/config] created file /home/vagrant/.ssh/config
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/config] updated file contents /home/vagrant/.ssh/config
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/config] owner changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/config] group changed to 1001
[2013-09-15T22:10:14+00:00] INFO: cookbook_file[/home/vagrant/.ssh/config] modechanged to 600
[2013-09-15T22:10:14+00:00] INFO: link[/home/vagrant/chef-repo] created
[2013-09-15T22:10:14+00:00] INFO: Chef Run complete in 43.603384421 seconds
[2013-09-15T22:10:14+00:00] INFO: Running report handlers
[2013-09-15T22:10:14+00:00] INFO: Report handlers complete

できました。
あとはvagrant packageすれば、box完成です。

手軽には出来ましたがpluginを使ったのでイマイチな感じがします。
やはりbox作るとなったら次はPackerでしょうか。
Packerは前少し触って失敗して以来放置してるんで
どこかで再挑戦してみたいですね。
あとは、できたBoxはServerspecでチェックしたいところ。