Skip to content

Instantly share code, notes, and snippets.

@naga41
Created February 27, 2013 14:10
Show Gist options
  • Save naga41/5048152 to your computer and use it in GitHub Desktop.
Save naga41/5048152 to your computer and use it in GitHub Desktop.
deploy.rb for instances under ELB
require "bundler/capistrano"
require "capistrano_colors"
require "aws-sdk"
set :ssh_options, {:keys => ["YOUR_SSH_KEY"], :auth_methods => ["publickey"]}
default_run_options[:pty] = true
set :application, "YOUR_APPLICATION"
set :repository, "YOUR_REPOSITORY"
set :branch, "master"
set :user, 'jenkins'
set :use_sudo, false
set :scm, :git
set :scm_verbose, true
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :copy
###
# Set roles with ELB instances
###
set :elb_name, "YOUR_ELB_NAME"
AWS.config({
:access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
:ec2_endpoint => "ec2.ap-northeast-1.amazonaws.com",
:elb_endpoint => "elasticloadbalancing.ap-northeast-1.amazonaws.com"
})
elb = AWS::ELB.new
lb_instances = elb.load_balancers[elb_name].instances
instance_ips = lb_instances.map { |instance| instance.private_ip_address }
role(:web) { instance_ips }
role(:app) { instance_ips }
role(:db, :primary => true) { instance_ips.first }
namespace :deploy do
task :start, :roles => :app do
run "cd #{current_path} && BUNDLE_GEMFILE=#{File.join(current_path, "Gemfile")} bundle exec unicorn_rails -D -E production -c config/unicorn.rb"
end
task :stop, :roles => :app do
run "kill -s QUIT `cat #{shared_path}/pids/unicorn.pid`"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "kill -s USR2 `cat #{shared_path}/pids/unicorn.pid`"
end
task :full_restart, :roles => :app, :except => { :no_release => true } do
stop
start
end
end
## test tasks
task :test_web, :roles => :web do
run "echo 'Hi!'"
end
task :test_app, :roles => :app do
run "echo 'Hi!'"
end
task :test_db, :roles => :db do
run "echo 'Hi!'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment