Skip to content

Instantly share code, notes, and snippets.

@Leko
Last active August 29, 2015 14:23
Show Gist options
  • Save Leko/dcd421081bdd96b31f3c to your computer and use it in GitHub Desktop.
Save Leko/dcd421081bdd96b31f3c to your computer and use it in GitHub Desktop.
Railsの開発環境。これさえあれば環境が整うVagrantfileを目指して。
# Usage
# git clone [email protected]:/dcd421081bdd96b31f3c.git some-project-name
# cd some-project-name
# rm -rf .git && git init
# vim Vagrantfile # if you need
# vagrant up --provision
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
INTERNAL_RUBY_VERSION = '2.2.2' # Install Ruby version
INTERNAL_RAILS_VERSION = '4.2.1' # Install Ruby on Rails version
EXTERNAL_PORT = 3000 # `http://localhost:3000` on your host OS
INTERNAL_PORT = 3000 # `bundle exec unicorn -p XXXX config/unicorn.rb` on VM
config.vm.box = "centos65_64bit"
config.vm.box_url = "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box"
config.vm.network "private_network", ip: "192.168.33.95"
config.vm.network "forwarded_port", guest: INTERNAL_PORT, host: EXTERNAL_PORT
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
end
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
config.vm.provision "shell", :privileged => false, :inline => <<-SHELL
# --- Update yum packages
sudo yum update -q -y
sudo yum upgrade -q -y
# --- Install PostgreSQL
if which postgres > /dev/null; then
sudo yum install -y postgresql-server postgresql-devel
sudo service postgresql initdb << __CONFIG__
vagrant_development
__CONFIG__
sudo service postgresql start
sudo chkconfig postgresql on
fi
# --- Install rbenv
if ! which rbenv > /dev/null; then
sudo yum install -q -y gcc gcc-c++ make git openssl-devel readline readline-devel
git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
# --- Install ruby-build and Ruby {INTERNAL_RUBY_VERSION}
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install #{INTERNAL_RUBY_VERSION}
rbenv global #{INTERNAL_RUBY_VERSION}
fi
# --- Install ntpdate
sudo yum install -q -y ntpdate
sudo ntpdate ntp.nict.jp
# --- Install Nginx
sudo yum install -q -y nginx
sudo chkconfig --level 35 nginx on
# --- Install JavaScript runtime(Nodejs)
sudo yum install -q -y nodejs
# --- Install gems
if ! which bundle > /dev/null; then
gem install bundler
rbenv rehash
fi
# --- Install Rails {INTERNAL_RAILS_VERSION}
if [ ! -e /vagrant/Gemfile ]; then
cd /vagrant
echo "source 'https://rubygems.org'" > Gemfile
echo "ruby '#{INTERNAL_RUBY_VERSION}'" >> Gemfile
echo "gem 'rails', '#{INTERNAL_RAILS_VERSION}'" >> Gemfile
bundle install -j4 --path=vendor/bundle
bundle exec rails new . -f --database=postgresql
fi
if [ ! -e /vagrant/Procfile ]; then
cd /vagrant
sed -i -e "s/# gem 'unicorn'/gem 'unicorn'/" Gemfile
bundle install -j4 --path=vendor/bundle
echo 'web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb' >> Procfile
cat << __UNICORN__ > config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
__UNICORN__
fi
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment