The Pragmatic Ball boy

iOSを中心にやってる万年球拾いの老害エンジニアメモ

Ruby on Railsの開発環境構築

Using Vagrant for Rails Development - GoRails のを参考に実施

VagrantVirtualBoxをインストール

Install Vagrant

Downloads – Oracle VM VirtualBox

$ vagrant plugin install vagrant-vbguest
$ vagrant plugin install vagrant-librarian-chef
$ cd MY_RAILS_PROJECT # Change this to your Rails project directory
$ vagrant init
$ touch Cheffile
site "http://community.opscode.com/api/v1"

cookbook 'apt'
cookbook 'build-essential'
cookbook 'mysql'
cookbook 'ruby_build'
cookbook 'nodejs', git: 'https://github.com/mdxp/nodejs-cookbook'
cookbook 'rbenv', git: 'https://github.com/fnichol/chef-rbenv'
cookbook 'vim'
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
  config.vm.box = "ubuntu/trusty64"

  # Configurate the virtual machine to use 2GB of RAM
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end

  # Forward the Rails server default port to the host
  config.vm.network :forwarded_port, guest: 3000, host: 3000

  # Use Chef Solo to provision our virtual machine
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["cookbooks", "site-cookbooks"]

    chef.add_recipe "apt"
    chef.add_recipe "nodejs"
    chef.add_recipe "ruby_build"
    chef.add_recipe "rbenv::user"
    chef.add_recipe "rbenv::vagrant"
    chef.add_recipe "vim"
    chef.add_recipe "mysql::server"
    chef.add_recipe "mysql::client"

    # Install Ruby 2.1.2 and Bundler
    # Set an empty root password for MySQL to make things simple
    chef.json = {
      rbenv: {
        user_installs: [{
          user: 'vagrant',
          rubies: ["2.1.2"],
          global: "2.1.2",
          gems: {
            "2.1.2" => [
              { name: "bundler" },
              { name: "rails", version: "4.1.6" }
            ]
          }
        }]
      },
      mysql: {
        server_root_password: ''
      }
    }
  end
end

以下エラー対応

==> default: could not find recipe server for cookbook mysql
==> default:
==> default: [2015-04-04T10:18:45+00:00] ERROR: Running exception handlers
==> default: [2015-04-04T10:18:45+00:00] ERROR: Exception handlers complete
==> default: [2015-04-04T10:18:45+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2015-04-04T10:18:45+00:00] ERROR: could not find recipe server for cookbook mysql
==> default: [2015-04-04T10:18:46+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

Cheffileを以下のように修正

cookbook 'mysql','5.6'
$ vagrant provision
==> default: [2015-04-04T10:48:42+00:00] INFO: Running queued delayed notifications before re-raising exception
==> default: [2015-04-04T10:48:42+00:00] ERROR: Running exception handlers
==> default: [2015-04-04T10:48:42+00:00] ERROR: Exception handlers complete
==> default: [2015-04-04T10:48:42+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2015-04-04T10:48:42+00:00] ERROR: rbenv_gem[2.1.2::bundler (vagrant)] (vagrant) (rbenv::user line 63) had an error: NoMethodError: undefined method `rbenv_rehash' for #<Chef::Provider::Package::RbenvRubygems:0x00000003e77ec8>
==> default: [2015-04-04T10:48:43+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

cookbooks/rbenv/libraries/chef_provider_package_rbenvrubygems.rbを

def rehash
  rbenv_rehash new_resource do
    root_path rbenv_root
    user rbenv_user if rbenv_user
    action :nothing
  end.run_action(:run)
end

以下に編集

def rehash
  e = ::Chef::Resource::RbenvRehash.new(new_resource, @run_context)
  e.root_path rbenv_root
  e.user rbenv_user if rbenv_user
  e.action :nothing
  e.run_action(:run)
end
$ vagrant provision

以上でできたわけですが、chef-rbenvのリポジトリは一年以上放置されていてるようだったので、forkして修正を取り込みました

https://github.com/yanamura3/chef-rbenv