lib/veewee/provider/core/helper/iso.rb in veewee-0.3.7 vs lib/veewee/provider/core/helper/iso.rb in veewee-0.3.9
- old
+ new
@@ -6,10 +6,11 @@
require 'open-uri'
require 'progressbar'
require 'highline/import'
require 'digest/md5'
+ require 'digest/sha1'
def download_iso(url,filename)
if !File.exists?(env.config.veewee.iso_dir)
ui.info "Creating an iso directory"
FileUtils.mkdir(env.config.veewee.iso_dir)
@@ -25,20 +26,24 @@
end
end
def download_progress(url,localfile)
pbar = nil
- URI.parse(url).open(
+ uri = URI.parse(url)
+ uri.open(
:content_length_proc => lambda {|t|
if t && 0 < t
pbar = ProgressBar.new("Fetching file", t)
pbar.file_transfer_mode
end
},
:progress_proc => lambda {|s|
pbar.set s if pbar
- }) { |src|
+ },
+ #consider proxy env vars only if host is not excluded
+ :proxy => !no_proxy?(uri.host)
+ ) { |src|
# We assume large 10K files, so this is tempfile object
env.logger.info "#{src.class}"
ui.info "Moving #{src.path} to #{localfile}"
# Force the close of the src stream to release handle before moving
# Not forcing the close may cause an issue on windows (Permission Denied)
@@ -48,13 +53,30 @@
#dst.write(src.read)
#}
}
end
+ #return true if host is excluded from proxy via no_proxy env var, false otherwise
+ def no_proxy? host
+ @no_proxy ||= (ENV['NO_PROXY'] || ENV['no_proxy'] || 'localhost, 127.0.0.1').split(/\s*,\s*/)
+ @no_proxy.each do |host_addr|
+ return true if host.match(Regexp.quote(host_addr)+'$')
+ end
+ return false
+ end
+
# Compute hash code
- def hashsum(filename)
- checksum=Digest::MD5.new
+ def hashsum(filename,type)
+ case type
+ when :md5
+ checksum=Digest::MD5.new
+ when :sha1
+ checksum=Digest::SHA1.new
+ else
+ raise Veewee::Error, "Unknown checksum type #{type}"
+ end
+
buflen=1024
open(filename, "rb") do |io|
counter = 0
while (!io.eof)
readBuf = io.readpartial(buflen)
@@ -63,19 +85,20 @@
end
end
return checksum.hexdigest
end
- def verify_md5sum(full_path)
+ def verify_sum(full_path,type)
filename = File.basename(full_path)
- ui.info "Verifying md5 checksum : #{self.iso_md5}"
- file_md5=hashsum(full_path)
+ required_sum = self.instance_variable_get('@iso_'+type.to_s)
+ ui.info "Verifying #{type} checksum : #{self.required_sum}"
+ file_sum = hashsum(full_path,type)
- unless file_md5==self.iso_md5
- ui.error "The MD5 checksums for file #{filename } do not match: "
- ui.error "- #{file_md5} (current) vs #{self.iso_md5} (specified)"
- raise Veewee::Error, "The MD5 checksums for file #{filename } do not match: \n"+ "- #{file_md5} (current) vs #{self.iso_md5} (specified)"
+ unless file_sum == required_sum
+ ui.error "The #{type} checksum for file #{filename } do not match: "
+ ui.error "- #{file_sum} (current) vs #{required_sum} (specified)"
+ raise Veewee::Error, "The #{type} checksum for file #{filename } do not match: \n"+ "- #{file_sum} (current) vs #{required_sum} (specified)"
end
end
def verify_iso(options)
filename=self.iso_file
@@ -93,11 +116,12 @@
ui.info ""
ui.info "We did not find an isofile here : #{full_path}. \n\nThe definition provided the following download information:"
unless "#{self.iso_src}"==""
ui.info "- Download url: #{self.iso_src}"
end
- ui.info "- Md5 Checksum: #{self.iso_md5}"
+ ui.info "- Md5 Checksum: #{self.iso_md5}" if self.iso_md5
+ ui.info "- Sha1 Checksum: #{self.iso_sha1}" if self.iso_sha1
ui.info "#{self.iso_download_instructions}"
ui.info ""
if self.iso_src == ""
ui.info "Please follow the instructions above:"
@@ -123,11 +147,12 @@
raise Veewee::Error, "There was an error downloading #{self.iso_src}:\n#{ex}"
end
else
ui.info "You have selected manual download: "
ui.info "curl -C - -L '#{self.iso_src}' -o '#{rel_path}'"
- ui.info "md5 '#{rel_path}' "
+ ui.info "md5 '#{rel_path}' " if self.iso_md5
+ ui.info "shasum '#{rel_path}' " if self.iso_sha1
ui.info ""
exit
end
unless File.readable?(full_path)
@@ -136,10 +161,11 @@
end
end
- verify_md5sum(full_path) if options["md5check"] && !self.iso_md5.nil?
+ verify_sum(full_path,:md5) if options["checksum"] && !self.iso_md5.nil?
+ verify_sum(full_path,:sha1) if options["checksum"] && !self.iso_sha1.nil?
end
end #Module
end #Module