Vagrant – 仮想化を使って開発フローを効率化しよう
数年前から仮想化が盛り上がっています。本番環境でハードウェアリソースを有効活用するための仮想化もありますが、今回は開発環境を仮想化し、効率化するVagrantを紹介します。
セットアップ
使い方はとても簡単です。ダウンロードとインストールが終わったら順番にコマンドを叩いていきます。
最初にボックスの作成。
$ vagrant box add base http://files.vagrantup.com/precise32.box
Downloading box from URL: http://files.vagrantup.com/precise32.box
Extracting box...te: 52818/s, Estimated time remaining: 0:00:03)
Successfully added box 'base' with provider 'virtualbox'!
終わったら初期化。Vagrantfileを作成します。
$ 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
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'base'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[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] Mounting shared folders...
[default] -- /vagrant
サーバは1分くらいで立ち上がります。立ち上がったらsshサブコマンドで接続します。
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$
とても簡単ですね。仮想サーバを落とすのはdestroyhaltというサブコマンドです。
$ vagrant --help
Usage: vagrant [-v] [-h] command [<args>]
-v, --version Print the version and exit.
-h, --help Print this help.
Available subcommands:
box manages boxes: installation, removal, etc.
destroy stops and deletes all traces of the vagrant machine
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment by creating a Vagrantfile
package packages a running vagrant environment into a box
plugin manages plugins: install, uninstall, update, etc.
provision provisions the vagrant machine
reload restarts vagrant machine, loads new Vagrantfile configuration
resume resume a suspended vagrant machine
ssh connects to machine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs status of the vagrant machine
suspend suspends the machine
up starts and provisions the vagrant environment
For help on any individual command run `vagrant COMMAND -h`
後はVagrantfileが多数有志によって公開されていますので、それを取ってきてupサブコマンドを実行すればその環境ができあがるといった仕組みです。例えばRails環境の場合は次のようになります。
$ git clone https://github.com/markdunphy/rails-vagrant.git
$ cd rails-vagrant
$ vagrant up
後は自社専用のイメージを作成してVagrantfileで設定を書いておくようにすれば、誰でも同じ環境でテストできるようになったり開発ができるようになります。Mac OSX、LinuxだけでなくWindowsもサポートされていますのでゲストOSによらず同じテストで同じ結果が得られるようになるはずです。
元々VirtualBoxをサポートしていましたがVMWare Fusion、AWS、RackSpace、LXCもサポートされています。自分一人ではなく、複数人での開発を考えるならば統一された環境がすぐに用意できるVagrantを使わない手はありません。さらに素早く立ち上げてすぐに落とせることを考えると実験環境としても最適と言えるでしょう。 VagrantはRuby製、MIT Licenseのオープンソース・ソフトウェアです。 Vagrant mitchellh/vagrant