Class: Chef::Recipe
- Inherits:
-
Object
- Object
- Chef::Recipe
- Includes:
- DSL::Recipe, Mixin::Deprecation, Mixin::FromFile
- Defined in:
- lib/chef/recipe.rb
Overview
== Chef::Recipe A Recipe object is the context in which Chef recipes are evaluated.
Instance Attribute Summary collapse
-
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#recipe ⇒ Object
Returns the value of attribute recipe.
-
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
-
#run_context ⇒ Object
Returns the value of attribute run_context.
Attributes included from Mixin::FromFile
Class Method Summary collapse
-
.parse_recipe_name(recipe_name, current_cookbook: nil) ⇒ Object
Parses a potentially fully-qualified recipe name into its cookbook name and recipe short name.
Instance Method Summary collapse
- #from_hash(hash) ⇒ Object
- #from_yaml(string) ⇒ Object
- #from_yaml_file(filename) ⇒ Object
-
#initialize(cookbook_name, recipe_name, run_context) ⇒ Recipe
constructor
A new instance of Recipe.
- #inspect ⇒ Object
-
#node ⇒ Object
Used in DSL mixins.
-
#tag(*tags) ⇒ Object
This was moved to Chef::Node#tag, redirecting here for compatibility.
- #to_s ⇒ Object
-
#untag(*tags) ⇒ Object
This was moved to Chef::Node#untag, redirecting here for compatibility.
Methods included from Mixin::Deprecation
#deprecated_attr, #deprecated_attr_reader, #deprecated_attr_writer, #deprecated_ivar
Methods included from Mixin::FromFile
Methods included from DSL::Recipe
#exec, #have_resource_class_for?, #resource_class_for
Methods included from Mixin::LazyModuleInclude
#descendants, #include, #included
Methods included from DSL::Definitions
add_definition, #evaluate_resource_definition, #has_resource_definition?
Methods included from DSL::Resources
add_resource_dsl, remove_resource_dsl
Methods included from DSL::Cheffish
Methods included from DSL::RebootPending
Methods included from DSL::PlatformIntrospection
#older_than_win_2012_or_8?, #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::IncludeRecipe
Methods included from Mixin::NotifyingBlock
#notifying_block, #subcontext_block
Methods included from DSL::DeclareResource
#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context
Methods included from Mixin::PowershellOut
#powershell_out, #powershell_out!
Methods included from Mixin::WindowsArchitectureHelper
#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory
Methods included from DSL::Secret
#default_secret_config, #default_secret_service, #secret, #with_secret_config, #with_secret_service
Methods included from DSL::RenderHelpers
#render_json, #render_toml, #render_yaml
Methods included from DSL::ReaderHelpers
#parse_file, #parse_json, #parse_toml, #parse_yaml
Methods included from DSL::Powershell
Methods included from DSL::ChefVault
#chef_vault, #chef_vault_item, #chef_vault_item_for_environment
Methods included from DSL::DataQuery
#data_bag, #data_bag_item, #search, #tagged?
Methods included from EncryptedDataBagItem::CheckEncrypted
Methods included from DSL::Compliance
#include_input, #include_profile, #include_waiver
Constructor Details
#initialize(cookbook_name, recipe_name, run_context) ⇒ Recipe
Returns a new instance of Recipe.
58 59 60 61 62 63 64 |
# 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 |
Instance Attribute Details
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
29 30 31 |
# File 'lib/chef/recipe.rb', line 29 def cookbook_name @cookbook_name end |
#params ⇒ Object
Returns the value of attribute params.
29 30 31 |
# File 'lib/chef/recipe.rb', line 29 def params @params end |
#recipe ⇒ Object
Returns the value of attribute recipe.
29 30 31 |
# File 'lib/chef/recipe.rb', line 29 def recipe @recipe end |
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
29 30 31 |
# File 'lib/chef/recipe.rb', line 29 def recipe_name @recipe_name end |
#run_context ⇒ Object
Returns the value of attribute run_context.
29 30 31 |
# File 'lib/chef/recipe.rb', line 29 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
45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
Instance Method Details
#from_hash(hash) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/chef/recipe.rb', line 104 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) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/chef/recipe.rb', line 95 def from_yaml(string) res = ::YAML.safe_load(string, permitted_classes: [Date]) 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/chef/recipe.rb', line 81 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 ⇒ Object
120 121 122 |
# File 'lib/chef/recipe.rb', line 120 def inspect to_s end |
#node ⇒ Object
Used in DSL mixins
67 68 69 |
# File 'lib/chef/recipe.rb', line 67 def node run_context.node end |
#tag(*tags) ⇒ Object
This was moved to Chef::Node#tag, redirecting here for compatibility
72 73 74 |
# File 'lib/chef/recipe.rb', line 72 def tag(*) node.tag(*) end |
#to_s ⇒ Object
116 117 118 |
# File 'lib/chef/recipe.rb', line 116 def to_s "cookbook: #{cookbook_name || "(none)"}, recipe: #{recipe_name || "(none)"} " end |
#untag(*tags) ⇒ Object
This was moved to Chef::Node#untag, redirecting here for compatibility
77 78 79 |
# File 'lib/chef/recipe.rb', line 77 def untag(*) node.untag(*) end |