Class: CapEC2::EC2Handler
- Inherits:
-
Object
- Object
- CapEC2::EC2Handler
- Defined in:
- lib/cap-ec2/ec2-handler.rb
Instance Method Summary collapse
- #application ⇒ Object
- #defined_roles ⇒ Object
- #each_region ⇒ Object
- #get_servers_for_role(role) ⇒ Object
-
#initialize(ec2_config = "config/ec2.yml") ⇒ EC2Handler
constructor
A new instance of EC2Handler.
- #instance_ids ⇒ Object
- #project_tag ⇒ Object
- #roles_tag ⇒ Object
- #server_names ⇒ Object
- #stage ⇒ Object
- #stages_tag ⇒ Object
- #status_table ⇒ Object
- #tag(tag_name) ⇒ Object
Constructor Details
#initialize(ec2_config = "config/ec2.yml") ⇒ 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
#application ⇒ Object
46 47 48 |
# File 'lib/cap-ec2/ec2-handler.rb', line 46 def application Capistrano::Configuration.env.fetch(:application).to_s end |
#defined_roles ⇒ Object
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_region ⇒ Object
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.[roles_tag] =~ /,{0,1}#{role}(,|$)/ && i.[stages_tag] =~ /,{0,1}#{stage}(,|$)/ end end servers.flatten end |
#instance_ids ⇒ Object
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_tag ⇒ Object
50 51 52 |
# File 'lib/cap-ec2/ec2-handler.rb', line 50 def project_tag @ec2_config["project_tag"] || "Project" end |
#roles_tag ⇒ Object
54 55 56 |
# File 'lib/cap-ec2/ec2-handler.rb', line 54 def roles_tag @ec2_config["roles_tag"] || "Roles" end |
#server_names ⇒ Object
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.["Name"]} .join("\n") end |
#stage ⇒ Object
42 43 44 |
# File 'lib/cap-ec2/ec2-handler.rb', line 42 def stage Capistrano::Configuration.env.fetch(:stage).to_s end |
#stages_tag ⇒ Object
58 59 60 |
# File 'lib/cap-ec2/ec2-handler.rb', line 58 def stages_tag @ec2_config["stages_tag"] || "Stage" end |
#status_table ⇒ Object
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 |