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

#initializeCookbookLoader

Returns a new instance of CookbookLoader.



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

def initialize()
  @cookbook = Hash.new
  @metadata = Hash.new
  @ignore_regexes = Hash.new { |hsh, key| hsh[key] = Array.new }
  load_cookbooks
end

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



28
29
30
# File 'lib/chef/cookbook_loader.rb', line 28

def cookbook
  @cookbook
end

#metadataObject

Returns the value of attribute metadata.



28
29
30
# File 'lib/chef/cookbook_loader.rb', line 28

def 
  @metadata
end

Instance Method Details

#[](cookbook) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/chef/cookbook_loader.rb', line 122

def [](cookbook)
  if @cookbook.has_key?(cookbook.to_sym)
    @cookbook[cookbook.to_sym]
  else
    raise ArgumentError, "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

#eachObject



130
131
132
133
134
# File 'lib/chef/cookbook_loader.rb', line 130

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

#load_cookbooksObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
84
85
86
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
115
116
117
118
119
120
# File 'lib/chef/cookbook_loader.rb', line 39

def load_cookbooks
  cookbook_settings = Hash.new
  [Chef::Config.cookbook_path].flatten.each do |cb_path|
    Dir[File.join(cb_path, "*")].each do |cookbook|
      next unless File.directory?(cookbook)          
      cookbook_name = File.basename(cookbook).to_sym
      unless cookbook_settings.has_key?(cookbook_name)
        cookbook_settings[cookbook_name] = { 
          :attribute_files  => Hash.new,
          :definition_files => Hash.new,
          :recipe_files     => Hash.new,
          :template_files   => Hash.new,
          :remote_files     => Hash.new,
          :lib_files        => Hash.new,
          :resource_files   => Hash.new,
          :provider_files   => Hash.new,
          :metadata_files   => Array.new
        }
      end
      ignore_regexes = load_ignore_file(File.join(cookbook, "ignore"))
      @ignore_regexes[cookbook_name].concat(ignore_regexes)
      
      load_files_unless_basename(
        File.join(cookbook, "attributes", "*.rb"), 
        cookbook_settings[cookbook_name][:attribute_files]
      )
      load_files_unless_basename(
        File.join(cookbook, "definitions", "*.rb"), 
        cookbook_settings[cookbook_name][:definition_files]
      )
      load_files_unless_basename(
        File.join(cookbook, "recipes", "*.rb"), 
        cookbook_settings[cookbook_name][:recipe_files]
      )
      load_files_unless_basename(
        File.join(cookbook, "libraries", "*.rb"),               
        cookbook_settings[cookbook_name][:lib_files]
      )
      load_cascading_files(
        "*.erb",
        File.join(cookbook, "templates"),
        cookbook_settings[cookbook_name][:template_files]
      )
      load_cascading_files(
        "*",
        File.join(cookbook, "files"),
        cookbook_settings[cookbook_name][:remote_files]
      )
      load_cascading_files(
        "*.rb",
        File.join(cookbook, "resources"),
        cookbook_settings[cookbook_name][:resource_files]
      )
      load_cascading_files(
        "*.rb",
        File.join(cookbook, "providers"),
        cookbook_settings[cookbook_name][:provider_files]
      )

      if File.exists?(File.join(cookbook, "metadata.json"))
        cookbook_settings[cookbook_name][:metadata_files] << File.join(cookbook, "metadata.json")
      end
    end
  end
  remove_ignored_files_from(cookbook_settings)
  
  cookbook_settings.each_key do |cookbook|
    @cookbook[cookbook] = Chef::Cookbook.new(cookbook)
    @cookbook[cookbook].attribute_files = cookbook_settings[cookbook][:attribute_files].values
    @cookbook[cookbook].definition_files = cookbook_settings[cookbook][:definition_files].values
    @cookbook[cookbook].recipe_files = cookbook_settings[cookbook][:recipe_files].values
    @cookbook[cookbook].template_files = cookbook_settings[cookbook][:template_files].values
    @cookbook[cookbook].remote_files = cookbook_settings[cookbook][:remote_files].values
    @cookbook[cookbook].lib_files = cookbook_settings[cookbook][:lib_files].values
    @cookbook[cookbook].resource_files = cookbook_settings[cookbook][:resource_files].values
    @cookbook[cookbook].provider_files = cookbook_settings[cookbook][:provider_files].values
    @metadata[cookbook] = Chef::Cookbook::Metadata.new(@cookbook[cookbook])
    cookbook_settings[cookbook][:metadata_files].each do |meta_json|
      @metadata[cookbook].from_json(IO.read(meta_json))
    end
  end
end