Class: Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookDir

Inherits:
ChefRepositoryFileSystemCookbookEntry show all
Defined in:
lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb

Overview

Represents ROOT/cookbooks/:cookbook

Instance Attribute Summary

Attributes inherited from ChefRepositoryFileSystemCookbookEntry

#file_path, #name, #parent, #path, #recursive, #ruby_only

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ChefRepositoryFileSystemCookbookEntry

#child, #compare_to, #create_child, #delete, #exists?, #initialize, #path_for_printing, #read, #root, #write_pretty_json

Constructor Details

This class inherits a constructor from Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookEntry

Class Method Details

.canonical_cookbook_name(entry_name) ⇒ Object

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


119
120
121
122
123
124
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 119

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

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)

107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 107

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?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 134

def can_upload?
  File.exist?(uploaded_cookbook_version_path) || children.size > 0
end

#canonical_cookbook_name(entry_name) ⇒ Object


126
127
128
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 126

def canonical_cookbook_name(entry_name)
  self.class.canonical_cookbook_name(entry_name)
end

#chef_objectObject

Customizations of base class


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 90

def chef_object
  cb = cookbook_version
  unless 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

#chefignoreObject

API Required by Repository::Directory


35
36
37
38
39
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 35

def chefignore
  @chefignore ||= Chef::Cookbook::Chefignore.new(file_path)
rescue Errno::EISDIR, Errno::EACCES
  # Work around a bug in Chefignore when chefignore is a directory
end

#childrenObject


103
104
105
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 103

def children
  super.select { |entry| !(entry.dir? && entry.children.size == 0 ) }
end

#create(file_contents = nil) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 60

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?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 56

def dir?
  true
end

#fs_entry_valid?Boolean

Returns:

  • (Boolean)

41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 41

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?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 52

def name_valid?
  !name.start_with?(".")
end

#uploaded_cookbook_version_pathObject


130
131
132
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 130

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) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 72

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
  unless File.exist?(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