Vagrantを触ってみた
きっかけ
Web開発にまつわる開発環境を「モダンないい感じ」にしたい!!!
「モダンないい感じ」にはいろいろ含まれますが、階層的に下の方から。
devops界隈も盛り上がっていることですし、お近づきになりたい!
2.インストール
Oracle VM VirtualBox からインストール
- Vagrant
> gem install vagrant > vagrant -v Vagrant version 1.0.7
3.VM(CentOS 6.4)の作成、起動
利用可能なboxは A list of base boxes for Vagrant - Vagrantbox.es にある。
とりあえず
- CentOS 6.4 x86_64 Minimal (VirtualBox Guest Additions 4.2.8, Chef 11.4.0, Puppet 3.1.0)
を利用する。
3.1.boxを追加
> vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130309.box [vagrant] Downloading with Vagrant::Downloaders::HTTP... [vagrant] Downloading box: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130309.box [vagrant] Extracting box...1597339 / 491722240)[vagrant] [vagrant] Verifying box... [vagrant] Cleaning up downloaded box...
3.2.VMを作成
> vagrant init centos 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.
起動したディレクトリに「Vagrantfile」という設定ファイルができます。
とりあえずはそのままで。
3.3.起動
> vagrant up [default] Importing base box 'centos'... [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.2.8 VirtualBox Version: 4.2.10 [default] Mounting shared folders... [default] -- v-root: /vagrant
バージョンがどうのこうの言っているが、とりあえず気にしない・・・。
4.接続
SSH接続しようとすると、Windowsではムリ!!とごねられる。
> vagrant ssh `vagrant ssh` isn't available on the Windows platform. You are still able to SSH into the virtual machine if you get a Windows SSH client (such as PuTTY). The authentication information is shown below:
4.1.あきらめません
たしか、GitHub for Windows をインストールした際に主要なUNIXコマンドが一緒にインストールされたはず・・・。
>where ssh C:\Users\zzzzz\AppData\Local\GitHub\PortableGit_xxxxxxx\bin\ssh.exe >ssh usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-e escape_char] [-F configfile] [-i identity_file] [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R [bind_address:]port:host:hostport] [-S ctl_path] [-w local_tun[:remote_tun]] [user@]hostname [command]
あるじゃない。パスも通っているのになぜだ。
4.2.デバッグモードで調査
> set VAGLANT_LOG=debug
とすることで、デバッグモードになるとのことなので、やってみる。
ERROR vagrant: C:/Ruby193/lib/ruby/gems/1.9.1/gems/vagrant-1.0.7/lib/vagrant/ssh.rb:59:in `exec' C:/Ruby193/lib/ruby/gems/1.9.1/gems/vagrant-1.0.7/lib/vagrant/command/ssh.rb:86:in `ssh_connect' C:/Ruby193/lib/ruby/gems/1.9.1/gems/vagrant-1.0.7/lib/vagrant/command/ssh.rb:53:in `block in execute'
ほほぅ。「ssh.rb」。。。おまえか。
4.3.勝利
ソースを確認してみると、
if Util::Platform.windows? raise Errors::SSHUnavailableWindows, :host => ssh_info[:host], :port => ssh_info[:port], :username => ssh_info[:username], :key_path => ssh_info[:private_key_path] end raise Errors::SSHUnavailable if !Kernel.system("which ssh > /dev/null 2>&1")
- windowsだと、有無を言わさずエラー
- whichコマンドでsshが見つからくてもエラー
となるようですね。これをコメントアウトしちまえば・・・乱暴だけど・・・。
C:\Users\nishi\VirtualBox VMs\centos>vagrant ssh Last login: Tue Apr 2 07:52:13 2013 from 10.0.2.2 Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$
イケたでござる!!!
5.その他雑多なこと
5.1.停止、再起動
- 停止
- vagrant halt
- 再起動
- vagrant reload
5.2.rootユーザーのパス
デフォルトは「vagrant」
5.3.ホストとの共有ディレクトリ
「Vagrantfile」のある場所が、/vagrantにマウントされる
v-root on /vagrant type vboxsf (uid=501,gid=501,rw)
5.4.ブリッジ接続
デフォルトではホストオンリーになっています、社内NW上に公開する場合が多いので。
Vagrantファイルの以下の設定をコメントアウトすればOK。
config.vm.network :bridged
!! 注意 !!
ただし、ブリッジ接続にすると、再起動時に
ERROR vagrant: The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! /sbin/ifup eth1 2> /dev/null
のエラーでこけます。いろいろ調べましたが、根本的な解決策が見つかっていません。
/etc/sysconfig/network-scripts/ifcfg-eth1
の
ONBOOT=yes
を「no」にしてやることで回避できます。
6.まとめ
- vagrantすげー
- chefと組み合わせるともっとおいしいらしい