Created
October 4, 2011 10:07
-
-
Save techbelly/1261289 to your computer and use it in GitHub Desktop.
EC2 facter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'open-uri' | |
| require 'timeout' | |
| require 'ping' | |
| def can_connect? | |
| begin | |
| Timeout::timeout(2) do | |
| open("http://169.254.169.254") | |
| return true | |
| end | |
| rescue Timeout::Error, Errno::EHOSTUNREACH | |
| return false | |
| end | |
| end | |
| end | |
| def add_ec2_groups() | |
| return unless can_connect? | |
| groups = open("http://169.254.169.254/2008-02-01/meta-data/security-groups").read | |
| groups = groups.split("\n") | |
| groups = groups.select {|g| g != 'ssh-access' } | |
| if groups.size > 0 | |
| sec_group = groups.first | |
| Facter.add("ec2_security_group") { | |
| setcode { sec_group } | |
| } | |
| puts "SECURITY GROUP #{sec_group}" | |
| end | |
| end | |
| begin | |
| Timeout::timeout(180) do | |
| add_ec2_groups() | |
| end | |
| rescue Timeout::Error, Errno::EHOSTUNREACH | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment