Class: Chef::Recipe

Overview

Chef::Recipe

A Recipe object is the context in which Chef recipes are evaluated.

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Deprecation

#deprecated_ivar

Methods included from Mixin::FromFile

#class_from_file, #from_file

Methods included from DSL::Audit

#control_group

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::PlatformIntrospection

#platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::Recipe

#build_resource, #declare_resource, #describe_self_for_error, #evaluate_resource_definition, #exec, #has_resource_definition?, #have_resource_class_for?, #method_missing, #resource_class_for

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe, #require_recipe

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Constructor Details

#initialize(cookbook_name, recipe_name, run_context) ⇒ Recipe

Returns a new instance of Recipe.



72
73
74
75
76
77
78
# File 'lib/chef/recipe.rb', line 72

def initialize(cookbook_name, recipe_name, run_context)
  @cookbook_name = cookbook_name
  @recipe_name = recipe_name
  @run_context = run_context
  # TODO: 5/19/2010 cw/tim: determine whether this can be removed
  @params = Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Attribute Details

#cookbook_nameObject

Returns the value of attribute cookbook_name.



49
50
51
# File 'lib/chef/recipe.rb', line 49

def cookbook_name
  @cookbook_name
end

#paramsObject

Returns the value of attribute params.



49
50
51
# File 'lib/chef/recipe.rb', line 49

def params
  @params
end

#recipeObject

Returns the value of attribute recipe.



49
50
51
# File 'lib/chef/recipe.rb', line 49

def recipe
  @recipe
end

#recipe_nameObject

Returns the value of attribute recipe_name.



49
50
51
# File 'lib/chef/recipe.rb', line 49

def recipe_name
  @recipe_name
end

#run_contextObject

Returns the value of attribute run_context.



49
50
51
# File 'lib/chef/recipe.rb', line 49

def run_context
  @run_context
end

Class Method Details

.parse_recipe_name(recipe_name, current_cookbook: nil) ⇒ Object

Parses a potentially fully-qualified recipe name into its cookbook name and recipe short name.

For example:

"aws::elastic_ip" returns [:aws, "elastic_ip"]
"aws" returns [:aws, "default"]
"::elastic_ip" returns [ current_cookbook, "elastic_ip" ]

– TODO: Duplicates functionality of RunListItem



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/recipe.rb', line 60

def self.parse_recipe_name(recipe_name, current_cookbook: nil)
  case recipe_name
  when /(.+?)::(.+)/
    [ $1.to_sym, $2 ]
  when /^::(.+)/
    raise "current_cookbook is nil, cannot resolve #{recipe_name}" if current_cookbook.nil?
    [ current_cookbook.to_sym, $1 ]
  else
    [ recipe_name.to_sym, "default" ]
  end
end

Instance Method Details

#nodeObject

Used in DSL mixins



81
82
83
# File 'lib/chef/recipe.rb', line 81

def node
  run_context.node
end

#resources(*args) ⇒ Object

Used by the DSL to look up resources when executing in the context of a recipe.



87
88
89
# File 'lib/chef/recipe.rb', line 87

def resources(*args)
  run_context.resource_collection.find(*args)
end

#tag(*tags) ⇒ Object

This was moved to Chef::Node#tag, redirecting here for compatibility



92
93
94
# File 'lib/chef/recipe.rb', line 92

def tag(*tags)
  run_context.node.tag(*tags)
end

#tagged?(*tags) ⇒ Boolean

Returns true if the node is tagged with all of the supplied tags.

Parameters

tags<Array>

A list of tags

Returns

true<TrueClass>

If all the parameters are present

false<FalseClass>

If any of the parameters are missing

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
# File 'lib/chef/recipe.rb', line 104

def tagged?(*tags)
  return false if run_context.node[:tags].nil?

  tags.each do |tag|
    return false unless run_context.node[:tags].include?(tag)
  end
  true
end

#untag(*tags) ⇒ Object

Removes the list of tags from the node.

Parameters

tags<Array>

A list of tags

Returns

tags<Array>

The current list of run_context.node



120
121
122
123
124
# File 'lib/chef/recipe.rb', line 120

def untag(*tags)
  tags.each do |tag|
    run_context.node.normal[:tags].delete(tag)
  end
end