diff options
author | John Keiser <jkeiser@opscode.com> | 2014-08-31 10:07:21 -0700 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2014-09-05 12:09:08 -0700 |
commit | 865ae8a61ccb3395c7f738871be0bf7a2d1b02ce (patch) | |
tree | 2e60dd013042dd4091a30206e33d5b7becce5b8e /lib/chef | |
parent | 27b05b2396459f3d84f0ebd924f8adc44a906cd1 (diff) | |
download | chef-865ae8a61ccb3395c7f738871be0bf7a2d1b02ce.tar.gz |
Add org.json, members.json, invitations.json for full org download
and upload support
Diffstat (limited to 'lib/chef')
13 files changed, 288 insertions, 23 deletions
diff --git a/lib/chef/chef_fs/config.rb b/lib/chef/chef_fs/config.rb index 536409a109..c9f187a3bd 100644 --- a/lib/chef/chef_fs/config.rb +++ b/lib/chef/chef_fs/config.rb @@ -68,7 +68,7 @@ class Chef def create_local_fs require 'chef/chef_fs/file_system/chef_repository_file_system_root_dir' - Chef::ChefFS::FileSystem::ChefRepositoryFileSystemRootDir.new(object_paths) + Chef::ChefFS::FileSystem::ChefRepositoryFileSystemRootDir.new(object_paths, Array(chef_config[:chef_repo_path]).flatten) end # Returns the given real path's location relative to the server root. diff --git a/lib/chef/chef_fs/data_handler/organization_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_data_handler.rb new file mode 100644 index 0000000000..1f2f9ffaf5 --- /dev/null +++ b/lib/chef/chef_fs/data_handler/organization_data_handler.rb @@ -0,0 +1,30 @@ +require 'chef/chef_fs/data_handler/data_handler_base' + +class Chef + module ChefFS + module DataHandler + class OrganizationDataHandler < DataHandlerBase + def normalize(organization, entry) + result = normalize_hash(organization, { + 'name' => entry.org, + 'full_name' => entry.org, + 'org_type' => 'Business', + 'clientname' => "#{entry.org}-validator", + 'billing_plan' => 'platform-free', + }) + result + end + + def preserve_key(key) + return key == 'name' + end + + def verify_integrity(object, entry, &on_error) + if entry.org != object['name'] + on_error.call("Name must be '#{entry.org}' (is '#{object['name']}')") + end + end + end + end + end +end diff --git a/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb new file mode 100644 index 0000000000..db56ecc504 --- /dev/null +++ b/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb @@ -0,0 +1,17 @@ +require 'chef/chef_fs/data_handler/data_handler_base' + +class Chef + module ChefFS + module DataHandler + class OrganizationInvitesDataHandler < DataHandlerBase + def normalize(invites, entry) + invites.map { |invite| invite.is_a?(Hash) ? invite['username'] : invite }.sort.uniq + end + + def minimize(invites, entry) + invites + end + end + end + end +end diff --git a/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb new file mode 100644 index 0000000000..afa331775c --- /dev/null +++ b/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb @@ -0,0 +1,17 @@ +require 'chef/chef_fs/data_handler/data_handler_base' + +class Chef + module ChefFS + module DataHandler + class OrganizationMembersDataHandler < DataHandlerBase + def normalize(members, entry) + members.map { |member| member.is_a?(Hash) ? member['user']['username'] : member }.sort.uniq + end + + def minimize(members, entry) + members + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system.rb b/lib/chef/chef_fs/file_system.rb index 4d15d7af33..730fa0e5cc 100644 --- a/lib/chef/chef_fs/file_system.rb +++ b/lib/chef/chef_fs/file_system.rb @@ -273,7 +273,6 @@ class Chef # case we shouldn't waste time trying PUT if we know the file doesn't # exist. # Will need to decide how that works with checksums, though. - error = false begin dest_path = format_path.call(dest_entry) if ui diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb index 6ccdc2cf5f..9acfe4b936 100644 --- a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb @@ -31,8 +31,12 @@ class Chef @data_handler = data_handler end + def write_pretty_json=(value) + @write_pretty_json = value + end + def write_pretty_json - root.write_pretty_json + @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json end def data_handler diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb index d615e0f415..14f3e8413b 100644 --- a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb @@ -34,31 +34,50 @@ class Chef module ChefFS module FileSystem class ChefRepositoryFileSystemRootDir < BaseFSDir - def initialize(child_paths) + def initialize(child_paths, root_paths=nil) super("", nil) @child_paths = child_paths + @root_paths = root_paths end attr_accessor :write_pretty_json + attr_reader :root_paths attr_reader :child_paths + CHILDREN = %w(invitations.json members.json org.json) + def children - @children ||= child_paths.keys.sort.map { |name| make_child_entry(name) }.select { |child| !child.nil? } + @children ||= begin + result = child_paths.keys.sort.map { |name| make_child_entry(name) }.select { |child| !child.nil? } + result += root_dir.children.select { |c| CHILDREN.include?(c.name) } if root_dir + result.sort_by { |c| c.name } + end end def can_have_child?(name, is_dir) - child_paths.has_key?(name) && is_dir + if is_dir + child_paths.has_key?(name) + elsif root_paths + CHILDREN.include?(name) + else + false + end end def create_child(name, file_contents = nil) - child_paths[name].each do |path| - begin - Dir.mkdir(path) - rescue Errno::EEXIST + if file_contents + d = root_dir + child = root_dir.create_child(name, file_contents) + else + child_paths[name].each do |path| + begin + Dir.mkdir(path) + rescue Errno::EEXIST + end end + child = make_child_entry(name) end - child = make_child_entry(name) @children = nil child end @@ -69,15 +88,15 @@ class Chef # Used to print out the filesystem def fs_description - repo_path = File.dirname(child_paths['cookbooks'][0]) - result = "repository at #{repo_path}\n" + repo_paths = root_paths || [ File.dirname(child_paths['cookbooks'][0]) ] + result = "repository at #{repo_paths.join(', ')}\n" if Chef::Config[:versioned_cookbooks] result << " Multiple versions per cookbook\n" else result << " One version per cookbook\n" end child_paths.each_pair do |name, paths| - if paths.any? { |path| File.dirname(path) != repo_path } + if paths.any? { |path| !repo_paths.include?(File.dirname(path)) } result << " #{name} at #{paths.join(', ')}\n" end end @@ -86,6 +105,14 @@ class Chef private + def root_dir + MultiplexedDir.new(root_paths.select { |path| File.exists?(path) }.map do |path| + dir = ChefRepositoryFileSystemEntry.new(name, parent, path) + dir.write_pretty_json = !!write_pretty_json + dir + end) + end + def make_child_entry(name) paths = child_paths[name].select do |path| File.exists?(path) diff --git a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb index 0083ee4cfa..069a2e29fa 100644 --- a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb @@ -23,6 +23,9 @@ require 'chef/chef_fs/file_system/rest_list_dir' require 'chef/chef_fs/file_system/cookbooks_dir' require 'chef/chef_fs/file_system/data_bags_dir' require 'chef/chef_fs/file_system/nodes_dir' +require 'chef/chef_fs/file_system/org_entry' +require 'chef/chef_fs/file_system/organization_invites_entry' +require 'chef/chef_fs/file_system/organization_members_entry' require 'chef/chef_fs/file_system/environments_dir' require 'chef/chef_fs/data_handler/client_data_handler' require 'chef/chef_fs/data_handler/role_data_handler' @@ -81,10 +84,13 @@ class Chef end def org - @org ||= if URI.parse(chef_server_url).path =~ /^\/+organizations\/+([^\/]+)$/ - $1 - else - nil + @org ||= begin + path = Pathname.new(URI.parse(chef_server_url).path).cleanpath + if File.dirname(path) == '/organizations' + File.basename(path) + else + nil + end end end @@ -102,7 +108,10 @@ class Chef RestListDir.new("clients", self, nil, Chef::ChefFS::DataHandler::ClientDataHandler.new), RestListDir.new("containers", self, nil, Chef::ChefFS::DataHandler::ContainerDataHandler.new), RestListDir.new("groups", self, nil, Chef::ChefFS::DataHandler::GroupDataHandler.new), - NodesDir.new(self) + NodesDir.new(self), + OrgEntry.new("org.json", self), + OrganizationMembersEntry.new("members.json", self), + OrganizationInvitesEntry.new("invitations.json", self) ] elsif repo_mode != 'static' result += [ diff --git a/lib/chef/chef_fs/file_system/org_entry.rb b/lib/chef/chef_fs/file_system/org_entry.rb new file mode 100644 index 0000000000..852956e1e5 --- /dev/null +++ b/lib/chef/chef_fs/file_system/org_entry.rb @@ -0,0 +1,34 @@ +require 'chef/chef_fs/file_system/rest_list_entry' +require 'chef/chef_fs/data_handler/organization_data_handler' + +class Chef + module ChefFS + module FileSystem + # /organizations/NAME/org.json + # Represents the actual data at /organizations/NAME (the full name, etc.) + class OrgEntry < RestListEntry + def initialize(name, parent, exists = nil) + super(name, parent) + @exists = exists + end + + def data_handler + Chef::ChefFS::DataHandler::OrganizationDataHandler.new + end + + # /organizations/foo/org.json -> GET /organizations/foo + def api_path + parent.api_path + end + + def exists? + parent.exists? + end + + def delete(recurse) + raise Chef::ChefFS::FileSystem::OperationNotAllowedError.new(:delete, self) + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system/organization_invites_entry.rb b/lib/chef/chef_fs/file_system/organization_invites_entry.rb new file mode 100644 index 0000000000..a83e058b77 --- /dev/null +++ b/lib/chef/chef_fs/file_system/organization_invites_entry.rb @@ -0,0 +1,59 @@ +require 'chef/chef_fs/file_system/rest_list_entry' +require 'chef/chef_fs/data_handler/organization_invites_data_handler' + +class Chef + module ChefFS + module FileSystem + # /organizations/NAME/invitations.json + # Represents the actual data at + # read: + # - GET /organizations/NAME/association_requests + # write: + # - remove from list: DELETE /organizations/NAME/association_requests/id + # - add to list: POST /organizations/NAME/association_requests + class OrganizationInvitesEntry < RestListEntry + def initialize(name, parent, exists = nil) + super(name, parent) + @exists = exists + end + + def data_handler + Chef::ChefFS::DataHandler::OrganizationInvitesDataHandler.new + end + + # /organizations/foo/invites.json -> /organizations/foo/association_requests + def api_path + File.join(parent.api_path, 'association_requests') + end + + def exists? + parent.exists? + end + + def delete(recurse) + raise Chef::ChefFS::FileSystem::OperationNotAllowedError.new(:delete, self) + end + + def write(contents) + desired_invites = minimize_value(JSON.parse(contents, :create_additions => false)) + actual_invites = _read_json.inject({}) { |h,val| h[val['username']] = val['id']; h } + invites = actual_invites.keys + (desired_invites - invites).each do |invite| + begin + rest.post(api_path, { 'user' => invite }) + rescue Net::HTTPServerException => e + if e.response.code == '409' + Chef::Log.warn("Could not invite #{invite} to organization #{org}: #{api_error_text(e.response)}") + else + raise + end + end + end + (invites - desired_invites).each do |invite| + rest.delete(File.join(api_path, actual_invites[invite])) + end + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system/organization_members_entry.rb b/lib/chef/chef_fs/file_system/organization_members_entry.rb new file mode 100644 index 0000000000..c1151413f5 --- /dev/null +++ b/lib/chef/chef_fs/file_system/organization_members_entry.rb @@ -0,0 +1,58 @@ +require 'chef/chef_fs/file_system/rest_list_entry' +require 'chef/chef_fs/data_handler/organization_members_data_handler' + +class Chef + module ChefFS + module FileSystem + # /organizations/NAME/members.json + # Represents the actual data at + # read: + # - GET /organizations/NAME/users + # write: + # - remove from list: DELETE /organizations/NAME/users/name + # - add to list: POST /organizations/NAME/users/name + class OrganizationMembersEntry < RestListEntry + def initialize(name, parent, exists = nil) + super(name, parent) + @exists = exists + end + + def data_handler + Chef::ChefFS::DataHandler::OrganizationMembersDataHandler.new + end + + # /organizations/foo/members.json -> /organizations/foo/users + def api_path + File.join(parent.api_path, 'users') + end + + def exists? + parent.exists? + end + + def delete(recurse) + raise Chef::ChefFS::FileSystem::OperationNotAllowedError.new(:delete, self) + end + + def write(contents) + desired_members = minimize_value(JSON.parse(contents, :create_additions => false)) + members = minimize_value(_read_json) + (desired_members - members).each do |member| + begin + rest.post(File.join(api_path, member), {}) + rescue Net::HTTPServerException => e + if e.response.code == '404' + raise "Chef server at #{api_path} does not allow you to directly add members. Please either upgrade your Chef server or move the users you want into invitations.json instead of members.json." + else + raise + end + end + end + (members - desired_members).each do |member| + rest.delete(File.join(api_path, member)) + end + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system/rest_list_entry.rb b/lib/chef/chef_fs/file_system/rest_list_entry.rb index 67252a6f2f..ac47ff4f25 100644 --- a/lib/chef/chef_fs/file_system/rest_list_entry.rb +++ b/lib/chef/chef_fs/file_system/rest_list_entry.rb @@ -80,13 +80,13 @@ class Chef end def read - Chef::JSONCompat.to_json_pretty(_read_hash) + Chef::JSONCompat.to_json_pretty(minimize_value(_read_json)) end - def _read_hash + def _read_json begin # Minimize the value (get rid of defaults) so the results don't look terrible - minimize_value(root.get_json(api_path)) + root.get_json(api_path) rescue Timeout::Error => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "Timeout reading: #{e}" rescue Net::HTTPServerException => e @@ -119,7 +119,7 @@ class Chef # Grab this value begin - value = _read_hash + value = _read_json rescue Chef::ChefFS::FileSystem::NotFoundError return [ false, :none, other_value_json ] end @@ -169,7 +169,16 @@ class Chef end end end + + def api_error_text(response) + begin + JSON.parse(response.body)['error'].join("\n") + rescue + response.body + end + end end + end end end diff --git a/lib/chef/chef_fs/parallelizer.rb b/lib/chef/chef_fs/parallelizer.rb index 116a626869..f29a2deae7 100644 --- a/lib/chef/chef_fs/parallelizer.rb +++ b/lib/chef/chef_fs/parallelizer.rb @@ -53,6 +53,7 @@ class Chef end def resize(to_threads, wait = true, timeout = nil) + to_threads = 0 if to_threads < num_threads threads_to_stop = @threads[to_threads..num_threads-1] @threads = @threads.slice(0, to_threads) @@ -89,6 +90,7 @@ class Chef begin while !@stop_thread[Thread.current] begin + puts "Got a task!" task = @tasks.pop task.call rescue |