Class: CapEC2::EC2Handler
- Inherits:
-
Object
- Object
- CapEC2::EC2Handler
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, #tag_delimiter, #tag_value
Constructor Details
Returns a new instance of EC2Handler.
7
8
9
10
11
12
13
14
|
# 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
|
Instance Method Details
#application ⇒ Object
54
55
56
|
# File 'lib/cap-ec2/ec2-handler.rb', line 54
def application
Capistrano::Configuration.env.fetch(:application).to_s
end
|
#defined_roles ⇒ Object
46
47
48
|
# 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) ⇒ Object
16
17
18
19
20
21
22
|
# 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) ⇒ Object
84
85
86
87
88
|
# 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) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# 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 ⇒ Object
38
39
40
41
42
43
44
|
# 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 ⇒ Object
30
31
32
33
34
35
36
|
# 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 ⇒ Object
50
51
52
|
# File 'lib/cap-ec2/ec2-handler.rb', line 50
def stage
Capistrano::Configuration.env.fetch(:stage).to_s
end
|
#status_table ⇒ Object
24
25
26
27
28
|
# 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) ⇒ Object
58
59
60
|
# File 'lib/cap-ec2/ec2-handler.rb', line 58
def tag(tag_name)
"tag:#{tag_name}"
end
|