Windows7上で Vagrant + Chef solo + knife-soloを使い、Ubuntu + ubuntu-desktopの環境を構築してみた

VirtualBoxを使って検証的な環境を作ったり壊したりしているのですが、いい加減手作業はツライので、最近目にするChef関連の環境を構築してみました。
なお、構築にあたり、naoyaさんの本が非常に役立ちました。ありがとうございました。
入門Chef Solo - Infrastructure as Code - 達人出版会


ちなみに、手元のWindows環境では rsnyc がうまく設定できなかったせいか、knife-soloの「knife solo cook」がうまくできませんでした。githubにもissueが上がっているようです。
ただ、「knife solo init」「knife solo prepare」がとても便利なので、knife-soloも環境構築に加えてあります。

環境

  • ホストOS: Windows7 x64
  • VirtualBox: 4.2.10 (4/8追記)
  • Ruby: 1.9.3p392
  • Git: 1.8.2 (SSHクライアントを兼務)
  • Vagrant: 1.1.5
  • Chef: 11.4.0
  • knife-solo: 0.3.0pre2
  • レシピ: Ubuntu12.04を作り、apt-getでubuntu-desktopを入れる (apt-getを試したかったため、日本語Remixではないです)

環境設定

Ruby

RubyInstallerでインストールします。
RubyInstaller for Windows

Git

Git for Windowsでインストールします。
Git

SSHクライアント

knife-soloで使うため、SSHクライアントを入れておきます。
Gitのものでも問題ないため、ssh.exeにパスを通しておきます。デフォルトインストールの場合、以下を環境変数PATHに設定しておきます。

C:\Program Files (x86)\Git\bin\
Vagrant

msiファイルが配布されているので、そちらでインストールします。
Vagrant

Chef

今回、ドキュメント系は不要なため、以下でインストールします。

gem install chef --no-ri --no-rdoc
knife-solo

「--pre」オプション付きではエラーで落ちたため、以下でバージョン指定してインストールしました。

gem install knife-solo -v 0.3.0.pre2 --no-ri --no-rdoc
knife-soloの設定

knife configure以降は、基本EnterでOKです。

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

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

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

Configuration file written to C:/Users//.chef/knife.rb

ただ、自分の場合は実行時に以下のエラーが出たため、「ffi」gemを追加しました。
エラー

custom_require.rb:36:in `require':cannot load such file -- ffi (LoadError)


追加

gem install ffi --no-ri --no-rdoc

作業の流れ

1. VagrantのBoxを追加

今回は、以下にて公開されているBoxを追加します。
その中のうち、ubuntu.comで配布されている「Official Ubuntu 12.04 daily Cloud Image i386 (No Guest Additions)」を「hoge」という名前で追加します。 (それなりに時間がかかります)
A list of base boxes for Vagrant - Vagrantbox.es


なお、セキュリティの関係などで共有boxを利用できない場合は、veeweeを利用します。
参考:vagrant ユーザよ、その VM は安全なのか? (veewee のすゝめ) - Hack like a rolling stone


実行

vagrant box add hoge http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-i386-disk1.box

Downloading with Vagrant::Downloaders::HTTP...
Downloading box: http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-i386-disk1.box
Downloading box: http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box
Extracting box...5118927 / 345303040)
Cleaning up downloaded box...
Successfully added box 'hoge' with provider 'virtualbox'!
2. 任意の場所にフォルダを作成・移動

今回はD直下に「fuga」というフォルダを作成します。

cd /d d:\
mkdir fuga
cd fuga
3. knife-soloを使って、Chef用のリポジトリを作成

事前に作成しておきます。作成結果は特に表示されません。

d:\fuga>knife solo init chef-repo
4. vagrantのchefをアップデート

Vagrant1.1.5時点では、少し古いChef10.14.2が入っていたため、一度VMを起動してChefのアップデートを行います。なお、Chefのアップデートが不要な場合には省略も可能です。


まずは上記で追加したbox「hoge」をもとに、VMのひな形を作ります。

d:\fuga>vagrant init hoge

A `Vagrantfile` has been placed in this directory. You are nowready 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-soloからSSH接続ができるよう、private networkの設定をします。
D:\fuga\Vagrantfile の23行目あたり

# Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # 以下をアンコメントします
  config.vm.network :private_network, ip: "192.168.33.10"

VMを起動します。

d:\fuga>vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
(略)
[default] -- /vagrant


Chefのアップデートの他、Chefで必要な「192.168.33.10.json」ファイルを自動生成するため、chef-repoの中に入って、knife solo prepareを実行します。

d:\fuga>cd chef-repo
d:\fuga\chef-repo>knife solo prepare vagrant@192.168.33.10

Bootstrapping Chef...


途中でVagrantのパスワードが求められるので、「vagrant」を入力します。

Enter the password for vagrant@192.168.33.10: vagrant


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6471  100  6471    0     0   4156      0  0:00:01  0:00:01 --:--:--  5039
Downloading Chef 11.4.0 for ubuntu...
Installing Chef 11.4.0
Selecting previously unselected package chef.
(Reading database ... 61658 files and directories currently installed.)
Unpacking chef (from .../chef_11.4.0_i386.deb) ...
Setting up chef (11.4.0-1.ubuntu.11.04) ...
Thank you for installing Chef!
Generating node config 'nodes/192.168.33.10.json'...


recipeまわりの設定をするため、一度停止します。

d:\fuga\chef-repo>vagrant halt
5. recipeへの記載

まずはcookbookを作成します。今回はcookbook「piyo」をcookbooksフォルダへ作成します。

d:\fuga\chef-repo>knife cookbook create piyo -o cookbooks

 ** Creating cookbook ubuntu-desktop
 ** Creating README for cookbook: ubuntu-desktop
 ** Creating CHANGELOG for cookbook: ubuntu-desktop
 ** Creating metadata for cookbook: ubuntu-desktop


以下のrecipeファイルを編集します。
D:\fuga\chef-repo\cookbooks\piyo\recipes\default.rb
apt-getの前にパッケージのアップデートを行わないと、エラーでapt-getができないため、最初にその処理を行なっています。
参考:cookbook - Installing multiple packages via Vagrant + Chef - Stack Overflow

# 「update package index」の処理を記載
execute "update package index" do
  command "apt-get update"
  ignore_failure true
  action :nothing
end.run_action(:run)

# update が終わったことをログ出力
log "done update"

# 「ubuntu-desktop」パッケージをインストールする
package "ubuntu-desktop" do
  action :install
end

# ubuntu-desktop のインストールが終わったことをログ出力
log "done install"
6. 各設定ファイルの設定

上記で作成したrecipeを使うよう、各ファイルを設定します。

  • d:\hoge\Vagrantfile

74行目辺りに追加します。

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  config.vm.provision :chef_solo do |chef|
    # cookbookのありか。Vagrantfileからの相対パス
    chef.cookbooks_path = "./chef-repo/cookbooks"

    # 使用するrecipeの名前
    chef.add_recipe "piyo"
  end
  • D:\fuga\chef-repo\nodes\192.168.33.10.json
{"run_list":[" recipe [ piyo ]"]}
7. VMの起動・インストール・終了

再度起動します。起動中にChefが走り、ubuntu-desktopがインストールされます (結構時間がかかります)。

d:\fuga\chef-repo>vagrant up

# 結果
[default] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: VagrantPlugins::Chef::Provisioner::ChefSolo...
Generating chef JSON and uploading...
Running chef-solo...
stdin: is not a tty
[2013-04-06T21:48:13+00:00] INFO: *** Chef 11.4.0 ***
[2013-04-06T21:48:14+00:00] INFO: Setting the run_list to ["recipe[piyo]"] from JSON
[2013-04-06T21:48:14+00:00] INFO: Run List is [recipe[piyo]]
[2013-04-06T21:48:14+00:00] INFO: Run List expands to [piyo]
[2013-04-06T21:48:14+00:00] INFO: Starting Chef Run for vagrant-ubuntu-precise-2
[2013-04-06T21:48:14+00:00] INFO: Running start handlers
[2013-04-06T21:48:14+00:00] INFO: Start handlers complete.
[2013-04-06T21:48:14+00:00] INFO: Processing execute[update package index] actin run (piyo::default line 11)
[2013-04-06T21:48:25+00:00] INFO: execute[update package index] ran successfull
[2013-04-06T21:48:25+00:00] INFO: Processing execute[update package index] actin nothing (piyo::default line 11)
[2013-04-06T21:48:25+00:00] INFO: Processing log[done update] action write (piyo::default line 18)
[2013-04-06T21:48:25+00:00] INFO: done update
[2013-04-06T21:48:25+00:00] INFO: Processing package[ubuntu-desktop] action install (piyo::default line 21)
[2013-04-06T22:50:14+00:00] INFO: Processing log[done install] action write (piyo::default line 26)
[2013-04-06T22:50:14+00:00] INFO: done install
[2013-04-06T22:50:14+00:00] INFO: Chef Run complete in 3720.292520252 seconds
[2013-04-06T22:50:14+00:00] INFO: Running report handlers
[2013-04-06T22:50:14+00:00] INFO: Report handlers complete


インストールが終わったら、インストール成功を確認するために、一度VMを停止します。

d:\fuga\chef-repo>vagrant halt
8. インストールの確認

Vagrantfileを修正し、GUI表示をするように変更します。
Vagrantfile の40行目あたり

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


起動して、GUIが表示されることを確認します。
recipeのapt-getのinstallなどが再実行されてしまいますが、特に問題なく起動します。気になる場合は、Vagrantファイルのrecipe設定を変更します。

d:\fuga\chef-repo>vagrant up


以上で、環境を構築することができました。
手作業に比べればかなり容易なため、これからもいろいろと試してみようと思います。



参考書籍

前述のnaoyaさんの書籍でChefについて理解した後は、SoftwareDesignの2012年10月号が参考になりました。
今でしたら、総集編に収録されているため、そちらのほうがおすすめです。

Software Design 総集編 【2001~2012】

Software Design 総集編 【2001~2012】