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

Overview

Author:

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


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ridley/chef/cookbook.rb', line 79

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
  )

  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

#manifestHashie::Mash (readonly)


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


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

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.

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

Raises:

  • (IOError)

    if the path does not contain a metadata.rb file


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

def from_path(path, options = {})
  path     = Pathname.new(path)
   = Cookbook::Metadata.from_file(path.join('metadata.rb'))

  name = if options[:name].present?
    options[:name]
  else
    .name.empty? ? File.basename(path) : .name
  end

  new(name, path, )
end

Instance Method Details

#<=>(other) ⇒ Object


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

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

#checksumsHash


107
108
109
110
111
112
113
# File 'lib/ridley/chef/cookbook.rb', line 107

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:

file_metadata(:root_files, "somefile.h") => {
  name: "default.rb",
  path: "recipes/default.rb",
  checksum: "fb1f925dcd5fc4ebf682c4442a21c619",
  specificity: "default"
}

132
133
134
135
136
137
138
139
140
141
# File 'lib/ridley/chef/cookbook.rb', line 132

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


147
148
149
150
151
152
153
154
155
# File 'lib/ridley/chef/cookbook.rb', line 147

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


162
163
164
# File 'lib/ridley/chef/cookbook.rb', line 162

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

#to_hashObject


179
180
181
182
183
184
185
186
187
# File 'lib/ridley/chef/cookbook.rb', line 179

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.to_hash
end

#to_json(*args) ⇒ Object


189
190
191
192
193
194
# File 'lib/ridley/chef/cookbook.rb', line 189

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

#to_sObject


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

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

#validateObject

Raises:

  • (IOError)

166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ridley/chef/cookbook.rb', line 166

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

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

  true
end