class Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookDir

Represents ROOT/cookbooks/:cookbook

Public Class Methods

canonical_cookbook_name(entry_name) click to toggle source

Exposed as a class method so that it can be used elsewhere

# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 110
def self.canonical_cookbook_name(entry_name)
  name_match = Chef::ChefFS::FileSystem::ChefServer::VersionedCookbookDir::VALID_VERSIONED_COOKBOOK_NAME.match(entry_name)
  return nil if name_match.nil?
  name_match[1]
end

Public Instance Methods

can_have_child?(name, is_dir) click to toggle source
Calls superclass method
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 99
def can_have_child?(name, is_dir)
  if is_dir && !%w{ root_files .. . }.include?(name)
    # Only the given directories will be uploaded.
    return true
  elsif name == Chef::Cookbook::CookbookVersionLoader::UPLOADED_COOKBOOK_VERSION_FILE
    return false
  end
  super(name, is_dir)
end
can_upload?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 124
def can_upload?
  File.exists?(uploaded_cookbook_version_path) || children.size > 0
end
canonical_cookbook_name(entry_name) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 116
def canonical_cookbook_name(entry_name)
  self.class.canonical_cookbook_name(entry_name)
end
chef_object() click to toggle source

Customizations of base class

# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 82
def chef_object
  cb = cookbook_version
  if !cb
    Chef::Log.error("Cookbook #{file_path} empty.")
    raise "Cookbook #{file_path} empty."
  end
  cb
rescue => e
  Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{e}")
  Chef::Log.error(e.backtrace.join("\n"))
  raise
end
children() click to toggle source
Calls superclass method
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 95
def children
  super.select { |entry| !(entry.dir? && entry.children.size == 0 ) }
end
create(file_contents = nil) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 53
def create(file_contents = nil)
  if exists?
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self)
  end
  begin
    Dir.mkdir(file_path)
  rescue Errno::EEXIST
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self)
  end
end
dir?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 49
def dir?
  true
end
fs_entry_valid?() click to toggle source

API Required by Respository::Directory

# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 35
def fs_entry_valid?
  return false unless File.directory?(file_path) && name_valid?
  if can_upload?
    true
  else
    Chef::Log.warn("Cookbook '#{name}' is empty or entirely chefignored at #{path_for_printing}")
    false
  end
end
name_valid?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 45
def name_valid?
  !name.start_with?(".")
end
uploaded_cookbook_version_path() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 120
def uploaded_cookbook_version_path
  File.join(file_path, Chef::Cookbook::CookbookVersionLoader::UPLOADED_COOKBOOK_VERSION_FILE)
end
write(cookbook_path, cookbook_version_json, from_fs) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 64
def write(cookbook_path, cookbook_version_json, from_fs)
  # Use the copy/diff algorithm to copy it down so we don't destroy
  # chefignored data.  This is terribly un-thread-safe.
  Chef::ChefFS::FileSystem.copy_to(Chef::ChefFS::FilePattern.new("/#{cookbook_path}"), from_fs, self, nil, { purge: true })

  # Write out .uploaded-cookbook-version.json
  # cookbook_file_path = File.join(file_path, cookbook_name) <- this should be the same as self.file_path
  if !File.exists?(file_path)
    FileUtils.mkdir_p(file_path)
  end
  uploaded_cookbook_version_path = File.join(file_path, Chef::Cookbook::CookbookVersionLoader::UPLOADED_COOKBOOK_VERSION_FILE)
  File.open(uploaded_cookbook_version_path, "w") do |file|
    file.write(cookbook_version_json)
  end
end

Protected Instance Methods

cookbook_version() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 134
def cookbook_version
  loader = Chef::Cookbook::CookbookVersionLoader.new(file_path, parent.chefignore)
  loader.load_cookbooks
  loader.cookbook_version
end
make_child_entry(child_name) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb, line 130
def make_child_entry(child_name)
  ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, false, true)
end