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>



56
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
# File 'lib/chef/cookbook/metadata.rb', line 56

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
  @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

#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



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

def version
  @version
end

Class Method Details

.from_hash(o) ⇒ Object



369
370
371
372
373
# File 'lib/chef/cookbook/metadata.rb', line 369

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

.from_json(string) ⇒ Object



395
396
397
398
# File 'lib/chef/cookbook/metadata.rb', line 395

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

Instance Method Details

#_check_version_expression(version_string) ⇒ Object



339
340
341
342
343
344
345
# File 'lib/chef/cookbook/metadata.rb', line 339

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
multiple_values<True>,<False>:: Whether it supports multiple values
type<String>:: "string", "hash" or "array" - default is "string"
required<True>,<False>:: Whether this attr is required - default false
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



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/chef/cookbook/metadata.rb', line 322

def attribute(name, options)
  validate(
    options,
    {
      :display_name => { :kind_of => String },
      :description => { :kind_of => String },
      :multiple_values => { :equal_to => [ true, false ], :default => false },
      :type => { :equal_to => [ "string", "array", "hash" ], :default => "string" },
      :required => { :equal_to => [ true, false ], :default => false },
      :recipes => { :kind_of => [ Array ], :default => [] },
      :default => { :kind_of => [ String, Array, Hash ] }
    }
  )
  @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



254
255
256
257
258
# File 'lib/chef/cookbook/metadata.rb', line 254

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



212
213
214
215
216
# File 'lib/chef/cookbook/metadata.rb', line 212

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



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

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

#from_hash(o) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/chef/cookbook/metadata.rb', line 375

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.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.recipes = o['recipes'] if o.has_key?('recipes')
  self.version = o['version'] if o.has_key?('version')
  self
end

#from_json(string) ⇒ Object



400
401
402
403
# File 'lib/chef/cookbook/metadata.rb', line 400

def from_json(string)
  o = JSON.parse(string)
  from_hash(o)
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



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

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



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

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.



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

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.



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

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.



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

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



272
273
274
275
276
# File 'lib/chef/cookbook/metadata.rb', line 272

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



300
301
302
# File 'lib/chef/cookbook/metadata.rb', line 300

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



226
227
228
229
230
# File 'lib/chef/cookbook/metadata.rb', line 226

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



286
287
288
289
290
# File 'lib/chef/cookbook/metadata.rb', line 286

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



240
241
242
243
244
# File 'lib/chef/cookbook/metadata.rb', line 240

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



198
199
200
201
202
# File 'lib/chef/cookbook/metadata.rb', line 198

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

#to_json(*a) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/chef/cookbook/metadata.rb', line 347

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,
    :recipes => self.recipes,
    :version => self.version
  }
  result.to_json(*a)
end