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


110
111
112
113
114
# 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

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
102
103
104
105
106
107
# 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?Boolean

Returns:

  • (Boolean)

124
125
126
# 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) ⇒ Object


116
117
118
# 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_objectObject

Customizations of base class


82
83
84
85
86
87
88
89
90
91
92
93
# 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

#childrenObject


95
96
97
# 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) ⇒ Object


53
54
55
56
57
58
59
60
61
62
# 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?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_dir.rb', line 49

def dir?
  true
end

#fs_entry_valid?Boolean

API Required by Respository::Directory

Returns:

  • (Boolean)

35
36
37
38
39
40
41
42
43
# 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?Boolean

Returns:

  • (Boolean)

45
46
47
# 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_pathObject


120
121
122
# 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) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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