bentoからvagrantのboxを作る。


## 環境

Mac OS X El Capitan

## bentoとは

* chef社が公開しているvagrant用のOSイメージ（box）

## vagrantのboxを追加する。

* centos 6.7を設置する。
* boxはparallels、virtualbox、vmware用が提供されている。ここではvirtualboxを選ぶ。
* ~/.vagrant.dの以下にboxがダウンロードされて保存される。

```sh
  # 実習用の新しいディレクトリを生成する。
  % mkdir chef
  % cd chef

  # bento/centos-6.7を追加
  % vagrant box add bento/centos-6.7
  ==> box: Loading metadata for box 'bento/centos-6.7'
      box: URL: https://atlas.hashicorp.com/bento/centos-6.7
  This box can work with multiple providers! The providers that it
  can work with are listed below. Please review the list and choose
  the provider you will be working with.

  1) parallels
  2) virtualbox
  3) vmware_desktop

  Enter your choice: 2
  ==> box: Adding box 'bento/centos-6.7' (v2.2.3) for provider: virtualbox
      box: Downloading: https://atlas.hashicorp.com/bento/boxes/centos-6.7/versions/2.2.3/providers/virtualbox.box
  ==> box: Successfully added box 'bento/centos-6.7' (v2.2.3) for 'virtualbox'!
```

## Vagrantfileを生成して、bento boxを使うように修正する。

```sh
  % vagrant init
  % vi Vagrantfile
  Vagrant.configure("2") do |config|
    config.vm.box = "bento/centos-6.7"
  end
```

## 仮想サーバ起動

```sh
  % vagrant up
  % vagrant status
  Current machine states:

  default                   running (virtualbox)

  The VM is running. To stop this VM, you can run `vagrant halt` to
  shut it down forcefully, or you can run `vagrant suspend` to simply
  suspend the virtual machine. In either case, to restart it again,
```

## 仮想サーバへのログインとログアウト

```sh
  % vagrant ssh
  [vagrant@localhost ~]$ cat /etc/redhat-release
  CentOS release 6.7 (Final)
  [vagrant@localhost ~]$ exit
  logout
  Connection to 127.0.0.1 closed.
```

## ssh-configでより実践的に使おう。

### Vagrantfileを修正する。

```ruby
  % vi Vagrantfile

  Vagrant.configure("2") do |config|
    config.vm.box = "bento/centos-6.7"
    config.vm.hostname = "dev"
    config.vm.network "forwarded_port", guest: 80, host: 8888
    config.vm.network "private_network", ip: "192.168.33.10"
    config.vm.provider "virtualbox" do |vb|
      vb.name = "dev"
      vb.customize ["modifyvm", :id, "--memory", "1024"]
    end
  end
```

### ssh config向けの設定を行う。

```sh
  % vagrant ssh-config --host dev >> ~/.ssh/config
  % cat ~/.ssh/config

  Host dev
    HostName 127.0.0.1
    User vagrant
    Port 2222
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no
    PasswordAuthentication no
    IdentityFile /Users/devnote/Documents/study/chef/.vagrant/machines/default/virtualbox/private_key
    IdentitiesOnly yes
    LogLevel FATAL
```

### ssh devでもう一度ログインしてみる。

* Vagrantfileを修正したので、仮想サーバを停止してもう一度起動する。

```sh
  % vagrant halt
  % vagrant up
```

* ssh devでログインする。

```sh
  % ssh dev
```

## トラブル

* forwarded port 8080がすでに使われているとエラーメッセージが表示されて起動されない。

```sh
  % vagrant up
  Bringing machine 'default' up with 'virtualbox' provider...
  ==> default: Checking if box 'bento/centos-6.7' is up to date...
  ==> default: Setting the name of the VM: dev
  ==> default: Clearing any previously set forwarded ports...
  Vagrant cannot forward the specified ports on this VM, since they
  would collide with some other application that is already listening
  on these ports. The forwarded port to 8080 is already in use
  on the host machine.

  To fix this, modify your current projects Vagrantfile to use another
  port. Example, where '1234' would be replaced by a unique host port:

    config.vm.network :forwarded_port, guest: 80, host: 1234

  Sometimes, Vagrant will attempt to auto-correct this for you. In this
  case, Vagrant was unable to. This is usually because the guest machine
  is in a state which doesn't allow modifying port forwarding.
```

* netstatで8080ポートを調べたら使われている。

```sh
  % netstat -na | grep 8080
  72:tcp4       0      0  *.8080                 *.*                    LISTEN
```

* lsofで8080ポートを使っているプロセスを調べたらrubyが使っていた。

```sh
  % lsof -i:8080
  COMMAND  PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
  ruby    1731 devnote   14u  IPv4 0x382bd533711e0f5d      0t0  TCP *:http-alt (LISTEN)
  ruby    1765 devnote   14u  IPv4 0x382bd533711e0f5d      0t0  TCP *:http-alt (LISTEN)
  ruby    1766 devnote   14u  IPv4 0x382bd533711e0f5d      0t0  TCP *:http-alt (LISTEN)
```

* ローカル上でredmineをnginxとunicornの組合で使っているので、unicornを停止してもう一度確認したら今度は正常に起動された。
* 3つのrubyプロセスが使われたのはunicorn.rbからworker数を2に設定したからだ。

```sh
  % launchctl unload ~/Library/LaunchAgents/local.unicorn.redmine.plist

  % vagrant up
  Bringing machine 'default' up with 'virtualbox' provider...
  ==> default: Checking if box 'bento/centos-6.7' is up to date...
  ==> 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: 80 => 8080 (adapter 1)
      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: Machine booted and ready!
  ==> default: Checking for guest additions in VM...
      default: The guest additions on this VM do not match the installed version of
      default: VirtualBox! In most cases this is fine, but in rare cases it can
      default: prevent things such as shared folders from working properly. If you see
      default: shared folder errors, please make sure the guest additions within the
      default: virtual machine match the version of VirtualBox you have installed on
      default: your host and reload your VM.
      default:
      default: Guest Additions Version: 5.0.12
      default: VirtualBox Version: 4.3
  ==> default: Setting hostname...
  ==> default: Configuring and enabling network interfaces...
  ==> default: Mounting shared folders...
      default: /vagrant => /Users/devnote/Documents/study/chef
  ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
  ==> default: to force provisioning. Provisioners marked to run always will still run.  
```

* redmineのconfig/unicorn.rbを開いてみたら下記の様な設定があった。
* unixドメインソケットとTCP portを同時に使うために設定したらしい。
* 今まで会社の本番環境にてnginxとunicornの組合で使っていたが、こんな設定ができるのは知らなかった。

```ruby
  ... snip ...
  # listen on both a Unix domain socket and a TCP port,
  # we use a shorter backlog for quicker failover when busy
  listen "#{APP_PATH}/tmp/sockets/unicorn.sock", :backlog => 64
  listen 8080, :tcp_nopush => true
  ... snip ...
```

* vagrantの設定から8080を別のポートに変更したほうが良さそう。

## 参考

* https://github.com/chef/bento
* Chef実践入門