Class: Ec2Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/capawsext/whoec2helper.rb

Instance Method Summary collapse

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 add_tags(resource_id, tags = {})
  tags.each { |key, value|
    @fog.tags.create(:resource_id => resource_id, :key => key, :value => value)
  }
end

#get_all_instancesObject



106
107
108
# File 'lib/capawsext/whoec2helper.rb', line 106

def get_all_instances
  return @instances
end

#get_all_stacksObject



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.tags['group']]
    if instances.nil?
      instances = Array.new
      stacks[instance.tags['group']] = instances
    end
    instances << instance
  }
  return stacks
end

#get_groupsObject



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.tags['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.tags['group']
    groups << group if not group.nil? and instance.tags["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.tags['name_s'] || "").casecmp(name) == 0
    return instance if (instance.tags['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.tags['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.tags['role'].nil? and instance.ready?
      unless instance.tags['role'].match(role).nil? or instance.tags['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

#printInstanceDetailsObject



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.tags["Name"] || "").green,instance.private_dns_name ,instance.id.red, instance.flavor_id.cyan,
          instance.dns_name.blue, instance.availability_zone.magenta, (instance.tags["role"] || "").yellow,
          (instance.tags["group"] || "").yellow, (instance.tags["app"] || "").green if instance.ready?
      end

end