Module: ChefHelpers::HasSource

Included in:
Chef::Recipe, Chef::Resource::File
Defined in:
lib/chef-helpers/has_source.rb

Overview

Provides means for checking whether a template or cookbook file exist in a recipe, or directly in a resource definition.

Instance Method Summary collapse

Instance Method Details

#has_cookbook_file?(cbf, cookbook = nil) ⇒ String?

Checks for existence of a cookbook file in a cookbook.

Parameters:

  • cbf (String)

    name of the cookbook file

  • cookbook (String, nil) (defaults to: nil)

    cookbook to look in, defaults to current cookbook

Returns:

  • (String, nil)

    full path to the template source or nil

See Also:


40
41
42
# File 'lib/chef-helpers/has_source.rb', line 40

def has_cookbook_file?(cbf, cookbook=nil)
  has_source?(cbf, :files, cookbook)
end

#has_source?(source, segment, cookbook = nil) ⇒ String?

Checks for existence of a cookbook file or template source in a cookbook.

Examples:

has_source?("foo.erb", :templates)
has_source?("bar.conf", :files, "a_cookbook")

Parameters:

  • source (String)

    name of the desired template or cookbook file source

  • segment (Symbol)

    :files or :templates

  • cookbook (String, nil) (defaults to: nil)

    to look in, defaults to current cookbook

Returns:

  • (String, nil)

    full path to the source or nil if it doesn't exist


16
17
18
19
20
21
22
23
24
# File 'lib/chef-helpers/has_source.rb', line 16

def has_source?(source, segment, cookbook=nil)
  cookbook ||= cookbook_name
  begin
    run_context.cookbook_collection[cookbook].
      preferred_filename_on_disk_location(run_context.node, segment, source)
  rescue Chef::Exceptions::FileNotFound
    nil
  end
end

#has_template?(tmpl, cookbook = nil) ⇒ String?

Checks for existence of a template source in a cookbook.

Parameters:

  • tmpl (String)

    name of the template source

  • cookbook (String, nil) (defaults to: nil)

    cookbook to look in, defaults to current cookbook

Returns:

  • (String, nil)

    full path to the template source or nil

See Also:


31
32
33
# File 'lib/chef-helpers/has_source.rb', line 31

def has_template?(tmpl, cookbook=nil)
  has_source?(tmpl, :templates, cookbook)
end

#try_files(*files) ⇒ Object

Return the first cookbook file off the list that exists.

See Also:


86
87
88
# File 'lib/chef-helpers/has_source.rb', line 86

def try_files(*files)
  try_sources(files, :files)
end

#try_sources(*sources) ⇒ Object

For a list of sources, returns first source that exist

Parameters:

  • sources (Array<String>)

    list of source (template source or cookbook file) names to look for. Source name can include a cookbook name, e.g. "mysql::my.cnf.erb"

    Last parameter can be a :templates or :files keword, to indicate what kind of source to look for. If it is ommitted, and method is called in a template or cookbook_file resource block, it is automatically guessed; otherwise, RuntimeError is raised.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef-helpers/has_source.rb', line 54

def try_sources(*sources)
  segment =
    if sources.last.is_a?(Symbol)
      sources.pop
    else
      case self
      when Chef::Resource::Template
        :templates
      when Chef::Resource::CookbookFile
        :files
      else
        raise RuntimeError, "Please provide :templates or :files as last argument"
      end
    end
  sources.find do |source|
    if source =~ /::/
      ckbk, src = $`, $'
    else
      ckbk, src = cookbook_name, source
    end
    has_source?(src, segment, ckbk)
  end
end

#try_templates(*templates) ⇒ Object

Return the first template source off the list that exists.

See Also:


80
81
82
# File 'lib/chef-helpers/has_source.rb', line 80

def try_templates(*templates)
  try_sources(templates, :templates)
end