class Chef::Recipe

Chef::Recipe

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

Attributes

cookbook_name[RW]
params[RW]
recipe[RW]
recipe_name[RW]
run_context[RW]

Public Class Methods

new(cookbook_name, recipe_name, run_context) click to toggle source
# File lib/chef/recipe.rb, line 58
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 = {}
end
parse_recipe_name(recipe_name, current_cookbook: nil) click to toggle source

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" ]
# File lib/chef/recipe.rb, line 45
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

Public Instance Methods

from_hash(hash) click to toggle source
# File lib/chef/recipe.rb, line 112
def from_hash(hash)
  hash["resources"].each do |rhash|
    type = rhash.delete("type").to_sym
    name = rhash.delete("name")
    res = declare_resource(type, name)
    rhash.each do |key, value|
      # FIXME?: we probably need a way to instance_exec a string that contains block code against the property?
      res.send(key, value)
    end
  end
end
from_yaml(string) click to toggle source
# File lib/chef/recipe.rb, line 103
def from_yaml(string)
  res = ::YAML.safe_load(string)
  unless res.is_a?(Hash) && res.key?("resources")
    raise ArgumentError, "YAML recipe '#{source_file}' must contain a top-level 'resources' hash (YAML sequence), i.e. 'resources:'"
  end

  from_hash(res)
end
from_yaml_file(filename) click to toggle source
# File lib/chef/recipe.rb, line 89
def from_yaml_file(filename)
  self.source_file = filename
  if File.file?(filename) && File.readable?(filename)
    yaml_contents = IO.read(filename)
    if ::YAML.load_stream(yaml_contents).length > 1
      raise ArgumentError, "YAML recipe '#{filename}' contains multiple documents, only one is supported"
    end

    from_yaml(yaml_contents)
  else
    raise IOError, "Cannot open or read file '#{filename}'!"
  end
end
inspect() click to toggle source
# File lib/chef/recipe.rb, line 128
def inspect
  to_s
end
node() click to toggle source

Used in DSL mixins

# File lib/chef/recipe.rb, line 67
def node
  run_context.node
end
tag(*tags) click to toggle source

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

# File lib/chef/recipe.rb, line 72
def tag(*tags)
  run_context.node.tag(*tags)
end
to_s() click to toggle source
# File lib/chef/recipe.rb, line 124
def to_s
  "cookbook: #{cookbook_name || "(none)"}, recipe: #{recipe_name || "(none)"} "
end
untag(*tags) click to toggle source

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.tags

# File lib/chef/recipe.rb, line 83
def untag(*tags)
  tags.each do |tag|
    run_context.node.tags.delete(tag)
  end
end