summaryrefslogtreecommitdiff
path: root/lib/chef/cookbook_manifest.rb
blob: 84b0c0d58cce6c7fa554d64e926a07656e5dd11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "forwardable" unless defined?(Forwardable)
require_relative "mixin/versioned_api"
require_relative "util/path_helper"
require_relative "cookbook/manifest_v0"
require_relative "cookbook/manifest_v2"
require_relative "log"

class Chef

  # Handles the details of representing a cookbook in JSON form for uploading
  # to a Chef Server.
  class CookbookManifest

    extend Forwardable

    attr_reader :cookbook_version

    def_delegator :@cookbook_version, :root_paths
    def_delegator :@cookbook_version, :name
    def_delegator :@cookbook_version, :identifier
    def_delegator :@cookbook_version, :metadata
    def_delegator :@cookbook_version, :full_name
    def_delegator :@cookbook_version, :version
    def_delegator :@cookbook_version, :frozen_version?

    # Create a new CookbookManifest object for the given `cookbook_version`.
    # You can subsequently call #to_h to get a Hash representation of the
    # cookbook_version in the "manifest" format, or #to_json to get a JSON
    # representation of the cookbook_version.
    #
    # The interface for this behavior is expected to change as we implement new
    # manifest formats. The entire class should be considered a private API for
    # now.
    #
    # @api private
    # @param policy_mode [Boolean] whether to convert cookbooks to Hash/JSON in
    #   the format used by the `cookbook_artifacts` endpoint (for policyfiles).
    #   Setting this option also changes the behavior of #save_url and
    #   #force_save_url such that CookbookVersions will be uploaded to the new
    #   `cookbook_artifacts` API.
    def initialize(cookbook_version, policy_mode: false)
      @cookbook_version = cookbook_version
      @policy_mode = !!policy_mode

      reset!
    end

    # Resets all lazily computed values.
    def reset!
      @manifest = nil
      @checksums = nil
      @manifest_records_by_path = nil
      true
    end

    # Returns a 'manifest' data structure that can be uploaded to a Chef
    # Server.
    #
    # The format is as follows:
    #
    #     {
    #       :cookbook_name  => name,            # String
    #       :metadata       => metadata,        # Chef::Cookbook::Metadata
    #       :version        => version,         # Chef::Version
    #       :name           => full_name,       # String of "#{name}-#{version}"
    #
    #       :recipes        => Array<FileSpec>,
    #       :definitions    => Array<FileSpec>,
    #       :libraries      => Array<FileSpec>,
    #       :attributes     => Array<FileSpec>,
    #       :files          => Array<FileSpec>,
    #       :templates      => Array<FileSpec>,
    #       :resources      => Array<FileSpec>,
    #       :providers      => Array<FileSpec>,
    #       :root_files     => Array<FileSpec>
    #     }
    #
    # Where a `FileSpec` is a Hash of the form:
    #
    #     {
    #       :name         => file_name,
    #       :path         => path,
    #       :checksum     => csum,
    #       :specificity  => specificity
    #     }
    #
    def manifest
      @manifest || generate_manifest
      @manifest
    end

    def checksums
      @manifest || generate_manifest
      @checksums
    end

    def manifest_records_by_path
      @manifest || generate_manifest
      @manifest_records_by_path
    end

    def policy_mode?
      @policy_mode
    end

    def to_h
      CookbookManifestVersions.to_h(self)
    end

    alias_method :to_hash, :to_h

    def to_json(*a)
      result = to_h
      result["json_class"] = "Chef::CookbookVersion"
      Chef::JSONCompat.to_json(result, *a)
    end

    # Return the URL to save (PUT) this object to the server via the
    # REST api. If there is an existing document on the server and it
    # is marked frozen, a PUT will result in a 409 Conflict.
    def save_url
      if policy_mode?
        "#{named_cookbook_url}/#{identifier}"
      else
        "#{named_cookbook_url}/#{version}"
      end
    end

    def named_cookbook_url
      "#{cookbook_url_path}/#{name}"
    end

    # Adds the `force=true` parameter to the upload URL. This allows
    # the user to overwrite a frozen cookbook (a PUT against the
    # normal #save_url raises a 409 Conflict in this case).
    def force_save_url
      "#{save_url}?force=true"
    end

    # Update this CookbookManifest from the contents of another manifest, and
    # make the corresponding changes to the cookbook_version object. Required
    # to provide backward compatibility with CookbookVersion#manifest= method.
    def update_from(new_manifest)
      @manifest = Chef::CookbookManifestVersions.from_hash(new_manifest)
      @checksums = extract_checksums_from_manifest(@manifest)
      @manifest_records_by_path = extract_manifest_records_by_path(@manifest)
    end

    # @api private
    # takes a list of hashes
    def add_files_to_manifest(files)
      manifest[:all_files].concat(Array(files))
      @checksums = extract_checksums_from_manifest(@manifest)
      @manifest_records_by_path = extract_manifest_records_by_path(@manifest)
    end

    def files_for(part)
      return root_files if part.to_s == "root_files"

      manifest[:all_files].select do |file|
        seg = file[:name].split("/")[0]
        part.to_s == seg
      end
    end

    def each_file(excluded_parts: [], &block)
      excluded_parts = Array(excluded_parts).map(&:to_s)

      manifest[:all_files].each do |file|
        seg = file[:name].split("/")[0]
        next if excluded_parts.include?(seg)

        yield file if block_given?
      end
    end

    def by_parent_directory
      @by_parent_directory ||=
        manifest[:all_files].inject({}) do |memo, file|
          parts = file[:name].split("/")
          parent = if parts.length == 1
                     "root_files"
                   else
                     parts[0]
                   end

          memo[parent] ||= []
          memo[parent] << file
          memo
        end
    end

    def root_files
      manifest[:all_files].select do |file|
        segment, name = file[:name].split("/")
        name.nil? || segment == "root_files"
      end
    end

    private

    def cookbook_url_path
      policy_mode? ? "cookbook_artifacts" : "cookbooks"
    end

    # See #manifest for a description of the manifest return value.
    # See #preferred_manifest_record for a description an individual manifest record.
    def generate_manifest
      manifest = Mash.new({
        all_files: [],
      })
      @checksums = {}

      if !root_paths || root_paths.size == 0
        Chef::Log.error("Cookbook #{name} does not have root_paths! Cannot generate manifest.")
        raise "Cookbook #{name} does not have root_paths! Cannot generate manifest."
      end

      @cookbook_version.all_files.each do |file|
        next if File.directory?(file)

        name, path, specificity = parse_file_from_root_paths(file)

        csum = checksum_cookbook_file(file)
        @checksums[csum] = file
        rs = Mash.new({
          name: name,
          path: path,
          checksum: csum,
          specificity: specificity,
          # full_path is not a part of the normal manifest, but is very useful to keep around.
          # uploaders should strip this out.
          full_path: file,
        })

        manifest[:all_files] << rs
      end

      manifest[:metadata] = metadata
      manifest[:version] = metadata.version

      if policy_mode?
        manifest[:name] = name.to_s
        manifest[:identifier] = identifier
      else
        manifest[:name] = full_name
        manifest[:cookbook_name] = name.to_s
      end

      @manifest_records_by_path = extract_manifest_records_by_path(manifest)
      @manifest = manifest
    end

    def parse_file_from_root_paths(file)
      root_paths.each do |root_path|
        pathname = Chef::Util::PathHelper.relative_path_from(root_path, file)

        parts = pathname.each_filename.take(2)
        # Check if path is actually under root_path
        next if parts[0] == ".."

        # if we have a root_file, such as metadata.rb, the first part will be "."
        return [ "root_files/#{pathname}", pathname.to_s, "default" ] if parts.length == 1

        segment = parts[0]

        name = File.join(segment, pathname.basename.to_s)

        if %w{templates files}.include?(segment)
          # Check if pathname looks like files/foo or templates/foo (unscoped)
          if pathname.each_filename.to_a.length == 2
            # Use root_default in case the same path exists at root_default and default
            return [ name, pathname.to_s, "root_default" ]
          else
            return [ name, pathname.to_s, parts[1] ]
          end
        else
          return [ name, pathname.to_s, "default" ]
        end
      end
      Chef::Log.error("Cookbook file #{file} not under cookbook root paths #{root_paths.inspect}.")
      raise "Cookbook file #{file} not under cookbook root paths #{root_paths.inspect}."
    end

    def extract_checksums_from_manifest(manifest)
      manifest[:all_files].inject({}) do |memo, manifest_record|
        memo[manifest_record[:checksum]] = nil
        memo
      end
    end

    def checksum_cookbook_file(filepath)
      CookbookVersion.checksum_cookbook_file(filepath)
    end

    def extract_manifest_records_by_path(manifest)
      manifest[:all_files].inject({}) do |memo, manifest_record|
        memo[manifest_record[:path]] = manifest_record
        memo
      end
    end

  end

  class CookbookManifestVersions

    extend Chef::Mixin::VersionedAPIFactory
    add_versioned_api_class Chef::Cookbook::ManifestV0
    add_versioned_api_class Chef::Cookbook::ManifestV2

    def_versioned_delegator :from_hash
    def_versioned_delegator :to_hash
    def_versioned_delegator :to_h
  end
end