Class: Chef::Cookbook::CookbookVersionLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/cookbook/cookbook_version_loader.rb

Overview

This class is only used directly from the Chef::CookbookLoader and from chef-fs, so it only affects legacy-mode chef-client runs and knife. It is not used by server or zolo/zero modes.

This seems to be mostly a glorified factory method for creating CookbookVersion objects now, with creating Metadata objects bolted onto the side? It used to be also responsible for the merging of multiple objects when creating shadowed/merged cookbook versions from multiple sources. It also handles Chefignore files.

Constant Summary collapse

UPLOADED_COOKBOOK_VERSION_FILE =
".uploaded-cookbook-version.json".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, chefignore = nil) ⇒ CookbookVersionLoader

Returns a new instance of CookbookVersionLoader.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 49

def initialize(path, chefignore = nil)
  @cookbook_path = File.expand_path( path ) # cookbook_path from which this was loaded

  @inferred_cookbook_name = File.basename( path )
  @chefignore = chefignore
  @metadata = nil
  @relative_path = %r{#{Regexp.escape(cookbook_path)}/(.+)$}
  @metadata_loaded = false
  @cookbook_settings = {
    all_files: {},
  }

  @metadata_filenames = []
  @metadata_error = nil
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



42
43
44
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 42

def cookbook_path
  @cookbook_path
end

#cookbook_settingsObject (readonly)

Returns the value of attribute cookbook_settings.



38
39
40
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 38

def cookbook_settings
  @cookbook_settings
end

#frozenObject (readonly)

Returns the value of attribute frozen.



39
40
41
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 39

def frozen
  @frozen
end

#inferred_cookbook_nameObject (readonly)

The cookbook’s name as inferred from its directory.



45
46
47
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 45

def inferred_cookbook_name
  @inferred_cookbook_name
end

#metadata_errorObject (readonly)

Returns the value of attribute metadata_error.



47
48
49
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 47

def 
  @metadata_error
end

#uploaded_cookbook_version_fileObject (readonly)

Returns the value of attribute uploaded_cookbook_version_file.



40
41
42
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 40

def uploaded_cookbook_version_file
  @uploaded_cookbook_version_file
end

Instance Method Details

#cookbook_nameObject



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 146

def cookbook_name
  # The `name` attribute is now required in metadata, so
  # inferred_cookbook_name generally should not be used. Per CHEF-2923,
  # we have to not raise errors in cookbook metadata immediately, so that
  # users can still `knife cookbook upload some-cookbook` when an
  # unrelated cookbook has an error in its metadata.  This situation
  # could prevent us from reading the `name` attribute from the metadata
  # entirely, but the name is used as a hash key in CookbookLoader, so we
  # fall back to the inferred name here.
  (.name || inferred_cookbook_name).to_sym
end

#cookbook_versionObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 104

def cookbook_version
  return nil if empty?

  Chef::CookbookVersion.new(cookbook_name, cookbook_path).tap do |c|
    c.all_files            = cookbook_settings[:all_files].values
    c.             = 

    c.freeze_version if frozen
  end
end

#loadObject Also known as: load_cookbooks



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 84

def load
  Chef.deprecated(:internal_api, "Chef::Cookbook::CookbookVersionLoader's load method is deprecated. Please use load! instead.")
   # force lazy evaluation to occur

  # re-raise any exception that occurred when reading the metadata
  

  load_all_files

  remove_ignored_files

  if empty?
    Chef::Log.warn "Found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping."
  end

  cookbook_settings
end

#load!Object

Load the cookbook. Raises an error if the cookbook_path given to the constructor doesn’t point to a valid cookbook.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 67

def load!
   # force lazy evaluation to occur

  # re-raise any exception that occurred when reading the metadata
  

  load_all_files

  remove_ignored_files

  if empty?
    raise Exceptions::CookbookNotFoundInRepo, "The directory #{cookbook_path} does not contain a cookbook"
  end

  cookbook_settings
end

#metadataObject

Generates the Cookbook::Metadata object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/chef/cookbook/cookbook_version_loader.rb', line 116

def 
  return @metadata unless @metadata.nil?

  @metadata = Chef::Cookbook::Metadata.new

  .each do ||
    case 
    when /\.rb$/
      ()
    when uploaded_cookbook_version_file
      ()
    when /\.json$/
      ()
    else
      raise "Invalid metadata file: #{} for cookbook: #{cookbook_version}"
    end
  end

  @metadata

  # Rescue errors so that users can upload cookbooks via `knife cookbook
  # upload` even if some cookbooks in their chef-repo have errors in
  # their metadata. We only rescue StandardError because you have to be
  # doing something *really* terrible to raise an exception that inherits
  # directly from Exception in your metadata.rb file.
rescue StandardError => e
  @metadata_error = e
  @metadata
end