Class: CapEC2::EC2Handler

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/cap-ec2/ec2-handler.rb

Instance Method Summary collapse

Methods included from Utils

contact_point, contact_point_mapping, #get_regions, #load_config, #project_tag, #roles_tag, #stages_tag

Constructor Details

#initializeEC2Handler

Returns a new instance of EC2Handler.



5
6
7
8
9
10
11
12
# File 'lib/cap-ec2/ec2-handler.rb', line 5

def initialize
  load_config
  configured_regions = get_regions(fetch(:ec2_region))
  @ec2 = {}
  configured_regions.each do |region|
    @ec2[region] = ec2_connect(region)
  end
end

Instance Method Details

#applicationObject



53
54
55
# File 'lib/cap-ec2/ec2-handler.rb', line 53

def application
  Capistrano::Configuration.env.fetch(:application).to_s
end

#defined_rolesObject



45
46
47
# File 'lib/cap-ec2/ec2-handler.rb', line 45

def defined_roles
  roles(:all).flat_map(&:roles_array).uniq.sort
end

#ec2_connect(region = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/cap-ec2/ec2-handler.rb', line 14

def ec2_connect(region=nil)
  AWS.start_memoizing
  AWS::EC2.new(
    access_key_id: fetch(:ec2_access_key_id),
    secret_access_key: fetch(:ec2_secret_access_key),
    region: region
  )
end

#get_server(instance_id) ⇒ Object



77
78
79
80
81
# File 'lib/cap-ec2/ec2-handler.rb', line 77

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

#get_servers_for_role(role) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cap-ec2/ec2-handler.rb', line 61

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|
      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
  servers.flatten.sort_by {|s| s.tags["Name"] || ''}
end

#instance_idsObject



37
38
39
40
41
42
43
# File 'lib/cap-ec2/ec2-handler.rb', line 37

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_namesObject



29
30
31
32
33
34
35
# File 'lib/cap-ec2/ec2-handler.rb', line 29

def server_names
  puts defined_roles.map {|r| get_servers_for_role(r)}
               .flatten
               .uniq {|i| i.instance_id}
               .map {|i| i.tags["Name"]}
               .join("\n")
end

#stageObject



49
50
51
# File 'lib/cap-ec2/ec2-handler.rb', line 49

def stage
  Capistrano::Configuration.env.fetch(:stage).to_s
end

#status_tableObject



23
24
25
26
27
# File 'lib/cap-ec2/ec2-handler.rb', line 23

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) ⇒ Object



57
58
59
# File 'lib/cap-ec2/ec2-handler.rb', line 57

def tag(tag_name)
  "tag:#{tag_name}"
end