switch to File.open for downloader temp file path to prevent minitar issues on windows
This commit is contained in:
parent
35e9fa53d4
commit
0c00201a6b
|
@ -8,4 +8,5 @@ _site/*
|
||||||
!templates/*
|
!templates/*
|
||||||
*.org
|
*.org
|
||||||
.yardoc/
|
.yardoc/
|
||||||
doc/
|
doc/
|
||||||
|
*~
|
|
@ -1,66 +1,71 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module Actions
|
module Actions
|
||||||
module Box
|
module Box
|
||||||
# An action which acts on a box by downloading the box file from
|
# An action which acts on a box by downloading the box file from
|
||||||
# the given URI into a temporary location. This action parses a
|
# the given URI into a temporary location. This action parses a
|
||||||
# given URI and handles downloading it via one of the many Vagrant
|
# given URI and handles downloading it via one of the many Vagrant
|
||||||
# downloads (such as {Vagrant::Downloaders::File}).
|
# downloads (such as {Vagrant::Downloaders::File}).
|
||||||
#
|
#
|
||||||
# This action cleans itself up by removing the downloaded box file.
|
# This action cleans itself up by removing the downloaded box file.
|
||||||
class Download < Base
|
class Download < Base
|
||||||
BASENAME = "box"
|
BASENAME = "box"
|
||||||
BUFFERSIZE = 1048576 # 1 MB
|
BUFFERSIZE = 1048576 # 1 MB
|
||||||
|
|
||||||
attr_reader :downloader
|
attr_reader :downloader
|
||||||
|
|
||||||
def prepare
|
def prepare
|
||||||
# Parse the URI given and prepare a downloader
|
# Parse the URI given and prepare a downloader
|
||||||
uri = URI.parse(@runner.uri)
|
uri = URI.parse(@runner.uri)
|
||||||
uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]
|
uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]
|
||||||
|
|
||||||
uri_map.find do |uri_type, downloader_klass|
|
uri_map.find do |uri_type, downloader_klass|
|
||||||
if uri.is_a?(uri_type)
|
if uri.is_a?(uri_type)
|
||||||
logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
|
logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
|
||||||
@downloader = downloader_klass.new
|
@downloader = downloader_klass.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
raise ActionException.new(:box_download_unknown_type) unless @downloader
|
raise ActionException.new(:box_download_unknown_type) unless @downloader
|
||||||
|
|
||||||
# Prepare the downloader
|
# Prepare the downloader
|
||||||
@downloader.prepare(@runner.uri)
|
@downloader.prepare(@runner.uri)
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute!
|
def execute!
|
||||||
with_tempfile do |tempfile|
|
with_tempfile do |tempfile|
|
||||||
download_to(tempfile)
|
download_to(tempfile)
|
||||||
@runner.temp_path = tempfile.path
|
@runner.temp_path = tempfile.path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cleanup
|
def cleanup
|
||||||
if @runner.temp_path && File.exist?(@runner.temp_path)
|
if @runner.temp_path && File.exist?(@runner.temp_path)
|
||||||
logger.info "Cleaning up downloaded box..."
|
logger.info "Cleaning up downloaded box..."
|
||||||
File.unlink(@runner.temp_path)
|
File.unlink(@runner.temp_path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def rescue(exception)
|
def rescue(exception)
|
||||||
cleanup
|
cleanup
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_tempfile
|
def with_tempfile
|
||||||
logger.info "Creating tempfile for storing box file..."
|
logger.info "Creating tempfile for storing box file..."
|
||||||
Tempfile.open(BASENAME, @runner.env.tmp_path) do |tempfile|
|
# create, write only, fail if the file exists
|
||||||
yield tempfile
|
File.open(file_temp_path, File::WRONLY|File::EXCL|File::CREAT) do |tempfile|
|
||||||
end
|
yield tempfile
|
||||||
end
|
end
|
||||||
|
end
|
||||||
def download_to(f)
|
|
||||||
logger.info "Copying box to temporary location..."
|
def file_temp_path
|
||||||
downloader.download!(@runner.uri, f)
|
File.join(@runner.env.tmp_path, BASENAME + Time.now.to_i.to_s)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
def download_to(f)
|
||||||
end
|
logger.info "Copying box to temporary location..."
|
||||||
end
|
downloader.download!(@runner.uri, f)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -260,4 +260,4 @@ module Vagrant
|
||||||
error_and_exit(:environment_not_created) if !vm
|
error_and_exit(:environment_not_created) if !vm
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,13 +81,15 @@ class DownloadBoxActionTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "tempfile" do
|
context "tempfile" do
|
||||||
should "create a tempfile in the vagrant tmp directory" do
|
should "create a tempfile in the vagrant tmp directory" do
|
||||||
Tempfile.expects(:open).with(Vagrant::Actions::Box::Download::BASENAME, @runner.env.tmp_path).once
|
File.expects(:open).with { |name, bitmask|
|
||||||
|
name =~ /#{Vagrant::Actions::Box::Download::BASENAME}/ && name =~ /#{@runner.env.tmp_path}/
|
||||||
|
}.once
|
||||||
@action.with_tempfile
|
@action.with_tempfile
|
||||||
end
|
end
|
||||||
|
|
||||||
should "yield the tempfile object" do
|
should "yield the tempfile object" do
|
||||||
@tempfile = mock("tempfile")
|
@tempfile = mock("tempfile")
|
||||||
Tempfile.expects(:open).yields(@tempfile)
|
File.expects(:open).yields(@tempfile)
|
||||||
|
|
||||||
@action.with_tempfile do |otherfile|
|
@action.with_tempfile do |otherfile|
|
||||||
assert @tempfile.equal?(otherfile)
|
assert @tempfile.equal?(otherfile)
|
||||||
|
|
|
@ -153,12 +153,13 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "loading the root path" do
|
context "loading the root path" do
|
||||||
setup do
|
setup do
|
||||||
@env.cwd = "/foo"
|
@env.cwd = "/foo"
|
||||||
end
|
end
|
||||||
|
|
||||||
should "default the path to the cwd instance var if nil" do
|
should "default the path to the cwd instance var if nil" do
|
||||||
@path = mock("path")
|
@path = mock("path")
|
||||||
@path.stubs(:root?).returns(true)
|
@path.stubs(:root?).returns(true)
|
||||||
|
File.expects(:expand_path).with(@env.cwd).returns(@env.cwd)
|
||||||
Pathname.expects(:new).with(@env.cwd).returns(@path)
|
Pathname.expects(:new).with(@env.cwd).returns(@path)
|
||||||
@env.load_root_path!(nil)
|
@env.load_root_path!(nil)
|
||||||
end
|
end
|
||||||
|
@ -181,7 +182,8 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
|
|
||||||
search_seq = sequence("search_seq")
|
search_seq = sequence("search_seq")
|
||||||
paths.each do |path|
|
paths.each do |path|
|
||||||
File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
|
# NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
|
||||||
|
File.expects(:exist?).with("#{File.expand_path(path)}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert !@env.load_root_path!(paths.first)
|
assert !@env.load_root_path!(paths.first)
|
||||||
|
@ -203,7 +205,8 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "should set the path for the rootfile" do
|
should "should set the path for the rootfile" do
|
||||||
path = "/foo"
|
# NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
|
||||||
|
path = File.expand_path("/foo")
|
||||||
File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true)
|
File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true)
|
||||||
|
|
||||||
assert @env.load_root_path!(Pathname.new(path))
|
assert @env.load_root_path!(Pathname.new(path))
|
||||||
|
|
Loading…
Reference in New Issue