Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created October 4, 2011 10:07
Show Gist options
  • Select an option

  • Save techbelly/1261289 to your computer and use it in GitHub Desktop.

Select an option

Save techbelly/1261289 to your computer and use it in GitHub Desktop.
EC2 facter
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