Class: CapEC2::EC2Handler

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

Instance Method Summary collapse

Constructor Details

#initialize(ec2_config = "config/ec2.yml") ⇒ EC2Handler

Returns a new instance of EC2Handler.



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

def initialize(ec2_config = "config/ec2.yml")
  @ec2_config = YAML.load_file ec2_config
  @ec2 = {}
  @ec2_config["regions"].each do |region|
    @ec2[region] = AWS::EC2.new(
    access_key_id: @ec2_config["access_key_id"],
    secret_access_key: @ec2_config["secret_access_key"],
    region: region
  )
  end
end

Instance Method Details

#applicationObject



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

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

#defined_rolesObject



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

def defined_roles
  Capistrano::Configuration.env.send(:servers).send(:available_roles)
end

#each_regionObject



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

def each_region
  @ec2.keys.each do |region|
    yield @ec2[region]
  end
end

#get_servers_for_role(role) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cap-ec2/ec2-handler.rb', line 66

def get_servers_for_role(role)
  servers = []
  each_region do |ec2|
    instances = ec2.instances
      .filter(tag(project_tag), application)
    servers << instances.select do |i| 
      i.tags[roles_tag] =~ /,{0,1}#{role}(,|$)/ && i.tags[stages_tag] =~ /,{0,1}#{stage}(,|$)/
    end
  end
  servers.flatten
end

#instance_idsObject



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

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

#project_tagObject



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

def project_tag
  @ec2_config["project_tag"] || "Project"
end

#roles_tagObject



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

def roles_tag
  @ec2_config["roles_tag"] || "Roles"
end

#server_namesObject



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

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



42
43
44
# File 'lib/cap-ec2/ec2-handler.rb', line 42

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

#stages_tagObject



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

def stages_tag
  @ec2_config["stages_tag"] || "Stages"
end

#status_tableObject



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

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



62
63
64
# File 'lib/cap-ec2/ec2-handler.rb', line 62

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