Class: Chef::Cookbook::Metadata

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

Overview

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
SOURCE_URL =
"source_url".freeze
ISSUES_URL =
"issues_url".freeze
PRIVACY =
"privacy".freeze
CHEF_VERSIONS =
"chef_versions".freeze
OHAI_VERSIONS =
"ohai_versions".freeze
GEMS =
"gems".freeze
COMPARISON_FIELDS =
[ :name, :description, :long_description, :maintainer,
:maintainer_email, :license, :platforms, :dependencies,
:recommendations, :suggestions, :conflicting, :providing,
:replacing, :attributes, :groupings, :recipes, :version,
:source_url, :issues_url, :privacy, :chef_versions, :ohai_versions,
:gems ]
VERSION_CONSTRAINTS =
{ :depends      => DEPENDENCIES,
:recommends   => RECOMMENDATIONS,
:suggests     => SUGGESTIONS,
:conflicts    => CONFLICTING,
:provides     => PROVIDING,
:replaces     => REPLACING,
:chef_version => CHEF_VERSIONS,
:ohai_version => OHAI_VERSIONS }

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

#lazy, #set_or_return, #validate

Constructor Details

#initializeMetadata

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>



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/cookbook/metadata.rb', line 111

def initialize
  @name = nil

  @description = ""
  @long_description = ""
  @license = "All rights reserved"

  @maintainer = nil
  @maintainer_email = nil

  @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")
  @source_url = ""
  @issues_url = ""
  @privacy = false
  @chef_versions = []
  @ohai_versions = []
  @gems = []

  @errors = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



89
90
91
# File 'lib/chef/cookbook/metadata.rb', line 89

def attributes
  @attributes
end

#chef_versionsArray<Gem::Dependency> (readonly)

Returns Array of supported Chef versions.

Returns:

  • (Array<Gem::Dependency>)

    Array of supported Chef versions



95
96
97
# File 'lib/chef/cookbook/metadata.rb', line 95

def chef_versions
  @chef_versions
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



86
87
88
# File 'lib/chef/cookbook/metadata.rb', line 86

def conflicting
  @conflicting
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



83
84
85
# File 'lib/chef/cookbook/metadata.rb', line 83

def dependencies
  @dependencies
end

#gemsArray<Array> (readonly)

Returns Array of gems to install with *args as an Array.

Returns:

  • (Array<Array>)

    Array of gems to install with *args as an Array



99
100
101
# File 'lib/chef/cookbook/metadata.rb', line 99

def gems
  @gems
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



90
91
92
# File 'lib/chef/cookbook/metadata.rb', line 90

def groupings
  @groupings
end

#ohai_versionsArray<Gem::Dependency> (readonly)

Returns Array of supported Ohai versions.

Returns:

  • (Array<Gem::Dependency>)

    Array of supported Ohai versions



97
98
99
# File 'lib/chef/cookbook/metadata.rb', line 97

def ohai_versions
  @ohai_versions
end

#platformsObject (readonly)

Returns the value of attribute platforms.



82
83
84
# File 'lib/chef/cookbook/metadata.rb', line 82

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



87
88
89
# File 'lib/chef/cookbook/metadata.rb', line 87

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



91
92
93
# File 'lib/chef/cookbook/metadata.rb', line 91

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



84
85
86
# File 'lib/chef/cookbook/metadata.rb', line 84

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



88
89
90
# File 'lib/chef/cookbook/metadata.rb', line 88

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



85
86
87
# File 'lib/chef/cookbook/metadata.rb', line 85

def suggestions
  @suggestions
end

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

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

Parameters

version<String>

The current version, as a string

Returns

version<String>

Returns the current version



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

def version
  @version
end

Class Method Details

.from_hash(o) ⇒ Object



600
601
602
603
604
# File 'lib/chef/cookbook/metadata.rb', line 600

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

.from_json(string) ⇒ Object



633
634
635
636
# File 'lib/chef/cookbook/metadata.rb', line 633

def self.from_json(string)
  o = Chef::JSONCompat.from_json(string)
  self.from_hash(o)
end

.validate_json(json_str) ⇒ Object



638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/chef/cookbook/metadata.rb', line 638

def self.validate_json(json_str)
  o = Chef::JSONCompat.from_json(json_str)
   = new()
  VERSION_CONSTRAINTS.each do |dependency_type, hash_key|
    if dependency_group = o[hash_key]
      dependency_group.each do |cb_name, constraints|
        if .respond_to?(method_name)
          .public_send(method_name, cb_name, *Array(constraints))
        end
      end
    end
  end
  true
end

Instance Method Details

#==(other) ⇒ Object



142
143
144
145
146
# File 'lib/chef/cookbook/metadata.rb', line 142

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



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/chef/cookbook/metadata.rb', line 496

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 => %w{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 ] },
      :source_url => { :kind_of => String },
      :issues_url => { :kind_of => String },
      :privacy => { :kind_of => [ 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

#chef_version(*version_args) ⇒ Array<Gem::Dependency>

Metadata DSL to set a valid chef_version. May be declared multiple times with the result being ‘OR’d such that if any statements match, the version is considered supported. Uses Gem::Requirement for its implementation.

Parameters:

  • version_args (Array<String>)

    Version constraint in String form

Returns:

  • (Array<Gem::Dependency>)

    Current chef_versions array



412
413
414
415
# File 'lib/chef/cookbook/metadata.rb', line 412

def chef_version(*version_args)
  @chef_versions << Gem::Dependency.new("chef", *version_args) unless version_args.empty?
  @chef_versions
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



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

def conflicts(cookbook, *version_args)
  version = new_args_format(:conflicts, cookbook, version_args)
  constraint = validate_version_constraint(:conflicts, cookbook, version)
  @conflicting[cookbook] = constraint.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



307
308
309
310
311
312
313
314
315
316
# File 'lib/chef/cookbook/metadata.rb', line 307

def depends(cookbook, *version_args)
  if cookbook == name
    Chef::Log.warn "Ignoring self-dependency in cookbook #{name}, please remove it (in the future this will be fatal)."
  else
    version = new_args_format(:depends, cookbook, version_args)
    constraint = validate_version_constraint(:depends, cookbook, version)
    @dependencies[cookbook] = constraint.to_s
  end
  @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



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

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

#errorsObject

A list of validation errors for this metadata object. See #valid? for comments about the validation criteria.

If there are any validation errors, one or more error strings will be returned. Otherwise an empty array is returned.

Returns

error messages<Array>

Whether this metadata object is valid



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

def errors
  run_validation
  @errors
end

#from_hash(o) ⇒ Object



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/chef/cookbook/metadata.rb', line 606

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                    = 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)
  @source_url                   = o[SOURCE_URL] if o.has_key?(SOURCE_URL)
  @issues_url                   = o[ISSUES_URL] if o.has_key?(ISSUES_URL)
  @privacy                      = o[PRIVACY] if o.has_key?(PRIVACY)
  @chef_versions                = gem_requirements_from_array("chef", o[CHEF_VERSIONS]) if o.has_key?(CHEF_VERSIONS)
  @ohai_versions                = gem_requirements_from_array("ohai", o[OHAI_VERSIONS]) if o.has_key?(OHAI_VERSIONS)
  @gems                         = o[GEMS] if o.has_key?(GEMS)
  self
end

#from_json(string) ⇒ Object



653
654
655
656
# File 'lib/chef/cookbook/metadata.rb', line 653

def from_json(string)
  o = Chef::JSONCompat.from_json(string)
  from_hash(o)
end

#gem(*args) ⇒ Array<Array>

Metadata DSL to set a gem to install from the cookbook metadata. May be declared multiple times. All the gems from all the cookbooks are combined into one Gemfile and depsolved together. Uses Bundler’s DSL for its implementation.

Parameters:

  • args (Array<String>)

    Gem name and options to pass to Bundler’s DSL

Returns:

  • (Array<Array>)

    Array of gem statements as args



434
435
436
437
# File 'lib/chef/cookbook/metadata.rb', line 434

def gem(*args)
  @gems << args unless args.empty?
  @gems
end

#gem_requirements_from_array(what, array) ⇒ Array<Gem::Dependency>

Convert an Array of Gem::Dependency objects (chef_version/ohai_version) to a hash.

This is the inverse of #gem_requirements_to_array

Parameters:

  • what (String)

    What version constraint we are constructing (‘chef’ or ‘ohai’ presently)

  • array (Array<Array<String>])

    Simple object representation of version constraints (from json)

Returns:

  • (Array<Gem::Dependency>)

    Multiple Gem-style version constraints



562
563
564
565
566
# File 'lib/chef/cookbook/metadata.rb', line 562

def gem_requirements_from_array(what, array)
  array.map do |dep|
    Gem::Dependency.new(what, *dep)
  end
end

#gem_requirements_to_array(*deps) ⇒ Array<Array<String>]

Convert an Array of Gem::Dependency objects (chef_version/ohai_version) to an Array.

Gem::Dependencey#to_s is not useful, and there is no #to_json defined on it or its component objets, so we have to write our own rendering method.

Gem::Dependency.new(“>= 12.5”), Gem::Dependency.new(“>= 11.18.0”, “< 12.0”)

results in:

[ “>= 12.5” ], [ “>= 11.18.0”, “< 12.0”

]

Parameters:

  • deps (Array<Gem::Dependency>)

    Multiple Gem-style version constraints

Returns:

  • (Array<Array<String>])

    Simple object representation of version constraints (for json)



547
548
549
550
551
552
553
# File 'lib/chef/cookbook/metadata.rb', line 547

def gem_requirements_to_array(*deps)
  deps.map do |dep|
    dep.requirement.requirements.map do |op, version|
      "#{op} #{version}"
    end.sort
  end
end

#grouping(name, options) ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
# File 'lib/chef/cookbook/metadata.rb', line 522

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

#issues_url(arg = nil) ⇒ Object

Sets the cookbook’s issues URL, or returns it.

Parameters

issues_url<String>

The issues URL

Returns

issues_url<String>

Returns the current issues URL.



680
681
682
683
684
685
686
# File 'lib/chef/cookbook/metadata.rb', line 680

def issues_url(arg = nil)
  set_or_return(
    :issues_url,
    arg,
    :kind_of => [ String ]
  )
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



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

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



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

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.



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

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.



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

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 current cookbook name.

Returns

name<String>

Returns the current cookbook name.



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

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

#ohai_version(*version_args) ⇒ Array<Gem::Dependency>

Metadata DSL to set a valid ohai_version. May be declared multiple times with the result being ‘OR’d such that if any statements match, the version is considered supported. Uses Gem::Requirement for its implementation.

Parameters:

  • version_args (Array<String>)

    Version constraint in String form

Returns:

  • (Array<Gem::Dependency>)

    Current ohai_versions array



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

def ohai_version(*version_args)
  @ohai_versions << Gem::Dependency.new("ohai", *version_args) unless version_args.empty?
  @ohai_versions
end

#privacy(arg = nil) ⇒ Object

Sets the cookbook’s privacy flag, or returns it.

Parameters

privacy<TrueClass,FalseClass>

Whether this cookbook is private or not

Returns

privacy<TrueClass,FalseClass>

Whether this cookbook is private or not



697
698
699
700
701
702
703
# File 'lib/chef/cookbook/metadata.rb', line 697

def privacy(arg = nil)
  set_or_return(
    :privacy,
    arg,
    :kind_of => [ TrueClass, FalseClass ]
  )
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



383
384
385
386
387
388
# File 'lib/chef/cookbook/metadata.rb', line 383

def provides(cookbook, *version_args)
  version = new_args_format(:provides, cookbook, version_args)
  constraint = validate_version_constraint(:provides, cookbook, version)
  @providing[cookbook] = constraint.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



447
448
449
# File 'lib/chef/cookbook/metadata.rb', line 447

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

#recipes_from_cookbook_version(cookbook) ⇒ Object

Sets the cookbook’s recipes to the list of recipes in the given cookbook. Any recipe that already has a description (if set by the #recipe method) will not be updated.

Parameters

cookbook<CookbookVersion>

CookbookVersion object representing the cookbook

description<String>

The description of the recipe

Returns

recipe_unqualified_names<Array>

An array of the recipe names given by the cookbook



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/chef/cookbook/metadata.rb', line 461

def recipes_from_cookbook_version(cookbook)
  cookbook.fully_qualified_recipe_names.map do |recipe_name|
    unqualified_name =
      if recipe_name =~ /::default$/
        self.name.to_s
      else
        recipe_name
      end

    @recipes[unqualified_name] ||= ""
    provides(unqualified_name)

    unqualified_name
  end
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



328
329
330
331
332
333
# File 'lib/chef/cookbook/metadata.rb', line 328

def recommends(cookbook, *version_args)
  version = new_args_format(:recommends, cookbook, version_args)
  constraint = validate_version_constraint(:recommends, cookbook, version)
  @recommendations[cookbook] = constraint.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



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

def replaces(cookbook, *version_args)
  version = new_args_format(:replaces, cookbook, version_args)
  constraint = validate_version_constraint(:replaces, cookbook, version)
  @replacing[cookbook] = constraint.to_s
  @replacing[cookbook]
end

#source_url(arg = nil) ⇒ Object

Sets the cookbook’s source URL, or returns it.

Parameters

maintainer<String>

The source URL

Returns

source_url<String>

Returns the current source URL.



665
666
667
668
669
670
671
# File 'lib/chef/cookbook/metadata.rb', line 665

def source_url(arg = nil)
  set_or_return(
    :source_url,
    arg,
    :kind_of => [ String ]
  )
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



345
346
347
348
349
350
# File 'lib/chef/cookbook/metadata.rb', line 345

def suggests(cookbook, *version_args)
  version = new_args_format(:suggests, cookbook, version_args)
  constraint = validate_version_constraint(:suggests, cookbook, version)
  @suggestions[cookbook] = constraint.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



290
291
292
293
294
295
# File 'lib/chef/cookbook/metadata.rb', line 290

def supports(platform, *version_args)
  version = new_args_format(:supports, platform, version_args)
  constraint = validate_version_constraint(:supports, platform, version)
  @platforms[platform] = constraint.to_s
  @platforms[platform]
end

#to_hashObject



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/chef/cookbook/metadata.rb', line 568

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,
    SOURCE_URL             => self.source_url,
    ISSUES_URL             => self.issues_url,
    PRIVACY                => self.privacy,
    CHEF_VERSIONS          => gem_requirements_to_array(*self.chef_versions),
    OHAI_VERSIONS          => gem_requirements_to_array(*self.ohai_versions),
    GEMS                   => self.gems,
  }
end

#to_json(*a) ⇒ Object



596
597
598
# File 'lib/chef/cookbook/metadata.rb', line 596

def to_json(*a)
  Chef::JSONCompat.to_json(to_hash, *a)
end

#valid?Boolean

Whether this metadata is valid. In order to be valid, all required fields must be set. Chef’s validation implementation checks the content of a given field when setting (and raises an error if the content does not meet the criteria), so the content of the fields is not considered when checking validity.

Returns

valid<Boolean>

Whether this metadata object is valid

Returns:

  • (Boolean)


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

def valid?
  run_validation
  @errors.empty?
end

#validate_chef_version!Object

Validates that the Chef::VERSION of the running chef-client matches one of the configured chef_version statements in this cookbooks metadata.



719
720
721
722
723
# File 'lib/chef/cookbook/metadata.rb', line 719

def validate_chef_version!
  unless gem_dep_matches?("chef", Gem::Version.new(Chef::VERSION), *chef_versions)
    raise Exceptions::CookbookChefVersionMismatch.new(Chef::VERSION, name, version, *chef_versions)
  end
end

#validate_ohai_version!Object

Validates that the Ohai::VERSION of the running chef-client matches one of the configured ohai_version statements in this cookbooks metadata.



709
710
711
712
713
# File 'lib/chef/cookbook/metadata.rb', line 709

def validate_ohai_version!
  unless gem_dep_matches?("ohai", Gem::Version.new(Ohai::VERSION), *ohai_versions)
    raise Exceptions::CookbookOhaiVersionMismatch.new(Ohai::VERSION, name, version, *ohai_versions)
  end
end