Class: Berkshelf::CachedCookbook

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/berkshelf/cached_cookbook.rb

Overview

Author:

Constant Summary collapse

DIRNAME_REGEXP =
/^(.+)-(.+)$/
CHEF_TYPE =
"cookbook_version".freeze
CHEF_JSON_CLASS =
"Chef::CookbookVersion".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, metadata) ⇒ CachedCookbook

Returns a new instance of CachedCookbook.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/berkshelf/cached_cookbook.rb', line 98

def initialize(name, path, )
  @cookbook_name = name
  @path = Pathname.new(path)
  @metadata = 
  @files = Array.new
  @manifest = Mash.new(
    recipes: Array.new,
    definitions: Array.new,
    libraries: Array.new,
    attributes: Array.new,
    files: Array.new,
    templates: Array.new,
    resources: Array.new,
    providers: Array.new,
    root_files: Array.new
  )

  load_files
end

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



72
73
74
# File 'lib/berkshelf/cached_cookbook.rb', line 72

def cookbook_name
  @cookbook_name
end

#manifestMash (readonly)

Returns a Mash containing Cookbook file category names as keys and an Array of Hashes containing metadata about the files belonging to that category. This is used to communicate what a Cookbook looks like when uploading to a Chef Server.

example:

{
  :recipes => [
    {
      name: "default.rb",
      path: "recipes/default.rb",
      checksum: "fb1f925dcd5fc4ebf682c4442a21c619",
      specificity: "default"
    }
  ]
  ...
  ...
}.

Returns:

  • (Mash)

    a Mash containing Cookbook file category names as keys and an Array of Hashes containing metadata about the files belonging to that category. This is used to communicate what a Cookbook looks like when uploading to a Chef Server.

    example:

    {
      :recipes => [
        {
          name: "default.rb",
          path: "recipes/default.rb",
          checksum: "fb1f925dcd5fc4ebf682c4442a21c619",
          specificity: "default"
        }
      ]
      ...
      ...
    }
    


94
95
96
# File 'lib/berkshelf/cached_cookbook.rb', line 94

def manifest
  @manifest
end

#metadataObject (readonly)

Returns the value of attribute metadata.



74
75
76
# File 'lib/berkshelf/cached_cookbook.rb', line 74

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



73
74
75
# File 'lib/berkshelf/cached_cookbook.rb', line 73

def path
  @path
end

Class Method Details

.checksum(filepath) ⇒ String

Returns a checksum that can be used to uniquely identify the file understood by a Chef Server.

Parameters:

  • filepath (String)

    a path on disk to the location of a file to checksum

Returns:

  • (String)

    a checksum that can be used to uniquely identify the file understood by a Chef Server.



61
62
63
# File 'lib/berkshelf/cached_cookbook.rb', line 61

def checksum(filepath)
  Chef::ChecksumCache.generate_md5_checksum_for_file(filepath)
end

.from_path(path) ⇒ Berkshelf::CachedCookbook

Creates a new instance of Berkshelf::CachedCookbook from a path on disk that contains a Cookbook. The name of the Cookbook will be determined first by the name attribute of the metadata.rb file if it is present. If the name attribute has not been set the Cookbook name will be determined by the basename of the given filepath.

Parameters:

  • path (#to_s)

    a path on disk to the location of a Cookbook

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/berkshelf/cached_cookbook.rb', line 18

def from_path(path)
  path = Pathname.new(path)
   = Chef::Cookbook::Metadata.new

  begin
    .from_file(path.join("metadata.rb").to_s)
  rescue IOError
    raise CookbookNotFound, "No 'metadata.rb' file found at: '#{path}'"
  end

  name = .name.empty? ? File.basename(path) : .name

  new(name, path, )
end

.from_store_path(path) ⇒ CachedCookbook

Returns an instance of CachedCookbook initialized by the contents found at the given path.

Parameters:

  • path (#to_s)

    a path on disk to the location of a Cookbook downloaded by the Downloader

Returns:

  • (CachedCookbook)

    an instance of CachedCookbook initialized by the contents found at the given path.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/berkshelf/cached_cookbook.rb', line 39

def from_store_path(path)
  path = Pathname.new(path)
  cached_name = File.basename(path.to_s).slice(DIRNAME_REGEXP, 1)
  return nil if cached_name.nil?

   = Chef::Cookbook::Metadata.new

  begin
    .from_file(path.join("metadata.rb").to_s)
  rescue IOError
    raise CookbookNotFound, "No 'metadata.rb' file found at: '#{path}'"
  end

  new(cached_name, path, )
end

Instance Method Details

#<=>(other_cookbook) ⇒ Object



216
217
218
# File 'lib/berkshelf/cached_cookbook.rb', line 216

def <=>(other_cookbook)
  [self.cookbook_name, self.version] <=> [other_cookbook.cookbook_name, other_cookbook.version]
end

#checksumsHash

Returns an hash containing the checksums and expanded file paths of all of the files found in the instance of CachedCookbook

example:

{
  "da97c94bb6acb2b7900cbf951654fea3" => "/Users/reset/.berkshelf/nginx-0.101.2/README.md"
}.

Returns:

  • (Hash)

    an hash containing the checksums and expanded file paths of all of the files found in the instance of CachedCookbook

    example:

    {
      "da97c94bb6acb2b7900cbf951654fea3" => "/Users/reset/.berkshelf/nginx-0.101.2/README.md"
    }
    


140
141
142
143
144
145
146
# File 'lib/berkshelf/cached_cookbook.rb', line 140

def checksums
  {}.tap do |checksums|
    files.each do |file|
      checksums[self.class.checksum(file)] = file
    end
  end
end

#dependenciesHash

Returns:

  • (Hash)


128
129
130
# File 'lib/berkshelf/cached_cookbook.rb', line 128

def dependencies
  .recommendations.merge(.dependencies)
end

#file_metadata(category, target) ⇒ Hash

Returns a Hash containing a name, path, checksum, and specificity key representing the metadata about a file contained in a Cookbook. This metadata is used when uploading a Cookbook’s files to a Chef Server.

example:

{
  name: "default.rb",
  path: "recipes/default.rb",
  checksum: "fb1f925dcd5fc4ebf682c4442a21c619",
  specificity: "default"
}.

Parameters:

  • category (Symbol)

    the category of file to generate metadata about

  • target (String)

    the filepath to the file to get metadata information about

Returns:

  • (Hash)

    a Hash containing a name, path, checksum, and specificity key representing the metadata about a file contained in a Cookbook. This metadata is used when uploading a Cookbook’s files to a Chef Server.

    example:

    {
      name: "default.rb",
      path: "recipes/default.rb",
      checksum: "fb1f925dcd5fc4ebf682c4442a21c619",
      specificity: "default"
    }
    


165
166
167
168
169
170
171
172
173
174
# File 'lib/berkshelf/cached_cookbook.rb', line 165

def (category, target)
  target = Pathname.new(target)

  {
    name: target.basename.to_s,
    path: target.relative_path_from(path).to_s,
    checksum: self.class.checksum(target),
    specificity: file_specificity(category, target)
  }
end

#nameString

Returns the name of the cookbook and the version number separated by a dash (-).

example:

"nginx-0.101.2".

Returns:

  • (String)

    the name of the cookbook and the version number separated by a dash (-).

    example:

    "nginx-0.101.2"
    


123
124
125
# File 'lib/berkshelf/cached_cookbook.rb', line 123

def name
  "#{cookbook_name}-#{version}"
end

#to_hashObject



195
196
197
198
199
200
201
202
203
# File 'lib/berkshelf/cached_cookbook.rb', line 195

def to_hash
  result = manifest.dup
  result['chef_type'] = 'cookbook_version'
  result['name'] = name
  result['cookbook_name'] = cookbook_name
  result['version'] = version
  result['metadata'] = 
  result.to_hash
end

#to_json(*a) ⇒ Object



205
206
207
208
209
210
# File 'lib/berkshelf/cached_cookbook.rb', line 205

def to_json(*a)
  result = self.to_hash
  result['json_class'] = chef_json_class
  result['frozen?'] = false
  result.to_json(*a)
end

#to_sObject



212
213
214
# File 'lib/berkshelf/cached_cookbook.rb', line 212

def to_s
  "#{cookbook_name} (#{version}) '#{path}'"
end

#validate!Boolean

Validates that this instance of CachedCookbook points to a valid location on disk that contains a cookbook which passes a Ruby and template syntax check. Raises an error if these assertions are not true.

Returns:

  • (Boolean)

    returns true if Cookbook is valid

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/berkshelf/cached_cookbook.rb', line 182

def validate!
  raise CookbookNotFound, "No Cookbook found at: #{path}" unless path.exist?

  unless quietly { syntax_checker.validate_ruby_files }
    raise CookbookSyntaxError, "Invalid ruby files in cookbook: #{name} (#{version})."
  end
  unless quietly { syntax_checker.validate_templates }
    raise CookbookSyntaxError, "Invalid template files in cookbook: #{name} (#{version})."
  end

  true
end