Class: Ridley::Chef::Cookbook

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ridley/chef/cookbook.rb,
lib/ridley/chef/cookbook/metadata.rb,
lib/ridley/chef/cookbook/syntax_check.rb

Defined Under Namespace

Classes: Metadata, MinimalMetadata, SyntaxCheck

Constant Summary collapse

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

Returns a new instance of Cookbook.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ridley/chef/cookbook.rb', line 82

def initialize(name, path, )
  @cookbook_name = name
  @path          = Pathname.new(path)
  @metadata      = 
  @files         = Array.new
  @manifest      = Hashie::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
  )
  @frozen        = false
  @chefignore    = Ridley::Chef::Chefignore.new(@path)

  load_files
end

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



54
55
56
# File 'lib/ridley/chef/cookbook.rb', line 54

def cookbook_name
  @cookbook_name
end

#frozenBoolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ridley/chef/cookbook.rb', line 78

def frozen
  @frozen
end

#manifestHashie::Mash (readonly)

Returns a Hashie::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:

  • (Hashie::Mash)

    a Hashie::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"
        }
      ]
      ...
      ...
    }
    


75
76
77
# File 'lib/ridley/chef/cookbook.rb', line 75

def manifest
  @manifest
end

#metadataObject (readonly)

Returns the value of attribute metadata.



56
57
58
# File 'lib/ridley/chef/cookbook.rb', line 56

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



55
56
57
# File 'lib/ridley/chef/cookbook.rb', line 55

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.



13
14
15
# File 'lib/ridley/chef/cookbook.rb', line 13

def checksum(filepath)
  Ridley::Chef::Digester.md5_checksum_for_file(filepath)
end

.from_path(path, options = {}) ⇒ Ridley::Chef::Cookbook

Creates a new instance of Ridley::Chef::Cookbook from a path on disk containing a Cookbook.

The name of the Cookbook is determined by the value of the name attribute set in the cookbooks’ metadata. If the name attribute is not present the name of the loaded cookbook is determined by directory containing the cookbook.

Parameters:

  • path (#to_s)

    a path on disk to the location of a Cookbook

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :name (String)

    explicitly supply the name of the cookbook we are loading. This is useful if you are dealing with a cookbook that does not have well-formed metadata

Returns:

Raises:

  • (IOError)

    if the path does not contain a metadata.rb or metadata.json file



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ridley/chef/cookbook.rb', line 34

def from_path(path, options = {})
  path     = Pathname.new(path)
   = if path.join('metadata.rb').exist?
    Cookbook::Metadata.from_file(path.join('metadata.rb'))
  elsif path.join('metadata.json').exist?
    Cookbook::Metadata.from_json(File.read(path.join('metadata.json')))
  else
    raise IOError, "no metadata.rb or metadata.json found at #{path}"
  end

  .name(options[:name].presence || .name.presence || File.basename(path))
  new(.name, path, )
end

Instance Method Details

#<=>(other) ⇒ Object



205
206
207
# File 'lib/ridley/chef/cookbook.rb', line 205

def <=>(other)
  [self.cookbook_name, self.version] <=> [other.cookbook_name, other.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/.ridley/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/.ridley/nginx-0.101.2/README.md"
    }
    


112
113
114
115
116
117
118
# File 'lib/ridley/chef/cookbook.rb', line 112

def checksums
  {}.tap do |checksums|
    files.each do |file|
      checksums[self.class.checksum(file)] = file
    end
  end
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.

Examples:

(:root_files, "somefile.h") => {
  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.



137
138
139
140
141
142
143
144
145
146
# File 'lib/ridley/chef/cookbook.rb', line 137

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

#file_specificity(category, target) ⇒ String

Parameters:

  • category (Symbol)
  • target (Pathname)

Returns:

  • (String)


152
153
154
155
156
157
158
159
160
# File 'lib/ridley/chef/cookbook.rb', line 152

def file_specificity(category, target)
  case category
  when :files, :templates
    relpath = target.relative_path_from(path).to_s
    relpath.slice(/(.+)\/(.+)\/.+/, 2)
  else
    'default'
  end
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"
    


167
168
169
# File 'lib/ridley/chef/cookbook.rb', line 167

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

#to_hashObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/ridley/chef/cookbook.rb', line 184

def to_hash
  result                 = manifest.dup
  result[:chef_type]     = CHEF_TYPE
  result[:name]          = name
  result[:cookbook_name] = cookbook_name
  result[:version]       = version
  result[:metadata]      = 
  result[:frozen?]       = frozen
  result.to_hash
end

#to_json(*args) ⇒ Object



195
196
197
198
199
# File 'lib/ridley/chef/cookbook.rb', line 195

def to_json(*args)
  result               = self.to_hash
  result['json_class'] = CHEF_JSON_CLASS
  result.to_json(*args)
end

#to_sObject



201
202
203
# File 'lib/ridley/chef/cookbook.rb', line 201

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

#validateObject

Raises:

  • (IOError)


171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ridley/chef/cookbook.rb', line 171

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

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

  true
end