わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

vagrantのboxをvagrant cloudからもらってくる

Vagrant CloudのVagrant Shareを試してみたら凄すぎて鼻血出た - 憂鬱な世界にネコパンチ!

を見つけたので、vagrant cloudを使ってみたくなった。

そこで以下のような手順を取った。(ほとんどのことは上のリンクに書かれてるので、そっちを参考に)

  1. VirtualBoxをアップデート(省略)
  2. vagrantをアップデート(省略)
  3. vagrant cloudにあるboxを追加

という訳で3番の説明だけする。

vagrant cloudにログイン

一応今回のvagrantのバージョンは

$ vagrant -v
Vagrant 1.5.1

vagrantを1.5.1にアップデートすると自動的にvagrant-loginvagrant-shareプラグインがインストールされる。(saharavagrant-vbguestは自分でインストールした分なので関係ない)

$ vagrant plugin list
sahara (0.0.16)
vagrant-login (1.0.1, system)
vagrant-share (1.0.1, system)
vagrant-vbguest (0.10.0)

早速vagrant loginを試してみる。Discover Vagrant Boxes | Atlas by HashiCorpでの登録はあらかじめすませておくこと。

$ vagrant login
In a moment we'll ask for your username and password to Vagrant Cloud.
After authenticating, we will store an access token locally. Your
login details will be transmitted over a secure connection, and are
never stored on disk locally.

If you don't have a Vagrant Cloud account, sign up at vagrantcloud.com

Username or Email: kimikimi714
Password (will be hidden):
You're now logged in!

よっしゃー。ログインできたー。

vagrant cloudを使ってboxを追加

Discover Vagrant Boxes | Atlas by HashiCorpにはvagrant cloudでどんなことが出来るのかがざっくり書かれてる。
これまでboxの追加はgithubとかにあがってたりA list of base boxes for Vagrant - Vagrantbox.esのあがってたりするboxをよしなに引っ張ってくる感じでやってたけど、それをもう少し共有しやすくしたイメージ。
実際にboxを追加してみる。

$ vagrant box add chef/centos-6.5
==> box: Loading metadata for box 'chef/centos-6.5'
    box: URL: https://vagrantcloud.com/chef/centos-6.5
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) virtualbox
2) vmware_desktop

Enter your choice: 1
==> box: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/chef/centos-6.5/version/1/provider/virtualbox.box
==> box: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'!

よっしゃ、追加できた。

気をつけないといけないのはvagrant 1.5以前のコマンドなら

vagrant box add <box-name> <url>

だったのが、vagrant 1.5以上から

vagrant box add <box-name>

に変わった。
以前のコマンドが使えるつもりでいたら

$ vagrant box add centos-6.5 chef/centos-6.5
==> box: Loading metadata for box 'chef/centos-6.5'
    box: URL: https://vagrantcloud.com/chef/centos-6.5
The box you're adding has a name different from the name you
requested. For boxes with metadata, you cannot override the name.
If you're adding a box using `vagrant box add`, don't specify
the `--name` parameter. If the box is being added via a Vagrantfile,
change the `config.vm.box` value to match the name below.

Requested name: centos-6.5
Actual name: chef/centos-6.5

とエラーをはくようになった。
もしurlを指定してboxを追加したい(ダウンロードしたい)のなら、例えば以下のようにしてやる必要がある(以下はScientific linuxのboxをダウンロードしようとしている様子)。 

$ vagrant box add --name sl6-64 http://lyte.id.au/vagrant/sl6-64-lyte.box

A list of base boxes for Vagrant - Vagrantbox.esが使えなくなった訳ではないけど、追加の仕方が変わったことには注意が必要だ。

vagrant cloudから落としてきたboxを立ち上げてみよう。
Vagrantfileにはconfig.vm.box = "chef/centos-6.5"と書いておく。そしておもむろに以下のコマンドをたたく。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'chef/centos-6.5' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
==> default: Machine booted and ready!
GuestAdditions 4.3.10 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/kimikimi714/Documents/vagrant/centos65
==> default: VM already provisioned. Run `vagrant provision` or use `--provision` to force it

おお、立ち上がった。
default: Checking if box 'chef/centos-6.5' is up to date...vagrant cloud上のboxに何か変更があったらvagrant upコマンドをたたいたときに自動で変更を取り込んでくれるらしい。

vagrant box addを使わないでboxの追加をする

ここまではvagrant box add実行するように書いたけれども実際にはvagrant box addを明示的に実行する必要はない。
例としてdebian-7.4のboxをvagrant cloudから取ってくる。
適当なディレクトリを作っておいて(debian-7.4とか)

$ vagrant init chef/debian-7.4
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.

これだけでconfig.vm.box = "chef/debian-7.4"が書かれた状態でVagrantfileが作成される。この状態で

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'chef/debian-7.4' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'chef/debian-7.4'
    default: URL: https://vagrantcloud.com/chef/debian-7.4
==> default: Adding box 'chef/debian-7.4' (v1.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/chef/debian-7.4/version/1/provider/virtualbox.box
==> default: Successfully added box 'chef/debian-7.4' (v1.0.0) for 'virtualbox'!
==> default: Importing base box 'chef/debian-7.4'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'chef/debian-7.4' is up to date...
==> default: Setting the name of the VM: debian-74_default_1396689831407_95814
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
==> default: Machine booted and ready!
GuestAdditions versions on your host (4.3.10) and guest (4.3.8) do not match.
stdin: is not a tty
Reading package lists...
Building dependency tree...
Reading state information...
dkms is already the newest version.
linux-headers-3.2.0-4-amd64 is already the newest version.
linux-headers-3.2.0-4-amd64 set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
stdin: is not a tty
mount: block device /tmp/VBoxGuestAdditions.iso is write-protected, mounting read-only
Installing Virtualbox Guest Additions 4.3.10 - guest version is 4.3.8
stdin: is not a tty
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.10 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.8 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
You should restart your guest to make sure the new modules are actually used

Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 4.3.10. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
stdin: is not a tty
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/kimikimi714/Documents/vagrant/debian-7.4

大事なのはログのdefault: Downloading: https://vagrantcloud.com/chef/debian-7.4/version/1/provider/virtualbox.boxのところで、vagrant box add chef/debian-7.4を実行したときと同じ処理が走っている。なので、

$ vagrant box list
chef/centos-6.5 (virtualbox)
chef/debian-7.4 (virtualbox)

で、boxが追加されていることが確認できる。

試しにchefのレシピが流し込めるかやってみる

仮想マシンが立ち上がってるんだから出来ないことないでしょ、とは思ってるけど、一応レシピ流しこめるかやってみる。

chef/centos-6.5が立ち上がってるディレクトリで

$ vagrant ssh-config --host chef_lessons >> ~/.ssh/config

これでchef_lessonsをホストに指定出来るようになった。次にchefのレシピがあるディレクトリで実際にレシピを流す。 logにHello Worldと表示させるだけのレシピにしている。

$ knife solo cook chef_lessons
Running Chef on chef_lessons...
Checking Chef version...
Uploading the kitchen...
Generating solo config...
Running Chef...
Starting Chef Client, version 11.10.4
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello World] action write

Running handlers:
Running handlers complete

Chef Client finished, 1/1 resources updated in 0.873104274 seconds

おk。boxのダウンロード方法が変わっただけなので、chefをどう使うかには影響がない。よかったよかった。

続きにはchefをmacに入れたときのログとか書いとく。

chefのインストール

chefのダウンロードページに進んで自分にあったものを選ぶ。この時選ぶのはChef Serverの方ではなくてChef Clientであることに注意。

f:id:kimikimi714:20140329185617p:plain

$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14401  100 14401    0     0   6365      0  0:00:02  0:00:02 --:--:--  6366Password:

Downloading Chef  for mac_os_x...
downloading https://www.opscode.com/chef/metadata?v=&prerelease=false&p=mac_os_x&pv=10.7&m=x86_64
  to file /tmp/install.sh.1611/metadata.txt
trying wget...
url https://opscode-omnibus-packages.s3.amazonaws.com/mac_os_x/10.7/x86_64/chef-11.10.4_1.mac_os_x.10.7.2.sh
md5 b318c6e42de19e7984f01bcf26fa53eb
sha256  b460c1944857ba7e56f7f0e528d8f3909b4948ab1f9555f521bac3a4bce8be1b
downloaded metadata file looks valid...
downloading https://opscode-omnibus-packages.s3.amazonaws.com/mac_os_x/10.7/x86_64/chef-11.10.4_1.mac_os_x.10.7.2.sh
  to file /tmp/install.sh.1611/chef-11.10.4_1.mac_os_x.10.7.2.sh
trying wget...
Comparing checksum with shasum...
Installing Chef
installing with sh...

インストールの最後にどういうわけかXQuarzが立ち上がって、そっちにログが出てきた。何が起こってるのかよく分かんなくて、"Press Enter"だけ見て反射的にEnter keyたたいたらウィンドウが消えてインストールログが消えてしまった。まぁ点々ばっかりだからいいんだけども。

knife soloのインストール

rubyのgemで入るので入れる。

$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-darwin13.0.0]
$ which ruby
/Users/kimikimi714/.rbenv/shims/ruby
$ gem -v
2.0.14
$ which gem
/Users/kimikimi714/.rbenv/shims/gem
$ sudo gem update --system
Password:
Updating rubygems-update
Fetching: rubygems-update-2.2.2.gem (100%)
(なんかいろいろ)
$ gem -v
2.2.2
$ gem install 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.
(なんかいろいろ)
Done installing documentation for mixlib-config, mixlib-cli, mixlib-log, mixlib-authentication, mixlib-shellout, systemu, yajl-ruby, ipaddress, ohai, mime-types, rest-client, net-ssh, net-ssh-gateway, net-ssh-multi, highline, erubis, diff-lcs, hashie, moneta, rack, chef-zero, puma, coderay, slop, method_source, pry, chef, knife-solo after 50 seconds
28 gems installed

どうもupdateも行われるらしい(知らなかった)。古いバージョンのものはお片付けしておく。

$ gem cleanup
Cleaning up installed gems...
Attempting to uninstall rake-10.1.1
Successfully uninstalled rake-10.1.1
Attempting to uninstall psych-2.0.4
Successfully uninstalled psych-2.0.4
Attempting to uninstall minitest-5.3.0
Successfully uninstalled minitest-5.3.0
Attempting to uninstall bundler-1.5.3
Successfully uninstalled bundler-1.5.3
Clean Up Complete

$ gem list

*** LOCAL GEMS ***

bigdecimal (1.2.5, 1.2.0)
bundler (1.6.0)
chef (11.10.4)
chef-zero (2.0.2, 1.7.3)
coderay (1.1.0)
diff-lcs (1.2.5)
erubis (2.7.0)
hashie (2.0.5)
highline (1.6.21)
io-console (0.4.2)
ipaddress (0.8.0)
json (1.8.1, 1.7.7)
knife-solo (0.4.1)
method_source (0.8.2)
mime-types (2.2, 1.25.1)
minitest (5.3.1, 4.3.2)
mixlib-authentication (1.3.0)
mixlib-cli (1.4.0)
mixlib-config (2.1.0)
mixlib-log (1.6.0)
mixlib-shellout (1.3.0)
moneta (0.7.20, 0.6.0)
net-ssh (2.8.0)
net-ssh-gateway (1.2.0)
net-ssh-multi (1.2.0)
ohai (6.20.0)
pry (0.9.12.6)
psych (2.0.5, 2.0.0)
puma (2.8.1, 1.6.3)
rack (1.5.2)
rake (10.2.2, 0.9.6)
rdoc (4.1.1, 4.0.0)
rest-client (1.6.7)
slop (3.5.0)
systemu (2.6.4, 2.5.2)
test-unit (2.5.5, 2.0.0.0)
yajl-ruby (1.2.0)

いくつかはちゃんと削除されたみたいだが、いくつかは残ったみたい。たぶんmacに初めから入ってたrubyのgemだろうなー。まぁいっか。

次にknifeコマンドの設定。とりあえずデフォルトで。

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

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

*****

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

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

レシピの準備

Hello Worldと表示させるだけのレシピを作る。

$ knife cookbook create hello -o site-cookbooks
** Creating cookbook hello
** Creating README for cookbook: hello
** Creating CHANGELOG for cookbook: hello
** Creating metadata for cookbook: hello

次のようにレシピを編集。
$ vim site-cookbooks/hello/recipes/default.rb

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

log "Hello World"

$ vim nodes/chef_lessons.json

{
    "run_list":[
        "recipe[hello]"
    ]
}

あとは上で書いたようにknife solo cook chef_lessonsを実行した。