core: support checksum on box add

This commit is contained in:
Mitchell Hashimoto 2013-12-01 22:36:41 -08:00
parent 4281af338f
commit 1094ac976e
5 changed files with 61 additions and 3 deletions

View File

@ -18,6 +18,22 @@ module Vagrant
def call(env)
@download_interrupted = false
# Determine the checksum type to use
checksum = env[:box_checksum]
checksum_klass = case env[:box_checksum_type]
when nil
nil
when :md5
Digest::MD5
when :sha1
Digest::SHA1
when :sha256
Digest::SHA2
else
raise Errors::BoxChecksumInvalidType,
type: env[:box_checksum_type].to_s
end
# Go through each URL and attempt to download it
download_error = nil
download_url = nil
@ -45,6 +61,18 @@ module Vagrant
# If all the URLs failed, then raise an exception
raise download_error if download_error
if checksum_klass
@logger.info("Validating checksum with #{checksum_klass}")
@logger.info("Expected checksum: #{checksum}")
actual = FileChecksum.new(@temp_path, checksum_klass).checksum
if actual != checksum
raise Errors::BoxChecksumMismatch,
actual: actual,
expected: checksum
end
end
box_formats = env[:box_provider]
if box_formats
# Determine the formats a box can support and allow the box to

View File

@ -120,6 +120,14 @@ module Vagrant
error_key(:already_exists, "vagrant.actions.box.unpackage")
end
class BoxChecksumInvalidType < VagrantError
error_key(:box_checksum_invalid_type)
end
class BoxChecksumMismatch < VagrantError
error_key(:box_checksum_mismatch)
end
class BoxConfigChangingBox < VagrantError
error_key(:box_config_changing_box)
end

View File

@ -8,7 +8,7 @@ class DigestClass
end
class FileChecksum
BUFFER_SIZE = 1024
BUFFER_SIZE = 16328
# Initializes an object to calculate the checksum of a file. The given
# ``digest_klass`` should implement the ``DigestClass`` interface. Note

View File

@ -11,6 +11,14 @@ module VagrantPlugins
o.banner = "Usage: vagrant box add <name> <url> [--provider provider] [-h]"
o.separator ""
o.on("--checksum VALUE", String, "Checksum") do |c|
options[:checksum] = c
end
o.on("--checksum-type VALUE", String, "Checksum type") do |c|
options[:checksum_type] = c
end
o.on("-c", "--clean", "Remove old temporary download if it exists.") do |c|
options[:clean] = c
end
@ -51,6 +59,8 @@ module VagrantPlugins
:box_name => argv[0],
:box_provider => provider,
:box_url => argv[1],
:box_checksum_type => options[:checksum_type],
:box_checksum => options[:checksum],
:box_clean => options[:clean],
:box_force => options[:force],
:box_download_ca_cert => options[:ca_cert],

View File

@ -177,6 +177,18 @@ en:
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
box_checksum_invalid_type: |-
The specified checksum type is not supported by Vagrant: %{type}.
Vagrant supports the following checksum types:
md5, sha1, sha256
box_checksum_mismatch: |-
The checksum of the dowloaded box did not match the expected
value. Please verify that you have the proper URL setup and that
you're downloading the proper file.
Expected: %{expected}
Received: %{actual}
box_config_changing_box: |-
While loading the Vagrantfile, the provider override specified
a new box. This box, in turn, specified a different box. This isn't