Method: Vagrant::BoxCollection#all

Defined in:
lib/vagrant/box_collection.rb

#allArray

This returns an array of all the boxes on the system, given by their name and their provider.

Returns:

  • (Array)

    Array of [name, version, provider, architecture] of the boxes installed on this system.



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
# File 'lib/vagrant/box_collection.rb', line 230

def all
  results = []

  with_collection_lock do
    @logger.debug("Finding all boxes in: #{@directory}")
    @directory.children(true).each do |child|
      # Ignore non-directories, since files are not interesting to
      # us in our folder structure.
      next if !child.directory?

      box_name = undir_name(child.basename.to_s)

      # Otherwise, traverse the subdirectories and see what versions
      # we have.
      child.children(true).each do |versiondir|
        next if !versiondir.directory?
        next if versiondir.basename.to_s.start_with?(".")

        version = versiondir.basename.to_s
        # Ensure version of box is correct before continuing
        if !Gem::Version.correct?(version)
          ui = Vagrant::UI::Prefixed.new(Vagrant::UI::Colored.new, "vagrant")
          ui.warn(I18n.t("vagrant.box_version_malformed",
                         version: version, box_name: box_name))
          @logger.debug("Invalid version #{version} for box #{box_name}")
          next
        end

        versiondir.children(true).each do |architecture_or_provider|
          # If the entry is not a directory, it is invalid and should be ignored
          if !architecture_or_provider.directory?
            @logger.debug("Invalid box #{box_name} (v#{version}) - invalid item: #{architecture_or_provider}")
            next
          end

          # Now the directory can be assumed to be the architecture
          architecture_name = architecture_or_provider.basename.to_s.to_sym

          # Cycle through directories to find providers
          architecture_or_provider.children(true).each do |provider|
            if !provider.directory?
              @logger.debug("Invalid box #{box_name} (v#{version}, #{architecture_name}) - invalid item: #{provider}")
              next
            end

            # If the entry contains a metadata file, add it
            if provider.join("metadata.json").file?
              provider_name = provider.basename.to_s.to_sym
              @logger.debug("Box: #{box_name} (#{provider_name} (#{architecture_name}), #{version})")
              results << [box_name, version, provider_name, architecture_name]
            end
          end

          # If the base entry contains a metadata file, then it was
          # added prior to architecture support and is a provider directory.
          # If it contains a metadata file, include it with the results only
          # if an entry hasn't already been included for the local system's
          # architecture
          if architecture_or_provider.join("metadata.json").file?
            provider_name = architecture_or_provider.basename.to_s.to_sym
            if results.include?([box_name, version, provider_name, Util::Platform.architecture.to_sym])
              next
            end
            @logger.debug("Box: #{box_name} (#{provider_name}, #{version})")
            results << [box_name, version, provider_name, nil]
          end
        end
      end
    end
  end
  # Sort the list to group like providers and properly ordered versions
  results.sort_by! do |box_result|
    [box_result[0], box_result[2], Gem::Version.new(box_result[1]), box_result[3]]
  end
  results
end