Class: Berkshelf::CachedCookbook

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

Constant Summary collapse

DIRNAME_REGEXP =
/^(.+)-(.+)$/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CachedCookbook

Returns a new instance of CachedCookbook.

[View source]

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

def initialize(path)
  @path = path
  # eagerly load to force throwing on bad metadata while constructing
  cookbook_name
  
end

Instance Attribute Details

#cookbook_versionObject


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

def cookbook_version
  @cookbook_version ||= loader.cookbook_version
end

#metadataObject


82
83
84
# File 'lib/berkshelf/cached_cookbook.rb', line 82

def 
  @metadata ||= cookbook_version.
end

#pathObject

Returns the value of attribute path.


55
56
57
# File 'lib/berkshelf/cached_cookbook.rb', line 55

def path
  @path
end

Class Method Details

.checksum(filepath) ⇒ Object

[View source]

42
43
44
# File 'lib/berkshelf/cached_cookbook.rb', line 42

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

.from_path(path) ⇒ Ridley::Chef::Cookbook

Creates a new instance of Berkshelf::CachedCookbook 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

Returns:

  • (Ridley::Chef::Cookbook)

Raises:

  • (IOError)

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

[View source]

36
37
38
39
40
# File 'lib/berkshelf/cached_cookbook.rb', line 36

def from_path(path)
  path = Pathname.new(path)

  new(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.

[View source]

15
16
17
18
19
20
21
# File 'lib/berkshelf/cached_cookbook.rb', line 15

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?

  loaded_cookbooks[path.to_s] ||= from_path(path)
end

Instance Method Details

#<=>(other) ⇒ Object

[View source]

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

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

#compile_metadata(path = self.path) ⇒ Object

[View source]

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

def (path = self.path)
  json_file = "#{path}/metadata.json"
  rb_file = "#{path}/metadata.rb"
  return nil if File.exist?(json_file)

  md = Chef::Cookbook::Metadata.new
  md.from_file(rb_file)
  f = File.open(json_file, "w")
  f.write(Chef::JSONCompat.to_json_pretty(md))
  f.close
  f.path
end

#cookbook_nameObject

[View source]

78
79
80
# File 'lib/berkshelf/cached_cookbook.rb', line 78

def cookbook_name
  @cookbook_name ||= cookbook_version.name
end

#dependenciesHash

Returns:

  • (Hash)
[View source]

111
112
113
# File 'lib/berkshelf/cached_cookbook.rb', line 111

def dependencies
  .dependencies
end

#loaderObject

[View source]

65
66
67
68
69
70
71
72
# File 'lib/berkshelf/cached_cookbook.rb', line 65

def loader
  @loader ||=
    begin
      loader = Chef::Cookbook::CookbookVersionLoader.new(@path)
      loader.load!
      loader
    end
end

#pretty_hashHash

High-level information about this cached cookbook in Hash format

Returns:

  • (Hash)
[View source]

138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/berkshelf/cached_cookbook.rb', line 138

def pretty_hash
  {}.tap do |h|
    h[:name]          = cookbook_name if cookbook_name && cookbook_name =~ /\S/
    h[:version]       = version if version && version =~ /\S/
    h[:description]   = description if description && description =~ /\S/
    h[:author]        = maintainer if maintainer && maintainer =~ /\S/
    h[:email]         = maintainer_email if maintainer_email && maintainer_email =~ /\S/
    h[:license]       = license if license && license =~ /\S/
    h[:platforms]     = platforms.to_hash if platforms && !platforms.empty?
    h[:dependencies]  = dependencies.to_hash if dependencies && !dependencies.empty?
  end
end

#pretty_jsonString

High-level information about this cached cookbook in JSON format

Returns:

[View source]

131
132
133
# File 'lib/berkshelf/cached_cookbook.rb', line 131

def pretty_json
  JSON.pretty_generate(pretty_hash)
end

#pretty_printObject

[View source]

115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/berkshelf/cached_cookbook.rb', line 115

def pretty_print
  [].tap do |a|
    a.push "        Name: #{cookbook_name}" if name && name =~ /\S/
    a.push "     Version: #{version}" if version && version =~ /\S/
    a.push " Description: #{.description}" if .description && .description =~ /\S/
    a.push "      Author: #{.maintainer}" if .maintainer && .maintainer =~ /\S/
    a.push "       Email: #{.maintainer_email}" if .maintainer_email && .maintainer_email =~ /\S/
    a.push "     License: #{.license}" if .license && .license =~ /\S/
    a.push "   Platforms: #{pretty_map(.platforms, 14)}" if .platforms && !.platforms.empty?
    a.push "Dependencies: #{pretty_map(dependencies, 14)}" if dependencies && !dependencies.empty?
  end.join("\n")
end

#reloadObject

[View source]

86
87
88
89
90
91
# File 'lib/berkshelf/cached_cookbook.rb', line 86

def reload
  @metadata = nil
  @cookbook_name = nil
  @cookbook_version = nil
  @loader = nil
end

#validateObject

Raises:

  • (IOError)
[View source]

151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/berkshelf/cached_cookbook.rb', line 151

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

  syntax_checker = Chef::Cookbook::SyntaxCheck.new(path.to_path)

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

  true
end