Vagrantへpluginを追加する

# Vagrantへpluginを追加する

|     操作     |                 コマンド                 |
| :----------- | :--------------------------------------- |
| インストール | `vagrant plugin install <plugin-name>`   |
| 削除         | `vagrant plugin uninstall <plugin-name>` |
| アップデート | `vagrant plugin update <plugin-name>`    |
| リストアップ | `vagrant plugin list`                    |


## ［Proxy環境限定］ホストOSの設定

* ホストOSの環境変数にプロキシ設定を追加する

|    変数名     |                   変数値                    |
| :------------ | :------------------------------------------ |
| `http_proxy`  | `<username>:<password>@<proxy_host>:<port>` |
| `https_proxy` | `<username>:<password>@<proxy_host>:<port>` |


## Plugin一覧

```bat
> vagrant plugin list
sahara (0.0.17)
vagrant-hostsupdater (0.0.11)
vagrant-login (1.0.1, system)
vagrant-share (1.1.0, system)
vagrant-vbguest (0.10.0)
vagrant-vbox-snapshot (0.0.5)
```


## sahara

* サンドボックス機能を提供

* コミットは、`vagrant halt`してから実施する。停止しないと時間がかかる
* コミットせずにsandboxを終了すると「最後にコミットした時点」or「sandboxを開始した時点（一度もコミットしてなければ）」に戻る

| sandboxの操作 |               コマンド               |
| :------------ | :----------------------------------- |
| 状態確認      | `vagrant sandbox status`             |
| sandbox開始   | `vagrant sandbox on <vm-name>`       |
| sandbox終了   | `vagrant sandbox off <vm-name>`      |
| コミット      | `vagrant sandbox commit <vm-name>`   |
| ロールバック  | `vagrant sandbox rollback <vm-name>` |


## vagrant-hostsupdater

* ホストOSの`hosts`を自動的に更新してくれる

* `hosts`ファイルのアクセス権限が不足していると以下エラーになる

```bat:コマンドプロンプト
> vagrant reload
==> default: Attempting graceful shutdown of VM...
==> 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: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Checking for host entries
==> default: adding to (C:/Windows/system32/drivers/etc/hosts) : 192.168.56.101  test01.centos65x64.example.com  # VAGRANT: c6734be027cff0c98de4545b78c7180e (default) / d4365584-0c25-429e-873f-c391db071b60
C:/Users/<ユーザ名>/.vagrant.d/gems/gems/vagrant-hostsupdater-0.0.11/lib/vagrant-hostsupdater/HostsUpdater.rb:82:in `initialize': Permission denied - C:/Windows/system32/drivers/etc/hosts (Errno::EACCES)
        from C:/Users/<ユーザ名>/.vagrant.d/gems/gems/vagrant-hostsupdater-0.0.11/lib/vagrant-hostsupdater/HostsUpdater.rb:82:in `open'
```


## vagrant-vbguest

* VirtualBoxとVirtualBox Guest Additionsのバージョンをあわせてくれる
* `vagrant up`のたびに稼動するので、vagrantの全体設定で無効化しておく

```rb:%HOME%/.vagrant.d/Vagrantfile
Vagrant.configure("2") do |config|
  config.vbguest.auto_update = false
end
```

### 実行例

* バージョンが一致しているか確認する

```bat:コマンドプロンプト
> vagrant vbguest --status

【バージョンが一致している場合】
DL is deprecated, please use Fiddle
GuestAdditions 4.3.6 running --- OK.

【バージョンが一致していない場合】
DL is deprecated, please use Fiddle
GuestAdditions versions on your host (4.3.8) and guest (4.3.6) do not match.
```

* 一致していなければ、該当するバージョンをインストールする

```bat:コマンドプロンプト
> vagrant vbguest --do install
DL is deprecated, please use Fiddle
GuestAdditions versions on your host (4.3.8) and guest (4.3.6) do not match.
Loaded plugins: fastestmirror, priorities, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Package kernel-devel-2.6.32-358.el6.x86_64 already installed and latest version
Package gcc-4.4.7-3.el6.x86_64 already installed and latest version
Package 1:make-3.81-20.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-129.el6.x86_64 already installed and latest version
Nothing to do
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestA
dditions.iso
Forcing installation of Virtualbox Guest Additions 4.3.8 - guest version is 4.3.6
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.8 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.6 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the OpenGL support module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
You should restart your guest to make sure the new modules are actually used

Installing the Window System drivers
Installing X.Org Server 1.13 modules[  OK  ]
Setting up the Window System to use the Guest Additions[  OK  ]
You may need to restart the hal service and the Window System (or just restart
the guest system) to enable the Guest Additions.

Installing graphics libraries and desktop services components[  OK  ]
An error occurred during installation of VirtualBox Guest Additions 4.3.8. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

> 
```


## vagrant-vbox-snapshot

| snapshotの操作 |                       コマンド                      |
| :------------- | :-------------------------------------------------- |
| 一覧確認       | `vagrant snapshot list`                             |
| 取得           | `vagrant snapshot take [vm-name] <SNAPSHOT_NAME>`   |
| 復元(直近から) | `vagrant snapshot back [vm-name]`                   |
| 復元(指定)     | `vagrant snapshot go [vm-name] <SNAPSHOT_NAME>`     |
| 削除           | `vagrant snapshot delete [vm-name] <SNAPSHOT_NAME>` |

* \<SNAPSHOT_NAME\>は識別しやすいようにつけるラベル

### 実行例

```bat:コマンドプロンプト
> vagrant halt
[default] Attempting graceful shutdown of VM...
[default] Forcing shutdown of VM...

> vagrant snapshot take test-snap
Taking snapshot test-snap
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

> 
> vagrant snapshot list
Listing snapshots for 'default':
   Name: test-snap (UUID: 1fe22c17-8e15-4f22-8eeb-1aa4579b4a15) *

> vagrant snapshot delete test-snap
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

> vagrant snapshot list
Listing snapshots for 'default':
This machine does not have any snapshots
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["snapshot", "77c43940-4dd3-4a9f-945a-110cbf9aee7d", "list", "--details"]

Stderr:

> 
```