Module: Awsbix::AmazonWebServices
- Included in:
- Awsbix
- Defined in:
- lib/awsbix/aws.rb
Instance Method Summary collapse
-
#aws_get_hosts(options = {}) ⇒ Object
retrieve non-excluded and running EC2 hosts => ‘exclude|include’, :regex => %r{}.
- #aws_get_regions ⇒ Object
- #ec2_connect ⇒ Object
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( = {}) # if no filter mode is provided default to 'include' [:filter] ||= 'include' # if no regex is provided default to '.*' [:regex] ||= %r{.*} # instance_status defaults to 'running' [: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(/#{[:instance_status]}/) then inst.security_groups.each do | sg | if [:regex].is_a?(Regexp) then case [:filter] when 'exclude' # do not process if sg is excluded unless sg.name.match([: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([: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 ⇒ Object
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_connect ⇒ Object
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 |