Class: Chef::CookbookLoader

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/chef/cookbook_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*repo_paths) ⇒ CookbookLoader

Returns a new instance of CookbookLoader.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/cookbook_loader.rb', line 38

def initialize(*repo_paths)
  @repo_paths = repo_paths.flatten
  raise ArgumentError, "You must specify at least one cookbook repo path" if @repo_paths.empty?
  @cookbooks_by_name = Mash.new
  @loaded_cookbooks = {}
  @metadata = Mash.new
  @cookbooks_paths = Hash.new {|h,k| h[k] = []} # for deprecation warnings

  # Used to track which cookbooks appear in multiple places in the cookbook repos
  # and are merged in to a single cookbook by file shadowing. This behavior is
  # deprecated, so users of this class may issue warnings to the user by checking
  # this variable
  @merged_cookbooks = []

  load_cookbooks
end

Instance Attribute Details

#cookbook_pathsObject (readonly)

Returns the value of attribute cookbook_paths.



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

def cookbook_paths
  @cookbook_paths
end

#cookbooks_by_nameObject (readonly)

Returns the value of attribute cookbooks_by_name.



32
33
34
# File 'lib/chef/cookbook_loader.rb', line 32

def cookbooks_by_name
  @cookbooks_by_name
end

#merged_cookbooksObject (readonly)

Returns the value of attribute merged_cookbooks.



33
34
35
# File 'lib/chef/cookbook_loader.rb', line 33

def merged_cookbooks
  @merged_cookbooks
end

#metadataObject

Returns the value of attribute metadata.



31
32
33
# File 'lib/chef/cookbook_loader.rb', line 31

def 
  @metadata
end

Instance Method Details

#[](cookbook) ⇒ Object Also known as: fetch



89
90
91
92
93
94
95
# File 'lib/chef/cookbook_loader.rb', line 89

def [](cookbook)
  if @cookbooks_by_name.has_key?(cookbook.to_sym)
    @cookbooks_by_name[cookbook.to_sym]
  else
    raise Exceptions::CookbookNotFoundInRepo, "Cannot find a cookbook named #{cookbook.to_s}; did you forget to add metadata to a cookbook? (http://wiki.opscode.com/display/chef/Metadata)"
  end
end

#cookbook_namesObject



111
112
113
# File 'lib/chef/cookbook_loader.rb', line 111

def cookbook_names
  @cookbooks_by_name.keys.sort
end

#eachObject



105
106
107
108
109
# File 'lib/chef/cookbook_loader.rb', line 105

def each
  @cookbooks_by_name.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |cname|
    yield(cname, @cookbooks_by_name[cname])
  end
end

#has_key?(cookbook_name) ⇒ Boolean Also known as: cookbook_exists?, key?

Returns:

  • (Boolean)


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

def has_key?(cookbook_name)
  @cookbooks_by_name.has_key?(cookbook_name)
end

#load_cookbooksObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/cookbook_loader.rb', line 61

def load_cookbooks
  cookbook_settings = Hash.new
  @repo_paths.each do |repo_path|
    repo_path = File.expand_path(repo_path)
    chefignore = Cookbook::Chefignore.new(repo_path)
    Dir[File.join(repo_path, "*")].each do |cookbook_path|
      next unless File.directory?(cookbook_path)
      loader = Cookbook::CookbookVersionLoader.new(cookbook_path, chefignore)
      loader.load_cookbooks
      next if loader.empty?
      @cookbooks_paths[loader.cookbook_name] << cookbook_path # for deprecation warnings
      if @loaded_cookbooks.key?(loader.cookbook_name)
        @merged_cookbooks << loader.cookbook_name # for deprecation warnings
        @loaded_cookbooks[loader.cookbook_name].merge!(loader)
      else
        @loaded_cookbooks[loader.cookbook_name] = loader
      end
    end
  end

  @loaded_cookbooks.each do |cookbook, loader|
    cookbook_version = loader.cookbook_version
    @cookbooks_by_name[cookbook] = cookbook_version
    @metadata[cookbook] = cookbook_version.
  end
  @cookbooks_by_name
end

#merged_cookbook_pathsObject

for deprecation warnings



55
56
57
58
59
# File 'lib/chef/cookbook_loader.rb', line 55

def merged_cookbook_paths # for deprecation warnings
  merged_cookbook_paths = {}
  @merged_cookbooks.each {|c| merged_cookbook_paths[c] = @cookbooks_paths[c]}
  merged_cookbook_paths
end

#valuesObject Also known as: cookbooks



115
116
117
# File 'lib/chef/cookbook_loader.rb', line 115

def values
  @cookbooks_by_name.values
end