Skip to content

Commit f77f78a

Browse files
Thom MaySeth Falcon
authored andcommitted
Implement versioned dependencies
1 parent 9d5dea9 commit f77f78a

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

chef-server-api/app/controllers/nodes.rb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def load_all_files
142142

143143
included_cookbooks = cookbooks_for_node(all_cookbooks)
144144
nodes_cookbooks = Hash.new
145-
included_cookbooks.each do |cookbook_name, cookbook|
146-
next unless cookbook
145+
included_cookbooks.each_pair do |cookbook_name, versions|
146+
cookbook = satisfy_all(cookbook_name, versions)
147147

148148
nodes_cookbooks[cookbook_name.to_s] = cookbook.generate_manifest_with_urls{|opts| absolute_url(:cookbook_file, opts) }
149149
end
@@ -156,9 +156,10 @@ def cookbooks_for_node(all_cookbooks)
156156
# expand returns a RunListExpansion which contains recipes, default and override attrs [cb]
157157
items = @node.run_list.expand('couchdb').run_list_items
158158

159+
# TODO: make cookbook loading respect environment's versions [stephen 8/25/10]
159160
# walk run list and accumulate included dependencies
160161
items.select{|i| i.recipe?}.inject({}) do |included_cookbooks, rli|
161-
expand_cookbook_deps(included_cookbooks, all_cookbooks, rli)
162+
expand_cookbook_deps(included_cookbooks, rli)
162163
included_cookbooks
163164
end
164165
end
@@ -169,9 +170,13 @@ def cookbooks_for_node(all_cookbooks)
169170
# recursed into
170171
# all_cookbooks == hash of name -> CookbookVersion, all cookbooks available
171172
# run_list_items == name of cookbook to include
172-
def expand_cookbook_deps(included_cookbooks, all_cookbooks, run_list_item)
173+
def expand_cookbook_deps(included_cookbooks, run_list_item, constraints = nil)
173174
# determine the run list item's parent cookbook, which might be run_list_item in the default case
174175
version = "latest"
176+
177+
# Fortunately or otherwise we need to deal with both RunListItem objects
178+
# and bare strings. strings will come to us via the dependency solver, and
179+
# I don't think it's valuable to bash them into an RLI just to avoid this.
175180
if run_list_item.kind_of? Chef::RunList::RunListItem
176181
cookbook_name = (run_list_item.name[/^(.+)::/, 1] || run_list_item.name)
177182
version = run_list_item.version unless run_list_item.version.nil?
@@ -180,25 +185,27 @@ def expand_cookbook_deps(included_cookbooks, all_cookbooks, run_list_item)
180185
end
181186

182187
Chef::Log.debug("Node requires #{cookbook_name} at #{version}")
188+
Chef::Log.debug("Requirement constrained by #{constraints}") if constraints
189+
190+
constraints ||= version
191+
183192
# include its dependencies
184-
included_cookbooks[cookbook_name] = Chef::CookbookVersion.cdb_load(cookbook_name, version)
185-
if !included_cookbooks[cookbook_name]
193+
included_cookbooks[cookbook_name] ||= Array.new
194+
included_cookbooks[cookbook_name] << constraints
195+
196+
cb = satisfy_all(cookbook_name, constraints)
197+
198+
if !cb
186199
return false
187200
# NOTE [dan/cw] We don't think changing this to an exception breaks stuff.
188201
# Chef::Log.warn "#{__FILE__}:#{__LINE__}: in expand_cookbook_deps, cookbook/role #{cookbook_name} could not be found, ignoring it in cookbook expansion"
189202
# return included_cookbooks
190203
end
191204

192-
# TODO: 5/27/2010 cw: implement dep_version_constraints according to
193-
# http://wiki.opscode.com/display/chef/Metadata#Metadata-depends,
194-
included_cookbooks[cookbook_name].metadata.dependencies.each do |depname, dep_version_constraints|
195-
# recursively expand dependencies into included_cookbooks unless
196-
# we've already done it
197-
unless included_cookbooks[depname]
198-
unless expand_cookbook_deps(included_cookbooks, all_cookbooks, depname)
199-
raise PreconditionFailed, "cookbook #{cookbook_name} depends on cookbook #{depname}, but #{depname} does not exist"
205+
cb.metadata.dependencies.each do |depname, dep_version_constraints|
206+
unless expand_cookbook_deps(included_cookbooks, depname, dep_version_constraints)
207+
raise PreconditionFailed, "cookbook #{cookbook_name} depends on cookbook #{depname}, but #{depname} does not exist"
200208

201-
end
202209
end
203210
end
204211
true

0 commit comments

Comments
 (0)