Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created September 19, 2015 17:23
Show Gist options
  • Save tomykaira/e95d6b0c6318885b0bda to your computer and use it in GitHub Desktop.
Save tomykaira/e95d6b0c6318885b0bda to your computer and use it in GitHub Desktop.
Capistrano config helper to retrieve automatically retrieve running instances to deploy
# deploy.rb
def retrieve_instances(name_prefix)
require 'aws-sdk'
next_token = nil
dns_list = []
begin
resp = Aws::EC2::Client.new.describe_instances(
filters: [{name: "tag:private:service", values: ['my-service']}],
next_token: next_token)
resp.reservations.each do |r|
r.instances.each do |i|
if i.tags.find { |t| t.key == 'Name' }.value.start_with?(name_prefix)
dns_list << i.network_interfaces.first.association.public_dns_name
end
end
end
end while (next_token = resp.next_token)
dns_list.sort.map { |d| "app@#{d}" }
end
set :ssh_options, {
forward_agent: true,
auth_methods: %w(publickey)
}.tap { |opt|
if ENV['CAPISTRANO_USE_SSH_PROXY']
require 'net/ssh/proxy/command'
opt[:proxy] = Net::SSH::Proxy::Command.new('ssh -W %h:%p example.com')
end
}
# for each stages
hosts = retrieve_instances('app001')
role :app, hosts
role :web, hosts
role :db, [hosts.first]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment