Skip to content

Instantly share code, notes, and snippets.

@kangguru
Created June 27, 2013 07:24
Show Gist options
  • Save kangguru/5874588 to your computer and use it in GitHub Desktop.
Save kangguru/5874588 to your computer and use it in GitHub Desktop.
#
# Author:: Tim Dysinger (<tim@dysinger.net>)
# Author:: Benjamin Black (<bb@opscode.com>)
# Author:: Christopher Brown (<cb@opscode.com>)
# Copyright:: Copyright (c) 2009 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
provides "brightbox"
require 'ohai/mixin/ec2_metadata'
require_plugin "hostname"
require_plugin "kernel"
require_plugin "network"
EC2_METADATA_URL = "/2009-04-04/meta-data"
EC2_USERDATA_URL = "/2009-04-04/user-data"
extend Ohai::Mixin::Ec2Metadata
def has_brightbox_mac?
network[:interfaces].values.each do |iface|
unless iface[:arp].nil?
if iface[:arp].any? {|k,v| v =~/^52:54:00:([0-9A-F]{2}[:-]){2}([0-9A-F]{2})$/i }
Ohai::Log.debug("has_brightbox_mac? == true")
return true
end
end
end
Ohai::Log.debug("has_brightbox_mac? == false")
false
end
def looks_like_brightbox?
# Try non-blocking connect so we don't "block" if
# the Xen environment is *not* EC2
hint?('brightbox') || has_brightbox_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
end
if looks_like_brightbox?
Ohai::Log.debug("looks_like_brightbox? == true")
brightbox Mash.new
fetch_metadata.each {|k, v| brightbox[k] = v }
else
Ohai::Log.debug("looks_like_brightbox? == false")
false
end
def fetch_metadata(id='')
metadata = Hash.new
`ec2metadata`.chomp.split("\n").each do |date|
key, value = date.split(": ")
if value.start_with? "[" and value.end_with? "]"
metadata[metadata_key(key)] = value.scan(/'([^']*)'/).flatten
elsif key == "security-groups"
metadata[metadata_key(key)] = value.split(" ")
else
metadata[metadata_key(key)] = value unless value == "unavailable"
end
end
metadata
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment