Skip to content

Instantly share code, notes, and snippets.

@kurbmedia
Created May 4, 2010 04:30
Show Gist options
  • Save kurbmedia/388959 to your computer and use it in GitHub Desktop.
Save kurbmedia/388959 to your computer and use it in GitHub Desktop.
Rails 3 Template
#
# This is a default deploy.rb
# it is included into the Rails 3 Basic Template by default.
#
require 'active_support'
DEPLOY_CONFIG = YAML::load(File.open("config/deploy.yml"))
default_run_options[:pty] = true
set :use_sudo, false
set :scm, :git
set :deploy_via, :remote_cache
set :application, DEPLOY_CONFIG['application']
set :repository, DEPLOY_CONFIG['repository']
DEPLOY_CONFIG['locations'].each_pair do |location, settings|
task "#{location}" do
settings.each_pair do |key, value|
set key.to_sym, value
end
role :app, domain
role :web, domain
role :db, domain, :primary => true
end
end
# Symlink configuration files into the shared folder
after "deploy:update_code","deploy:symlink_configs"
# Bundle the new release into /vendor
after 'deploy:update_code', 'bundler:bundle_new_release'
# Make sure our releases don't get out of control.
after 'deploy:restart', 'deploy:cleanup'
namespace :deploy do
desc "Restarting passenger"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
# Symlink our database config and any other files in the "configs" list in deploy.yml
task :symlink_configs, :roles => :app, :except => {:no_symlink => true} do
configs = []
configurations = DEPLOY_CONFIG['configs'].split(",")
unless configurations.empty?
configurations.each{ |file| configs << "ln -nfs #{shared_path}/config/#{file} #{release_path}/config/#{file}" }
run "cd #{release_path} && #{configs.join(" && ")} && ln -nfs #{shared_path}/system #{release_path}/public/system"
end
run "cd #{release_path} && ln -nfs #{shared_path}/caches #{release_path}/tmp/cache"
end
end
##############################
# Bundler related tasks
##############################
desc "Install bundler, and bundle new releases."
namespace :bundler do
task :install do
run("gem install bundler --source=http://gemcutter.org")
end
task :bundle_new_release do
unless DEPLOY_CONFIG['bundler_commands']
bundler_commands = DEPLOY_CONFIG['locations'].keys.delete_if{ |k| k.eql?("#{rails_env}") }.concat(['development','test']).uniq
bundler_commands.concat(bundle_without.split(',')) if defined?(bundle_without) && !bundle_without.nil?
bundler_commands = "--without=#{bundler_commands.join(' ')}"
else
bundler_commands = DEPLOY_CONFIG['bundler_commands']
end
# Use system gems if specified in deploy.yml, otherwise use vendor/bundle
unless DEPLOY_CONFIG['use_system_gems']
run "mkdir -p #{shared_path}/gems"
run "cd #{release_path} && ln -nfs #{shared_path}/gems #{release_path}/vendor/bundle"
run "cd #{release_path} && bundle install vendor/bundle --deployment #{bundler_commands}"
else
run "cd #{release_path} && bundle install --deployment --system #{bundler_commands}"
end
end
desc 'Remove the bundled gems (packaged) so they can be reset.'
task :reset, :roles => :app do
run("cd #{shared_path}/gems && rm -rf ruby")
end
end
##############################
# Functionality to tail/view logs
##############################
desc "Tail logs"
task :monitor, :roles => :app do
run "tail -f #{shared_path}/log/#{rails_env}.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:server]} -> #{data}"
break if stream == :err
end
end
###############################
# SCP configuration files
###############################
desc "Write config files"
task :configure, :roles => :app do
if DEPLOY_CONFIG['configs'].nil?
puts " No configuration files found... exiting."
exit
end
configs = DEPLOY_CONFIG['configs'].split(",")
exit if configs.empty? or configs.nil?
run "mkdir -p #{shared_path}/config"
configs.each do |file|
data = YAML::load(File::open("config/#{file}"))
run "rm #{shared_path}/config/#{file}" if capture("if [ -e #{shared_path}/config/#{file} ]; then echo 'true'; fi").strip.eql?('true')
put YAML::dump(data), "#{shared_path}/config/#{file}"
end
end
###############################
# Bluepill Management
###############################
#after "deploy:update", "bluepill:quit", "bluepill:start"
namespace :bluepill do
desc "Stop processes that bluepill is monitoring and quit bluepill"
task :quit, :roles => [:app] do
sudo "bluepill stop"
sudo "bluepill quit"
sleep 5
end
desc "Load bluepill configuration and start it"
task :start, :roles => [:app] do
sudo "bluepill load #{deploy_to}/current/config/bluepill/#{rails_env}.pill"
end
desc "Prints bluepills monitored processes statuses"
task :status, :roles => [:app] do
sudo "bluepill status"
end
end
application: ""
repository: ""
configs: "database.yml"
use_system_gems: false
defaults: &defaults
keep_releases: 3
branch: "master"
locations:
staging:
<<: *defaults
server: ""
domain: ""
deploy_to: ""
user: ""
port: 22
rails_env: "staging"
production:
<<: *defaults
server: ""
domain: ""
branch: "production"
deploy_to: ""
user: ""
port: 22
rails_env: "production"
#
# This template allows adding gems at project setup.
# -- Allows you to add gems at project creation
# -- Removes prototype and installs rails.js for jquery.
# -- Downloads the Blueprint ie and print.css files, and installs omnifroodle's Blueprint.less into app/stylesheets
#
# Its recommended to use our TextMate LESS bundle for autosaving your less files if you are using this template:
# http://github.com/kurbmedia/less.tmbundle
#
# Add copyright
run "echo ©#{Time.now.year} > README"
# Remove files we don't care about
['public/javascripts/controls.js',
'public/javascripts/dragdrop.js',
'public/javascripts/effects.js',
'public/javascripts/prototype.js',
'public/images/rails.png',
'public/javascripts/rails.js',
'config/database.yml.example',
'public/index.html',
'public/favicon.ico',
'public/images/rails.html',
'Gemfile'
].each do |f|
run("rm #{f}")
end
# Remove useless temporary directories
["./tmp/pids", "./tmp/sessions", "./tmp/sockets", "./tmp/cache"].each do |f|
run("rmdir ./#{f}")
end
# Setup rvm
rvm_ruby = ask('RVM Ruby Install?: ')
rvm_gemset = ask("Gemset? (leave blank to use '#{@app_name}'): ")
rvm_gemset = @app_name if rvm_gemset.blank?
run("echo 'rvm #{rvm_ruby}@#{rvm_gemset} --create' > .rvmrc")
# Add Capistrano
capify!
# Ask to use active_record
if no?('Use ActiveRecord? (yes / no)')
gsub_file 'config/application.rb', /(require \'rails\/all\')/, <<-EOF
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
EOF
end
# Load models from sub-folders under app/models
gsub_file 'config/application.rb', '# config.autoload_paths += %W(#{config.root}/extras)', 'config.autoload_paths += Dir["#{Rails.root}/app/models/**/**"]'
# Allow for gem installation
# Create a new Gemfile
file 'Gemfile', <<-EOF
source 'http://rubygems.org'
EOF
gem "rails", "#{Rails::VERSION::STRING}"
if yes?('Manage gems now? (yes / no)')
include_gem = ask("Add gem (leave blank to stop adding gems): ")
until include_gem.blank? or include_gem == ""
gem "#{include_gem}"
include_gem = ask("Add gem (leave blank to stop adding gems): ")
end
end
# Add mongrel support in development
append_file 'Gemfile', <<-EOF
group :development do
gem 'mongrel'
end
EOF
# Add our default deploy.rb and deploy.yml
inside 'config' do
run 'rm deploy.rb'
run 'rm deploy.yml'
run 'curl -L http://gist.github.com/raw/388959/Default%20Deploy.rb > deploy.rb'
run 'curl -L http://gist.github.com/raw/388959/Default%20Deploy.yml > deploy.yml'
end
# Install rails.js for jQuery
run 'curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js > public/javascripts/rails.js'
# Add our Helpful plugin.
plugin 'helpful', :git => 'git://github.com/kurbmedia/helpful.git'
# Make a .gitignore
run 'rm .gitignore'
file '.gitignore', <<-EOF
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
public/system/*
public/uploads/*
EOF
# Init git repository
git :init
git :add => '.gitignore'
git :commit => "-m 'Add gitignore'"
git :add => '.'
git :commit => "-m 'Initialize repository / Create Project'"
#
# This template allows adding gems at project setup.
# -- Allows you to add gems at project creation
# -- Removes prototype and installs rails.js for jquery.
# -- Downloads the Blueprint ie and print.css files, and installs omnifroodle's Blueprint.less into app/stylesheets
#
# Its recommended to use our TextMate LESS bundle for autosaving your less files if you are using this template:
# http://github.com/kurbmedia/less.tmbundle
#
# Add copyright
run "echo ©#{Time.now.year} > README"
# Remove files we don't care about
['public/javascripts/controls.js',
'public/javascripts/dragdrop.js',
'public/javascripts/effects.js',
'public/javascripts/prototype.js',
'public/images/rails.png',
'public/javascripts/rails.js',
'config/database.yml.example',
'public/index.html',
'public/favicon.ico',
'public/images/rails.html',
'Gemfile'
].each do |f|
run("rm #{f}")
end
# Remove useless temporary directories
["./tmp/pids", "./tmp/sessions", "./tmp/sockets", "./tmp/cache"].each do |f|
run("rmdir ./#{f}")
end
# Add Capistrano
capify!
# Ask to use active_record
if no?('Use ActiveRecord? (yes / no)')
gsub_file 'config/application.rb', /(require \'rails\/all\')/, <<-EOF
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
EOF
end
# Load models from sub-folders under app/models
gsub_file 'config/application.rb', '# config.autoload_paths += %W(#{config.root}/extras)', 'config.autoload_paths += Dir["#{Rails.root}/app/models/**/**"]'
# Allow for gem installation
# Create a new Gemfile
file 'Gemfile', <<-EOF
source 'http://rubygems.org'
EOF
gem "rails", "#{Rails::VERSION::STRING}"
if yes?('Manage gems now? (yes / no)')
include_gem = ask("Add gem (leave blank to stop adding gems): ")
until include_gem.blank? or include_gem == ""
gem "#{include_gem}"
include_gem = ask("Add gem (leave blank to stop adding gems): ")
end
end
# Add mongrel support in development
append_file 'Gemfile', <<-EOF
group :development do
gem 'mongrel'
end
EOF
# Add our default deploy.rb and deploy.yml
inside 'config' do
run 'rm deploy.rb'
run 'rm deploy.yml'
run 'curl -L http://gist.github.com/raw/388959/Default%20Deploy.rb > deploy.rb'
run 'curl -L http://gist.github.com/raw/388959/Default%20Deploy.yml > deploy.yml'
end
# Load up omnifroodle's Blueprint.less
inside 'app' do
run 'git clone git://github.com/omnifroodle/blueprint.less.git stylesheets'
inside 'stylesheets' do
run 'rm -rf .git && rm README && rm print.less && rm ie.less'
run 'mv screen.less screen.less.tmp && touch screen.less'
run 'echo "// lessc: screen.less ../../public/stylesheets/screen.css" > screen.less'
run 'cat screen.less.tmp >> screen.less && rm screen.less.tmp'
end
end
# Grab the Blueprint ie.css and print.css
run 'curl -L http://github.com/joshuaclayton/blueprint-css/raw/master/blueprint/ie.css > public/stylesheets/ie.css'
run 'curl -L http://github.com/joshuaclayton/blueprint-css/raw/master/blueprint/print.css > public/stylesheets/print.css'
# Install rails.js for jQuery
run 'curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js > public/javascripts/rails.js'
# Add our Helpful plugin.
plugin 'helpful', :git => 'git://github.com/kurbmedia/helpful.git'
# Make a .gitignore
run 'rm .gitignore'
file '.gitignore', <<-EOF
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
public/system/*
public/uploads/*
vendor/bin/*
vendor/bundler/*
vendor/doc/*
vendor/gems/*
vendor/specifications/*
vendor/bundle/*
EOF
# Init git repository
git :init
git :add => '.gitignore'
git :commit => "-m 'Add gitignore'"
git :add => '.'
git :commit => "-m 'Initialize repository / Create Project'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment