Vagrant DigitalOcean プラグインの導入

インスタンス(Droplet)の起動が速くてしかも安価なDigitalOceanを利用したら、Vagrant環境を利用した開発の効率を上げられそうなので試してみた

VagrantのDigitalOceanプロバイダの情報はGithubのreadme vagrant-digitaloceanを参照した

vagrant-digitaloceanのインストール

$ vagrant plugin install vagrant-digitalocean

Macの場合はOpenSSLが読み込む証明書のパスをexportしてあげる必要があるらしい
パスを確認

$ ruby -ropenssl -e "p OpenSSL::X509::DEFAULT_CERT_FILE"
"/System/Library/OpenSSL/cert.pem"

“Vagrant DigitalOcean プラグインの導入” の続きを読む

Vagrant v2 既存環境をコピーして新しいboxファイルを作成するには

前の記事「Vagrant v2 プロビジョニングの使い方、Chef SofoプロビジョナーでApacheをインストール」では、公開されているUbuntu 14.04のboxをローカル環境に追加(add)したものからプロビジョニングで必要な環境をセットアップしましたが、インストールするパッケージが多くプロビジョニングが必要な場合はboxに固めてそれを配布した方が効率が良い場合もあります。
例えば、ソースインストールする必要があるパッケージがある場合コンパイルに時間がかかります。そういった場合には便利です。

Vagrantの公式ドキュメントにでは、「Boxes – Vagrant Documentation」にまとまっています。

box コマンドおさらい

boxの管理はvagrant boxコマンドを利用します。
よく使うboxコマンドについてまとめておきます。

box add

Vagrantをインストールして、おそらく最初に実行するコマンドです。
boxをネットワークからダウンロードしてローカルのVagrant環境に登録します。
[bash gutter=”false”]
$ vagrant box add ubuntu/trusty64
==> box: Loading metadata for box ‘ubuntu/trusty64’
box: URL: https://vagrantcloud.com/ubuntu/trusty64
==> box: Adding box ‘ubuntu/trusty64’ (v14.04) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/1/providers/virtualbox.box
==> box: Successfully added box ‘ubuntu/trusty64’ (v14.04) for ‘virtualbox’!
[/bash]
VagrantのサイトDiscover Featured Vagrant Boxesで公開されているboxは、boxを名前で指定できます。
A list of base boxes for Vagrant – Vagrantbox.esなどで公開されているboxを追加する場合は以下のように名前に続けてboxのURLを指定します。
[bash gutter=”false”]
$ vagrant box add opscode/centos-7.0 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-7.0_chef-provisionerless.box
==> box: Adding box ‘opscode/centos-7.0’ (v0) for provider:
box: Downloading: http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-7.0_chef-provisionerless.box
==> box: Box download is resuming from prior download progress
==> box: Successfully added box ‘opscode/centos-7.0’ (v0) for ‘virtualbox’!
[/bash]
“Vagrant v2 既存環境をコピーして新しいboxファイルを作成するには” の続きを読む

Vagrant v2 プロビジョニングの使い方、Chef SofoプロビジョナーでApacheをインストール

Vagrantのプロビジョニングを利用すると、開発環境で必要なソフトウェアを揃えたVagrant環境を用意することができます。

プロビジョニングは、シェルスクリプト、Chef、Puppet、Dockerと様々な方法をサポートしています。
Chefレシピを使ったApacheのインストールを試した最も基本的な例を試したのでまとめておきます。

プロジェクト用のディレクトリvagrant_chefを作成してUbuntu 14.04.1 LTS (Trusty Tahr)を指定してvagrant initを実行します。
[bash gutter=”false”]
$ mkdir vagrant_chef
$ cd vagrant_chef
$ vagrant init ubuntu/trusty64
[/bash]
“ubuntu/trusty64″はVagrantの公式イメージを指定しています。
https://vagrantcloud.com/ubuntu/boxes/trusty32

Chef Soloによるプロビジョニングを有効にします。
[bash title=”Vagrantfile”]
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.provision "chef_solo" do |chef|
chef.add_recipe "apache"
end
end
[/bash]

デフォルトでは、Vagrantfileが置かれているディレクトリの”cookbooks”ディレクトリからレシピを読み込みます。
[bash gutter=”false”]
$ mkdir -p cookbooks/apache/recipes/
$ cd cookbooks/apache/recipes/
[/bash]

Apache2をセットアップするレシピは以下のように記述します。
[ruby title=”cookbooks/apache/recipes/default.rb”]
execute ‘apt-get update’
package ‘apache2’
execute ‘rm -rf /var/www’
link ‘/var/www’ do
to ‘/vagrant’
end
[/ruby]
Apache2をインストールした後、ゲストマシンの /var/www ディレクトリを共有フォルダ /vagrant にリンクしています。

[bash gutter=”false”]
$ vagrant up

==> default: Forwarding ports…
default: 80 => 8080 (adapter 1)
default: 22 => 2222 (adapter 1)


==> default: Running provisioner: chef_solo…
Generating chef JSON and uploading…
Running chef-solo…
stdin: is not a tty
[2014-10-05T15:17:44+00:00] INFO: Forking chef instance to converge…
[2014-10-05T15:17:44+00:00] INFO: *** Chef 11.8.2 ***
[2014-10-05T15:17:44+00:00] INFO: Chef-client pid: 1556
[2014-10-05T15:17:44+00:00] INFO: Setting the run_list to ["recipe[apache]"] from JSON
[2014-10-05T15:17:44+00:00] INFO: Run List is [recipe[apache]]
[2014-10-05T15:17:44+00:00] INFO: Run List expands to [apache]
[2014-10-05T15:17:44+00:00] INFO: Starting Chef Run for vagrant-ubuntu-trusty-64
[2014-10-05T15:17:44+00:00] INFO: Running start handlers
[2014-10-05T15:17:44+00:00] INFO: Start handlers complete.
[2014-10-05T15:18:11+00:00] INFO: execute[apt-get update] ran successfully
[2014-10-05T15:18:29+00:00] INFO: execute[rm -rf /var/www] ran successfully
[2014-10-05T15:18:29+00:00] INFO: link[/var/www] created
[2014-10-05T15:18:29+00:00] INFO: Chef Run complete in 44.377179971 seconds
[2014-10-05T15:18:29+00:00] INFO: Running report handlers
[2014-10-05T15:18:29+00:00] INFO: Report handlers complete
[2014-10-05T15:17:44+00:00] INFO: Forking chef instance to converge…
[/bash]

UbuntuにパッケージインストールしたApacheはデフォルト設定では /var/www/html がDocumentRootです。
/var/www は共有フォルダ /vargrant へのリンクにセットしたので、プロジェクトのhtmlディレクトリがDocumentRootになります。
プロジェクトにhtmlディレクトリを作成してindex.htmlを追加します。
[bash gutter=”false”]
$ mkdir html
$ echo ‘Hello Vagrant!’ > html/index.html
[/bash]

プロジェクトのディレクトリ構成は以下のようになります。
[bash gutter=”false”]
$ tree
.
├── Vagrantfile
├── cookbooks
│   └── lamp
│   └── recipes
│   └── default.rb
└── html
└── index.html

4 directories, 3 files
[/bash]

ブラウザでhttp://localhost:8080にアクセスすると、以下のようにホストマシンのindex.htmlが表示されます。
VagrantProvisioning_CheckApacheResponse

VagrantをMac OSXにセットアップ

バーチャルボックスのインストール

[bash gutter=”false”]
Vagrant has detected that you have a version of VirtualBox installed
that is not supported. Please install one of the supported versions
listed below to use Vagrant:

4.0, 4.1, 4.2
[/bash]

2014.2.3現在VagrantはVirtualBoxの最新4.3をサポートしていないので、Download VirtualBox (Old Builds): VirtualBox 4.2 よりインストーラをダウンロードしてインストールしました。

vagrantのインストール

[bash gutter=”false”]
$ gem install vagrant
Fetching: archive-tar-minitar-0.5.2.gem (100%)
archive-tar-minitar’s executable "minitar" conflicts with minitar
Overwrite the executable? [yN] y

Done installing documentation for archive-tar-minitar, childprocess, log4r, net-scp, net-ssh, vagrant after 7 seconds
6 gems installed
[/bash]

OS イメージ “box” の追加

以下のコマンドでOSイメージを追加できる
[bash gutter=”false”]
vagrant box add {title} {url}
[/bash]

通常利用するようなOSのboxイメージは http://www.vagrantbox.es に公開されています。

Ubuntu 12.04を選択して起動してみます。
[bash gutter=”false”]
$ vagrant box add Ubuntu1204 http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box
[/bash]
title にデフォルトの “base” 以外を指定した場合は Vagrantfile に設定が必要です。
[ruby title=”Vagrantfile”]
Vagrant::Config.run do |config|

# Every Vagrant virtual environment requires a box to build off of.
# config.vm.box = "base"
config.vm.box = "Ubuntu1204"

end
[/ruby]
設定後 init して upします。
[bash gutter=”false”]
$ vagrant init
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 ‘Ubuntu1204’…
[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…
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[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.12
VirtualBox Version: 4.2.22
[default] Mounting shared folders…
[default] — v-root: /vagrant
[/bash]
ssh で起動したboxにアクセスします。
[bash gutter=”false”]
$ vagrant ssh
[/bash]

追記 2014/4/21

久しぶりにboxを追加しようとしたら以下のエラーになった
[bash gutter=”false”]
$ vagrant box add CentOS_6_5 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP…
[vagrant] Downloading box: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
[vagrant] Downloading box: https://s3.amazonaws.com/github-cloud/releases/9086871/062c1990-5dbe-11e3-8636-484e1779fc04.box?response-content-disposition=attachment%3B%20filename%3Dcentos65-x86_64-20131205.box&AWSAccessKeyId=AKIAISTNZFOVBIJMK3TQ&Expires=1398065366&Signature=9l7%2BBA06F7tX0NYrP3ydB%2FGgNYY%3D
[vagrant] Extracting box…
[vagrant] Cleaning up downloaded box…
Failed to untar the box file. This is usually because you’re
attempting to add a box that isn’t a valid box file. Please
double check that the box file is properly packaged.
[/bash]
そこで、vagrantをupdateしたところ、RubyGem版はもうサポートしないのでインストーラからインストールしろとのこと
[bash gutter=”false”]
$ gem update vagrant
Vagrant version 1.0.7
localhost@hrendoh:~ $ gem update vagrant
Updating installed gems
Updating vagrant
Fetching: vagrant-1.5.0.gem (100%)
Thanks for wanting to use Vagrant! Unfortunately, this is not the way
to install Vagrant anymore. We now make installers for the various operating
systems Vagrant supports.

Vagrant is no longer distributed as a RubyGem. Please download the latest
version for your operating system from the URL below. If you still wish
to use the RubyGem version, you can manually install version 1.0.7. Note that
the RubyGem version hasn’t been updated in over a year and will no longer
receive any updates.

Prior to installing Vagrant using the installer, make sure you uninstall
all your Vagrant gems, since they sometimes conflict.

http://www.vagrantup.com

If you want to learn more about why we don’t distribute using RubyGems
anymore, please read this: http://mitchellh.com/abandoning-rubygems
Successfully installed vagrant-1.5.0
Parsing documentation for vagrant-1.5.0
Installing ri documentation for vagrant-1.5.0
Installing darkfish documentation for vagrant-1.5.0
Done installing documentation for vagrant after 0 seconds
Gems updated: vagrant
[/bash]

RubyGemのVagrantをアンインストールして
[bash gutter=”false”]
$ gem uninstall vagrant

Select gem to uninstall:
1. vagrant-1.0.7
2. vagrant-1.5.0
3. All versions
> 3
Successfully uninstalled vagrant-1.0.7
Remove executables:
vagrant

in addition to the gem? [Yn] y
Removing vagrant
Successfully uninstalled vagrant-1.5.0
[/bash]

http://www.vagrantup.com/downloads.html
からインストーラ(vagrant_1.5.3.dmg)をダウンロードしてインストール

boxを追加してみると成功
[bash gutter=”false”]
$ vagrant box add CentOS6.5_64 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
==> box: Adding box ‘CentOS6.5_64’ (v0) for provider:
box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
==> box: Successfully added box ‘CentOS6.5_64’ (v0) for ‘virtualbox’!
[/bash]
追加したboxをVagrantfileに指定して
[ruby title=”Vagrantfile”]
# config.vm.box = "hashicorp/precise32"
config.vm.box = "CentOS6.5_64"
[/ruby]
起動
[bash gutter=”false”]
$ vagrant up
[/bash]