Skip to content

Commit

Permalink
Merge pull request forward3d#29 from Futurelearn/access-server-tags
Browse files Browse the repository at this point in the history
Provide access to EC2 server tags in Capistrano recipes
  • Loading branch information
andytinycat committed Sep 22, 2014
2 parents 56dd1b4 + 80844aa commit fa6e3e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/cap-ec2/capistrano.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ def ec2_handler

def ec2_role(name, options={})
ec2_handler.get_servers_for_role(name).each do |server|
env.role(name, CapEC2::Utils.contact_point(server), options)
env.role(name, CapEC2::Utils.contact_point(server),
options_with_instance_id(options, server))
end
end

def env
Configuration.env
end

private

def options_with_instance_id(options, server)
options.merge({aws_instance_id: server.instance_id})
end

end
end
end

self.extend Capistrano::DSL::Ec2

Capistrano::Configuration::Server.send(:include, CapEC2::Utils::Server)
6 changes: 6 additions & 0 deletions lib/cap-ec2/ec2-handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def get_servers_for_role(role)
servers.flatten.sort_by {|s| s.tags["Name"] || ''}
end

def get_server(instance_id)
@ec2.reduce([]) do |acc, (_, ec2)|
acc << ec2.instances[instance_id]
end.flatten.first
end

private

def instance_has_tag?(instance, key, value)
Expand Down
7 changes: 7 additions & 0 deletions lib/cap-ec2/utils.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module CapEC2
module Utils

module Server
def ec2_tags
id = self.properties.fetch(:aws_instance_id)
ec2_handler.get_server(id).tags
end
end

def project_tag
fetch(:ec2_project_tag)
end
Expand Down

0 comments on commit fa6e3e6

Please sign in to comment.