Class: Chef::CookbookLoader

Inherits:
Object
  • 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()
  @cookbooks_by_name = Mash.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



147
148
149
150
151
152
153
# File 'lib/chef/cookbook_loader.rb', line 147

def [](cookbook)
  if @cookbooks_by_name.has_key?(cookbook.to_sym)
    @cookbooks_by_name[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

#cookbook_namesObject



166
167
168
# File 'lib/chef/cookbook_loader.rb', line 166

def cookbook_names
  @cookbooks_by_name.keys.sort
end

#eachObject



160
161
162
163
164
# File 'lib/chef/cookbook_loader.rb', line 160

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?

Returns:

  • (Boolean)


155
156
157
# File 'lib/chef/cookbook_loader.rb', line 155

def has_key?(cookbook_name)
  @cookbooks_by_name.has_key?(cookbook_name)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/cookbook_loader.rb', line 39

def load_cookbooks
  cookbook_settings = Hash.new
  [Chef::Config.cookbook_path].flatten.each do |cb_path|
    cb_path = File.expand_path(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_filenames  => Hash.new,
          :definition_filenames => Hash.new,
          :recipe_filenames     => Hash.new,
          :template_filenames   => Hash.new,
          :file_filenames       => Hash.new,
          :library_filenames    => Hash.new,
          :resource_filenames   => Hash.new,
          :provider_filenames   => Hash.new,
          :root_filenames       => Hash.new,
          :metadata_filenames   => 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_filenames]
      )
      load_files_unless_basename(
        File.join(cookbook, "definitions", "*.rb"), 
        cookbook_settings[cookbook_name][:definition_filenames]
      )
      load_files_unless_basename(
        File.join(cookbook, "recipes", "*.rb"), 
        cookbook_settings[cookbook_name][:recipe_filenames]
      )
      load_files_unless_basename(
        File.join(cookbook, "libraries", "*.rb"),               
        cookbook_settings[cookbook_name][:library_filenames]
      )
      load_cascading_files(
        "*",
        File.join(cookbook, "templates"),
        cookbook_settings[cookbook_name][:template_filenames]
      )
      load_cascading_files(
        "*",
        File.join(cookbook, "files"),
        cookbook_settings[cookbook_name][:file_filenames]
      )
      load_cascading_files(
        "*.rb",
        File.join(cookbook, "resources"),
        cookbook_settings[cookbook_name][:resource_filenames]
      )
      load_cascading_files(
        "*.rb",
        File.join(cookbook, "providers"),
        cookbook_settings[cookbook_name][:provider_filenames]
      )
      load_files(
        "*",
        cookbook,
        cookbook_settings[cookbook_name][:root_filenames]
      )
      cookbook_settings[cookbook_name][:root_dir] = cookbook
      if File.exists?(File.join(cookbook, "metadata.json"))
        cookbook_settings[cookbook_name][:metadata_filenames] << File.join(cookbook, "metadata.json")
      end

      empty = cookbook_settings[cookbook_name].inject(true) do |all_empty, files|
        all_empty && files.last.empty?
      end

      if empty
        Chef::Log.warn "found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping."
        cookbook_settings.delete(cookbook_name)
      end
    end
  end
  remove_ignored_files_from(cookbook_settings)

  cookbook_settings.each_key do |cookbook|
    @cookbooks_by_name[cookbook] = Chef::CookbookVersion.new(cookbook)
    @cookbooks_by_name[cookbook].root_dir = cookbook_settings[cookbook][:root_dir]
    @cookbooks_by_name[cookbook].attribute_filenames = cookbook_settings[cookbook][:attribute_filenames].values
    @cookbooks_by_name[cookbook].definition_filenames = cookbook_settings[cookbook][:definition_filenames].values
    @cookbooks_by_name[cookbook].recipe_filenames = cookbook_settings[cookbook][:recipe_filenames].values
    @cookbooks_by_name[cookbook].template_filenames = cookbook_settings[cookbook][:template_filenames].values
    @cookbooks_by_name[cookbook].file_filenames = cookbook_settings[cookbook][:file_filenames].values
    @cookbooks_by_name[cookbook].library_filenames = cookbook_settings[cookbook][:library_filenames].values
    @cookbooks_by_name[cookbook].resource_filenames = cookbook_settings[cookbook][:resource_filenames].values
    @cookbooks_by_name[cookbook].provider_filenames = cookbook_settings[cookbook][:provider_filenames].values
    @cookbooks_by_name[cookbook].root_filenames = cookbook_settings[cookbook][:root_filenames].values
    @cookbooks_by_name[cookbook]. = cookbook_settings[cookbook][:metadata_filenames]
    @metadata[cookbook] = Chef::Cookbook::Metadata.new(@cookbooks_by_name[cookbook])
    cookbook_settings[cookbook][:metadata_filenames].each do |meta_json|
      begin
        @metadata[cookbook].from_json(IO.read(meta_json))
      rescue JSON::ParserError
        Chef::Log.fatal("Couldn't parse JSON in " + meta_json)
        raise
      end
    end
    @cookbooks_by_name[cookbook]. = @metadata[cookbook]
  end
end

#valuesObject Also known as: cookbooks



170
171
172
# File 'lib/chef/cookbook_loader.rb', line 170

def values
  @cookbooks_by_name.values
end