diff --git a/README.md b/README.md index 4d6ef04..c488e77 100644 --- a/README.md +++ b/README.md @@ -72,9 +72,9 @@ If running on EC2 the IAM instance profile credentials will be used if credentia * project_tag - Cap-EC2 will look for a tag with this name when searching for instances that belong to this project. - Cap-EC2 will look for a value which matches the :application setting in your deploy.rb. - The tag name defaults to "Project" and must be set on your instances. + Cap-EC2 will look for a tag with this name when searching for instances that belong to this project. + Cap-EC2 will look for a value which matches the :application setting in your deploy.rb. The tag name + defaults to "Project". If this tag is not set on the instances, then use `set :ec2_project_tag, nil`. * stages_tag diff --git a/lib/cap-ec2/ec2-handler.rb b/lib/cap-ec2/ec2-handler.rb index 2d54887..9224a7a 100644 --- a/lib/cap-ec2/ec2-handler.rb +++ b/lib/cap-ec2/ec2-handler.rb @@ -59,19 +59,20 @@ def tag(tag_name) end def get_servers_for_role(role) - servers = [] - @ec2.each do |_, ec2| - instances = ec2.instances - .filter(tag(project_tag), "*#{application}*") - .filter('instance-state-name', 'running') - servers << instances.select do |i| + @ec2.map do |_, ec2| + instances = project_tag ? + ec2.instances.filter(tag(project_tag), "*#{application}*") : + ec2.instances + filtered = instances.filter('instance-state-name', 'running') + + filtered.select do |i| instance_has_tag?(i, roles_tag, role) && instance_has_tag?(i, stages_tag, stage) && - instance_has_tag?(i, project_tag, application) && + (project_tag.nil? || instance_has_tag?(i, project_tag, application)) && (fetch(:ec2_filter_by_status_ok?) ? instance_status_ok?(i) : true) - end + + end.flatten.sort_by { |s| s.tage["Name"] || ''} end - servers.flatten.sort_by {|s| s.tags["Name"] || ''} end def get_server(instance_id)