vagrantとfabricとvirtualbox

ちょっとインフラ的なこと試してました
以下を見たら、vagrantって仮想OSをあっさり作ってくれる素敵なものって知ったので、試してみたくなった

# vagrantインストール

まず、vagrantはgemからインストール

```
gem install vagrant
```

これはあっさり終了

# easy_install

そのあと、fabricの前にeasy_install

```
  501  curl -O http://peak.telecommunity.com/dist/ez_setup.py
  502  python ez_setup.py
  503  which easy_install
```

入ったことを確認できました

次は、fabric〜

# fabric

こっから苦戦

```
sudo easy_install fabric
```

エラー。ググって巡り巡って、これにたどり着いた
http://qiita.com/oonishin/items/8e61459ef3e7ca08e31a

ってことで

```
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install fabric
```

成功！

ちゃんとエラー文理解してればこんな苦労しなかったのにね

じゃあ、vagrantで仮想マシン作ります

# vagrantで使える仮想マシン一覧を見るには

以下にアクセス
http://www.vagrantbox.es/
＊初期に使うコマンドも書いてあるので、参考になる！


# vagrantで仮想マシン作成


```
server:~ shiratsu$ vagrant init
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
```

あれ、うまくいってないっぽい
よく読むと、gemだともうダメで、自分でダウンロードしてインストールしろって言ってる

## vagrantインストールやり直し

ってことで、vagrantのサイト言って、DLしてインストール

Pathはこうなった
## vagrantのパス

```
server:~ shiratsu$ which vagrant
/usr/bin/vagrant
```

ってことで初期化！

```
server:~ shiratsu$ 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.
```

アップして、ログイン

```
server:vm_machine shiratsu$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos_65_minimal' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Adding box 'centos_65_minimal' (v0) for provider: virtualbox
    default: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
    default: Progress: 15% (Rate: 162k/s, Estimated time remaining: 0:18:39)
==> default: Successfully added box 'centos_65_minimal' (v0) for 'virtualbox'!
==> default: Importing base box 'centos_65_minimal'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vm_machine_default_1396666192513_31129
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/shiratsu/vm_machine
server:vm_machine shiratsu$
server:vm_machine shiratsu$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.
```
失敗した場合、
```
vagrant init
```
の際に、使う仮想サーバ名を指定してください

こんな感じ
```
/usr/bin/vagrant init 20140717_centos
```
ってことでログアウトして終了

次にfabfile作って、vagrantで実行してみます

## fabricお試し

```
server:vm_machine shiratsu$ cat fabfile.py
from fabric.api import sudo

def install_git():
  sudo("yum -y install git")
  sudo('yum -y install httpd php php-mbstring')
```

vagrantでの接続情報がわからんと実行できないので取得

```
server:vm_machine shiratsu$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/shiratsu/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
```

オッケー、実行

```
server:vm_machine shiratsu$ fab -H 127.0.0.1 --port 2222 -u vagrant -i ~/.vagrant.d/insecure_private_key install_git
```

詳細は省略しますが、無事成功してます