lib/vagrant/box.rb in vagrant-unbundled-2.2.19.0 vs lib/vagrant/box.rb in vagrant-unbundled-2.3.2.0

- old
+ new

@@ -14,10 +14,15 @@ # Represents a "box," which is a package Vagrant environment that is used # as a base image when creating a new guest machine. class Box include Comparable + autoload :Remote, "vagrant/box/remote" + + # The required fields in a boxes `metadata.json` file + REQUIRED_METADATA_FIELDS = ["provider"] + # Number of seconds to wait between checks for box updates BOX_UPDATE_CHECK_INTERVAL = 3600 # The box name. This is the logical name used when adding the box. # @@ -70,17 +75,30 @@ metadata_file = directory.join("metadata.json") raise Errors::BoxMetadataFileNotFound, name: @name if !metadata_file.file? begin @metadata = JSON.parse(directory.join("metadata.json").read) + validate_metadata_json(@metadata) rescue JSON::ParserError raise Errors::BoxMetadataCorrupted, name: @name end @logger = Log4r::Logger.new("vagrant::box") end + def validate_metadata_json(metadata) + metatdata_fields = metadata.keys + REQUIRED_METADATA_FIELDS.each do |field| + if !metatdata_fields.include?(field) + raise Errors::BoxMetadataMissingRequiredFields, + name: @name, + required_field: field, + all_fields: REQUIRED_METADATA_FIELDS.join(", ") + end + end + end + # This deletes the box. This is NOT undoable. def destroy! # Delete the directory to delete the box. FileUtils.rm_r(@directory) @@ -138,10 +156,10 @@ d = Util::Downloader.new(url, tf.path, opts) if @hook @hook.call(:authenticate_box_downloader, downloader: d) end d.download! - BoxMetadata.new(File.open(tf.path, "r")) + BoxMetadata.new(File.open(tf.path, "r"), url: url) rescue Errors::DownloaderError => e raise Errors::BoxMetadataDownloadError, message: e.extra_data[:message] ensure tf.unlink if tf