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
|
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Christopher Brown (<cb@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef' / 'cookbook_loader'
require 'chef' / 'cookbook' / 'metadata'
class ChefServerApi::Cookbooks < ChefServerApi::Application
provides :json
before :authenticate_every
include Chef::Mixin::Checksum
def index
cl = Chef::CookbookLoader.new
cookbook_list = Hash.new
cl.each do |cookbook|
cookbook_list[cookbook.name] = absolute_slice_url(:cookbook, :id => cookbook.name.to_s)
end
display cookbook_list
end
def show
cl = Chef::CookbookLoader.new
cookbook = cl[params[:id]]
raise NotFound unless cookbook
results = load_cookbook_files(cookbook)
results[:name] = cookbook.name.to_s
results[:metadata] = cl.metadata[cookbook.name.to_sym]
display results
end
def show_segment
cl = Chef::CookbookLoader.new
cookbook = cl[params[:cookbook_id]]
raise NotFound unless cookbook
cookbook_files = load_cookbook_files(cookbook)
raise NotFound unless cookbook_files.has_key?(params[:segment].to_sym)
if params[:id]
case params[:segment]
when "templates","files"
serve_segment_preferred(cookbook, params[:segment], cookbook_files[params[:segment].to_sym])
else
serve_segment_file(cookbook, params[:segment], cookbook_files[params[:segment].to_sym])
end
else
display cookbook_files[params[:segment].to_sym]
end
end
def serve_segment_preferred(cookbook, segment, files)
to_send = nil
preferences = [
"host-#{params[:fqdn]}",
"#{params[:platform]}-#{params[:version]}",
"#{params[:platform]}",
"default"
]
preferences.each do |pref|
unless to_send
to_send = files.detect { |file| Chef::Log.debug("#{pref.inspect} #{file.inspect}"); file[:name] == params[:id] && file[:specificity] == pref }
end
end
raise NotFound, "Cannot find a suitable #{segment} file!" unless to_send
current_checksum = to_send[:checksum]
Chef::Log.debug("#{to_send[:name]} Client Checksum: #{params[:checksum]}, Server Checksum: #{current_checksum}")
if current_checksum == params[:checksum]
raise NotModified, "File #{to_send[:name]} has not changed"
else
file_name = nil
segment_files(segment.to_sym, cookbook).each do |f|
if f =~ /#{to_send[:specificity]}\/#{to_send[:name]}$/
file_name = File.expand_path(f)
break
end
end
raise NotFound, "Cannot find the real file for #{to_send[:specificity]} #{to_send[:name]} - this is a 42 error (shouldn't ever happen)" unless file_name
send_file(file_name)
end
end
def serve_segment_file(cookbook, segment, files)
to_send = files.detect { |f| f[:name] == params[:id] }
raise NotFound, "Cannot find a suitable #{segment} file!" unless to_send
current_checksum = to_send[:checksum]
Chef::Log.debug("#{to_send[:name]} Client Checksum: #{params[:checksum]}, Server Checksum: #{current_checksum}")
if current_checksum == params[:checksum]
raise NotModified, "File #{to_send[:name]} has not changed"
else
file_name = nil
segment_files(segment.to_sym, cookbook).each do |f|
next unless File.basename(f) == to_send[:name]
file_name = File.expand_path(f)
end
raise NotFound, "Cannot find the real file for #{to_send[:name]} - this is a 42 error (shouldn't ever happen)" unless file_name
send_file(file_name)
end
end
end
|