Class: Chef::ChefFS::FileSystem::ChefServer::CookbooksDir

Inherits:
RestListDir show all
Includes:
Mixin::FileClass
Defined in:
lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb

Overview

/cookbooks

Example children: apache2/ mysql/

Direct Known Subclasses

CookbookArtifactsDir, VersionedCookbooksDir

Instance Attribute Summary

Attributes inherited from RestListDir

#api_path, #data_handler

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods included from Mixin::FileClass

#file_class

Methods inherited from RestListDir

#create_child, #environment, #initialize, #org, #rest

Methods inherited from BaseFSDir

#dir?, #empty?, #initialize

Methods inherited from BaseFSObject

#chef_object, #child, #compare_to, #create_child, #delete, #dir?, #exists?, #initialize, #path_for_printing, #read, #root, #write

Constructor Details

This class inherits a constructor from Chef::ChefFS::FileSystem::ChefServer::RestListDir

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 98

def can_have_child?(name, is_dir)
  is_dir
end

#chef_restObject


84
85
86
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 84

def chef_rest
  Chef::ServerAPI.new(root.chef_rest.url, root.chef_rest.options.merge(version_class: Chef::CookbookManifestVersions))
end

#childrenObject


47
48
49
50
51
52
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 47

def children
  @children ||= begin
    result = root.get_json(api_path).keys.map { |cookbook_name| CookbookDir.new(cookbook_name, self, exists: true) }
    result.sort_by(&:name)
  end
end

#create_child_from(other, options = {}) ⇒ Object


54
55
56
57
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 54

def create_child_from(other, options = {})
  @children = nil
  upload_cookbook_from(other, options)
end

#make_child_entry(name) ⇒ Object


42
43
44
45
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 42

def make_child_entry(name)
  result = @children.find { |child| child.name == name } if @children
  result || CookbookDir.new(name, self)
end

#upload_cookbook(other, options) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 74

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 => chef_rest)

  with_actual_cookbooks_dir(other.parent.file_path) do
    uploader.upload_cookbooks
  end
end

#upload_cookbook_from(other, options = {}) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 59

def upload_cookbook_from(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
  case e.response.code
  when "409"
    raise Chef::ChefFS::FileSystem::CookbookFrozenError.new(:write, self, e, "Cookbook #{other.name} is frozen")
  else
    raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e, "HTTP error writing: #{e}")
  end
rescue Chef::Exceptions::CookbookFrozen => e
  raise Chef::ChefFS::FileSystem::CookbookFrozenError.new(:write, self, e, "Cookbook #{other.name} is frozen")
end

#with_actual_cookbooks_dir(actual_cookbook_path) ⇒ Object

Work around the fact that CookbookUploader doesn't understand chef_repo_path (yet)


89
90
91
92
93
94
95
96
# File 'lib/chef/chef_fs/file_system/chef_server/cookbooks_dir.rb', line 89

def with_actual_cookbooks_dir(actual_cookbook_path)
  old_cookbook_path = Chef::Config.cookbook_path
  Chef::Config.cookbook_path = actual_cookbook_path if !Chef::Config.cookbook_path

  yield
ensure
  Chef::Config.cookbook_path = old_cookbook_path
end