summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2016-01-12 16:50:03 -0800
committerJohn Keiser <john@johnkeiser.com>2016-01-13 13:40:13 -0800
commitb750db5517c5c88e52f7bad9e75b9f7ababe9e42 (patch)
tree06d589ad33204fd272eb444e8c3a3fbc99570cdf
parent1edd3dba71b4531bb1a570dd7e387620e8ee61de (diff)
downloadchef-b750db5517c5c88e52f7bad9e75b9f7ababe9e42.tar.gz
Split versioned and unversioned cookbooks into separate classes
-rw-r--r--lib/chef/chef_fs/file_system/chef_server/chef_server_root_dir.rb3
-rw-r--r--lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb78
-rw-r--r--lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb90
3 files changed, 106 insertions, 65 deletions
diff --git a/lib/chef/chef_fs/file_system/chef_server/chef_server_root_dir.rb b/lib/chef/chef_fs/file_system/chef_server/chef_server_root_dir.rb
index 1ac03166c2..4524dda608 100644
--- a/lib/chef/chef_fs/file_system/chef_server/chef_server_root_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_server/chef_server_root_dir.rb
@@ -21,6 +21,7 @@ require 'chef/chef_fs/file_system/chef_server/acls_dir'
require 'chef/chef_fs/file_system/base_fs_dir'
require 'chef/chef_fs/file_system/chef_server/rest_list_dir'
require 'chef/chef_fs/file_system/chef_server/cookbooks_dir'
+require 'chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir'
require 'chef/chef_fs/file_system/chef_server/data_bags_dir'
require 'chef/chef_fs/file_system/chef_server/nodes_dir'
require 'chef/chef_fs/file_system/chef_server/org_entry'
@@ -141,7 +142,7 @@ class Chef
@children ||= begin
result = [
# /cookbooks
- CookbooksDir.new("cookbooks", self),
+ versioned_cookbooks ? VersionedCookbooksDir.new("cookbooks", self) : CookbooksDir.new("cookbooks", self),
# /data_bags
DataBagsDir.new("data_bags", self, "data"),
# /environments
diff --git a/lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb b/lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb
index 28d88ea330..060999339d 100644
--- a/lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb
@@ -29,6 +29,15 @@ class Chef
module ChefFS
module FileSystem
module ChefServer
+ # cookbooks/
+ # apache2/
+ # mysql/
+ # cookbook_artifacts/
+ # apache2/
+ # 1.0.0/
+ # 1.0.1/
+ # mysql/
+ # 2.0.5/
class CookbooksDir < RestListDir
include Chef::Mixin::FileClass
@@ -40,16 +49,7 @@ class Chef
def children
@children ||= begin
- if root.versioned_cookbooks
- result = []
- root.get_json("#{api_path}/?num_versions=all").each_pair do |cookbook_name, cookbooks|
- cookbooks['versions'].each do |cookbook_version|
- result << CookbookDir.new("#{cookbook_name}-#{cookbook_version['version']}", self, :exists => true)
- end
- end
- else
- result = root.get_json(api_path).keys.map { |cookbook_name| CookbookDir.new(cookbook_name, self, :exists => true) }
- end
+ result = root.get_json(api_path).keys.map { |cookbook_name| CookbookDir.new(cookbook_name, self, :exists => true) }
result.sort_by(&:name)
end
end
@@ -60,7 +60,7 @@ class Chef
end
def upload_cookbook_from(other, options = {})
- root.versioned_cookbooks ? upload_versioned_cookbook(other, options) : upload_unversioned_cookbook(other, options)
+ upload_cookbook(other, options)
rescue Timeout::Error => e
raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e, "Timeout writing: #{e}")
rescue Net::HTTPServerException => e
@@ -74,53 +74,13 @@ class Chef
raise Chef::ChefFS::FileSystem::CookbookFrozenError.new(:write, self, e, "Cookbook #{other.name} is frozen")
end
- # Knife currently does not understand versioned cookbooks
- # Cookbook Version uploader also requires a lot of refactoring
- # to make this work. So instead, we make a temporary cookbook
- # symlinking back to real cookbook, and upload the proxy.
- def upload_versioned_cookbook(other, options)
- cookbook_name = Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookDir.canonical_cookbook_name(other.name)
-
- Dir.mktmpdir do |temp_cookbooks_path|
- proxy_cookbook_path = "#{temp_cookbooks_path}/#{cookbook_name}"
-
- # Make a symlink
- file_class.symlink other.file_path, proxy_cookbook_path
-
- # Instantiate a proxy loader using the temporary symlink
- proxy_loader = Chef::Cookbook::CookbookVersionLoader.new(proxy_cookbook_path, other.parent.chefignore)
- proxy_loader.load_cookbooks
-
- cookbook_to_upload = proxy_loader.cookbook_version
- cookbook_to_upload.freeze_version if options[:freeze]
-
- # Instantiate a new uploader based on the proxy loader
- uploader = Chef::CookbookUploader.new(cookbook_to_upload, :force => options[:force], :rest => root.chef_rest)
-
- with_actual_cookbooks_dir(temp_cookbooks_path) do
- upload_cookbook!(uploader)
- end
-
- #
- # When the temporary directory is being deleted on
- # windows, the contents of the symlink under that
- # directory is also deleted. So explicitly remove
- # the symlink without removing the original contents if we
- # are running on windows
- #
- if Chef::Platform.windows?
- Dir.rmdir proxy_cookbook_path
- end
- end
- end
-
- def upload_unversioned_cookbook(other, options)
+ def upload_cookbook(other, options)
cookbook_to_upload = other.chef_object
cookbook_to_upload.freeze_version if options[:freeze]
uploader = Chef::CookbookUploader.new(cookbook_to_upload, :force => options[:force], :rest => root.chef_rest)
with_actual_cookbooks_dir(other.parent.file_path) do
- upload_cookbook!(uploader)
+ uploader.upload_cookbooks
end
end
@@ -134,18 +94,8 @@ class Chef
Chef::Config.cookbook_path = old_cookbook_path
end
- def upload_cookbook!(uploader, options = {})
- if uploader.respond_to?(:upload_cookbook)
- uploader.upload_cookbook
- else
- uploader.upload_cookbooks
- end
- end
-
def can_have_child?(name, is_dir)
- return false if !is_dir
- return false if root.versioned_cookbooks && name !~ Chef::ChefFS::FileSystem::ChefServer::CookbookDir::VALID_VERSIONED_COOKBOOK_NAME
- return true
+ is_dir
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb b/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb
new file mode 100644
index 0000000000..3ead25f533
--- /dev/null
+++ b/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb
@@ -0,0 +1,90 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2012 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/chef_fs/file_system/chef_server/cookbooks_dir'
+
+class Chef
+ module ChefFS
+ module FileSystem
+ module ChefServer
+ # cookbooks/
+ # apache2-1.0.0/
+ # apache2-1.0.1/
+ # mysql-2.0.5/
+ class VersionedCookbooksDir < CookbooksDir
+
+ def children
+ @children ||= begin
+ result = []
+ root.get_json("#{api_path}/?num_versions=all").each_pair do |cookbook_name, cookbooks|
+ cookbooks['versions'].each do |cookbook_version|
+ result << CookbookDir.new("#{cookbook_name}-#{cookbook_version['version']}", self, :exists => true)
+ end
+ end
+ result.sort_by(&:name)
+ end
+ end
+
+ # Knife currently does not understand versioned cookbooks
+ # Cookbook Version uploader also requires a lot of refactoring
+ # to make this work. So instead, we make a temporary cookbook
+ # symlinking back to real cookbook, and upload the proxy.
+ def upload_cookbook(other, options)
+ cookbook_name = Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookDir.canonical_cookbook_name(other.name)
+
+ Dir.mktmpdir do |temp_cookbooks_path|
+ proxy_cookbook_path = "#{temp_cookbooks_path}/#{cookbook_name}"
+
+ # Make a symlink
+ file_class.symlink other.file_path, proxy_cookbook_path
+
+ # Instantiate a proxy loader using the temporary symlink
+ proxy_loader = Chef::Cookbook::CookbookVersionLoader.new(proxy_cookbook_path, other.parent.chefignore)
+ proxy_loader.load_cookbooks
+
+ cookbook_to_upload = proxy_loader.cookbook_version
+ cookbook_to_upload.freeze_version if options[:freeze]
+
+ # Instantiate a new uploader based on the proxy loader
+ uploader = Chef::CookbookUploader.new(cookbook_to_upload, :force => options[:force], :rest => root.chef_rest)
+
+ with_actual_cookbooks_dir(temp_cookbooks_path) do
+ uploader.upload_cookbooks
+ end
+
+ #
+ # When the temporary directory is being deleted on
+ # windows, the contents of the symlink under that
+ # directory is also deleted. So explicitly remove
+ # the symlink without removing the original contents if we
+ # are running on windows
+ #
+ if Chef::Platform.windows?
+ Dir.rmdir proxy_cookbook_path
+ end
+ end
+ end
+
+ def can_have_child?(name, is_dir)
+ is_dir && name =~ Chef::ChefFS::FileSystem::ChefServer::CookbookDir::VALID_VERSIONED_COOKBOOK_NAME
+ end
+ end
+ end
+ end
+ end
+end