Skip to content

Instantly share code, notes, and snippets.

@hedgehog
Created February 21, 2012 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedgehog/1873800 to your computer and use it in GitHub Desktop.
Save hedgehog/1873800 to your computer and use it in GitHub Desktop.
Robust ec2 meta-data collection
# Ohai Eec2 meta-data collections and `looks_like_ec2?`
# were broken in at least: 0.6.4 and 0.6.10
#
# Invitation for wrath of the G-ds:
# This gist always seems to have worked to extract (some?/all?) meta-data
#
# Aslo allows us to avoid #all_plugins, which has needed wait functionality.
# See:
# http://www.danpisarski.com/2010/08/22/loading-only-the-data-you-want-in-ohai/
#
# Usage:
# o = ::Ohai::System.new
# o.require_plugin('ec2')
# o.wait_on_children
require 'ohai'
class ::Ohai::System
def wait_on_children
unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
# Catch any errant children who need to be reaped
begin
true while Process.wait(-1, Process::WNOHANG)
rescue Errno::ECHILD
end
end
end
end
o = ::Ohai::System.new
o.require_plugin('ec2')
o.wait_on_children
if o.looks_like_ec2?
ec2_data = o[:ec2]
else
# Ohai 0.6.4 is broken so we can't use the above.
begin
ec2_data = o.fetch_metadata
rescue Errno::EHOSTUNREACH
ec2_data = {}
end
end
ec2_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment