LoginSignup
16
12

More than 5 years have passed since last update.

Ruby on Rails チュートリアルの第1章、環境構築までをほぼ自動化するシェルスクリプト

Last updated at Posted at 2015-08-17

概要

Ruby on Rails チュートリアルは周回を重ねるたびに理解が深まる。しかし、環境構築に何度も時間を割くのは無駄とは言えないまでも、得策ではない。ということで、Ruby on Rails チュートリアルの第1章、環境構築までをほぼ自動化するシェルスクリプトを書いた。もっとスマートなやり方があるよ、など助言お待ちしています。

続編(Ruby on Rails チュートリアルのユーザー機能作成あたりまでをほぼ自動化するシェルスクリプト)も作った

動作環境

  • Windows/Mac(Windows環境しか動作確認していません)
  • VirtualBoxインストール済
  • Vagrantインストール済

内容

Ruby on Rails チュートリアルの第1章で最初に使用するサーバ環境プラスα、プラスαの内容は以下のとおり

  • タイムゾーンをJSTに変更
  • 秘密トークンを動的に生成
  • rspecのインストール

手順

1. 適当なフォルダを作成・移動し、仮想マシンを立ち上げる

$ vagrant init chef/centos-6.5
$ vagrant up

2. 作成したフォルダ内に次の3つのシェルスクリプトを置く

  • provision.sh
  • rvm.sh
  • ruby_on_rails.sh それぞれのファイルの変数は適当な内容に変更する
provision.sh
#!/bin/sh
yum install -y zlib-devel openssl-devel sqlite-devel libyaml-devel

# シェルスクリプト内で使う変数
UserName="Gitのユーザ名"
UserEmail="Gitのメールアドレス"

# JavaScript Runtime
yum install -y epel-release
yum install -y npm
yum install -y nodejs

# Git
sudo yum install -y git
git config --global user.name ${UserName}
git config --global user.email ${UserEmail}
git config --global alias.co checkout

# Cange the timezone
cp /etc/localtime /etc/localtime.org
ln -sf  /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
echo "ZONE="Asia/Tokyo"" > /etc/sysconfig/clock
rvm.sh
#!/bin/sh

# Install RVM
curl -sSL https://get.rvm.io | bash -s stable
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://get.rvm.io | bash -s stable
source ~/.profile

# Install Ruby
rvm install 2.0.0 --with-openssl-dir=/home/vagrant/.rvm/usr
ruby_on_rails.sh
#!/bin/sh

# シェルスクリプト内で使う変数
ProjectName="プロジェクト名"
AppName="アプリケーション名"

# Ruby on Rails
gem update --system 2.0.3
gem install rails --no-document --version 4.0.5

# Make a Project
mkdir ${ProjectName}
cd ${ProjectName}

# Make a Rails APP
rails new ${AppName}_app
cd ${AppName}_app

# Make a Gemfile
cat <<_EOF_ > Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=${ProjectName}

gem 'rails', '4.0.5'

group :development do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
end

gem 'sass-rails', '4.0.5'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end
_EOF_
bundle update
bundle install
bundle update

# Improve .gitignore
cat <<_EOF_ > .gitignore
# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore other unneeded files.
doc/
*.swp
*~
.project
.DS_Store
.idea
.secret
_EOF_

# Improve a secret_token
cat <<_EOF_ > config/initializers/secret_token.rb
# Be sure to restart your server when you modify this file.

# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!

# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rake secret` to generate a secure secret key.

# Make sure your secret_key_base is kept private
# if you're sharing your code publicly.
require 'securerandom'

def secure_token
  token_file = Rails.root.join('.secret')
  if File.exist?(token_file)
    # Use the existing token.
    File.read(token_file).chomp
  else
    # Generate a new token and store it in token_file.
    token = SecureRandom.hex(64)
    File.write(token_file, token)
    token
  end
end

${AppName^}App::Application.config.secret_key_base = secure_token
_EOF_

# RSpec
rails generate rspec:install

3. vagrantfileを書き換える

vagrantfile
Vagrant.configure(2) do |config|
 config.vm.network "private_network", ip: "192.168.33.10"
 config.vm.synced_folder "../作成したフォルダ名/作成したフォルダ名", "/home/vagrant/作成したフォルダ名"
  config.vm.provision :shell, :path => "provision.sh"
end

4. 仕上げる

  1. $ vagrant reload
  2. $ vagrant provision
  3. ターミナルから仮想マシンに、ID/PW:vagrantでログインする
  4. $ sh /vagrant/rvm.sh
  5. $ source ~/.profile
  6. $ rvm use 2.0.0@${ProjectName} --create --default (${ProjectName}は自分のプロジェクト名に置き換える)
  7. $ sh /vagrant/ruby_on_rails.sh

感想

本当は1つのシェルスクリプトに纏めたかったが、rvm useコマンドの使用は、ターミナルにログインしていることが前提のようなので、泣く泣く分割した

16
12
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12