Skip to content

Instantly share code, notes, and snippets.

@rlister
Created August 21, 2015 17:27
Show Gist options
  • Save rlister/50c216cad73d9f41579c to your computer and use it in GitHub Desktop.
Save rlister/50c216cad73d9f41579c to your computer and use it in GitHub Desktop.
creates capistrano autoscaling_servers command to get instance list from AWS autoscaling groups
require 'aws-sdk-v1'
require 'capistrano/framework'
@autoscaling_groups = []
def autoscaling_servers(opts = {})
Array(opts[:regions]).each do |region|
AWS::AutoScaling.new(region: region).groups[opts[:group]].tap do |group|
@autoscaling_groups.push(group)
end.ec2_instances.each do |instance|
if instance.status == :running
server instance.ip_address, roles: opts[:roles], user: opts[:user]
end
end
end
end
def load_balancers(opts = {})
Array(opts[:regions]).map do |region|
AWS::AutoScaling.new(region: region).groups[opts[:group]].load_balancer_names
end
end
namespace :aws do
desc 'ssh and run command on servers, e.g. cap staging "aws:servers[hostname]"'
task :ssh, :cmd do |task, args|
on roles(:all) do |server|
execute args[:cmd]
end
end
desc 'List server IPs'
task :servers do
on roles(:all) do |server|
puts "#{server}\n"
end
end
desc 'List load-balancers and instance health'
task :elb do
@autoscaling_groups.map(&:load_balancers).flatten.each do |lb|
puts "#{lb.name}:"
lb.instances.health.each do |h|
puts "#{h[:instance].public_ip_address}\t#{h.state}"
end
end
end
end
## helpful aliases
task :elb => 'aws:elb'
task :lb => 'aws:elb'
## example usage
require 'autoscaling'
set :stage, :production
set :application, 'web-prod@*'
set :branch, 'master'
autoscaling_servers group: 'web-prod', regions: %w[us-east-1], user: 'core', roles: %w[app web]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment