Skip to content

Commit

Permalink
[OHAI-267] Reworked cloud file to be a more generic hint system
Browse files Browse the repository at this point in the history
  • Loading branch information
stormsilver authored and btm committed Apr 19, 2012
1 parent 8b3580a commit b614999
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 66 deletions.
13 changes: 13 additions & 0 deletions lib/ohai/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@
module Ohai
class Config
extend Mixlib::Config

# from chef/config.rb, should maybe be moved to mixlib-config?
def self.platform_specific_path(path)
if RUBY_PLATFORM =~ /mswin|mingw|windows/
# turns /etc/chef/client.rb into C:/chef/client.rb
path = File.join(ENV['SYSTEMDRIVE'], path.split('/')[2..-1])
# ensure all forward slashes are backslashes
path.gsub!(File::SEPARATOR, (File::ALT_SEPARATOR || '\\'))
end
path
end


log_level :info
log_location STDOUT
plugin_path [ File.expand_path(File.join(File.dirname(__FILE__), 'plugins'))]
disabled_plugins []
hints_path [ platform_specific_path('/etc/chef/ohai/hints') ]
end
end
38 changes: 0 additions & 38 deletions lib/ohai/mixin/cloud.rb

This file was deleted.

4 changes: 1 addition & 3 deletions lib/ohai/plugins/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
provides "ec2"

require 'ohai/mixin/ec2_metadata'
require 'ohai/mixin/cloud'

require_plugin "hostname"
require_plugin "kernel"
require_plugin "network"

extend Ohai::Mixin::Ec2Metadata
extend Ohai::Mixin::Cloud

def has_ec2_mac?
network[:interfaces].values.each do |iface|
Expand All @@ -45,7 +43,7 @@ def has_ec2_mac?
def looks_like_ec2?
# Try non-blocking connect so we don't "block" if
# the Xen environment is *not* EC2
cloud_file?('ec2') || has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
hint?('ec2') || has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
end

if looks_like_ec2?
Expand Down
4 changes: 1 addition & 3 deletions lib/ohai/plugins/eucalyptus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
provides "eucalyptus"

require 'ohai/mixin/ec2_metadata'
require 'ohai/mixin/cloud'

require_plugin "hostname"
require_plugin "kernel"
require_plugin "network"

extend Ohai::Mixin::Ec2Metadata
extend Ohai::Mixin::Cloud

def get_mac_address(addresses)
detected_addresses = addresses.detect { |address, keypair| keypair == {"family"=>"lladdr"} }
Expand All @@ -52,7 +50,7 @@ def has_euca_mac?
def looks_like_euca?
# Try non-blocking connect so we don't "block" if
# the Xen environment is *not* EC2
cloud_file?('eucalyptus') || has_euca_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
hint?('eucalyptus') || has_euca_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
end

if looks_like_euca?
Expand Down
6 changes: 1 addition & 5 deletions lib/ohai/plugins/rackspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

provides "rackspace"

require 'ohai/mixin/cloud'

require_plugin "kernel"
require_plugin "network"

extend Ohai::Mixin::Cloud

# Checks for matching rackspace kernel name
#
# === Return
Expand Down Expand Up @@ -52,7 +48,7 @@ def has_rackspace_mac?
# true:: If the rackspace cloud can be identified
# false:: Otherwise
def looks_like_rackspace?
cloud_file?('rackspace') || has_rackspace_mac? || has_rackspace_kernel?
hint?('rackspace') || has_rackspace_mac? || has_rackspace_kernel?
end

# Names rackspace ip address
Expand Down
27 changes: 26 additions & 1 deletion lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

module Ohai
class System
attr_accessor :data, :seen_plugins
attr_accessor :data, :seen_plugins, :hints

include Ohai::Mixin::FromFile
include Ohai::Mixin::Command
Expand All @@ -36,6 +36,7 @@ def initialize
@seen_plugins = Hash.new
@providers = Mash.new
@plugin_path = ""
@hints = Hash.new
end

def [](key)
Expand Down Expand Up @@ -101,6 +102,27 @@ def set_attribute(name, *values)
def get_attribute(name)
@data[name]
end

def hint?(name)
@json_parser ||= Yajl::Parser.new

return @hints[name] if @hints[name]

Ohai::Config[:hints_path].each do |path|
filename = File.join(path, "#{name}.json")
if File.exist?(filename)
begin
hash = @json_parser.parse(File.read(filename))
@hints[name] = hash || Hash.new # hint should exist because the file did, even if it didn't contain anything
rescue Yajl::ParseError => e
Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
end
end
end

@hints[name]
end


def all_plugins
require_plugin('os')
Expand Down Expand Up @@ -159,6 +181,9 @@ def refresh_plugins(path = '/')

refreshments = collect_providers(h)
Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")

# remove the hints cache
@hints = Hash.new

refreshments.each do |r|
@seen_plugins.delete(r) if @seen_plugins.has_key?(r)
Expand Down
10 changes: 5 additions & 5 deletions spec/ohai/plugins/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,25 @@
it_should_behave_like "ec2"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return('ec2')
File.stub!(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('')
end
end

describe "without cloud file" do
it_should_behave_like "!ec2"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(false)
File.stub!(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(false)
end
end

describe "with rackspace cloud file" do
it_should_behave_like "!ec2"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return('rackspace')
File.stub!(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('')
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/ohai/plugins/eucalyptus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@
it_should_behave_like "eucalyptus"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return('eucalyptus')
File.stub!(:exist?).with('/etc/chef/ohai/hints/eucalyptus.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/eucalyptus.json').and_return('')
end
end

describe "without cloud file" do
it_should_behave_like "!eucalyptus"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(false)
File.stub!(:exist?).with('/etc/chef/ohai/hints/eucalyptus.json').and_return(false)
end
end

describe "with rackspace cloud file" do
describe "with ec2 cloud file" do
it_should_behave_like "!eucalyptus"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return('rackspace')
File.stub!(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('')
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/ohai/plugins/rackspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,25 @@
it_should_behave_like "rackspace"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return("rackspace\n")
File.stub!(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('')
end
end

describe "without cloud file" do
it_should_behave_like "!rackspace"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(false)
File.stub!(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(false)
end
end

describe "with ec2 cloud file" do
it_should_behave_like "!rackspace"

before(:each) do
File.stub!(:exist?).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return(true)
File.stub!(:read).with(Ohai::Mixin::Cloud::CLOUD_FILE).and_return('ec2')
File.stub!(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true)
File.stub!(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('')
end
end

Expand Down

0 comments on commit b614999

Please sign in to comment.