Class: Ridley::Chef::Cookbook::Metadata

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

Overview

Borrowed and modified from: https://raw.github.com/opscode/chef/11.4.0/lib/chef/cookbook/metadata.rb

Copyright

Copyright 2008-2010 Opscode, Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Chef::Cookbook::Metadata

Chef::Cookbook::Metadata provides a convenient DSL for declaring metadata about Chef Cookbooks.

Direct Known Subclasses

MinimalMetadata

Constant Summary collapse

NAME =
'name'.freeze
DESCRIPTION =
'description'.freeze
LONG_DESCRIPTION =
'long_description'.freeze
MAINTAINER =
'maintainer'.freeze
MAINTAINER_EMAIL =
'maintainer_email'.freeze
LICENSE =
'license'.freeze
PLATFORMS =
'platforms'.freeze
DEPENDENCIES =
'dependencies'.freeze
RECOMMENDATIONS =
'recommendations'.freeze
SUGGESTIONS =
'suggestions'.freeze
CONFLICTING =
'conflicting'.freeze
PROVIDING =
'providing'.freeze
REPLACING =
'replacing'.freeze
ATTRIBUTES =
'attributes'.freeze
GROUPINGS =
'groupings'.freeze
RECIPES =
'recipes'.freeze
VERSION =
'version'.freeze
COMPILED_FILE_NAME =
"metadata.json".freeze
RAW_FILE_NAME =
"metadata.rb".freeze
COMPARISON_FIELDS =
[
  :name, :description, :long_description, :maintainer,
  :maintainer_email, :license, :platforms, :dependencies,
  :recommendations, :suggestions, :conflicting, :providing,
  :replacing, :attributes, :groupings, :recipes, :version
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::FromFile

#class_from_file, #from_file, included

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Constructor Details

#initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none') ⇒ 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>



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ridley/chef/cookbook/metadata.rb', line 87

def initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none')
  @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 = Hashie::Mash.new
  @dependencies = Hashie::Mash.new
  @recommendations = Hashie::Mash.new
  @suggestions = Hashie::Mash.new
  @conflicting = Hashie::Mash.new
  @providing = Hashie::Mash.new
  @replacing = Hashie::Mash.new
  @attributes = Hashie::Mash.new
  @groupings = Hashie::Mash.new
  @recipes = Hashie::Mash.new
  @version = Semverse::Version.new("0.0.0")
  if cookbook
    @recipes = cookbook.fully_qualified_recipe_names.inject({}) do |r, e|
      e = self.name if e =~ /::default$/
      r[e] = ""
      self.provides e
      r
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



72
73
74
# File 'lib/ridley/chef/cookbook/metadata.rb', line 72

def attributes
  @attributes
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



69
70
71
# File 'lib/ridley/chef/cookbook/metadata.rb', line 69

def conflicting
  @conflicting
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



64
65
66
# File 'lib/ridley/chef/cookbook/metadata.rb', line 64

def cookbook
  @cookbook
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



66
67
68
# File 'lib/ridley/chef/cookbook/metadata.rb', line 66

def dependencies
  @dependencies
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



73
74
75
# File 'lib/ridley/chef/cookbook/metadata.rb', line 73

def groupings
  @groupings
end

#platformsObject (readonly)

Returns the value of attribute platforms.



65
66
67
# File 'lib/ridley/chef/cookbook/metadata.rb', line 65

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



70
71
72
# File 'lib/ridley/chef/cookbook/metadata.rb', line 70

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



74
75
76
# File 'lib/ridley/chef/cookbook/metadata.rb', line 74

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



67
68
69
# File 'lib/ridley/chef/cookbook/metadata.rb', line 67

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



71
72
73
# File 'lib/ridley/chef/cookbook/metadata.rb', line 71

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



68
69
70
# File 'lib/ridley/chef/cookbook/metadata.rb', line 68

def suggestions
  @suggestions
end

#version(arg = nil) ⇒ Object (readonly)

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



205
206
207
# File 'lib/ridley/chef/cookbook/metadata.rb', line 205

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



24
25
26
# File 'lib/ridley/chef/cookbook/metadata.rb', line 24

def from_hash(hash)
  new.from_hash(hash)
end

.from_json(json) ⇒ Object



28
29
30
# File 'lib/ridley/chef/cookbook/metadata.rb', line 28

def from_json(json)
  new.from_json(json)
end

Instance Method Details

#==(other) ⇒ Object



116
117
118
119
120
# File 'lib/ridley/chef/cookbook/metadata.rb', line 116

def ==(other)
  COMPARISON_FIELDS.inject(true) do |equal_so_far, field|
    equal_so_far && other.respond_to?(field) && (other.send(field) == send(field))
  end
end

#attribute(name, options) ⇒ Object

Adds an attribute )hat 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



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

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", "symbol", "boolean", "numeric" ], :default => "string" },
      :required => { :equal_to => [ "required", "recommended", "optional", true, false ], :default => "optional" },
      :recipes => { :kind_of => [ Array ], :default => [] },
      :default => { :kind_of => [ String, Array, Hash, Symbol, Numeric, TrueClass, FalseClass ] }
    }
  )
  options[:required] = remap_required_attribute(options[:required]) unless options[:required].nil?
  validate_choice_array(options)
  validate_calculated_default_rule(options)
  validate_choice_default_rule(options)

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

#conflicts(cookbook, *version_args) ⇒ Object

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

Parameters

cookbook<String>

The cookbook

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



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

def conflicts(cookbook, *version_args)
  version = version_args.first
  @conflicting[cookbook] = Semverse::Constraint.new(version).to_s
  @conflicting[cookbook]
end

#depends(cookbook, *version_args) ⇒ Object

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

Parameters

cookbook<String>

The cookbook

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



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

def depends(cookbook, *version_args)
  version = version_args.first
  @dependencies[cookbook] = Semverse::Constraint.new(version).to_s
  @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



174
175
176
177
178
179
180
# File 'lib/ridley/chef/cookbook/metadata.rb', line 174

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

#from_hash(o) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/ridley/chef/cookbook/metadata.rb', line 436

def from_hash(o)
  @name             = o[NAME] if o.has_key?(NAME)
  @description      = o[DESCRIPTION] if o.has_key?(DESCRIPTION)
  @long_description = o[LONG_DESCRIPTION] if o.has_key?(LONG_DESCRIPTION)
  @maintainer       = o[MAINTAINER] if o.has_key?(MAINTAINER)
  @maintainer_email = o[MAINTAINER_EMAIL] if o.has_key?(MAINTAINER_EMAIL)
  @license          = o[LICENSE] if o.has_key?(LICENSE)
  @platforms        = handle_deprecated_constraints(o[PLATFORMS]) if o.has_key?(PLATFORMS)
  @dependencies     = handle_deprecated_constraints(o[DEPENDENCIES]) if o.has_key?(DEPENDENCIES)
  @recommendations  = handle_deprecated_constraints(o[RECOMMENDATIONS]) if o.has_key?(RECOMMENDATIONS)
  @suggestions      = handle_deprecated_constraints(o[SUGGESTIONS]) if o.has_key?(SUGGESTIONS)
  @conflicting      = handle_deprecated_constraints(o[CONFLICTING]) if o.has_key?(CONFLICTING)
  @providing        = o[PROVIDING] if o.has_key?(PROVIDING)
  @replacing        = handle_deprecated_constraints(o[REPLACING]) if o.has_key?(REPLACING)
  @attributes       = o[ATTRIBUTES] if o.has_key?(ATTRIBUTES)
  @groupings        = o[GROUPINGS] if o.has_key?(GROUPINGS)
  @recipes          = o[RECIPES] if o.has_key?(RECIPES)
  @version          = o[VERSION] if o.has_key?(VERSION)
  self
end

#from_json(json) ⇒ Object



457
458
459
# File 'lib/ridley/chef/cookbook/metadata.rb', line 457

def from_json(json)
  from_hash JSON.parse(json)
end

#grouping(name, options) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
# File 'lib/ridley/chef/cookbook/metadata.rb', line 397

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



159
160
161
162
163
164
165
# File 'lib/ridley/chef/cookbook/metadata.rb', line 159

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



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

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.



129
130
131
132
133
134
135
# File 'lib/ridley/chef/cookbook/metadata.rb', line 129

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.



144
145
146
147
148
149
150
# File 'lib/ridley/chef/cookbook/metadata.rb', line 144

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.



220
221
222
223
224
225
226
# File 'lib/ridley/chef/cookbook/metadata.rb', line 220

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

#provides(cookbook, *version_args) ⇒ 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

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



322
323
324
325
326
# File 'lib/ridley/chef/cookbook/metadata.rb', line 322

def provides(cookbook, *version_args)
  version = version_args.first
  @providing[cookbook] = Semverse::Constraint.new(version).to_s
  @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



351
352
353
# File 'lib/ridley/chef/cookbook/metadata.rb', line 351

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

#recommends(cookbook, *version_args) ⇒ Object

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

Parameters

cookbook<String>

The cookbook

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



270
271
272
273
274
# File 'lib/ridley/chef/cookbook/metadata.rb', line 270

def recommends(cookbook, *version_args)
  version = version_args.first
  @recommendations[cookbook] = Semverse::Constraint.new(version).to_s
  @recommendations[cookbook]
end

#replaces(cookbook, *version_args) ⇒ Object

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

Parameters

cookbook<String>

The cookbook we replace

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



337
338
339
340
341
# File 'lib/ridley/chef/cookbook/metadata.rb', line 337

def replaces(cookbook, *version_args)
  version = version_args.first
  @replacing[cookbook] = Semverse::Constraint.new(version).to_s
  @replacing[cookbook]
end

#suggests(cookbook, *version_args) ⇒ Object

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

Parameters

cookbook<String>

The cookbook

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the formx.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



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

def suggests(cookbook, *version_args)
  version = version_args.first
  @suggestions[cookbook] = Semverse::Constraint.new(version).to_s
  @suggestions[cookbook]
end

#supports(platform, *version_args) ⇒ Object

Adds a supported platform, with version checking strings.

Parameters

platform<String>,<Symbol>

The platform (like :ubuntu or :mac_os_x)

version<String>

A version constraint of the form “OP VERSION”,

where OP is one of < <= = > >= ~> and VERSION has the form x.y.z or x.y.

Returns

versions<Array>

Returns the list of versions for the platform



238
239
240
241
242
# File 'lib/ridley/chef/cookbook/metadata.rb', line 238

def supports(platform, *version_args)
  version = version_args.first
  @platforms[platform] = Semverse::Constraint.new(version).to_s
  @platforms[platform]
end

#to_hashObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/ridley/chef/cookbook/metadata.rb', line 409

def to_hash
  {
    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
  }
end

#to_jsonString

Returns:

  • (String)


432
433
434
# File 'lib/ridley/chef/cookbook/metadata.rb', line 432

def to_json
  JSON.fast_generate(to_hash)
end