Class: Ridley::Chef::Cookbook
- Inherits:
-
Object
- Object
- Ridley::Chef::Cookbook
- 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
-
#cookbook_name ⇒ Object
readonly
Returns the value of attribute cookbook_name.
- #frozen ⇒ Boolean
-
#manifest ⇒ Hashie::Mash
readonly
A Hashie::Mash containing Cookbook file category names as keys and an Array of Hashes containing metadata about the files belonging to that category.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.checksum(filepath) ⇒ String
A checksum that can be used to uniquely identify the file understood by a Chef Server.
-
.from_path(path) ⇒ Ridley::Chef::Cookbook
Creates a new instance of Ridley::Chef::Cookbook from a path on disk containing a Cookbook.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#checksums ⇒ Hash
An hash containing the checksums and expanded file paths of all of the files found in the instance of CachedCookbook.
-
#compile_metadata(out = self.path) ⇒ String
Compiles the raw metadata of the cookbook and writes it to a metadata.json file at the given out path.
-
#compiled_metadata? ⇒ Boolean
Returns true if the cookbook instance has a compiled metadata file and false if it does not.
-
#file_metadata(category, target) ⇒ Hash
A Hash containing a name, path, checksum, and specificity key representing the metadata about a file contained in a Cookbook.
- #file_specificity(category, target) ⇒ String
-
#initialize(name, path, metadata) ⇒ Cookbook
constructor
A new instance of Cookbook.
-
#name ⇒ String
The name of the cookbook and the version number separated by a dash (-).
-
#reload ⇒ Object
Reload the cookbook from the files located on disk at ‘#path`.
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(name, path, metadata) ⇒ Cookbook
Returns a new instance of Cookbook.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ridley/chef/cookbook.rb', line 85 def initialize(name, path, ) @cookbook_name = name @path = Pathname.new(path) @metadata = @frozen = false @chefignore = Ridley::Chef::Chefignore.new(@path) rescue nil clear_files load_files end |
Instance Attribute Details
#cookbook_name ⇒ Object (readonly)
Returns the value of attribute cookbook_name.
56 57 58 |
# File 'lib/ridley/chef/cookbook.rb', line 56 def cookbook_name @cookbook_name end |
#frozen ⇒ Boolean
81 82 83 |
# File 'lib/ridley/chef/cookbook.rb', line 81 def frozen @frozen end |
#manifest ⇒ Hashie::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"
}
]
...
...
}.
78 79 80 |
# File 'lib/ridley/chef/cookbook.rb', line 78 def manifest @manifest end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
58 59 60 |
# File 'lib/ridley/chef/cookbook.rb', line 58 def @metadata end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
57 58 59 |
# File 'lib/ridley/chef/cookbook.rb', line 57 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.
15 16 17 |
# File 'lib/ridley/chef/cookbook.rb', line 15 def checksum(filepath) Ridley::Chef::Digester.md5_checksum_for_file(filepath) end |
.from_path(path) ⇒ 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ridley/chef/cookbook.rb', line 32 def from_path(path) path = Pathname.new(path) if (file = path.join(Metadata::COMPILED_FILE_NAME)).exist? = Metadata.from_json(File.read(file)) elsif (file = path.join(Metadata::RAW_FILE_NAME)).exist? = Metadata.from_file(file) else raise IOError, "no #{Metadata::COMPILED_FILE_NAME} or #{Metadata::RAW_FILE_NAME} found at #{path}" end unless .name.presence raise Ridley::Errors::MissingNameAttribute.new(path) end new(.name, path, ) end |
Instance Method Details
#<=>(other) ⇒ Object
228 229 230 |
# File 'lib/ridley/chef/cookbook.rb', line 228 def <=>(other) [self.cookbook_name, self.version] <=> [other.cookbook_name, other.version] end |
#checksums ⇒ Hash
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"
}.
104 105 106 107 108 109 110 |
# File 'lib/ridley/chef/cookbook.rb', line 104 def checksums {}.tap do |checksums| files.each do |file| checksums[self.class.checksum(file)] = file end end end |
#compile_metadata(out = self.path) ⇒ String
Compiles the raw metadata of the cookbook and writes it to a metadata.json file at the given out path. The default out path is the directory containing the cookbook itself.
120 121 122 123 124 125 126 127 |
# File 'lib/ridley/chef/cookbook.rb', line 120 def (out = self.path) filepath = File.join(out, Metadata::COMPILED_FILE_NAME) File.open(filepath, "w+") do |f| f.write(.to_json) end filepath end |
#compiled_metadata? ⇒ Boolean
Returns true if the cookbook instance has a compiled metadata file and false if it does not.
133 134 135 |
# File 'lib/ridley/chef/cookbook.rb', line 133 def manifest[:root_files].any? { |file| file[:name].downcase == Metadata::COMPILED_FILE_NAME } 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.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ridley/chef/cookbook.rb', line 154 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
169 170 171 172 173 174 175 176 177 |
# File 'lib/ridley/chef/cookbook.rb', line 169 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 |
#name ⇒ String
Returns the name of the cookbook and the version number separated by a dash (-).
example:
"nginx-0.101.2".
184 185 186 |
# File 'lib/ridley/chef/cookbook.rb', line 184 def name "#{cookbook_name}-#{version}" end |
#reload ⇒ Object
Reload the cookbook from the files located on disk at ‘#path`.
189 190 191 192 |
# File 'lib/ridley/chef/cookbook.rb', line 189 def reload clear_files load_files end |
#to_hash ⇒ Object
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ridley/chef/cookbook.rb', line 207 def to_hash result = manifest.dup result[:chef_type] = CHEF_TYPE result[:name] = name result[:cookbook_name] = cookbook_name result[:version] = version result[:metadata] = .to_hash result[:frozen?] = frozen result end |
#to_json(*args) ⇒ Object
218 219 220 221 222 |
# File 'lib/ridley/chef/cookbook.rb', line 218 def to_json(*args) result = self.to_hash result['json_class'] = CHEF_JSON_CLASS result.to_json(*args) end |
#to_s ⇒ Object
224 225 226 |
# File 'lib/ridley/chef/cookbook.rb', line 224 def to_s "#{cookbook_name} (#{version}) '#{path}'" end |
#validate ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/ridley/chef/cookbook.rb', line 194 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 |