class Chef::DSL::PlatformIntrospection::PlatformDependentValue

Implementation class for determining platform dependent values

Public Class Methods

new(platform_hash) click to toggle source

Create a platform dependent value object.

Arguments

platform_hash (Hash) a hash of the same structure as Chef::Platform, like this:

{
  :debian => {:default => 'the value for all debian'}
  [:centos, :redhat, :fedora] => {:default => "value for all EL variants"}
  :ubuntu => { :default => "default for ubuntu", '10.04' => "value for 10.04 only"},
  :default => "the default when nothing else matches"
}
  • platforms can be specified as Symbols or Strings

  • multiple platforms can be grouped by using an Array as the key

  • values for platforms need to be Hashes of the form: {platform_version => value_for_that_version}

  • the exception to the above is the default value, which is given as :default => default_value

# File lib/chef/dsl/platform_introspection.rb, line 46
def initialize(platform_hash)
  @values = {}
  platform_hash.each { |platforms, value| set(platforms, value) }
end

Public Instance Methods

value_for_node(node) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 51
def value_for_node(node)
  platform, version = node[:platform].to_s, node[:platform_version].to_s
  # Check if we match a version constraint via Chef::VersionConstraint::Platform and Chef::Version::Platform
  matched_value = match_versions(node)
  if @values.key?(platform) && @values[platform].key?(version)
    @values[platform][version]
  elsif matched_value
    matched_value
  elsif @values.key?(platform) && @values[platform].key?("default")
    @values[platform]["default"]
  elsif @values.key?("default")
    @values["default"]
  else
    nil
  end
end

Private Instance Methods

assert_valid_platform_values!(platforms, value) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 126
def assert_valid_platform_values!(platforms, value)
  unless value.kind_of?(Hash)
    msg = "platform dependent values must be specified in the format :platform => {:version => value} "
    msg << "you gave a value #{value.inspect} for platform(s) #{platforms}"
    raise ArgumentError, msg
  end
end
match_versions(node) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 70
def match_versions(node)
  platform, version = node[:platform].to_s, node[:platform_version].to_s
  return nil unless @values.key?(platform)
  node_version = Chef::Version::Platform.new(version)
  key_matches = []
  keys = @values[platform].keys
  keys.each do |k|
    begin
      if Chef::VersionConstraint::Platform.new(k).include?(node_version)
        key_matches << k
      end
    rescue Chef::Exceptions::InvalidVersionConstraint => e
      Chef::Log.trace "Caught InvalidVersionConstraint. This means that a key in value_for_platform cannot be interpreted as a Chef::VersionConstraint::Platform."
      Chef::Log.trace(e)
    end
  end
  return @values[platform][version] if key_matches.include?(version)
  case key_matches.length
  when 0
    return nil
  when 1
    return @values[platform][key_matches.first]
  else
    raise "Multiple matches detected for #{platform} with values #{@values}. The matches are: #{key_matches}"
  end
rescue Chef::Exceptions::InvalidCookbookVersion => e
  # Lets not break because someone passes a weird string like 'default' :)
  Chef::Log.trace(e)
  Chef::Log.trace "InvalidCookbookVersion exceptions are common and expected here: the generic constraint matcher attempted to match something which is not a constraint. Moving on to next version or constraint"
  nil
rescue Chef::Exceptions::InvalidPlatformVersion => e
  Chef::Log.trace "Caught InvalidPlatformVersion, this means that Chef::Version::Platform does not know how to turn #{node_version} into an x.y.z format"
  Chef::Log.trace(e)
  nil
end
normalize_keys(hash) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 116
def normalize_keys(hash)
  hash.inject({}) do |h, key_value|
    keys, value = *key_value
    Array(keys).each do |key|
      h[key.to_s] = value
    end
    h
  end
end
set(platforms, value) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 106
def set(platforms, value)
  if platforms.to_s == "default"
    @values["default"] = value
  else
    assert_valid_platform_values!(platforms, value)
    Array(platforms).each { |platform| @values[platform.to_s] = normalize_keys(value) }
    value
  end
end