-
-
Save osamu/14fa4063e2910c7cc6a5 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
require 'capistrano' | |
require 'aws-sdk' | |
require 'pry' | |
module Capistrano | |
module Ec2tag | |
def self.extend(configuration) | |
configuration.load do | |
Capistrano::Configuration.instance.load do | |
_cset(:access_key_id, ENV['AWS_ACCESS_KEY_ID']) | |
_cset(:secret_access_key, ENV['AWS_SECRET_ACCESS_KEY']) | |
def tag(which, *args) | |
@ec2 ||= AWS::EC2.new({access_key_id: fetch(:aws_access_key_id), secret_access_key: fetch(:aws_secret_access_key)}.merge! fetch(:aws_params, {})) | |
unless @target_instances | |
@target_instances = {} | |
AWS.memoize do | |
@ec2.instances.filter('tag-key', 'deploy').to_a.each do |instance| | |
tag_name = instance.tags.to_h["deploy"] | |
@target_instances[tag_name] ||= [] | |
@target_instances[tag_name] << [ instance.private_ip_address, instance.status] | |
end | |
end | |
end | |
if @target_instances[which] | |
@target_instances[which].each do |ip, status| | |
server ip, *args if status == :running | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
if Capistrano::Configuration.instance | |
Capistrano::Ec2tag.extend(Capistrano::Configuration.instance) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment