module Awsbix::AmazonWebServices

Public Instance Methods

aws_get_hosts(options = {}) click to toggle source

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

# 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_regions() click to toggle source
# 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_connect() click to toggle source
# 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