Module: Chef::Mixin::Language

Included in:
RecipeDefinitionDSLCore, Recipe, Resource
Defined in:
lib/chef/mixin/language.rb

Instance Method Summary collapse

Instance Method Details

#data_bag(bag) ⇒ Object



96
97
98
99
# File 'lib/chef/mixin/language.rb', line 96

def data_bag(bag)
  rbag = Chef::DataBag.load(bag)
  rbag.keys
end

#data_bag_item(bag, item) ⇒ Object



101
102
103
# File 'lib/chef/mixin/language.rb', line 101

def data_bag_item(bag, item)
  Chef::DataBagItem.load(bag, item)
end

#platform?(*args) ⇒ Boolean

Given a list of platforms, returns true if the current recipe is being run on a node with that platform, false otherwise.

Parameters

args

A list of platforms

Returns

true

If the current platform is in the list

false

If the current platform is not in the list

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/chef/mixin/language.rb', line 71

def platform?(*args)
  has_platform = false
  
  args.flatten.each do |platform|
    has_platform = true if platform == @node[:platform]
  end
  
  has_platform
end

#search(*args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/mixin/language.rb', line 81

def search(*args, &block)
  # If you pass a block, or have at least the start argument, do raw result parsing
  # 
  # Otherwise, do the iteration for the end user
  if Kernel.block_given? || args.length >= 4 
    Chef::Search::Query.new.search(*args, &block)
  else 
    results = Array.new
    Chef::Search::Query.new.search(*args) do |o|
      results << o 
    end
    results
  end
end

#value_for_platform(platform_hash) ⇒ Object

Given a hash similar to the one we use for Platforms, select a value from the hash. Supports per platform defaults, along with a single base default. Arrays may be passed as hash keys and will be expanded.

Parameters

platform_hash

A platform-style hash.

Returns

value

Whatever the most specific value of the hash is.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/mixin/language.rb', line 36

def value_for_platform(platform_hash)
  result = nil
  
  platform_hash.each_pair do |key, value|
    if key.is_a?(Array)
      key.each { |array_key| platform_hash[array_key] = value }
      platform_hash.delete(key)
    end
  end
  if platform_hash.has_key?(@node[:platform])
    if platform_hash[@node[:platform]].has_key?(@node[:platform_version])
      result = platform_hash[@node[:platform]][@node[:platform_version]]
    elsif platform_hash[@node[:platform]].has_key?("default")
      result = platform_hash[@node[:platform]]["default"]
    end
  end
  
  unless result
    if platform_hash.has_key?("default")
      result = platform_hash["default"]
    end
  end  
  
  result
end