1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
require 'chef/cookbook_version'
require 'chef/cookbook/chefignore'
require 'chef/cookbook/metadata'
class Chef
class Cookbook
class CookbookVersionLoader
FILETYPES_SUBJECT_TO_IGNORE = [ :attribute_filenames,
:definition_filenames,
:recipe_filenames,
:template_filenames,
:file_filenames,
:library_filenames,
:resource_filenames,
:provider_filenames]
UPLOADED_COOKBOOK_VERSION_FILE = ".uploaded-cookbook-version.json".freeze
attr_reader :cookbook_settings
attr_reader :cookbook_paths
attr_reader :metadata_filenames
attr_reader :frozen
attr_reader :uploaded_cookbook_version_file
attr_reader :cookbook_path
# The cookbook's name as inferred from its directory.
attr_reader :inferred_cookbook_name
def initialize(path, chefignore=nil)
@cookbook_path = File.expand_path( path ) # cookbook_path from which this was loaded
# We keep a list of all cookbook paths that have been merged in
@cookbook_paths = [ cookbook_path ]
# TODO: Add a "strict mode" setting, use this when not in strict mode
@inferred_cookbook_name = File.basename( path )
@chefignore = chefignore
@metadata = nil
@relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/
@metadata_loaded = false
@cookbook_settings = {
:attribute_filenames => {},
:definition_filenames => {},
:recipe_filenames => {},
:template_filenames => {},
:file_filenames => {},
:library_filenames => {},
:resource_filenames => {},
:provider_filenames => {},
:root_filenames => {}
}
@metadata_filenames = []
end
def load!
file_paths_map = load
if empty?
raise Exceptions::CookbookNotFoundInRepo, "The directory #{cookbook_path} does not contain a cookbook"
end
file_paths_map
end
def load
load_as(:attribute_filenames, 'attributes', '*.rb')
load_as(:definition_filenames, 'definitions', '*.rb')
load_as(:recipe_filenames, 'recipes', '*.rb')
load_as(:library_filenames, 'libraries', '*.rb')
load_recursively_as(:template_filenames, "templates", "*")
load_recursively_as(:file_filenames, "files", "*")
load_recursively_as(:resource_filenames, "resources", "*.rb")
load_recursively_as(:provider_filenames, "providers", "*.rb")
load_root_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
alias :load_cookbooks :load
def metadata_filenames
return @metadata_filenames unless @metadata_filenames.empty?
if File.exists?(File.join(cookbook_path, UPLOADED_COOKBOOK_VERSION_FILE))
@uploaded_cookbook_version_file = File.join(cookbook_path, UPLOADED_COOKBOOK_VERSION_FILE)
end
if File.exists?(File.join(cookbook_path, "metadata.rb"))
@metadata_filenames << File.join(cookbook_path, "metadata.rb")
elsif File.exists?(File.join(cookbook_path, "metadata.json"))
@metadata_filenames << File.join(cookbook_path, "metadata.json")
elsif @uploaded_cookbook_version_file
@metadata_filenames << @uploaded_cookbook_version_file
end
# Set frozen based on .uploaded-cookbook-version.json
set_frozen
@metadata_filenames
end
def cookbook_version
return nil if empty?
Chef::CookbookVersion.new(cookbook_name, *cookbook_paths).tap do |c|
c.attribute_filenames = cookbook_settings[:attribute_filenames].values
c.definition_filenames = cookbook_settings[:definition_filenames].values
c.recipe_filenames = cookbook_settings[:recipe_filenames].values
c.template_filenames = cookbook_settings[:template_filenames].values
c.file_filenames = cookbook_settings[:file_filenames].values
c.library_filenames = cookbook_settings[:library_filenames].values
c.resource_filenames = cookbook_settings[:resource_filenames].values
c.provider_filenames = cookbook_settings[:provider_filenames].values
c.root_filenames = cookbook_settings[:root_filenames].values
c.metadata_filenames = metadata_filenames
c.metadata = metadata
c.freeze_version if @frozen
end
end
def cookbook_name
(metadata.name || @inferred_cookbook_name).to_sym
end
# Generates the Cookbook::Metadata object
def metadata
return @metadata unless @metadata.nil?
@metadata = Chef::Cookbook::Metadata.new
metadata_filenames.each do |metadata_file|
case metadata_file
when /\.rb$/
apply_ruby_metadata(metadata_file)
when @uploaded_cookbook_version_file
apply_json_cookbook_version_metadata(metadata_file)
when /\.json$/
apply_json_metadata(metadata_file)
else
raise RuntimeError, "Invalid metadata file: #{metadata_file} for cookbook: #{cookbook_version}"
end
end
# Compatibility if metadata is missing the name attribute:
# TODO: probably should live elsewhere.
if @metadata.name.nil?
@metadata.name(@inferred_cookbook_name)
end
@metadata
end
def empty?
cookbook_settings.values.all? { |files_hash| files_hash.empty? } && metadata_filenames.size == 0
end
def merge!(other_cookbook_loader)
other_cookbook_settings = other_cookbook_loader.cookbook_settings
cookbook_settings.each do |file_type, file_list|
file_list.merge!(other_cookbook_settings[file_type])
end
metadata_filenames.concat(other_cookbook_loader.metadata_filenames)
@cookbook_paths += other_cookbook_loader.cookbook_paths
@frozen = true if other_cookbook_loader.frozen
@metadata = nil # reset metadata so it gets reloaded and all metadata files applied.
self
end
def chefignore
@chefignore ||= Chefignore.new(File.basename(cookbook_path))
end
def load_root_files
Dir.glob(File.join(cookbook_path, '*'), File::FNM_DOTMATCH).each do |file|
next if File.directory?(file)
next if File.basename(file) == UPLOADED_COOKBOOK_VERSION_FILE
cookbook_settings[:root_filenames][file[@relative_path, 1]] = file
end
end
def load_recursively_as(category, category_dir, glob)
file_spec = File.join(cookbook_path, category_dir, '**', glob)
Dir.glob(file_spec, File::FNM_DOTMATCH).each do |file|
next if File.directory?(file)
cookbook_settings[category][file[@relative_path, 1]] = file
end
end
def load_as(category, *path_glob)
Dir[File.join(cookbook_path, *path_glob)].each do |file|
cookbook_settings[category][file[@relative_path, 1]] = file
end
end
def remove_ignored_files
cookbook_settings.each_value do |file_list|
file_list.reject! do |relative_path, full_path|
chefignore.ignored?(relative_path)
end
end
end
def apply_ruby_metadata(file)
begin
@metadata.from_file(file)
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Error evaluating metadata.rb for #@inferred_cookbook_name in " + file)
raise
end
end
def apply_json_metadata(file)
begin
@metadata.from_json(IO.read(file))
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@inferred_cookbook_name in " + file)
raise
end
end
def apply_json_cookbook_version_metadata(file)
begin
data = Chef::JSONCompat.from_json(IO.read(file), :create_additions => false)
@metadata.from_hash(data['metadata'])
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@inferred_cookbook_name in " + file)
raise
end
end
def set_frozen
if uploaded_cookbook_version_file
begin
data = Chef::JSONCompat.from_json(IO.read(uploaded_cookbook_version_file), :create_additions => false)
@frozen = data['frozen?']
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@inferred_cookbook_name in #{uploaded_cookbook_version_file}")
raise
end
end
end
end
end
end
|