Skip to content

Instantly share code, notes, and snippets.

@csouls
Created July 19, 2013 08:00
Show Gist options
  • Save csouls/6037486 to your computer and use it in GitHub Desktop.
Save csouls/6037486 to your computer and use it in GitHub Desktop.
AWSの"Role"タグからデプロイ対象のサーバを取得
# config/deploy.rb
def tagged_servers(tag_key, tag_value, default=[])
@ec2 ||= AWS::EC2.new(ec2_endpoint: 'ec2.ap-northeast-1.amazonaws.com')
ret = @ec2.instances.map do |instance|
next if instance.tags[tag_key] != tag_value
next if instance.status != :running
instance.dns_name.empty? ? instance.ip_address : instance.dns_name
end.compact
return default if ret.empty?
ret
end
def tag(tag_value, *args)
AWS.memoize {
tagged_servers(tag_key, tag_value).each do |host|
server(host, *args)
end
}
end
# config/deploy/environment.rb
tag 'app', :web, :app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment