Class: Chef::Cookbook::Metadata

Inherits:
Object
  • Object
show all
Includes:
Mixin::CheckHelper, Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/cookbook/metadata.rb,
lib/chef/cookbook/metadata/version.rb

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::FromFile

#class_from_file, #from_file

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::CheckHelper

#set_if_args

Constructor Details

#initialize(cookbook = nil, maintainer = 'Your Name', maintainer_email = '[email protected]', license = 'Apache v2.0') ⇒ Metadata

Builds a new Chef::Cookbook::Metadata object.

Parameters

cookbook<String>

An optional cookbook object

maintainer<String>

An optional maintainer

maintainer_email<String>

An optional maintainer email

license<String>::An optional license. Default is Apache v2.0

Returns

metadata<Chef::Cookbook::Metadata>



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/cookbook/metadata.rb', line 57

def initialize(cookbook=nil, maintainer='Your Name', maintainer_email='[email protected]', license='Apache v2.0')
  @cookbook = cookbook
  @name = cookbook ? cookbook.name : "" 
  @long_description = ""
  self.maintainer(maintainer)
  self.maintainer_email(maintainer_email)
  self.license(license)
  self.description('A fabulous new cookbook')
  @platforms = Mash.new
  @dependencies = Mash.new
  @recommendations = Mash.new
  @suggestions = Mash.new
  @conflicting = Mash.new
  @providing = Mash.new
  @replacing = Mash.new
  @attributes = Mash.new
  @groupings = Mash.new
  @recipes = Mash.new
  @version = Version.new "0.0.0"
  if cookbook
    @recipes = cookbook.recipes.inject({}) do |r, e| 
      e = self.name if e =~ /::default$/ 
      r[e] = ""
      self.provides e
      r
    end
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def attributes
  @attributes
end

#conflictingObject

Returns the value of attribute conflicting.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def conflicting
  @conflicting
end

#cookbookObject

Returns the value of attribute cookbook.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def cookbook
  @cookbook
end

#dependenciesObject

Returns the value of attribute dependencies.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def dependencies
  @dependencies
end

#groupingsObject

Returns the value of attribute groupings.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def groupings
  @groupings
end

#platformsObject

Returns the value of attribute platforms.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def platforms
  @platforms
end

#providingObject

Returns the value of attribute providing.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def providing
  @providing
end

#recipesObject

Returns the value of attribute recipes.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def recipes
  @recipes
end

#recommendationsObject

Returns the value of attribute recommendations.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def recommendations
  @recommendations
end

#replacingObject

Returns the value of attribute replacing.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def replacing
  @replacing
end

#suggestionsObject

Returns the value of attribute suggestions.



34
35
36
# File 'lib/chef/cookbook/metadata.rb', line 34

def suggestions
  @suggestions
end

#version(arg = nil) ⇒ Object

Sets the current cookbook version, or returns it. Can be two or three digits, seperated by dots. ie: ‘2.1’, ‘1.5.4’ or ‘0.9’.

Parameters

version<String>

The curent version, as a string

Returns

version<String>

Returns the current version



169
170
171
# File 'lib/chef/cookbook/metadata.rb', line 169

def version
  @version
end

Class Method Details

.from_hash(o) ⇒ Object



391
392
393
394
395
# File 'lib/chef/cookbook/metadata.rb', line 391

def self.from_hash(o)
  cm = self.new() 
  cm.from_hash(o)
  cm
end

.from_json(string) ⇒ Object



419
420
421
422
# File 'lib/chef/cookbook/metadata.rb', line 419

def self.from_json(string)
  o = JSON.parse(string)
  self.from_hash(o)
end

Instance Method Details

#_check_version_expression(version_string) ⇒ Object



360
361
362
363
364
365
366
# File 'lib/chef/cookbook/metadata.rb', line 360

def _check_version_expression(version_string)
  if version_string =~ /^(>>|>=|=|<=|<<) (.+)$/
    [ $1, $2 ]
  else
    raise ArgumentError, "Version expression #{version_string} is invalid!"
  end
end

#attribute(name, options) ⇒ Object

Adds an attribute that a user needs to configure for this cookbook. Takes a name (with the / notation for a nested attribute), followed by any of these options

display_name<String>:: What a UI should show for this attribute
description<String>:: A hint as to what this attr is for
choice<Array>:: An array of choices to present to the user.
calculated<Boolean>:: If true, the default value is calculated by the recipe and cannot be displayed.
type<String>:: "string" or "array" - default is "string"  ("hash" is supported for backwards compatibility)
required<String>:: Whether this attr is 'required', 'recommended' or 'optional' - default 'optional' (true/false values also supported for backwards compatibility)
recipes<Array>:: An array of recipes which need this attr set.
default<String>,<Array>,<Hash>:: The default value

Parameters

name<String>

The name of the attribute (‘foo’, or ‘apache2/log_dir’)

options<Hash>

The description of the options

Returns

options<Hash>

Returns the current options hash



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/chef/cookbook/metadata.rb', line 325

def attribute(name, options)
  validate(
    options,
    {
      :display_name => { :kind_of => String },
      :description => { :kind_of => String },
      :choice => { :kind_of => [ Array ], :default => [] },
      :calculated => { :equal_to => [ true, false ], :default => false },
      :type => { :equal_to => [ "string", "array", "hash" ], :default => "string" },
      :required => { :equal_to => [ "required", "recommended", "optional", true, false ], :default => "optional" },
      :recipes => { :kind_of => [ Array ], :default => [] },
      :default => { :kind_of => [ String, Array, Hash ] }
    }
  )
  options[:required] = remap_required_attribute(options[:required]) unless options[:required].nil?
  validate_string_array(options[:choice])
  validate_calculated_default_rule(options)
  validate_choice_default_rule(options)

  @attributes[name] = options 
  @attributes[name]
end

#conflicts(cookbook, *versions) ⇒ Object

Adds a conflict for another cookbook, with version checking strings.

Parameters

cookbook<String>

The cookbook

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



256
257
258
259
260
# File 'lib/chef/cookbook/metadata.rb', line 256

def conflicts(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @conflicting[cookbook] = versions
  @conflicting[cookbook] 
end

#depends(cookbook, *versions) ⇒ Object

Adds a dependency on another cookbook, with version checking strings.

Parameters

cookbook<String>

The cookbook

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



214
215
216
217
218
# File 'lib/chef/cookbook/metadata.rb', line 214

def depends(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @dependencies[cookbook] = versions
  @dependencies[cookbook]
end

#description(arg = nil) ⇒ Object

Sets the current description, or returns it. Should be short - one line only!

Parameters

description<String>

The new description

Returns

description<String>

Returns the description



138
139
140
141
142
143
144
# File 'lib/chef/cookbook/metadata.rb', line 138

def description(arg=nil)
  set_or_return(
    :description,
    arg,
    :kind_of => [ String ]
  )
end

#from_hash(o) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/chef/cookbook/metadata.rb', line 397

def from_hash(o)
  self.name o['name'] if o.has_key?('name')
  self.description o['description'] if o.has_key?('description')
  self.long_description o['long_description'] if o.has_key?('long_description')
  self.maintainer o['maintainer'] if o.has_key?('maintainer')
  self.maintainer_email o['maintainer_email'] if o.has_key?('maintainer_email')
  self.license o['license'] if o.has_key?('license')
  self.version o['version'] if o.has_key?('version')
  self.platforms = o['platforms'] if o.has_key?('platforms')
  self.dependencies = o['dependencies'] if o.has_key?('dependencies')
  self.recommendations = o['recommendations'] if o.has_key?('recommendations')
  self.suggestions = o['suggestions'] if o.has_key?('suggestions')
  self.conflicting = o['conflicting'] if o.has_key?('conflicting')
  self.providing = o['providing'] if o.has_key?('providing')
  self.replacing = o['replacing'] if o.has_key?('replacing')
  self.attributes = o['attributes'] if o.has_key?('attributes')
  self.groupings = o['groupings'] if o.has_key?('groupings')
  self.recipes = o['recipes'] if o.has_key?('recipes')
  self.version = o['version'] if o.has_key?('version')
  self
end

#from_json(string) ⇒ Object



424
425
426
427
# File 'lib/chef/cookbook/metadata.rb', line 424

def from_json(string)
  o = JSON.parse(string)
  from_hash(o)
end

#grouping(name, options) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
# File 'lib/chef/cookbook/metadata.rb', line 348

def grouping(name, options)
  validate(
    options,
    {
      :title => { :kind_of => String },
      :description => { :kind_of => String }
    }
  )
  @groupings[name] = options 
  @groupings[name]
end

#license(arg = nil) ⇒ Object

Sets the current license, or returns it.

Parameters

license<String>

The current license.

Returns

license<String>

Returns the current license



123
124
125
126
127
128
129
# File 'lib/chef/cookbook/metadata.rb', line 123

def license(arg=nil)
  set_or_return(
    :license,
    arg,
    :kind_of => [ String ]
  )
end

#long_description(arg = nil) ⇒ Object

Sets the current long description, or returns it. Might come from a README, say.

Parameters

long_description<String>

The new long description

Returns

long_description<String>

Returns the long description



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

def long_description(arg=nil)
  set_or_return(
    :long_description,
    arg,
    :kind_of => [ String ]
  )
end

#maintainer(arg = nil) ⇒ Object

Sets the cookbooks maintainer, or returns it.

Parameters

maintainer<String>

The maintainers name

Returns

maintainer<String>

Returns the current maintainer.



93
94
95
96
97
98
99
# File 'lib/chef/cookbook/metadata.rb', line 93

def maintainer(arg=nil)
  set_or_return(
    :maintainer,
    arg,
    :kind_of => [ String ]
  )
end

#maintainer_email(arg = nil) ⇒ Object

Sets the maintainers email address, or returns it.

Parameters

maintainer_email<String>

The maintainers email address

Returns

maintainer_email<String>

Returns the current maintainer email.



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

def maintainer_email(arg=nil)
  set_or_return(
    :maintainer_email,
    arg,
    :kind_of => [ String ]
  )
end

#name(arg = nil) ⇒ Object

Sets the name of the cookbook, or returns it.

Parameters

name<String>

The curent cookbook name.

Returns

name<String>

Returns the current cookbook name.



184
185
186
187
188
189
190
# File 'lib/chef/cookbook/metadata.rb', line 184

def name(arg=nil)
  set_or_return(
    :name,
    arg,
    :kind_of => [ String ]
  )
end

#provides(cookbook, *versions) ⇒ Object

Adds a recipe, definition, or resource provided by this cookbook.

Recipes are specified as normal Definitions are followed by (), and can include :params for prototyping Resources are the stringified version (service)

Parameters

recipe, definition, resource<String>

The thing we provide

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



274
275
276
277
278
# File 'lib/chef/cookbook/metadata.rb', line 274

def provides(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @providing[cookbook] = versions
  @providing[cookbook] 
end

#recipe(name, description) ⇒ Object

Adds a description for a recipe.

Parameters

recipe<String>

The recipe

description<String>

The description of the recipe

Returns

description<String>

Returns the current description



302
303
304
# File 'lib/chef/cookbook/metadata.rb', line 302

def recipe(name, description)
  @recipes[name] = description 
end

#recommends(cookbook, *versions) ⇒ Object

Adds a recommendation for another cookbook, with version checking strings.

Parameters

cookbook<String>

The cookbook

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



228
229
230
231
232
# File 'lib/chef/cookbook/metadata.rb', line 228

def recommends(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @recommendations[cookbook] = versions
  @recommendations[cookbook]
end

#replaces(cookbook, *versions) ⇒ Object

Adds a cookbook that is replaced by this one, with version checking strings.

Parameters

cookbook<String>

The cookbook we replace

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



288
289
290
291
292
# File 'lib/chef/cookbook/metadata.rb', line 288

def replaces(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @replacing[cookbook] = versions
  @replacing[cookbook] 
end

#suggests(cookbook, *versions) ⇒ Object

Adds a suggestion for another cookbook, with version checking strings.

Parameters

cookbook<String>

The cookbook

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



242
243
244
245
246
# File 'lib/chef/cookbook/metadata.rb', line 242

def suggests(cookbook, *versions)
  versions.each { |v| _check_version_expression(v) }
  @suggestions[cookbook] = versions
  @suggestions[cookbook] 
end

#supports(platform, *versions) ⇒ Object

Adds a supported platform, with version checking strings.

Parameters

platform<String>,<Symbol>

The platform (like :ubuntu or :mac_os_x)

*versions<String>

A list of versions matching << <= = >= >> followed by a version.

Returns

versions<Array>

Returns the list of versions for the platform



200
201
202
203
204
# File 'lib/chef/cookbook/metadata.rb', line 200

def supports(platform, *versions)
  versions.each { |v| _check_version_expression(v) }
  @platforms[platform] = versions
  @platforms[platform]
end

#to_json(*a) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/chef/cookbook/metadata.rb', line 368

def to_json(*a)
  result = {
    :name => self.name,
    :description => self.description,
    :long_description => self.long_description,
    :maintainer => self.maintainer,
    :maintainer_email => self.maintainer_email,
    :license => self.license,
    :platforms => self.platforms,
    :dependencies => self.dependencies,
    :recommendations => self.recommendations,
    :suggestions => self.suggestions,
    :conflicting => self.conflicting,
    :providing => self.providing,
    :replacing => self.replacing,
    :attributes => self.attributes,
    :groupings => self.groupings,
    :recipes => self.recipes,
    :version => self.version
  }
  result.to_json(*a)
end