Skip to content

Commit

Permalink
Fixing CHEF-749, we now properly cache cookbooks again.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhjk committed Nov 27, 2009
1 parent bc60ba1 commit f7c5d64
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ namespace :features do
Cucumber::Rake::Task.new(:run_interval) do |t|
t.profile = "client_run_interval"
end

Cucumber::Rake::Task.new(:cookbook_sync) do |t|
t.profile = "client_cookbook_sync"
end
end

desc "Run cucumber tests for the cookbooks"
Expand Down
37 changes: 23 additions & 14 deletions chef/lib/chef/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ def update_file_cache(cookbook_name, parts)
current_checksum = checksum(Chef::FileCache.load(cache_file, false))
end

rf_url = generate_cookbook_url(
rf['name'],
cookbook_name,
segment,
@node,
current_checksum ? { 'checksum' => current_checksum } : nil
)
if current_checksum != rf['checksum']
rf_url = generate_cookbook_url(
rf['name'],
cookbook_name,
segment,
@node,
current_checksum ? { 'checksum' => current_checksum } : nil
)

changed = true
begin
raw_file = @rest.get_rest(rf_url, true)
Expand All @@ -250,15 +251,15 @@ def update_file_cache(cookbook_name, parts)
end
end

Chef::FileCache.list.each do |cache_file|
if cache_file =~ /^cookbooks\/(recipes|attributes|definitions|libraries)\//
unless file_canonical[cache_file]
Chef::Log.info("Removing #{cache_file} from the cache; it is no longer on the server.")
Chef::FileCache.delete(cache_file)
end
end

Chef::FileCache.list.each do |cache_file|
if cache_file =~ /^cookbooks\/#{cookbook_name}\/(recipes|attributes|definitions|libraries|resources|providers)\//
unless file_canonical[cache_file]
Chef::Log.info("Removing #{cache_file} from the cache; it is no longer on the server.")
Chef::FileCache.delete(cache_file)
end
end

end

end
Expand All @@ -271,6 +272,14 @@ def sync_cookbooks
Chef::Log.debug("Synchronizing cookbooks")
cookbook_hash = @rest.get_rest("nodes/#{@safe_name}/cookbooks")
Chef::Log.debug("Cookbooks to load: #{cookbook_hash.inspect}")
Chef::FileCache.list.each do |cache_file|
if cache_file =~ /^cookbooks\/(.+?)\//
unless cookbook_hash.has_key?($1)
Chef::Log.info("Removing #{cache_file} from the cache; it's cookbook is no longer needed on this client.")
Chef::FileCache.delete(cache_file)
end
end
end
cookbook_hash.each do |cookbook_name, parts|
update_file_cache(cookbook_name, parts)
end
Expand Down
1 change: 1 addition & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ api_search_list: --tags @api_search_list --format pretty -r features/steps -r fe
api_search_show: --tags @api_search_show --format pretty -r features/steps -r features/support features
client: --tags @client --format pretty -r features/steps -r features/support features
client_roles: --tags client_roles --format pretty -r features/steps -r features/support features
client_cookbook_sync: --tags @client-cookbook-sync --format pretty -r features/steps -r features/support features
search: --tags @search --format pretty -r features/steps -r features/support features
provider: --tags @provider --format pretty -r features/steps -r features/support features
provider_directory: --tags @provider_directory --format pretty -r features/steps -r features/support features
Expand Down
24 changes: 23 additions & 1 deletion features/chef-client/cookbook_sync.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@client
@client @client-cookbook-sync
Feature: Synchronize cookbooks from the server
In order to configure a system according to a centralized repository
As an Administrator
Expand All @@ -19,3 +19,25 @@ Feature: Synchronize cookbooks from the server
And 'stdout' should have 'INFO: Storing updated cookbooks/synchronize_deps/recipes/default.rb in the cache.'
And 'stdout' should have 'INFO: Storing updated cookbooks/synchronize/recipes/default.rb in the cache.'

Scenario: Removes files from the cache that are no longer needed
Given a validated node
And it includes the recipe 'synchronize_deps'
When I run the chef-client with '-l info'
Then the run should exit '0'
And 'stdout' should have 'INFO: Storing updated cookbooks/synchronize_deps/recipes/default.rb in the cache.'
Given we have an empty file named 'cookbooks/synchronize_deps/recipes/woot.rb' in the client cache
When I run the chef-client with '-l info'
Then the run should exit '0'
And 'stdout' should have 'INFO: Removing cookbooks/synchronize_deps/recipes/woot.rb from the cache; it is no longer on the server.'

Scenario: Remove cookbooks that are no longer needed
Given a validated node
And it includes the recipe 'synchronize_deps'
When I run the chef-client with '-l info'
Then the run should exit '0'
And 'stdout' should have 'INFO: Storing updated cookbooks/synchronize_deps/recipes/default.rb in the cache.'
Given it includes no recipes
When I run the chef-client with '-l info'
Then the run should exit '0'
And 'stdout' should have 'INFO: Removing cookbooks/synchronize_deps/recipes/default.rb from the cache; it's cookbook is no longer needed on this client.'

6 changes: 6 additions & 0 deletions features/steps/file_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
filename.close
end

Given /^we have an empty file named '(.+)' in the client cache$/ do |filename|
cache_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "tmp", "cache"))
filename = File.new(File.join(cache_dir, filename), 'w')
filename.close
end

Given /^we have the atime\/mtime of '(.+)'$/ do |filename|
@mtime = File.mtime(File.join(tmpdir, filename))
@atime = File.atime(File.join(tmpdir, filename))
Expand Down
6 changes: 6 additions & 0 deletions features/steps/node_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
client.save_node
end

Given /^it includes no recipes$/ do
self.recipe = ""
client.node.run_list.reset
client.save_node
end

Given /^it includes the role '(.+)'$/ do |role|
self.recipe = "role[#{role}]"
client.node.run_list << "role[#{role}]"
Expand Down

0 comments on commit f7c5d64

Please sign in to comment.