class CapEC2::EC2Handler
Public Class Methods
new()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 7 def initialize load_config configured_regions = get_regions(fetch(:ec2_region)) @ec2 = {} configured_regions.each do |region| @ec2[region] = ec2_connect(region) end end
Public Instance Methods
application()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 54 def application Capistrano::Configuration.env.fetch(:application).to_s end
defined_roles()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 46 def defined_roles roles(:all).flat_map(&:roles_array).uniq.sort end
ec2_connect(region=nil)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 16 def ec2_connect(region=nil) Aws::EC2::Client.new( access_key_id: fetch(:ec2_access_key_id), secret_access_key: fetch(:ec2_secret_access_key), region: region ) end
get_server(instance_id)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 84 def get_server(instance_id) @ec2.reduce([]) do |acc, (_, ec2)| acc << ec2.instances[instance_id] end.flatten.first end
get_servers_for_role(role)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 62 def get_servers_for_role(role) filters = [ {name: 'tag-key', values: [stages_tag, project_tag]}, {name: tag(project_tag), values: ["*#{application}*"]}, {name: 'instance-state-name', values: %w(running)} ] servers = [] @ec2.each do |_, ec2| ec2.describe_instances(filters: filters).reservations.each do |r| servers += r.instances.select do |i| instance_has_tag?(i, roles_tag, role) && instance_has_tag?(i, stages_tag, stage) && instance_has_tag?(i, project_tag, application) && (fetch(:ec2_filter_by_status_ok?) ? instance_status_ok?(i) : true) end end end servers.sort_by { |s| tag_value(s, 'Name') || '' } end
instance_ids()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 38 def instance_ids puts defined_roles.map {|r| get_servers_for_role(r)} .flatten .uniq {|i| i.instance_id} .map {|i| i.instance_id} .join("\n") end
server_names()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 30 def server_names puts defined_roles.map {|r| get_servers_for_role(r)} .flatten .uniq {|i| i.instance_id} .map {|i| tag_value(i, 'Name')} .join("\n") end
stage()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 50 def stage Capistrano::Configuration.env.fetch(:stage).to_s end
status_table()
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 24 def status_table CapEC2::StatusTable.new( defined_roles.map {|r| get_servers_for_role(r)}.flatten.uniq {|i| i.instance_id} ) end
tag(tag_name)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 58 def tag(tag_name) "tag:#{tag_name}" end
Private Instance Methods
instance_has_tag?(instance, key, value)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 92 def instance_has_tag?(instance, key, value) (tag_value(instance, key) || '').split(tag_delimiter).map(&:strip).include?(value.to_s) end
instance_status_ok?(instance)
click to toggle source
# File lib/cap-ec2/ec2-handler.rb, line 96 def instance_status_ok?(instance) @ec2.any? do |_, ec2| ec2.describe_instance_status( instance_ids: [instance.instance_id], filters: [{ name: 'instance-status.status', values: %w(ok) }] ).instance_statuses.length == 1 end end