環境構築の自動化に関して


## 参考サイト
http://inokara.hateblo.jp/entry/2013/12/31/184645
http://threetreeslight.com/post/58009785849/boxen-mac
http://qiita.com/yuku_t/items/c6f20de0e4f4c352046c

// vagrant + chef + Berkshelf スライド
http://www.engineyard.co.jp/blog/2013/vagrant-and-berkshelf/

// vagrant chef-solo document
http://docs.vagrantup.com/v2/provisioning/chef_solo.html

//opscode github cookbook集
https://github.com/opscode-cookbooks

// VagrantFile生成サイト
https://puphpet.com/
## 自分のmac環境を作成

## vagrant
sample集
https://github.com/patrickdlee/vagrant-examples

``` shell
 # vagrant my setting
VAGRANTFILE_API_VERSION = "2"
box_name = "centos64"
box_url  = "https://s3.amazonaws.com/itmat-public/centos-6.3-chef-10.14.2.box"
ip       = "192.168.33.10"
ram      = "1024"
hostname = "v_dev"
owner    = "vagrant"
group    = ""

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box     = box_name
  config.vm.box_url = box_url
  # config.vm.host_name = hostname + '.' + domain
  config.vm.network :private_network, ip: ip

  config.vm.provider :virtualbox do |vb|
    vb.customize [
      "modifyvm", :id,
      '--name'  , hostname,
      '--memory', ram
    ]
	# IPv6とDNSでのネットワーク遅延対策で追記
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
  end

  config.vm.synced_folder "./", "/var/www",
    :nfs   => false,
    :owner => owner,
    :group => group,
    :mount_options => ["dmode=775,fmode=775"]

 # chef installed check plugin exsist
 # config.omnibus.chef_version = :latest
 # config.berkshelf.enabled = true

 # chef solo run
  config.vm.provision :chef_solo do |chef|
    chef.run_list = ["cookbook_name"]
  end
```


## chef solo (自分自身にのみ実行する)

### 用語

* knife (実行者)
* CookBook (gemみたいなライブラリ集)
* Recipe (実行コマンド集)
* Brekshelf

> Chef にはいくつかの抽象化するための要素があり、構築手順の汎用性を高めることができます。つまり1つの Cookbook をいろいろな OS、いろいろな設定に使いまわすことができるのです。

> そこで、いろんな人が作った Cookbook を使いまわすために RubyGems に対する Bundler のような Cookbook 管理ツールがあります。それが Berkshelf です。

http://berkshelf.com/


### 作法
cookbooks
> 汎用的なcookbooksを配置する場所

site-cookbooks
> 対象環境固有のcookbookを配置する場所

### 全体像

berkshelf > cookbook > recipes > *.rb

### install

``` shell
// vertualBox install
// vagrant install

mkdir ~/v_develop
cd ~/v_develop
vagrant init box_name box_url
vim Vagrantfile # setting
vagrant up

// ssh conf setting
// ssh v_dev

// local
// chef installed check tool
vagrant plugin install vagrant-omnibus
// gem の 様なtool
vagrant plugin install vagrant-berkshelf
gem i berkshelf

cd ~/v_devlop
vim Berksfile
mkdir site-cookbooks
cd site-cookbooks
berks cookbook hello

```

``` ruby:Berkfile
# 参照先指定
site :opscode
# cookbookを指定 path = パス指定 github = githubからも可能
cookbook "hello", path: "site-cookbooks/hello"

```

``` shell:settingSsh
Host v_dev
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/undertree/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ServerAliveInterval 60
```

``` ruby:chefAddVagrantfile

  config.omnibus.chef_version = :latest
  config.berkshelf.enabled = true

# chef solo
  config.vm.provision :chef_solo do |chef|
    # cookbook setting
    chef.run_list = [
      "recipe[mysql::server]",
      "recipe[mysql::client]",
      "recipe[apache2]",
      "recipe[apache2::mod_ssl]",
      "recipe[apache2::mod_rewrite]",
      "recipe[git]",
      "recipe[vim]",
      "recipe[php]",
      "recipe[rbenv]"
    ]

	# attribute setting
    chef.json = {
      :mysql => {
        :version => "5.5",
        :server_root_password => "dokomodake1",
        :server_debian_password => "dokomodake1",
        :server_repl_password => "dokomodake1",
        :bind_address => "127.0.0.1"
      },
      :apache2 => {
        :version => "2.2"
      },
      :php => {
        :version => "5.3",
        :package => ["php-mbstring", "php-mysql", "php-zip", "php-session", "php-gd"]
      },
      :rbenv => {
        :user_installs => {
          :user => "vagrant",
          :rubies => ["2.0.0-p353"],
          :global => "2.0.0-p353",
        }
      }
    }
  end

```