Class: Ec2Helper
- Inherits:
-
Object
- Object
- Ec2Helper
- Defined in:
- lib/capawsext/whoec2helper.rb
Instance Method Summary collapse
- #add_tags(resource_id, tags = {}) ⇒ Object
- #get_all_instances ⇒ Object
- #get_all_stacks ⇒ Object
- #get_groups ⇒ Object
- #get_groups_byapp(app) ⇒ Object
- #get_instance_by_name(name) ⇒ Object
- #get_instance_by_pri_pub_dns(dns) ⇒ Object
- #get_instances(group) ⇒ Object
- #get_instances_role(group, role) ⇒ Object
-
#initialize(region, cloud_config = "config/cloud.yml") ⇒ Ec2Helper
constructor
A new instance of Ec2Helper.
- #lb_register_instances(lb_name, instances) ⇒ Object
- #lb_unregister_instances(lb_name, instances) ⇒ Object
- #populate_instances(region, config) ⇒ Object
- #printInstanceDetails ⇒ Object
Constructor Details
#initialize(region, cloud_config = "config/cloud.yml") ⇒ Ec2Helper
Returns a new instance of Ec2Helper.
7 8 9 10 11 |
# File 'lib/capawsext/whoec2helper.rb', line 7 def initialize(region, cloud_config = "config/cloud.yml") @cloud_config = YAML.load_file cloud_config @instances =[] populate_instances(region, @cloud_config) end |
Instance Method Details
#add_tags(resource_id, tags = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/capawsext/whoec2helper.rb', line 100 def (resource_id, = {}) .each { |key, value| @fog..create(:resource_id => resource_id, :key => key, :value => value) } end |
#get_all_instances ⇒ Object
106 107 108 |
# File 'lib/capawsext/whoec2helper.rb', line 106 def get_all_instances return @instances end |
#get_all_stacks ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/capawsext/whoec2helper.rb', line 52 def get_all_stacks stacks = {} @instances.each {|instance| instances = stacks[instance.['group']] if instances.nil? instances = Array.new stacks[instance.['group']] = instances end instances << instance } return stacks end |
#get_groups ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/capawsext/whoec2helper.rb', line 66 def get_groups groups = Set.new @instances.each { |instance| group = instance.['group'] groups.add(group) if not group.nil? } return groups end |
#get_groups_byapp(app) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/capawsext/whoec2helper.rb', line 75 def get_groups_byapp(app) groups = Set.new @instances.each { |instance| group = instance.['group'] groups << group if not group.nil? and instance.["app"] == app } return groups end |
#get_instance_by_name(name) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/capawsext/whoec2helper.rb', line 92 def get_instance_by_name(name) @instances.each {|instance| return instance if (instance.['name_s'] || "").casecmp(name) == 0 return instance if (instance.['Name'] || "").casecmp(name) == 0 } return nil end |
#get_instance_by_pri_pub_dns(dns) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/capawsext/whoec2helper.rb', line 44 def get_instance_by_pri_pub_dns(dns) @instances.each { |instance| if instance.private_dns_name == dns or instance.dns_name == dns return instance end } end |
#get_instances(group) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/capawsext/whoec2helper.rb', line 84 def get_instances(group) ret_instances = Array.new @instances.each { |instance| ret_instances << instance if instance.['group'] == group and instance.ready? } return ret_instances end |
#get_instances_role(group, role) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/capawsext/whoec2helper.rb', line 22 def get_instances_role(group, role) ret_instances = Array.new @instances.each {|instance| if not instance.['role'].nil? and instance.ready? unless instance.['role'].match(role).nil? or instance.['group'] != group ret_instances << instance end end } return ret_instances end |
#lb_register_instances(lb_name, instances) ⇒ Object
115 116 117 118 |
# File 'lib/capawsext/whoec2helper.rb', line 115 def lb_register_instances(lb_name, instances) lb = get_lb(lb_name) lb.register_instances(instances.map{|i| i.id }) end |
#lb_unregister_instances(lb_name, instances) ⇒ Object
110 111 112 113 |
# File 'lib/capawsext/whoec2helper.rb', line 110 def lb_unregister_instances(lb_name, instances) lb = get_lb(lb_name) lb.deregister_instances(instances.map{|i| i.id }) end |
#populate_instances(region, config) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/capawsext/whoec2helper.rb', line 13 def populate_instances(region, config) #Choose the defualt prover and region raise "aws_access_key_id or aws_secret_access_key keys are not present in the config file" if config[:aws_access_key_id].nil? or config[:aws_secret_access_key].nil? temp_config = {:provider => 'AWS'} config[:region] = region @fog = Fog::Compute.new(temp_config.merge!(config)) @fog.servers.each {|server| @instances << server } end |
#printInstanceDetails ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/capawsext/whoec2helper.rb', line 34 def printInstanceDetails() @instances.each_with_index do |instance, i| puts sprintf "%02d: %-20s %-20s %-20s %-20s %-25s %-20s (%s) (%s) (%s)", i, (instance.["Name"] || "").green,instance.private_dns_name ,instance.id.red, instance.flavor_id.cyan, instance.dns_name.blue, instance.availability_zone.magenta, (instance.["role"] || "").yellow, (instance.["group"] || "").yellow, (instance.["app"] || "").green if instance.ready? end end |