Skip to content

Commit f560b3d

Browse files
Thom MaySeth Falcon
authored andcommitted
Add satisfy_all function
This function takes a cookbook and a list of version constraints and attempts to return a single cookbook that satisfies those constraints
1 parent 2580139 commit f560b3d

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# Licensed under the Apache License, Version 2.0 (the "License");
88
# you may not use this file except in compliance with the License.
99
# You may obtain a copy of the License at
10-
#
10+
#
1111
# http://www.apache.org/licenses/LICENSE-2.0
12-
#
12+
#
1313
# Unless required by applicable law or agreed to in writing, software
1414
# distributed under the License is distributed on an "AS IS" BASIS,
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,12 +21,12 @@
2121
require 'chef' / 'node'
2222

2323
class Nodes < Application
24-
24+
2525
provides :json
26-
27-
before :authenticate_every
26+
27+
before :authenticate_every
2828
before :admin_or_requesting_node, :only => [ :update, :destroy, :cookbooks ]
29-
29+
3030
CMP = {
3131
"<<" => lambda { |v, r| v < r },
3232
"<=" => lambda { |v, r| v <= r },
@@ -39,7 +39,7 @@ class Nodes < Application
3939
PATTERN = /\A\s*(#{qcmp})?\s*(#{Chef::Cookbook::Metadata::Version::PATTERN})\s*\z/
4040

4141
def index
42-
@node_list = Chef::Node.cdb_list
42+
@node_list = Chef::Node.cdb_list
4343
display(@node_list.inject({}) do |r,n|
4444
r[n] = absolute_url(:node, n); r
4545
end)
@@ -58,7 +58,7 @@ def show
5858
def create
5959
@node = params["inflated_object"]
6060
begin
61-
Chef::Node.cdb_load(@node.name)
61+
Chef::Node.cdb_load(@node.name)
6262
raise Conflict, "Node already exists"
6363
rescue Chef::Exceptions::CouchDBNotFound
6464
end
@@ -83,7 +83,7 @@ def update
8383
def destroy
8484
begin
8585
@node = Chef::Node.cdb_load(params[:id])
86-
rescue Chef::Exceptions::CouchDBNotFound => e
86+
rescue Chef::Exceptions::CouchDBNotFound => e
8787
raise NotFound, "Cannot load node #{params[:id]}"
8888
end
8989
@node.cdb_destroy
@@ -94,7 +94,7 @@ def destroy
9494
def cookbooks
9595
begin
9696
@node = Chef::Node.cdb_load(params[:id])
97-
rescue Chef::Exceptions::CouchDBNotFound => e
97+
rescue Chef::Exceptions::CouchDBNotFound => e
9898
raise NotFound, "Cannot load node #{params[:id]}"
9999
end
100100

@@ -117,6 +117,26 @@ def satisfy(cookbook, req=nil)
117117
end
118118
end
119119

120+
def satisfy_all(cookbook, reqs=[])
121+
vers = Array.new
122+
123+
if reqs.empty?
124+
return Chef::CookbookVersion.cdb_load(cookbook, satisfy(cookbook).last)
125+
end
126+
127+
reqs.each do |pat|
128+
v = satisfy(cookbook, pat)
129+
raise ArgumentError, "Can't satisfy dependency #{pat} for cookbook #{cookbook}" if v.empty?
130+
if vers.empty?
131+
vers = v
132+
else
133+
vers = vers & v
134+
raise ArgumentError, "Conflicting dependencies for #{cookbook}" if vers.empty?
135+
end
136+
end
137+
Chef::CookbookVersion.cdb_load(cookbook, vers.last)
138+
end
139+
120140
def load_all_files
121141
all_cookbooks = Chef::Environment.cdb_load_filtered_cookbook_versions(@node.chef_environment)
122142

0 commit comments

Comments
 (0)