Created
September 17, 2012 22:36
-
-
Save rottmanj/3740212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load 'deploy' | |
# ================================================================ | |
# ROLES | |
# ================================================================ | |
role :app, "ec2-81-243-196-157.compute-1.amazonaws.com", {:primary=>true} | |
role :background, "ec2-81-243-205-119.compute-1.amazonaws.com" | |
# ================================================================ | |
# VARIABLES | |
# ================================================================ | |
# Webistrano defaults | |
set :webistrano_project, "sweet_high" | |
set :webistrano_stage, "staging" | |
set :application, "unicorn_test" | |
set :bundle_disable_shared_gems, "\"1\"" | |
set :bundle_path, "vendor/bundler_gems" | |
set :certificate_name, "test_key" | |
set :default_shell, "bash --login" | |
set :deploy_to, "/data/\#{application}/\#{webistrano_stage}" | |
set :deploy_via, :remote_cache | |
set :domain, "staging.myapp.com" | |
set :environment, "staging" | |
set :group, "ubuntu" | |
set :keep_releases, "2" | |
set :nginx_bind_options, "default deferred" | |
set :nginx_conf_path, "/etc/nginx" | |
set :nginx_worker_count, "16" | |
set :password, "deployment_user(SSH user) password" | |
set :precompile_assets, true | |
set :rails_env, "staging" | |
set :repository, "git@github.com:XXXXXXXXXXX/XXXXXXXXXX.git" | |
set :runner, "ubuntu" | |
set :rvm_bin_path, "/home/ubuntu/.rvm/bin/rvm" | |
set :rvm_type, :user | |
set :scm, :git | |
set :scm_password, "your_SVN_password" | |
set :scm_username, "your_SVN_user" | |
set :ssh_options, {:forward_agent => true,:paranoid => false,:keys => ["/home/#{user}/.ssh/unicorn_test.pem"]} | |
set :unicorn_config, "\#{shared_path}/config/unicorn.rb" | |
set :unicorn_worker_count, "16" | |
set :use_sudo, true | |
set :user, "ubuntu" | |
set(:branch) do | |
Capistrano::CLI.ui.ask "Please enter 'branch': " | |
end | |
# ================================================================ | |
# TEMPLATE TASKS | |
# ================================================================ | |
# allocate a pty by default as some systems have problems without | |
default_run_options[:pty] = true | |
# set Net::SSH ssh options through normal variables | |
# at the moment only one SSH key is supported as arrays are not | |
# parsed correctly by Webistrano::Deployer.type_cast (they end up as strings) | |
[:ssh_port, :ssh_keys].each do |ssh_opt| | |
if exists? ssh_opt | |
logger.important("SSH options: setting #{ssh_opt} to: #{fetch(ssh_opt)}") | |
ssh_options[ssh_opt.to_s.gsub(/ssh_/, '').to_sym] = fetch(ssh_opt) | |
end | |
end | |
# ================================================================ | |
# CUSTOM RECIPES | |
# ================================================================ | |
before "deploy:setup", :init_config | |
after "deploy:setup", :setup_cleanup | |
namespace :init_config do | |
desc <<-DESC | |
Created directory structure, changes ownership, etc... | |
DESC | |
task :default do | |
run "sudo chown -R #{user}:#{group} /home/#{user}/.rvm" | |
run "gem install bundler -no-ri -no-rdoc" | |
run "sudo mkdir -p #{deploy_to}" | |
run "sudo chown -R #{user}:#{group} #{deploy_to}" | |
run "mkdir -p #{shared_path}/config" | |
run "mkdir -p #{shared_path}/certs" | |
run "mkdir -p #{shared_path}/sockets" | |
end | |
end | |
namespace :setup_cleanup do | |
task :default do | |
run "sudo chown -R #{user}:#{group} #{deploy_to}" | |
end | |
end | |
def remote_file_exists?(full_path) | |
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip | |
end | |
namespace :deploy do | |
task :restart, :roles=>:app do | |
run "sudo service nginx restart" | |
end | |
task :start, :roles=>:app do | |
run "sudo service nginx start" | |
end | |
task :stop, :roles=>:app do | |
run "sudo service nginx stop" | |
end | |
task :migrations, :roles=>:app, :only => {:primary => true} do | |
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{environment} db:migrate" | |
end | |
task :migrate, :roles=>:app, :only => {:primary => true} do | |
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{environment} db:migrate" | |
end | |
end | |
def get_binding | |
binding # So that everything can be used in templates generated for the servers | |
end | |
def from_template(file) | |
require 'erb' | |
template = File.read(File.join(RAILS_ROOT, "/", file)) | |
result = ERB.new(template).result(self.get_binding) | |
end | |
after "deploy:setup", :nginx | |
namespace :nginx do | |
desc "Create nginx.conf and generates nginx.conf symlink" | |
task :default, :roles => :app do | |
# CREATES THE BASE NGINX CONFIG | |
config_content = from_template("config/templates/nginx.conf.erb") | |
put config_content, "#{shared_path}/config/nginx.conf" | |
run "sudo ln -nfs #{shared_path}/config/nginx.conf #{nginx_conf_path}" | |
# CREATES THE VHOST NGINX CONFIG | |
config_content = from_template("config/templates/vhost.erb") | |
put config_content, "#{shared_path}/config/#{webistrano_stage}" | |
run "sudo ln -nfs #{shared_path}/config/#{webistrano_stage} #{nginx_conf_path}/sites-enabled/#{webistrano_stage}" | |
#upload the cert file | |
upload("#{RAILS_ROOT}/config/certificates/#{certificate_name}.crt", "#{shared_path}/certs/#{certificate_name}.crt") | |
#upload the key file | |
upload("#{RAILS_ROOT}/config/certificates/#{certificate_name}.key", "#{shared_path}/certs/#{certificate_name}.key") | |
run "chmod 600 #{shared_path}/certs/#{certificate_name}.key" | |
end | |
end | |
after "deploy:rollback:revision", "post_deploy:bundle" | |
after "deploy:update_code", "post_deploy:bundle" | |
after "post_deploy:bundle", "post_deploy:assets:precompile" | |
after "post_deploy:assets:precompile", "post_deploy:assets:upload" | |
#after "post_deploy:bundle", "post_deploy:solr" | |
namespace :post_deploy do | |
namespace :assets do | |
task :precompile, :roles => :app, :except => { :no_release => true } do | |
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{environment} assets:precompile --trace" | |
end | |
task :upload, :roles => :app, :except => { :no_release => true } do | |
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{environment} assets:upload --trace" | |
end | |
end | |
namespace :solr do | |
task :default, :roles=>:background do | |
run "cd #{release_path} && rake RAILS_ENV=#{environment} sunspot:solr:start --trace" | |
end | |
end | |
namespace :bundle do | |
task :default, :roles=>[:app, :background] do | |
run "cd #{release_path} && mkdir -p .bundler" | |
run "cd #{release_path}/.bundler && touch config" | |
run "echo BUNDLE_PATH: #{bundle_path} >> #{release_path}/.bundler/config" | |
run "echo BUNDLE_DISABLE_SHARED_GEMS: #{bundle_disable_shared_gems} >> #{release_path}/.bundler/config" | |
run "cd #{release_path} && bundle install --binstubs" | |
on_rollback do | |
if previous_release | |
run "cd #{previous_release} && bundle install --binstubs" | |
else | |
logger.important "no previous release to rollback to, rollback of bundler:install skipped" | |
end | |
end | |
end | |
end | |
end | |
after "deploy:setup", "unicorn:setup" | |
namespace :unicorn do | |
task :setup, :roles=>:app do | |
config_content = from_template("config/templates/unicorn.rb.erb") | |
put config_content, "#{shared_path}/config/unicorn.rb" | |
config_content = from_template("config/templates/unicorn_init.sh.erb") | |
put config_content, "#{shared_path}/config/unicorn_init.sh" | |
run "chmod +x #{shared_path}/config/unicorn_init.sh" | |
end | |
%w[start stop restart].each do |command| | |
desc "#{command} unicorn" | |
task command, roles: :app do | |
run "sh #{shared_path}/config/unicorn_init.sh #{command}" | |
end | |
after "deploy:#{command}", "unicorn:#{command}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment