Module: Awsbix::AmazonWebServices

Included in:
Awsbix
Defined in:
lib/awsbix/aws.rb

Instance Method Summary collapse

Instance Method Details

#aws_get_hosts(options = {}) ⇒ Object

retrieve non-excluded and running EC2 hosts => ‘exclude|include’, :regex => %r{}



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/awsbix/aws.rb', line 48

def aws_get_hosts(options = {})
    # if no filter mode is provided default to 'include' 
    options[:filter] ||= 'include'
    # if no regex is provided default to '.*' 
    options[:regex] ||= %r{.*}
    # instance_status defaults to 'running' 
    options[:instance_status] ||= 'running'
    @ec2_hosts = Array.new

    self.ec2_connect()
    # loop through all hosts, across all regions in config
    self.aws_get_regions().each do |region|
        self.debug_print("info: processing #{region}")
        AWS.memoize do
            @ec2.regions[region].instances.each do | inst |
                if inst.status.match(/#{options[:instance_status]}/) then
                    inst.security_groups.each do | sg |
                        if options[:regex].is_a?(Regexp) then
                            case options[:filter]
                                when 'exclude'
                                    # do not process if sg is excluded
                                    unless sg.name.match(options[:regex]) then
                                        # do not push if already present
                                        unless @ec2_hosts.include?(inst) then
                                            @ec2_hosts.push(inst)
                                        end
                                    end
                                when 'include'
                                    # process if sg is included
                                    if sg.name.match(options[:regex]) then
                                        # do not push if already present
                                        unless @ec2_hosts.include?(inst) then
                                            @ec2_hosts.push(inst)
                                        end
                                    end
                            end
                        end
                    end
                end
            end
        end
    end
    return @ec2_hosts
end

#aws_get_regionsObject



23
24
25
26
# File 'lib/awsbix/aws.rb', line 23

def aws_get_regions()
    # return an array of all regions in config
    self.get_conf('aws_regions')
end

#ec2_connectObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awsbix/aws.rb', line 28

def ec2_connect()
    # credentials -> IAM role -> Raise exception
    if self.get_conf('aws_access_key') and self.get_conf('aws_secret_key') then
        AWS.config(
            :access_key_id => self.get_conf('aws_access_key'),
            :secret_access_key => self.get_conf('aws_secret_key')
        )
        @ec2 = AWS::EC2.new()
    else
        # try IAM role
        begin
            @ec2 = AWS::EC2.new()
        rescue
            raise ErrorAWSAuthentication
        end
    end
end