Skip to content

Commit eb4c433

Browse files
committed
Moving preferred file selection to a mixin
1 parent 1c1b16d commit eb4c433

5 files changed

Lines changed: 229 additions & 92 deletions

File tree

chef-server/lib/controllers/cookbook_files.rb

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,16 @@
1818

1919
require 'chef' / 'mixin' / 'checksum'
2020
require 'chef' / 'cookbook_loader'
21+
require 'chef' / 'mixin' / 'find_preferred_file'
2122

2223
class CookbookFiles < Application
2324

2425
provides :html, :json
2526

2627
include Chef::Mixin::Checksum
28+
include Chef::Mixin::FindPreferredFile
2729

2830
layout nil
29-
30-
def load_cookbook_files()
31-
@cl = Chef::CookbookLoader.new
32-
@cookbook = @cl[params[:cookbook_id]]
33-
raise NotFound unless @cookbook
34-
35-
@remote_files = Hash.new
36-
@cookbook.remote_files.each do |rf|
37-
full = File.expand_path(rf)
38-
name = File.basename(full)
39-
rf =~ /^.+#{params[:cookbook_id]}[\\|\/]files[\\|\/](.+?)[\\|\/]#{name}/
40-
singlecopy = $1
41-
@remote_files[full] = {
42-
:name => name,
43-
:singlecopy => singlecopy,
44-
:file => full,
45-
}
46-
end
47-
Chef::Log.debug("Remote files found: #{@remote_files.inspect}")
48-
@remote_files
49-
end
5031

5132
def index
5233
if params[:id]
@@ -56,26 +37,40 @@ def index
5637
show
5738
end
5839
else
59-
load_cookbook_files()
40+
@remote_files = load_cookbook_files(params[:cookbook_id], :remote_file)
6041
display @remote_files
6142
end
6243
end
6344

6445
def show
6546
only_provides :json
66-
to_send = find_preferred_file
47+
to_send = find_preferred_file(
48+
params[:cookbook_id],
49+
:remote_file,
50+
params[:id],
51+
params[:fqdn],
52+
params[:platform],
53+
params[:version]
54+
)
6755
raise NotFound, "Cannot find a suitable file!" unless to_send
6856
current_checksum = checksum(to_send)
6957
Chef::Log.debug("old sum: #{params[:checksum]}, new sum: #{current_checksum}")
7058
if current_checksum == params[:checksum]
71-
display "File #{to_send} has not changed", :status => 304
59+
render "File #{to_send} has not changed", :status => 304
7260
else
7361
send_file(to_send)
7462
end
7563
end
7664

7765
def show_directory
78-
dir_to_send = find_preferred_file
66+
dir_to_send = find_preferred_file(
67+
params[:cookbook_id],
68+
:remote_file,
69+
params[:id],
70+
params[:fqdn],
71+
params[:platform],
72+
params[:version]
73+
)
7974
unless (dir_to_send && File.directory?(dir_to_send))
8075
raise NotFound, "Cannot find a suitable directory"
8176
end
@@ -90,30 +85,4 @@ def show_directory
9085
display @directory_listing
9186
end
9287

93-
protected
94-
95-
def find_preferred_file
96-
load_cookbook_files()
97-
preferences = [
98-
File.join("host-#{params[:fqdn]}", "#{params[:id]}"),
99-
File.join("#{params[:platform]}-#{params[:version]}", "#{params[:id]}"),
100-
File.join("#{params[:platform]}", "#{params[:id]}"),
101-
File.join("default", "#{params[:id]}")
102-
]
103-
to_send = nil
104-
@remote_files.each_key do |file|
105-
Chef::Log.debug("Looking at #{file}")
106-
preferences.each do |pref|
107-
Chef::Log.debug("Compared to #{pref}")
108-
if file =~ /#{pref}$/
109-
Chef::Log.debug("Matched #{pref} for #{file}!")
110-
to_send = file
111-
break
112-
end
113-
end
114-
break if to_send
115-
end
116-
to_send
117-
end
118-
11988
end

chef-server/lib/controllers/cookbook_templates.rb

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,58 @@
1818

1919
require 'chef' / 'mixin' / 'checksum'
2020
require 'chef' / 'cookbook_loader'
21+
require 'chef' / 'mixin' / 'find_preferred_file'
2122

2223
class CookbookTemplates < Application
2324

2425
provides :html, :json
2526

2627
include Chef::Mixin::Checksum
28+
include Chef::Mixin::FindPreferredFile
2729

28-
def load_cookbook_templates()
29-
@cl = Chef::CookbookLoader.new
30-
@cookbook = @cl[params[:cookbook_id]]
31-
raise NotFound unless @cookbook
32-
33-
@templates = Hash.new
34-
@cookbook.template_files.each do |tf|
35-
full = File.expand_path(tf)
36-
name = File.basename(full)
37-
tf =~ /^.+#{params[:cookbook_id]}[\\|\/]templates[\\|\/](.+?)[\\|\/]#{name}/
38-
singlecopy = $1
39-
@templates[full] = {
40-
:name => name,
41-
:singlecopy => singlecopy,
42-
:file => full,
43-
}
44-
end
45-
@templates
46-
end
30+
# def load_cookbook_templates()
31+
# @cl = Chef::CookbookLoader.new
32+
# @cookbook = @cl[params[:cookbook_id]]
33+
# raise NotFound unless @cookbook
34+
#
35+
# @templates = Hash.new
36+
# @cookbook.template_files.each do |tf|
37+
# full = File.expand_path(tf)
38+
# name = File.basename(full)
39+
# tf =~ /^.+#{params[:cookbook_id]}[\\|\/]templates[\\|\/](.+?)[\\|\/]#{name}/
40+
# singlecopy = $1
41+
# @templates[full] = {
42+
# :name => name,
43+
# :singlecopy => singlecopy,
44+
# :file => full,
45+
# }
46+
# end
47+
# @templates
48+
# end
4749

4850
def index
4951
if params[:id]
5052
show
5153
else
52-
load_cookbook_templates()
54+
@templates = load_cookbook_files(params[:cookbook_id], :template)
5355
display @templates
5456
end
5557
end
5658

5759
def show
58-
load_cookbook_templates()
59-
preferences = [
60-
File.join("host-#{params[:fqdn]}", "#{params[:id]}"),
61-
File.join("#{params[:platform]}-#{params[:version]}", "#{params[:id]}"),
62-
File.join("#{params[:platform]}", "#{params[:id]}"),
63-
File.join("default", "#{params[:id]}")
64-
]
65-
to_send = nil
66-
@templates.each_key do |file|
67-
Chef::Log.debug("Looking at #{file}")
68-
preferences.each do |pref|
69-
Chef::Log.debug("Compared to #{pref}")
70-
if file =~ /#{pref}/
71-
Chef::Log.debug("Matched #{pref} for #{file}!")
72-
to_send = file
73-
break
74-
end
75-
end
76-
break if to_send
77-
end
60+
to_send = find_preferred_file(
61+
params[:cookbook_id],
62+
:template,
63+
params[:id],
64+
params[:fqdn],
65+
params[:platform],
66+
params[:version]
67+
)
7868
raise NotFound, "Cannot find a suitable template!" unless to_send
7969
current_checksum = checksum(to_send)
8070
Chef::Log.debug("old sum: #{params[:checksum]}, new sum: #{current_checksum}")
8171
if current_checksum == params[:checksum]
82-
display "Template #{to_send} has not changed", :status => 304
72+
render "Template #{to_send} has not changed", :status => 304
8373
else
8474
send_file(to_send)
8575
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/cookbook_loader'
20+
21+
class Chef
22+
module Mixin
23+
module FindPreferredFile
24+
25+
def load_cookbook_files(cookbook_id, file_type)
26+
unless file_type == :remote_file || file_type == :template
27+
raise ArgumentError, "You must supply :remote_file or :template as the file_type"
28+
end
29+
30+
cl = Chef::CookbookLoader.new
31+
cookbook = cl[cookbook_id]
32+
raise NotFound unless cookbook
33+
34+
files = Hash.new
35+
36+
cookbook_method = nil
37+
38+
case file_type
39+
when :remote_file
40+
cookbook_method = :remote_files
41+
when :template
42+
cookbook_method = :template_files
43+
end
44+
45+
cookbook.send(cookbook_method).each do |rf|
46+
full = File.expand_path(rf)
47+
name = File.basename(full)
48+
case file_type
49+
when :remote_file
50+
rf =~ /^.+#{cookbook_id}[\\|\/]files[\\|\/](.+?)[\\|\/]#{name}/
51+
when :template
52+
rf =~ /^.+#{cookbook_id}[\\|\/]templates[\\|\/](.+?)[\\|\/]#{name}/
53+
end
54+
singlecopy = $1
55+
files[full] = {
56+
:name => name,
57+
:singlecopy => singlecopy,
58+
:file => full,
59+
}
60+
end
61+
Chef::Log.debug("Preferred #{file_type} list: #{files.inspect}")
62+
63+
files
64+
end
65+
66+
def find_preferred_file(cookbook_id, file_type, file_name, fqdn, platform, version)
67+
file_list = load_cookbook_files(cookbook_id, file_type)
68+
69+
preferences = [
70+
File.join("host-#{fqdn}", "#{file_name}"),
71+
File.join("#{platform}-#{version}", "#{file_name}"),
72+
File.join("#{platform}", "#{file_name}"),
73+
File.join("default", "#{file_name}")
74+
]
75+
to_send = nil
76+
77+
preferences.each do |pref|
78+
Chef::Log.debug("Looking for #{pref}")
79+
file_list.each_key do |file|
80+
Chef::Log.debug("Checking for #{pref} #{file} ")
81+
if file =~ /#{pref}$/
82+
Chef::Log.debug("Matched #{pref} for #{file}!")
83+
to_send = file
84+
break
85+
end
86+
end
87+
break if to_send
88+
end
89+
90+
to_send
91+
end
92+
93+
end
94+
end
95+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"You couldn't dam that river, and maybe I don't give a damn anyway"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#
2+
# Cookbook Name:: CHEF-37
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Example Com
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
cookbook_dir = "/var/chef/cookbooks/CHEF-37"
21+
22+
file_dir_list = {
23+
:fqdn => File.expand_path(File.join(cookbook_dir, "files", "host-#{node[:fqdn]}")),
24+
:default => File.expand_path(File.join(cookbook_dir, "files", "default")),
25+
:platform => File.expand_path(File.join(cookbook_dir, "files", node[:platform])),
26+
:platform_version => File.expand_path(File.join(cookbook_dir, "files", "#{node[:platform]}-#{node[:platform_version]}"))
27+
}
28+
29+
template_dir_list = {
30+
:fqdn => File.expand_path(File.join(cookbook_dir, "templates", "host-#{node[:fqdn]}")),
31+
:default => File.expand_path(File.join(cookbook_dir, "templates", "default")),
32+
:platform => File.expand_path(File.join(cookbook_dir, "templates", node[:platform])),
33+
:platform_version => File.expand_path(File.join(cookbook_dir, "templates", "#{node[:platform]}-#{node[:platform_version]}"))
34+
}
35+
36+
contents = "Frozen in the place I hide, not afraid to paint my sky with some who say I have lost my mind"
37+
template_contents = '<%= @who %> sky with some who say I have lost my mind'
38+
39+
# Create the directory and every version of the file
40+
[ :fqdn, :default, :platform, :platform_version ].each do |dirname|
41+
directory file_dir_list[dirname] do
42+
action :create
43+
end
44+
45+
execute "create-#{dirname}-chef-37.txt" do
46+
command "echo '#{dirname}: #{contents}' > #{file_dir_list[dirname]}/chef-37.txt"
47+
end
48+
49+
directory template_dir_list[dirname] do
50+
action :create
51+
end
52+
53+
execute "create-template-#{dirname}-chef-37.txt" do
54+
command "echo '#{dirname}: #{template_contents}' > #{template_dir_list[dirname]}/chef-37-#{dirname}.txt.erb"
55+
end
56+
end
57+
58+
# Then work down - fetch the file, should be the most specific one.
59+
# Then delete that one from the cookbooks on the server, since we don't want it to
60+
# interfere with our next test.
61+
[ :fqdn, :platform_version, :platform, :default ].each do |dirname|
62+
remote_file "/tmp/chef-37-#{dirname}.txt" do
63+
source "chef-37.txt"
64+
action :create
65+
end
66+
67+
file "#{file_dir_list[dirname]}/chef-37.txt" do
68+
action :delete
69+
end
70+
71+
template "/tmp/chef-template-37-#{dirname}.txt" do
72+
source "chef-37-#{dirname}.txt.erb"
73+
variables({
74+
:who => "paint my"
75+
})
76+
action :create
77+
end
78+
79+
file "#{template_dir_list[dirname]}/chef-37-#{dirname}.txt.erb" do
80+
action :delete
81+
end
82+
end

0 commit comments

Comments
 (0)