Skip to content

Instantly share code, notes, and snippets.

@asux
Last active September 27, 2015 04:38
Show Gist options
  • Save asux/1212880 to your computer and use it in GitHub Desktop.
Save asux/1212880 to your computer and use it in GitHub Desktop.
My typical multistaging deploy.rb for Vlad
require 'bundler/vlad'
require 'vlad/rvm'
set :default_stage, 'production'
set :shared_paths, {
'log' => 'log',
'system' => 'public/system',
'pids' => 'tmp/pids',
'sockets' => 'tmp/sockets',
'cache' => 'tmp/cache',
'bundle' => 'vendor/bundle',
'assets' => 'public/assets',
'uploads' => 'public/uploads'
}
set :symlinks, {
'database.yml' => 'config/database.yml',
}
set :skip_scm, false
set :perm_owner, 'git'
set :perm_group, 'git'
set :user, "git"
set :domain, "#{user}@example.com"
set :application, "my_app"
set :repository, "git@bitbucket.org:asux_team/#{application}.git"
set :application_root, "/var/www/#{application}"
set :ruby_version, "1.9.3"
set :bundle_cmd, "bundle"
set :rake_cmd, "#{bundle_cmd} exec rake"
set(:thin_conf) { "/etc/thin/#{application}.yml" }
set(:thin_user) { perm_owner }
set(:thin_group) { perm_group }
set(:thin_log_file) { "log/thin.#{rails_env}.log" }
set(:thin_pid_file) { "tmp/pids/thin.#{rails_env}.pid" }
set(:thin_socket) { "tmp/sockets/thin.#{rails_env}.sock" }
set :thin_servers, 1
# Don't forget create config/stage.yml file
set :stages, YAML.load(File.read(Rails.root.join('config', 'stages.yml')))
set :deploy_tasks, %w(
vlad:update
vlad:update_symlinks
vlad:rvm:trust:release
vlad:rvm:trust:current
vlad:bundle:install
vlad:migrate
vlad:new_relic
vlad:start_app
vlad:cleanup
)
def colorize(text, color_code)
"#{color_code}#{text}\e[0m"
end
def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end
def say(msg)
puts green(">>> #{msg}")
end
def rake(command)
cmd = "cd #{current_path} && RAILS_ENV=#{rails_env} #{rake_cmd} #{command}"
cmd << " --trace" if ENV['VERBOSE']
run cmd
end
def invoke(task)
Rake::Task[task].invoke
end
namespace :vlad do
desc "Run database migration"
remote_task :migrate_current, roles: :app do
say "Run database migration"
rake "db:migrate"
end
desc "Drop, create and migrate database"
remote_task "db:make", roles: :app do
say "Drop, create and migrate database"
invoke "vlad:stop_app"
rake "db:make"
invoke "vlad:start_app"
end
desc "Dump data from database to YAML (db/base/*.yml)"
remote_task :dump_data, roles: :app do
say "Dump data from database to YAML (db/base/*.yml)"
rake "db:data:dump_dir dir=base"
end
desc "Load data to database from YAML (db/base/*.yml)"
remote_task :load_data, roles: :app do
say "Load data to database from YAML (db/base/*.yml)"
rake "db:data:load_dir dir=base"
end
desc "Notify NewRelic for deployment"
remote_task :new_relic do
say "Notify NewRelic for deployment"
run "cd #{current_release} && #{bundle_cmd} exec newrelic deployments -e #{rails_env} -r #{revision}"
end
namespace :log do
desc "View application log"
remote_task :app, roles: :app do
say "View application log"
run "[ -f #{shared_path}/log/#{rails_env}.log ] && tail -f #{shared_path}/log/#{rails_env}.log || echo 'Log file not exists' >&2"
end
desc "View thin log"
remote_task :thin, roles: :app do
say "View thin log"
run "[ -f #{shared_path}/#{thin_log_file} ] && tail -f #{shared_path}/#{thin_log_file} || echo 'Log file not exists' >&2"
end
end
end
stages.each do |stage, opts|
desc "Set #{stage} stage variables"
task stage do
say "Loaded stage: #{stage}"
set :deploy_to, "#{application_root}/#{stage}"
set :revision, "origin/#{opts[:branch]}"
set :rails_env, stage
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment